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
17 #include "qt_helpers.h"
18 #include "support/lstrings.h"
21 #include "ControlToc.h"
23 #include "QTocDialog.h"
27 #include <qlistview.h>
28 #include <qpushbutton.h>
29 #include <qcombobox.h>
36 typedef Qt2CB<ControlToc, Qt2DB<QTocDialog> > base_class;
39 : base_class(qt_("Table of contents")), depth_(1)
43 void QToc::build_dialog()
45 dialog_.reset(new QTocDialog(this));
47 // Manage the cancel/close button
48 bc().setCancel(dialog_->closePB);
52 void QToc::updateType()
54 dialog_->typeCO->clear();
56 vector<string> const & choice = controller().getTypes();
57 string const & type = toc::getType(controller().params().getCmdName());
59 for (vector<string>::const_iterator it = choice.begin();
60 it != choice.end(); ++it) {
61 dialog_->typeCO->insertItem(toqstr(*it));
63 dialog_->typeCO->setCurrentItem(it - choice.begin());
64 dialog_->setCaption(toqstr(type));
70 void QToc::update_contents()
77 void QToc::updateToc(int newdepth)
79 string type = fromqstr(dialog_->typeCO->currentText());
81 toc::Toc const & contents = controller().getContents(type);
83 // Check if all elements are the same.
84 if (newdepth == depth_ && toclist == contents) {
88 dialog_->tocLV->clear();
97 dialog_->tocLV->setUpdatesEnabled(false);
100 stack<pair<QListViewItem *, QListViewItem *> > istack;
101 QListViewItem * last = 0;
102 QListViewItem * parent = 0;
103 QListViewItem * item;
105 // Yes, it is this ugly. Two reasons - root items must have
106 // a QListView parent, rather than QListViewItem; and the
107 // TOC can move in and out an arbitrary number of levels
109 for (toc::Toc::const_iterator iter = toclist.begin();
110 iter != toclist.end(); ++iter) {
111 if (iter->depth == curdepth) {
112 // insert it after the last one we processed
114 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
116 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
117 } else if (iter->depth > curdepth) {
118 int diff = iter->depth - curdepth;
119 // first save old parent and last
121 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
122 item = (last ? new QListViewItem(last) : new QListViewItem(dialog_->tocLV));
125 int diff = curdepth - iter->depth;
126 pair<QListViewItem *, QListViewItem * > top;
134 // insert it after the last one we processed
136 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
138 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
142 << "Table of contents\n"
143 << "Added item " << iter->str
144 << " at depth " << iter->depth
145 << ", previous sibling \""
146 << (last ? fromqstr(last->text(0)) : "0")
148 << (parent ? fromqstr(parent->text(0)) : "0") << '"'
150 item->setText(0, toqstr(iter->str));
151 item->setOpen(iter->depth < depth_);
152 curdepth = iter->depth;
156 dialog_->tocLV->setUpdatesEnabled(true);
157 dialog_->tocLV->update();
161 void QToc::select(string const & text)
163 toc::Toc::const_iterator iter = toclist.begin();
165 for (; iter != toclist.end(); ++iter) {
166 if (iter->str == text)
170 if (iter == toclist.end()) {
171 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry: "
176 controller().goTo(*iter);
180 void QToc::set_depth(int depth)