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 ***************************************************************************/
26 #include "support/lstrings.h"
27 #include "QtLyXView.h"
36 // FIXME: we should be able to move sections around like klyx can.
38 FormToc::FormToc(LyXView *v, Dialogs *d)
39 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
40 toclist(0), type(Buffer::TOC_TOC), depth(1)
42 // let the dialog be shown
43 // This is a permanent connection so we won't bother
44 // storing a copy because we won't be disconnecting.
45 d->showTOC.connect(slot(this, &FormToc::showTOC));
46 d->createTOC.connect(slot(this, &FormToc::createTOC));
54 void FormToc::showTOC(InsetCommand * const inset)
56 // FIXME: when could inset be 0 here ?
61 ih_ = inset_->hide.connect(slot(this,&FormToc::hide));
62 params = inset->params();
67 void FormToc::createTOC(string const & arg)
72 params.setFromString(arg);
76 void FormToc::updateToc(int newdepth)
78 if (!lv_->view()->available()) {
80 dialog_->tree->clear();
84 vector< vector<Buffer::TocItem> > tmp =
85 lv_->view()->buffer()->getTocList();
87 // Check if all elements are the same.
88 if (newdepth==depth && toclist.size() == tmp[type].size()) {
90 for (; i < toclist.size(); ++i) {
91 if (toclist[i] != tmp[type][i])
94 if (i >= toclist.size())
102 dialog_->tree->clear();
106 dialog_->tree->setUpdatesEnabled(false);
109 stack< pair< QListViewItem *, QListViewItem *> > istack;
110 QListViewItem *last = 0;
111 QListViewItem *parent = 0;
114 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
115 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
118 for (vector< Buffer::TocItem >::const_iterator iter = toclist.begin();
119 iter != toclist.end(); ++iter) {
120 if (iter->depth == curdepth) {
121 // insert it after the last one we processed
123 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
125 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
126 } else if (iter->depth > curdepth) {
127 int diff = iter->depth - curdepth;
128 // first save old parent and last
130 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
131 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tree));
134 int diff = curdepth - iter->depth;
135 pair< QListViewItem *, QListViewItem * > top;
143 // insert it after the last one we processed
145 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
147 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
149 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
150 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0) : "0")
151 << "\", parent \"" << (parent ? parent->text(0) : "0") << "\"" << endl;
152 item->setText(0,iter->str.c_str());
153 item->setOpen(iter->depth < depth);
154 curdepth = iter->depth;
158 dialog_->tree->setUpdatesEnabled(true);
159 dialog_->tree->update();
162 void FormToc::setType(Buffer::TocType toctype)
166 case Buffer::TOC_TOC:
167 dialog_->setCaption(_("Table of Contents"));
168 dialog_->tree->setColumnText(0,_("Table of Contents"));
169 dialog_->depth->setEnabled(true);
171 case Buffer::TOC_LOF:
172 dialog_->setCaption(_("List of Figures"));
173 dialog_->tree->setColumnText(0,_("List of Figures"));
174 dialog_->depth->setEnabled(false);
176 case Buffer::TOC_LOT:
177 dialog_->setCaption(_("List of Tables"));
178 dialog_->tree->setColumnText(0,_("List of Tables"));
179 dialog_->depth->setEnabled(false);
181 case Buffer::TOC_LOA:
182 dialog_->setCaption(_("List of Algorithms"));
183 dialog_->tree->setColumnText(0,_("List of Algorithms"));
184 dialog_->depth->setEnabled(false);
189 void FormToc::set_depth(int newdepth)
195 // we can safely ignore the parameter because we can always update
196 void FormToc::update(bool)
198 if (params.getCmdName()=="tableofcontents") {
199 setType(Buffer::TOC_TOC);
200 dialog_->menu->setCurrentItem(0);
201 } else if (params.getCmdName()=="listoffigures") {
202 setType(Buffer::TOC_LOF);
203 dialog_->menu->setCurrentItem(1);
204 } else if (params.getCmdName()=="listoftables") {
205 setType(Buffer::TOC_LOT);
206 dialog_->menu->setCurrentItem(2);
208 setType(Buffer::TOC_LOA);
209 dialog_->menu->setCurrentItem(3);
215 void FormToc::select(const char *text)
217 if (!lv_->view()->available())
220 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
221 for (; iter != toclist.end(); ++iter) {
226 if (iter==toclist.end()) {
227 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
231 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
234 void FormToc::set_type(Buffer::TocType toctype)
246 dialog_ = new TocDialog(this, 0, _("LyX: Table of Contents"), false);
248 if (!dialog_->isVisible()) {
249 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
250 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::update));
254 dialog_->setActiveWindow();
260 void FormToc::close()