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)
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()
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 (toclist.size() == tmp[type].size()) {
87 for (; i < toclist.size(); ++i) {
88 if (toclist[i] != tmp[type][i])
91 if (i >= toclist.size())
97 dialog_->tree->clear();
99 dialog_->tree->setUpdatesEnabled(false);
102 stack< pair< QListViewItem *, QListViewItem *> > istack;
103 QListViewItem *last = 0;
104 QListViewItem *parent = 0;
107 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
108 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
111 for (vector< Buffer::TocItem >::const_iterator iter = toclist.begin();
112 iter != toclist.end(); ++iter) {
113 if (iter->depth == depth) {
114 // insert it after the last one we processed
116 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
118 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
119 } else if (iter->depth > depth) {
120 int diff = iter->depth - depth;
121 // first save old parent and last
123 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
124 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tree));
127 int diff = depth - iter->depth;
128 pair< QListViewItem *, QListViewItem * > top;
136 // insert it after the last one we processed
138 item = (last) ? (new QListViewItem(dialog_->tree,last)) : (new QListViewItem(dialog_->tree));
140 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
142 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
143 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0) : "0")
144 << "\", parent \"" << (parent ? parent->text(0) : "0") << "\"" << endl;
145 item->setText(0,iter->str.c_str());
150 dialog_->tree->setUpdatesEnabled(true);
151 dialog_->tree->update();
154 void FormToc::setType(Buffer::TocType toctype)
158 case Buffer::TOC_TOC:
159 dialog_->setCaption(_("Table of Contents"));
160 dialog_->tree->setColumnText(0,_("Table of Contents"));
162 case Buffer::TOC_LOF:
163 dialog_->setCaption(_("List of Figures"));
164 dialog_->tree->setColumnText(0,_("List of Figures"));
166 case Buffer::TOC_LOT:
167 dialog_->setCaption(_("List of Tables"));
168 dialog_->tree->setColumnText(0,_("List of Tables"));
170 case Buffer::TOC_LOA:
171 dialog_->setCaption(_("List of Algorithms"));
172 dialog_->tree->setColumnText(0,_("List of Algorithms"));
177 void FormToc::update()
179 if (params.getCmdName()=="tableofcontents") {
180 setType(Buffer::TOC_TOC);
181 dialog_->menu->setCurrentItem(0);
182 } else if (params.getCmdName()=="listoffigures") {
183 setType(Buffer::TOC_LOF);
184 dialog_->menu->setCurrentItem(1);
185 } else if (params.getCmdName()=="listoftables") {
186 setType(Buffer::TOC_LOT);
187 dialog_->menu->setCurrentItem(2);
189 setType(Buffer::TOC_LOA);
190 dialog_->menu->setCurrentItem(3);
196 void FormToc::select(const char *text)
198 if (!lv_->view()->available())
201 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
202 for (; iter != toclist.end(); ++iter) {
207 if (iter==toclist.end()) {
208 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
212 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
215 void FormToc::set_type(Buffer::TocType toctype)
227 dialog_ = new FormTocDialog(this, 0, _("LyX: Table of Contents"), false);
229 if (!dialog_->isVisible()) {
230 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
231 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::update));
235 dialog_->setActiveWindow();
241 void FormToc::close()