3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
8 * Full author contact details are available in file CREDITS.
14 #include "QTocDialog.h"
16 #include "qt_helpers.h"
20 #include "controllers/ControlToc.h"
22 #include <qcombobox.h>
23 #include <qlistview.h>
24 #include <qpushbutton.h>
38 typedef QController<ControlToc, QView<QTocDialog> > base_class;
40 QToc::QToc(Dialog & parent)
41 : base_class(parent, _("Table of Contents")), depth_(1)
45 void QToc::build_dialog()
47 dialog_.reset(new QTocDialog(this));
49 // Manage the cancel/close button
50 bcview().setCancel(dialog_->closePB);
51 type_ = toc::getType(controller().params().getCmdName());
52 dialog_->enableButtons();
56 void QToc::updateType()
58 dialog_->typeCO->clear();
60 vector<string> const & choice = controller().getTypes();
61 string const & type = toc::getType(controller().params().getCmdName());
63 for (vector<string>::const_iterator it = choice.begin();
64 it != choice.end(); ++it) {
65 string const & guiname = controller().getGuiName(*it);
66 dialog_->typeCO->insertItem(toqstr(guiname));
68 dialog_->typeCO->setCurrentItem(it - choice.begin());
69 setTitle(lyx::from_ascii(guiname));
73 dialog_->enableButtons();
77 void QToc::update_contents()
84 void QToc::updateToc(int newdepth)
86 vector<string> const & choice = controller().getTypes();
89 type = choice[dialog_->typeCO->currentItem()];
91 dialog_->enableButtons();
93 toc::Toc const & contents = controller().getContents(type);
95 // Check if all elements are the same.
96 if (newdepth == depth_ && toclist == contents) {
100 dialog_->tocLV->clear();
109 dialog_->tocLV->setUpdatesEnabled(false);
112 stack<pair<QListViewItem *, QListViewItem *> > istack;
113 QListViewItem * last = 0;
114 QListViewItem * parent = 0;
115 QListViewItem * item;
116 QListViewItem * selected_item = 0;
117 bool multiple = false;
119 // Yes, it is this ugly. Two reasons - root items must have
120 // a QListView parent, rather than QListViewItem; and the
121 // TOC can move in and out an arbitrary number of levels
123 for (toc::Toc::const_iterator iter = toclist.begin();
124 iter != toclist.end(); ++iter) {
125 if (iter->depth() == curdepth) {
126 // insert it after the last one we processed
128 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
130 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
131 } else if (iter->depth() > curdepth) {
132 int diff = iter->depth() - curdepth;
133 // first save old parent and last
135 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
136 item = (last ? new QListViewItem(last) : new QListViewItem(dialog_->tocLV));
139 int diff = curdepth - iter->depth();
140 pair<QListViewItem *, QListViewItem * > top;
148 // insert it after the last one we processed
150 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
152 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
156 << "Table of contents\n"
157 << "Added item " << iter->str()
158 << " at depth " << iter->depth()
159 << ", previous sibling \""
160 << (last ? fromqstr(last->text(0)) : "0")
162 << (parent ? fromqstr(parent->text(0)) : "0") << '"'
164 item->setText(0, toqstr(iter->str()));
165 item->setOpen(iter->depth() < depth_);
166 curdepth = iter->depth();
169 // Recognise part past the counter
170 if (iter->str().substr(iter->str().find(' ') + 1) == text_) {
171 if (selected_item == 0)
172 selected_item = item;
174 // more than one match
179 dialog_->tocLV->setUpdatesEnabled(true);
180 dialog_->tocLV->update();
182 dialog_->tocLV->scrollBy(0, selected_item->itemPos()
183 - dialog_->tocLV->height() / 2);
184 dialog_->tocLV->setSelected(selected_item, true);
186 setTitle(qstring_to_ucs4(dialog_->typeCO->currentText()));
190 bool QToc::canOutline()
192 return controller().canOutline(type_);
196 void QToc::select(string const & text)
198 toc::Toc::const_iterator iter = toclist.begin();
200 for (; iter != toclist.end(); ++iter) {
201 if (iter->str() == text)
205 if (iter == toclist.end()) {
206 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry: "
211 // Lop off counter part and save:
212 text_ = text.substr(text.find(' ') + 1);
213 controller().goTo(*iter);
217 void QToc::set_depth(int depth)
226 controller().outlineUp();
233 controller().outlineDown();
240 controller().outlineIn();
247 controller().outlineOut();
252 } // namespace frontend