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 ***************************************************************************/
20 #include "formtocdialog.h"
26 #include "support/lstrings.h"
27 #include "QtLyXView.h"
35 FormToc::FormToc(LyXView *v, Dialogs *d)
36 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
37 toclist(0), type(Buffer::TOC_TOC), depth(1)
39 // let the dialog be shown
40 // This is a permanent connection so we won't bother
41 // storing a copy because we won't be disconnecting.
42 d->showTOC.connect(slot(this, &FormToc::showTOC));
43 d->createTOC.connect(slot(this, &FormToc::createTOC));
51 void FormToc::showTOC(InsetCommand * const inset)
53 // FIXME: when could inset be 0 here ?
58 ih_ = inset_->hide.connect(slot(this,&FormToc::hide));
59 params = inset->params();
64 void FormToc::createTOC(string const & arg)
69 params.setFromString(arg);
73 void FormToc::updateToc(int newdepth)
75 if (!lv_->view()->available()) {
77 dialog_->tree->clear();
81 vector< vector<Buffer::TocItem> > tmp =
82 lv_->view()->buffer()->getTocList();
84 // Check if all elements are the same.
85 if (newdepth==depth && toclist.size() == tmp[type].size()) {
87 for (; i < toclist.size(); ++i) {
88 if (toclist[i] != tmp[type][i])
91 if (i >= toclist.size())
99 dialog_->tree->clear();
103 dialog_->tree->setUpdatesEnabled(false);
106 stack< pair< QListViewItem *, QListViewItem *> > istack;
107 QListViewItem *last = 0;
108 QListViewItem *parent = 0;
111 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
112 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
115 for (vector< Buffer::TocItem >::const_iterator iter = toclist.begin();
116 iter != toclist.end(); ++iter) {
117 if (iter->depth == curdepth) {
118 // insert it after the last one we processed
120 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
122 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
123 } else if (iter->depth > curdepth) {
124 int diff = iter->depth - curdepth;
125 // first save old parent and last
127 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
128 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tree));
131 int diff = curdepth - iter->depth;
132 pair< QListViewItem *, QListViewItem * > top;
140 // insert it after the last one we processed
142 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
144 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
146 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
147 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0) : "0")
148 << "\", parent \"" << (parent ? parent->text(0) : "0") << "\"" << endl;
149 item->setText(0,iter->str.c_str());
150 item->setOpen(iter->depth < depth);
151 curdepth = iter->depth;
155 dialog_->tree->setUpdatesEnabled(true);
156 dialog_->tree->update();
159 void FormToc::setType(Buffer::TocType toctype)
163 case Buffer::TOC_TOC:
164 dialog_->setCaption(_("Table of Contents"));
165 dialog_->tree->setColumnText(0,_("Table of Contents"));
166 dialog_->depth->setEnabled(true);
168 case Buffer::TOC_LOF:
169 dialog_->setCaption(_("List of Figures"));
170 dialog_->tree->setColumnText(0,_("List of Figures"));
171 dialog_->depth->setEnabled(false);
173 case Buffer::TOC_LOT:
174 dialog_->setCaption(_("List of Tables"));
175 dialog_->tree->setColumnText(0,_("List of Tables"));
176 dialog_->depth->setEnabled(false);
178 case Buffer::TOC_LOA:
179 dialog_->setCaption(_("List of Algorithms"));
180 dialog_->tree->setColumnText(0,_("List of Algorithms"));
181 dialog_->depth->setEnabled(false);
186 void FormToc::set_depth(int newdepth)
192 void FormToc::update()
194 if (params.getCmdName()=="tableofcontents") {
195 setType(Buffer::TOC_TOC);
196 dialog_->menu->setCurrentItem(0);
197 } else if (params.getCmdName()=="listoffigures") {
198 setType(Buffer::TOC_LOF);
199 dialog_->menu->setCurrentItem(1);
200 } else if (params.getCmdName()=="listoftables") {
201 setType(Buffer::TOC_LOT);
202 dialog_->menu->setCurrentItem(2);
204 setType(Buffer::TOC_LOA);
205 dialog_->menu->setCurrentItem(3);
211 void FormToc::select(const char *text)
213 if (!lv_->view()->available())
216 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
217 for (; iter != toclist.end(); ++iter) {
222 if (iter==toclist.end()) {
223 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
227 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
230 void FormToc::set_type(Buffer::TocType toctype)
242 dialog_ = new FormTocDialog(this, 0, _("LyX: Table of Contents"), false);
244 if (!dialog_->isVisible()) {
245 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
246 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::update));
250 dialog_->setActiveWindow();
256 void FormToc::close()