3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
19 #include "support/lstrings.h"
20 #include "QtLyXView.h"
29 // FIXME: we should be able to move sections around like klyx1 can
31 FormToc::FormToc(LyXView *v, Dialogs *d)
32 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
33 toclist(0), type(Buffer::TOC_TOC), depth(1)
35 // let the dialog be shown
36 // This is a permanent connection so we won't bother
37 // storing a copy because we won't be disconnecting.
38 d->showTOC.connect(slot(this, &FormToc::showTOC));
39 d->createTOC.connect(slot(this, &FormToc::createTOC));
49 void FormToc::showTOC(InsetCommand * const inset)
51 // FIXME: when could inset be 0 here ?
56 ih_ = inset_->hide.connect(slot(this,&FormToc::hide));
57 params = inset->params();
63 void FormToc::createTOC(string const & arg)
68 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;
109 QListViewItem * item;
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();
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);
188 void FormToc::set_depth(int newdepth)
190 if (newdepth != depth)
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);
216 void FormToc::select(char const * text)
218 if (!lv_->view()->available())
221 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
222 for (; iter != toclist.end(); ++iter) {
223 if (iter->str == text)
227 if (iter == toclist.end()) {
228 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
232 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
236 void FormToc::set_type(Buffer::TocType toctype)
249 dialog_ = new TocDialog(this, 0, _("LyX: Table of Contents"), false);
251 if (!dialog_->isVisible()) {
252 h_ = d_->hideBufferDependent.connect(slot(this, &FormToc::hide));
253 u_ = d_->updateBufferDependent.connect(slot(this, &FormToc::update));
257 dialog_->setActiveWindow();
264 void FormToc::close()