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 #pragma implementation
20 #include "qt_helpers.h"
21 #include "support/lstrings.h"
24 #include "ControlToc.h"
26 #include "QTocDialog.h"
30 #include <qlistview.h>
31 #include <qpushbutton.h>
32 #include <qcombobox.h>
39 typedef Qt2CB<ControlToc, Qt2DB<QTocDialog> > base_class;
42 : base_class(qt_("Table of contents")), depth_(1)
46 void QToc::build_dialog()
48 dialog_.reset(new QTocDialog(this));
50 // Manage the cancel/close button
51 bc().setCancel(dialog_->closePB);
55 void QToc::updateType()
57 dialog_->typeCO->clear();
59 vector<string> const & choice = controller().getTypes();
60 string const & type = toc::getType(controller().params().getCmdName());
62 for (vector<string>::const_iterator it = choice.begin();
63 it != choice.end(); ++it) {
64 dialog_->typeCO->insertItem(toqstr(*it));
66 dialog_->typeCO->setCurrentItem(it - choice.begin());
67 dialog_->setCaption(toqstr(type));
73 void QToc::update_contents()
80 void QToc::updateToc(int newdepth)
82 string type = fromqstr(dialog_->typeCO->currentText());
84 toc::Toc const & contents = controller().getContents(type);
86 // Check if all elements are the same.
87 if (newdepth == depth_ && toclist == contents) {
91 dialog_->tocLV->clear();
100 dialog_->tocLV->setUpdatesEnabled(false);
103 stack<pair<QListViewItem *, QListViewItem *> > istack;
104 QListViewItem * last = 0;
105 QListViewItem * parent = 0;
106 QListViewItem * item;
108 // Yes, it is this ugly. Two reasons - root items must have
109 // a QListView parent, rather than QListViewItem; and the
110 // TOC can move in and out an arbitrary number of levels
112 for (toc::Toc::const_iterator iter = toclist.begin();
113 iter != toclist.end(); ++iter) {
114 if (iter->depth == curdepth) {
115 // insert it after the last one we processed
117 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
119 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
120 } else if (iter->depth > curdepth) {
121 int diff = iter->depth - curdepth;
122 // first save old parent and last
124 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
125 item = (last ? new QListViewItem(last) : new QListViewItem(dialog_->tocLV));
128 int diff = curdepth - iter->depth;
129 pair<QListViewItem *, QListViewItem * > top;
137 // insert it after the last one we processed
139 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
141 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
145 << "Table of contents\n"
146 << "Added item " << iter->str
147 << " at depth " << iter->depth
148 << ", previous sibling \""
149 << (last ? fromqstr(last->text(0)) : "0")
151 << (parent ? fromqstr(parent->text(0)) : "0") << '"'
153 item->setText(0, toqstr(iter->str));
154 item->setOpen(iter->depth < depth_);
155 curdepth = iter->depth;
159 dialog_->tocLV->setUpdatesEnabled(true);
160 dialog_->tocLV->update();
164 void QToc::select(string const & text)
166 toc::Toc::const_iterator iter = toclist.begin();
168 for (; iter != toclist.end(); ++iter) {
169 if (iter->str == text)
173 if (iter == toclist.end()) {
174 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry: "
179 controller().goTo(*iter);
183 void QToc::set_depth(int depth)