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"
31 #include <qlistview.h>
32 #include <qcombobox.h>
34 QToc::QToc(LyXView *v, Dialogs *d)
35 : dialog_(0), lv_(v), d_(d), inset_(0), h_(0), u_(0), ih_(0),
36 /*toclist(0),*/ depth(1)
38 d->showTOC.connect(slot(this, &QToc::showTOC));
39 d->createTOC.connect(slot(this, &QToc::createTOC));
49 void QToc::showTOC(InsetCommand * const inset)
51 // FIXME: when could inset be 0 here ?
56 //FIXME ih_ = inset_->hide.connect(slot(this,&QToc::hide));
57 params = inset->params();
63 void QToc::createTOC(string const & arg)
68 params.setFromString(arg);
73 void QToc::updateToc(int newdepth)
75 if (!lv_->view()->available()) {
77 dialog_->tocLV->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_->tocLV->clear();
104 dialog_->tocLV->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_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
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_->tocLV));
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_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
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).latin1() : "0")
149 << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
150 item->setText(0,iter->str.c_str());
151 item->setOpen(iter->depth < depth);
152 curdepth = iter->depth;
156 dialog_->tocLV->setUpdatesEnabled(true);
157 dialog_->tocLV->update();
161 void QToc::setType(Buffer::TocType toctype)
165 case Buffer::TOC_TOC:
166 dialog_->setCaption(_("Table of Contents"));
167 dialog_->tocLV->setColumnText(0,_("Table of Contents"));
168 dialog_->depthSL->setEnabled(true);
170 case Buffer::TOC_LOF:
171 dialog_->setCaption(_("List of Figures"));
172 dialog_->tocLV->setColumnText(0,_("List of Figures"));
173 dialog_->depthSL->setEnabled(false);
175 case Buffer::TOC_LOT:
176 dialog_->setCaption(_("List of Tables"));
177 dialog_->tocLV->setColumnText(0,_("List of Tables"));
178 dialog_->depthSL->setEnabled(false);
180 case Buffer::TOC_LOA:
181 dialog_->setCaption(_("List of Algorithms"));
182 dialog_->tocLV->setColumnText(0,_("List of Algorithms"));
183 dialog_->depthSL->setEnabled(false);
189 void QToc::set_depth(int newdepth)
200 if (params.getCmdName()=="tableofcontents") {
201 setType(Buffer::TOC_TOC);
202 dialog_->typeCO->setCurrentItem(0);
203 } else if (params.getCmdName()=="listoffigures") {
204 setType(Buffer::TOC_LOF);
205 dialog_->typeCO->setCurrentItem(1);
206 } else if (params.getCmdName()=="listoftables") {
207 setType(Buffer::TOC_LOT);
208 dialog_->typeCO->setCurrentItem(2);
210 setType(Buffer::TOC_LOA);
211 dialog_->typeCO->setCurrentItem(3);
218 void QToc::select(const char *text)
220 if (!lv_->view()->available())
223 vector <Buffer::TocItem>::const_iterator iter = toclist.begin();
224 for (; iter != toclist.end(); ++iter) {
229 if (iter==toclist.end()) {
230 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : " << text << endl;
234 lv_->getLyXFunc()->Dispatch(LFUN_GOTO_PARAGRAPH, tostr(iter->par->id()).c_str());
238 void QToc::set_type(Buffer::TocType toctype)
252 dialog_ = new QTocDialog(this, 0, _("LyX: Table of Contents"), false);
254 if (!dialog_->isVisible()) {
255 h_ = d_->hideBufferDependent.connect(slot(this, &QToc::hide));
256 //u_ = d_->updateBufferDependent.connect(slot(this, &QToc::update));
260 dialog_->setActiveWindow();