3 * Copyright 2001 the LyX Team
4 * Read the file COPYING
6 * \author John Levon <moz@compsoc.man.ac.uk>
12 #pragma implementation
18 #include <qlistview.h>
19 #include <qpushbutton.h>
20 #include <qcombobox.h>
22 #include "QTocDialog.h"
26 #include "support/lstrings.h"
29 #include "QtLyXView.h"
36 typedef Qt2CB<ControlToc, Qt2DB<QTocDialog> > base_class;
38 QToc::QToc(ControlToc & c)
39 : base_class(c, _("Table of contents"))
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 string const type = dialog_->typeCO->currentText().latin1();
81 Buffer::SingleList const & contents = controller().getContents(type);
83 // Check if all elements are the same.
84 if (newdepth == depth_ && toclist == contents) {
88 dialog_->tocLV->clear();
97 dialog_->tocLV->setUpdatesEnabled(false);
100 stack< pair< QListViewItem *, QListViewItem *> > istack;
101 QListViewItem *last = 0;
102 QListViewItem *parent = 0;
105 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
106 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
109 for (Buffer::SingleList::const_iterator iter = toclist.begin();
110 iter != toclist.end(); ++iter) {
111 if (iter->depth == curdepth) {
112 // insert it after the last one we processed
114 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
116 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
117 } else if (iter->depth > curdepth) {
118 int diff = iter->depth - curdepth;
119 // first save old parent and last
121 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
122 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
125 int diff = curdepth - iter->depth;
126 pair< QListViewItem *, QListViewItem * > top;
134 // insert it after the last one we processed
136 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
138 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
141 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
142 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
143 << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
144 item->setText(0,iter->str.c_str());
145 item->setOpen(iter->depth < depth_);
146 curdepth = iter->depth;
150 dialog_->tocLV->setUpdatesEnabled(true);
151 dialog_->tocLV->update();
155 void QToc::select(string const & text)
157 Buffer::SingleList::const_iterator iter = toclist.begin();
159 for (; iter != toclist.end(); ++iter) {
160 if (iter->str == text)
164 if (iter == toclist.end()) {
165 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : "
170 controller().Goto(iter->par->id());
174 void QToc::set_depth(int depth)