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, _("LyX: 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);
54 void QToc::updateType()
56 dialog_->typeCO->clear();
58 vector<string> const & choice = controller().getTypes();
59 string const & type = toc::getType(controller().params().getCmdName());
61 for (vector<string>::const_iterator it = choice.begin();
62 it != choice.end(); ++it) {
63 dialog_->typeCO->insertItem(toqstr(*it));
65 dialog_->typeCO->setCurrentItem(it - choice.begin());
72 void QToc::update_contents()
79 void QToc::updateToc(int newdepth)
81 string type = fromqstr(dialog_->typeCO->currentText());
83 toc::Toc const & contents = controller().getContents(type);
85 // Check if all elements are the same.
86 if (newdepth == depth_ && toclist == contents) {
90 dialog_->tocLV->clear();
99 dialog_->tocLV->setUpdatesEnabled(false);
102 stack<pair<QListViewItem *, QListViewItem *> > istack;
103 QListViewItem * last = 0;
104 QListViewItem * parent = 0;
105 QListViewItem * item;
107 // Yes, it is this ugly. Two reasons - root items must have
108 // a QListView parent, rather than QListViewItem; and the
109 // TOC can move in and out an arbitrary number of levels
111 for (toc::Toc::const_iterator iter = toclist.begin();
112 iter != toclist.end(); ++iter) {
113 if (iter->depth == curdepth) {
114 // insert it after the last one we processed
116 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
118 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
119 } else if (iter->depth > curdepth) {
120 int diff = iter->depth - curdepth;
121 // first save old parent and last
123 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
124 item = (last ? new QListViewItem(last) : new QListViewItem(dialog_->tocLV));
127 int diff = curdepth - iter->depth;
128 pair<QListViewItem *, QListViewItem * > top;
136 // insert it after the last one we processed
138 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
140 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
144 << "Table of contents\n"
145 << "Added item " << iter->str
146 << " at depth " << iter->depth
147 << ", previous sibling \""
148 << (last ? fromqstr(last->text(0)) : "0")
150 << (parent ? fromqstr(parent->text(0)) : "0") << '"'
152 item->setText(0, toqstr(iter->str));
153 item->setOpen(iter->depth < depth_);
154 curdepth = iter->depth;
158 dialog_->tocLV->setUpdatesEnabled(true);
159 dialog_->tocLV->update();
163 void QToc::select(string const & text)
165 toc::Toc::const_iterator iter = toclist.begin();
167 for (; iter != toclist.end(); ++iter) {
168 if (iter->str == text)
172 if (iter == toclist.end()) {
173 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry: "
178 controller().goTo(*iter);
182 void QToc::set_depth(int depth)
188 } // namespace frontend