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"
36 FormToc::FormToc(LyXView *v, Dialogs *d)
37 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
38 toclist(0), type(Buffer::TOC_TOC), depth(1)
40 // let the dialog be shown
41 // This is a permanent connection so we won't bother
42 // storing a copy because we won't be disconnecting.
43 d->showTOC.connect(slot(this, &FormToc::showTOC));
44 d->createTOC.connect(slot(this, &FormToc::createTOC));
52 void FormToc::showTOC(InsetCommand * const inset)
54 // FIXME: when could inset be 0 here ?
59 ih_ = inset_->hide.connect(slot(this,&FormToc::hide));
60 params = inset->params();
65 void FormToc::createTOC(string const & arg)
70 params.setFromString(arg);
74 void FormToc::updateToc(int newdepth)
76 if (!lv_->view()->available()) {
78 dialog_->tree->clear();
82 vector< vector<Buffer::TocItem> > tmp =
83 lv_->view()->buffer()->getTocList();
85 // Check if all elements are the same.
86 if (newdepth==depth && toclist.size() == tmp[type].size()) {
88 for (; i < toclist.size(); ++i) {
89 if (toclist[i] != tmp[type][i])
92 if (i >= toclist.size())
100 dialog_->tree->clear();
104 dialog_->tree->setUpdatesEnabled(false);
107 stack< pair< QListViewItem *, QListViewItem *> > istack;
108 QListViewItem *last = 0;
109 QListViewItem *parent = 0;
112 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
113 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
116 for (vector< Buffer::TocItem >::const_iterator iter = toclist.begin();
117 iter != toclist.end(); ++iter) {
118 if (iter->depth == curdepth) {
119 // insert it after the last one we processed
121 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
123 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
124 } else if (iter->depth > curdepth) {
125 int diff = iter->depth - curdepth;
126 // first save old parent and last
128 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
129 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tree));
132 int diff = curdepth - iter->depth;
133 pair< QListViewItem *, QListViewItem * > top;
141 // insert it after the last one we processed
143 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
145 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
147 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
148 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0) : "0")
149 << "\", parent \"" << (parent ? parent->text(0) : "0") << "\"" << endl;
150 item->setText(0,iter->str.c_str());
151 item->setOpen(iter->depth < depth);
152 curdepth = iter->depth;
156 dialog_->tree->setUpdatesEnabled(true);
157 dialog_->tree->update();
160 void FormToc::setType(Buffer::TocType toctype)
164 case Buffer::TOC_TOC:
165 dialog_->setCaption(_("Table of Contents"));
166 dialog_->tree->setColumnText(0,_("Table of Contents"));
167 dialog_->depth->setEnabled(true);
169 case Buffer::TOC_LOF:
170 dialog_->setCaption(_("List of Figures"));
171 dialog_->tree->setColumnText(0,_("List of Figures"));
172 dialog_->depth->setEnabled(false);
174 case Buffer::TOC_LOT:
175 dialog_->setCaption(_("List of Tables"));
176 dialog_->tree->setColumnText(0,_("List of Tables"));
177 dialog_->depth->setEnabled(false);
179 case Buffer::TOC_LOA:
180 dialog_->setCaption(_("List of Algorithms"));
181 dialog_->tree->setColumnText(0,_("List of Algorithms"));
182 dialog_->depth->setEnabled(false);
187 void FormToc::set_depth(int newdepth)
193 void FormToc::update()
195 if (params.getCmdName()=="tableofcontents") {
196 setType(Buffer::TOC_TOC);
197 dialog_->menu->setCurrentItem(0);
198 } else if (params.getCmdName()=="listoffigures") {
199 setType(Buffer::TOC_LOF);
200 dialog_->menu->setCurrentItem(1);
201 } else if (params.getCmdName()=="listoftables") {
202 setType(Buffer::TOC_LOT);
203 dialog_->menu->setCurrentItem(2);
205 setType(Buffer::TOC_LOA);
206 dialog_->menu->setCurrentItem(3);
212 void FormToc::select(const char *text)
214 if (!lv_->view()->available())
217 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
218 for (; iter != toclist.end(); ++iter) {
223 if (iter==toclist.end()) {
224 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
228 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
231 void FormToc::set_type(Buffer::TocType toctype)
243 dialog_ = new FormTocDialog(this, 0, _("LyX: Table of Contents"), false);
245 if (!dialog_->isVisible()) {
246 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
247 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::update));
251 dialog_->setActiveWindow();
257 void FormToc::close()