3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
14 #include "QTocDialog.h"
15 #include "QtLyXView.h"
18 #include "BufferView.h"
22 #include "support/lstrings.h"
26 #include <qlistview.h>
27 #include <qcombobox.h>
36 QToc::QToc(LyXView *v, Dialogs *d)
37 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
38 /*toclist(0),*/ depth(1)
40 d->showTOC.connect(slot(this, &QToc::showTOC));
41 d->createTOC.connect(slot(this, &QToc::createTOC));
51 void QToc::showTOC(InsetCommand * const inset)
53 // FIXME: when could inset be 0 here ?
58 //FIXME ih_ = inset_->hide.connect(slot(this,&QToc::hide));
59 params = inset->params();
65 void QToc::createTOC(string const & arg)
70 params.setFromString(arg);
75 void QToc::updateToc(int newdepth)
77 if (!lv_->view()->available()) {
79 dialog_->tocLV->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_->tocLV->clear();
106 dialog_->tocLV->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_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
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_->tocLV));
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_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
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).latin1() : "0")
151 << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
152 item->setText(0,iter->str.c_str());
153 item->setOpen(iter->depth < depth);
154 curdepth = iter->depth;
158 dialog_->tocLV->setUpdatesEnabled(true);
159 dialog_->tocLV->update();
163 void QToc::setType(Buffer::TocType toctype)
167 case Buffer::TOC_TOC:
168 dialog_->setCaption(_("Table of Contents"));
169 dialog_->tocLV->setColumnText(0,_("Table of Contents"));
170 dialog_->depthSL->setEnabled(true);
172 case Buffer::TOC_LOF:
173 dialog_->setCaption(_("List of Figures"));
174 dialog_->tocLV->setColumnText(0,_("List of Figures"));
175 dialog_->depthSL->setEnabled(false);
177 case Buffer::TOC_LOT:
178 dialog_->setCaption(_("List of Tables"));
179 dialog_->tocLV->setColumnText(0,_("List of Tables"));
180 dialog_->depthSL->setEnabled(false);
182 case Buffer::TOC_LOA:
183 dialog_->setCaption(_("List of Algorithms"));
184 dialog_->tocLV->setColumnText(0,_("List of Algorithms"));
185 dialog_->depthSL->setEnabled(false);
191 void QToc::set_depth(int newdepth)
202 if (params.getCmdName()=="tableofcontents") {
203 setType(Buffer::TOC_TOC);
204 dialog_->typeCO->setCurrentItem(0);
205 } else if (params.getCmdName()=="listoffigures") {
206 setType(Buffer::TOC_LOF);
207 dialog_->typeCO->setCurrentItem(1);
208 } else if (params.getCmdName()=="listoftables") {
209 setType(Buffer::TOC_LOT);
210 dialog_->typeCO->setCurrentItem(2);
212 setType(Buffer::TOC_LOA);
213 dialog_->typeCO->setCurrentItem(3);
220 void QToc::select(const char *text)
222 if (!lv_->view()->available())
225 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
226 for (; iter != toclist.end(); ++iter) {
231 if (iter==toclist.end()) {
232 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
236 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
240 void QToc::set_type(Buffer::TocType toctype)
254 dialog_ = new QTocDialog(this, 0, _("LyX: Table of Contents"), false);
256 if (!dialog_->isVisible()) {
257 h_ = d_->hideBufferDependent.connect(slot(this, &QToc::hide));
258 //u_ = d_->updateBufferDependent.connect(slot(this, &QToc::update));
262 dialog_->setActiveWindow();