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
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(_("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(it->c_str());
66 dialog_->typeCO->setCurrentItem(it - choice.begin());
67 dialog_->setCaption(type.c_str());
73 void QToc::update_contents()
80 void QToc::updateToc(int newdepth)
82 char const * str = dialog_->typeCO->currentText().latin1();
83 string type (str ? str : "");
85 toc::Toc const & contents = controller().getContents(type);
87 // Check if all elements are the same.
88 if (newdepth == depth_ && toclist == contents) {
92 dialog_->tocLV->clear();
101 dialog_->tocLV->setUpdatesEnabled(false);
104 stack<pair<QListViewItem *, QListViewItem *> > istack;
105 QListViewItem * last = 0;
106 QListViewItem * parent = 0;
107 QListViewItem * item;
109 // Yes, it is this ugly. Two reasons - root items must have
110 // a QListView parent, rather than QListViewItem; and the
111 // TOC can move in and out an arbitrary number of levels
113 for (toc::Toc::const_iterator iter = toclist.begin();
114 iter != toclist.end(); ++iter) {
115 if (iter->depth == curdepth) {
116 // insert it after the last one we processed
118 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
120 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
121 } else if (iter->depth > curdepth) {
122 int diff = iter->depth - curdepth;
123 // first save old parent and last
125 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
126 item = (last ? new QListViewItem(last) : new QListViewItem(dialog_->tocLV));
129 int diff = curdepth - iter->depth;
130 pair<QListViewItem *, QListViewItem * > top;
138 // insert it after the last one we processed
140 item = (last ? new QListViewItem(dialog_->tocLV,last) : new QListViewItem(dialog_->tocLV));
142 item = (last ? new QListViewItem(parent,last) : new QListViewItem(parent));
146 << "Table of contents\n"
147 << "Added item " << iter->str.c_str()
148 << " at depth " << iter->depth
149 << ", previous sibling \""
150 << (last ? last->text(0).latin1() : "0")
152 << (parent ? parent->text(0).latin1() : "0") << '"'
154 item->setText(0,iter->str.c_str());
155 item->setOpen(iter->depth < depth_);
156 curdepth = iter->depth;
160 dialog_->tocLV->setUpdatesEnabled(true);
161 dialog_->tocLV->update();
165 void QToc::select(string const & text)
167 toc::Toc::const_iterator iter = toclist.begin();
169 for (; iter != toclist.end(); ++iter) {
170 if (iter->str == text)
174 if (iter == toclist.end()) {
175 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : "
180 controller().goTo(*iter);
184 void QToc::set_depth(int depth)