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.
16 #include "qt_helpers.h"
18 #include "ControlToc.h"
20 #include "QTocDialog.h"
23 #include <qlistview.h>
24 #include <qpushbutton.h>
25 #include <qcombobox.h>
35 typedef QController<ControlToc, QView<QTocDialog> > base_class;
37 QToc::QToc(Dialog & parent)
38 : base_class(parent, _("LyX: Table of Contents")), depth_(1)
42 void QToc::build_dialog()
44 dialog_.reset(new QTocDialog(this));
46 // Manage the cancel/close button
47 bcview().setCancel(dialog_->closePB);
51 void QToc::updateType()
53 dialog_->typeCO->clear();
55 vector<string> const & choice = controller().getTypes();
56 string const & type = lyx::toc::getType(controller().params().getCmdName());
58 for (vector<string>::const_iterator it = choice.begin();
59 it != choice.end(); ++it) {
60 dialog_->typeCO->insertItem(toqstr(*it));
62 dialog_->typeCO->setCurrentItem(it - choice.begin());
69 void QToc::update_contents()
76 void QToc::updateToc(int newdepth)
78 string type = fromqstr(dialog_->typeCO->currentText());
80 lyx::toc::Toc const & contents = controller().getContents(type);
82 // Check if all elements are the same.
83 if (newdepth == depth_ && toclist == contents) {
87 dialog_->tocLV->clear();
96 dialog_->tocLV->setUpdatesEnabled(false);
99 stack<pair<QListViewItem *, QListViewItem *> > istack;
100 QListViewItem * last = 0;
101 QListViewItem * parent = 0;
102 QListViewItem * item;
104 // Yes, it is this ugly. Two reasons - root items must have
105 // a QListView parent, rather than QListViewItem; and the
106 // TOC can move in and out an arbitrary number of levels
108 for (lyx::toc::Toc::const_iterator iter = toclist.begin();
109 iter != toclist.end(); ++iter) {
110 if (iter->depth == curdepth) {
111 // insert it after the last one we processed
113 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
115 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
116 } else if (iter->depth > curdepth) {
117 int diff = iter->depth - curdepth;
118 // first save old parent and last
120 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
121 item = (last ? new QListViewItem(last) : new QListViewItem(dialog_->tocLV));
124 int diff = curdepth - iter->depth;
125 pair<QListViewItem *, QListViewItem * > top;
133 // insert it after the last one we processed
135 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
137 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
141 << "Table of contents\n"
142 << "Added item " << iter->str
143 << " at depth " << iter->depth
144 << ", previous sibling \""
145 << (last ? fromqstr(last->text(0)) : "0")
147 << (parent ? fromqstr(parent->text(0)) : "0") << '"'
149 item->setText(0, toqstr(iter->str));
150 item->setOpen(iter->depth < depth_);
151 curdepth = iter->depth;
155 dialog_->tocLV->setUpdatesEnabled(true);
156 dialog_->tocLV->update();
160 void QToc::select(string const & text)
162 lyx::toc::Toc::const_iterator iter = toclist.begin();
164 for (; iter != toclist.end(); ++iter) {
165 if (iter->str == text)
169 if (iter == toclist.end()) {
170 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry: "
175 controller().goTo(*iter);
179 void QToc::set_depth(int depth)