4 * John Levon, moz@compsoc.man.ac.uk
7 /***************************************************************************
9 * This program is free software; you can redistribute it and/or modify *
10 * it under the terms of the GNU General Public License as published by *
11 * the Free Software Foundation; either version 2 of the License, or *
12 * (at your option) any later version. *
14 ***************************************************************************/
21 #include "FormTocDialog.h"
28 #include "support/lstrings.h"
29 #include "QtLyXView.h"
38 #include <qlistview.h>
39 #include <qcombobox.h>
41 FormToc::FormToc(LyXView *v, Dialogs *d)
42 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
43 toclist(0), type(Buffer::TOC_TOC), depth(1)
45 // let the dialog be shown
46 // This is a permanent connection so we won't bother
47 // storing a copy because we won't be disconnecting.
48 d->showTOC.connect(slot(this, &FormToc::showTOC));
49 d->createTOC.connect(slot(this, &FormToc::createTOC));
57 void FormToc::showTOC(InsetCommand * const inset)
59 // FIXME: when could inset be 0 here ?
64 ih_ = inset_->hide.connect(slot(this,&FormToc::hide));
65 params = inset->params();
70 void FormToc::createTOC(string const & arg)
75 params.setFromString(arg);
79 void FormToc::updateToc(int newdepth)
81 if (!lv_->view()->available()) {
83 dialog_->tocLV->clear();
87 vector< vector<Buffer::TocItem> > tmp =
88 lv_->view()->buffer()->getTocList();
90 // Check if all elements are the same.
91 if (newdepth==depth && toclist.size() == tmp[type].size()) {
93 for (; i < toclist.size(); ++i) {
94 if (toclist[i] != tmp[type][i])
97 if (i >= toclist.size())
105 dialog_->tocLV->clear();
109 dialog_->tocLV->setUpdatesEnabled(false);
112 stack< pair< QListViewItem *, QListViewItem *> > istack;
113 QListViewItem *last = 0;
114 QListViewItem *parent = 0;
117 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
118 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
121 for (vector< Buffer::TocItem >::const_iterator iter = toclist.begin();
122 iter != toclist.end(); ++iter) {
123 if (iter->depth == curdepth) {
124 // insert it after the last one we processed
126 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
128 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
129 } else if (iter->depth > curdepth) {
130 int diff = iter->depth - curdepth;
131 // first save old parent and last
133 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
134 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
137 int diff = curdepth - iter->depth;
138 pair< QListViewItem *, QListViewItem * > top;
146 // insert it after the last one we processed
148 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
150 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
152 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
153 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
154 << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
155 item->setText(0,iter->str.c_str());
156 item->setOpen(iter->depth < depth);
157 curdepth = iter->depth;
161 dialog_->tocLV->setUpdatesEnabled(true);
162 dialog_->tocLV->update();
165 void FormToc::setType(Buffer::TocType toctype)
169 case Buffer::TOC_TOC:
170 dialog_->setCaption(_("Table of Contents"));
171 dialog_->tocLV->setColumnText(0,_("Table of Contents"));
172 dialog_->depthSL->setEnabled(true);
174 case Buffer::TOC_LOF:
175 dialog_->setCaption(_("List of Figures"));
176 dialog_->tocLV->setColumnText(0,_("List of Figures"));
177 dialog_->depthSL->setEnabled(false);
179 case Buffer::TOC_LOT:
180 dialog_->setCaption(_("List of Tables"));
181 dialog_->tocLV->setColumnText(0,_("List of Tables"));
182 dialog_->depthSL->setEnabled(false);
184 case Buffer::TOC_LOA:
185 dialog_->setCaption(_("List of Algorithms"));
186 dialog_->tocLV->setColumnText(0,_("List of Algorithms"));
187 dialog_->depthSL->setEnabled(false);
192 void FormToc::set_depth(int newdepth)
198 void FormToc::update()
200 if (params.getCmdName()=="tableofcontents") {
201 setType(Buffer::TOC_TOC);
202 dialog_->typeCO->setCurrentItem(0);
203 } else if (params.getCmdName()=="listoffigures") {
204 setType(Buffer::TOC_LOF);
205 dialog_->typeCO->setCurrentItem(1);
206 } else if (params.getCmdName()=="listoftables") {
207 setType(Buffer::TOC_LOT);
208 dialog_->typeCO->setCurrentItem(2);
210 setType(Buffer::TOC_LOA);
211 dialog_->typeCO->setCurrentItem(3);
217 void FormToc::select(const char *text)
219 if (!lv_->view()->available())
222 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
223 for (; iter != toclist.end(); ++iter) {
228 if (iter==toclist.end()) {
229 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
233 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
236 void FormToc::set_type(Buffer::TocType toctype)
248 dialog_ = new FormTocDialog(this, 0, _("LyX: Table of Contents"), false);
250 if (!dialog_->isVisible()) {
251 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
252 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::update));
256 dialog_->setActiveWindow();
262 void FormToc::close()