3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
12 #pragma implementation
19 #include "support/lstrings.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(_("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(it->c_str());
63 dialog_->typeCO->setCurrentItem(it - choice.begin());
64 dialog_->setCaption(type.c_str());
70 void QToc::update_contents()
77 void QToc::updateToc(int newdepth)
79 char const * str = dialog_->typeCO->currentText().latin1();
80 string type (str ? str : "");
82 toc::Toc const & contents = controller().getContents(type);
84 // Check if all elements are the same.
85 if (newdepth == depth_ && toclist == contents) {
89 dialog_->tocLV->clear();
98 dialog_->tocLV->setUpdatesEnabled(false);
101 stack< pair< QListViewItem *, QListViewItem *> > istack;
102 QListViewItem *last = 0;
103 QListViewItem *parent = 0;
106 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
107 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
110 for (toc::Toc::const_iterator iter = toclist.begin();
111 iter != toclist.end(); ++iter) {
112 if (iter->depth == curdepth) {
113 // insert it after the last one we processed
115 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
117 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
118 } else if (iter->depth > curdepth) {
119 int diff = iter->depth - curdepth;
120 // first save old parent and last
122 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
123 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
126 int diff = curdepth - iter->depth;
127 pair< QListViewItem *, QListViewItem * > top;
135 // insert it after the last one we processed
137 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
139 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
142 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
143 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
144 << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
145 item->setText(0,iter->str.c_str());
146 item->setOpen(iter->depth < depth_);
147 curdepth = iter->depth;
151 dialog_->tocLV->setUpdatesEnabled(true);
152 dialog_->tocLV->update();
156 void QToc::select(string const & text)
158 toc::Toc::const_iterator iter = toclist.begin();
160 for (; iter != toclist.end(); ++iter) {
161 if (iter->str == text)
165 if (iter == toclist.end()) {
166 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : "
171 controller().goTo(*iter);
175 void QToc::set_depth(int depth)