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"
33 typedef Qt2CB<ControlToc, Qt2DB<QTocDialog> > base_class;
35 QToc::QToc(ControlToc & c)
36 : base_class(c, _("Table of contents"))
40 void QToc::build_dialog()
42 dialog_.reset(new QTocDialog(this));
44 // Manage the cancel/close button
45 bc().setCancel(dialog_->closePB);
49 void QToc::updateType()
51 dialog_->typeCO->clear();
53 vector<string> const & choice = controller().getTypes();
54 string const & type = toc::getType(controller().params().getCmdName());
56 for (vector<string>::const_iterator it = choice.begin();
57 it != choice.end(); ++it) {
58 dialog_->typeCO->insertItem(it->c_str());
60 dialog_->typeCO->setCurrentItem(it - choice.begin());
61 dialog_->setCaption(type.c_str());
67 void QToc::update_contents()
74 void QToc::updateToc(int newdepth)
76 string const type = dialog_->typeCO->currentText().latin1();
78 Buffer::SingleList const & contents = controller().getContents(type);
80 // Check if all elements are the same.
81 if (newdepth == depth_ && toclist == contents) {
85 dialog_->tocLV->clear();
94 dialog_->tocLV->setUpdatesEnabled(false);
97 stack< pair< QListViewItem *, QListViewItem *> > istack;
98 QListViewItem *last = 0;
99 QListViewItem *parent = 0;
102 // Yes, it is this ugly. Two reasons - root items must have a QListView parent,
103 // rather than QListViewItem; and the TOC can move in and out an arbitrary number
106 for (Buffer::SingleList::const_iterator iter = toclist.begin();
107 iter != toclist.end(); ++iter) {
108 if (iter->depth == curdepth) {
109 // insert it after the last one we processed
111 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
113 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
114 } else if (iter->depth > curdepth) {
115 int diff = iter->depth - curdepth;
116 // first save old parent and last
118 istack.push(pair< QListViewItem *, QListViewItem * >(parent,last));
119 item = (last) ? (new QListViewItem(last)) : (new QListViewItem(dialog_->tocLV));
122 int diff = curdepth - iter->depth;
123 pair< QListViewItem *, QListViewItem * > top;
131 // insert it after the last one we processed
133 item = (last) ? (new QListViewItem(dialog_->tocLV,last)) : (new QListViewItem(dialog_->tocLV));
135 item = (last) ? (new QListViewItem(parent,last)) : (new QListViewItem(parent));
138 lyxerr[Debug::GUI] << "Table of contents" << endl << "Added item " << iter->str.c_str()
139 << " at depth " << iter->depth << ", previous sibling \"" << (last ? last->text(0).latin1() : "0")
140 << "\", parent \"" << (parent ? parent->text(0).latin1() : "0") << "\"" << endl;
141 item->setText(0,iter->str.c_str());
142 item->setOpen(iter->depth < depth_);
143 curdepth = iter->depth;
147 dialog_->tocLV->setUpdatesEnabled(true);
148 dialog_->tocLV->update();
152 void QToc::select(string const & text)
154 Buffer::SingleList::const_iterator iter = toclist.begin();
156 for (; iter != toclist.end(); ++iter) {
157 if (iter->str == text)
161 if (iter == toclist.end()) {
162 lyxerr[Debug::GUI] << "Couldn't find highlighted TOC entry : "
167 controller().Goto(iter->par->id());
171 void QToc::set_depth(int depth)