3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "QTocDialog.h"
16 #include "qt_helpers.h"
17 #include "controllers/ControlToc.h"
21 #include <QTreeWidget>
22 #include <QTreeWidgetItem>
23 #include <QPushButton>
24 #include <QCloseEvent>
39 QTocDialog::QTocDialog(QToc * form)
40 : form_(form), depth_(2)
45 tocTW->setSortingEnabled(false);
46 tocTW->setColumnCount(1);
47 // hide the pointless QHeader
48 // QWidget * w = static_cast<QWidget*>(tocTW->child("list view header"));
52 connect(closePB, SIGNAL(clicked()),
53 form, SLOT(slotClose()));
55 connect( tocTW, SIGNAL(itemClicked(QTreeWidgetItem*)), this, SLOT(select_adaptor(QTreeWidgetItem*)));
56 connect( typeCO, SIGNAL( activated(int) ), this, SLOT( activate_adaptor(int) ) );
57 connect( updatePB, SIGNAL( clicked() ), this, SLOT( update_adaptor() ) );
58 connect( depthSL, SIGNAL( valueChanged(int) ), this, SLOT( depth_adaptor(int) ) );
62 QTocDialog::~QTocDialog()
66 void QTocDialog::updateType()
70 vector<string> const & choice = form_->controller().getTypes();
71 string const & type = toc::getType(form_->controller().params().getCmdName());
73 for (vector<string>::const_iterator it = choice.begin();
74 it != choice.end(); ++it) {
75 string const & guiname = form_->controller().getGuiName(*it);
76 typeCO->insertItem(toqstr(guiname));
78 typeCO->setCurrentItem(it - choice.begin());
79 form_->setTitle(guiname);
84 void QTocDialog::updateToc(bool newdepth)
86 vector<string> const & choice = form_->controller().getTypes();
89 type = choice[typeCO->currentItem()];
91 toc::Toc const & contents = form_->controller().getContents(type);
93 // Check if all elements are the same.
94 if (!newdepth && form_->get_toclist() == contents) {
100 form_->get_toclist() = contents;
102 if (form_->get_toclist().empty())
105 tocTW->setUpdatesEnabled(false);
107 QTreeWidgetItem * topLevelItem;
109 toc::Toc::const_iterator iter = form_->get_toclist().begin();
111 while (iter != form_->get_toclist().end()) {
113 if (iter->depth == 1) {
114 topLevelItem = new QTreeWidgetItem(tocTW);
115 // tocTW->addTopLevelItem(topLevelItem);
116 topLevelItem->setText(0, toqstr(iter->str));
117 if (iter->depth < depth_) tocTW->collapseItem(topLevelItem);
120 << "Table of contents\n"
121 << "Added Top Level item " << iter->str
122 << " at depth " << iter->depth
125 populateItem(topLevelItem, iter);
128 if (iter == form_->get_toclist().end())
134 tocTW->setUpdatesEnabled(true);
136 form_->setTitle(fromqstr(typeCO->currentText()));
140 void QTocDialog::populateItem(QTreeWidgetItem * parentItem, toc::Toc::const_iterator& iter)
142 int curdepth = iter->depth+1;
143 QTreeWidgetItem * item;
145 while (iter != form_->get_toclist().end()) {
149 if (iter == form_->get_toclist().end())
152 if (iter->depth < curdepth) {
156 if (iter->depth > curdepth) {
161 item = new QTreeWidgetItem(parentItem);
162 item->setText(0, toqstr(iter->str));
163 // parentItem->addChild(item);
165 if (iter->depth < depth_) tocTW->collapseItem(item);
166 // else tocTW->expandItem(item);
168 << "Table of contents: Added item " << iter->str
169 << " at depth " << iter->depth
171 << fromqstr(parentItem->text(0)) << '"'
174 populateItem(item, iter);
178 void QTocDialog::activate_adaptor(int)
184 void QTocDialog::depth_adaptor(int depth)
194 void QTocDialog::select_adaptor(QTreeWidgetItem * item)
196 form_->select(fromqstr(item->text(0)));
200 void QTocDialog::update_adaptor()
206 void QTocDialog::closeEvent(QCloseEvent * e)
212 } // namespace frontend
217 stack<pair<QTreeWidgetItem *, QTreeWidgetItem *> > istack;
218 QTreeWidgetItem * last = 0;
219 QTreeWidgetItem * parent = 0;
220 QTreeWidgetItem * item;
222 // Yes, it is this ugly. Two reasons - root items must have
223 // a QListView parent, rather than QListViewItem; and the
224 // TOC can move in and out an arbitrary number of levels
226 for (toc::Toc::const_iterator iter = form_->get_toclist().begin();
227 iter != form_->get_toclist().end(); ++iter) {
229 if (iter->depth == 0) {
232 if (iter->depth == curdepth) {
233 // insert it after the last one we processed
235 item = (last ? new QTreeWidgetItem(tocTW,last) : new QTreeWidgetItem(tocTW));
237 item = (last ? new QTreeWidgetItem(parent,last) : new QTreeWidgetItem(parent));
238 } else if (iter->depth > curdepth) {
239 int diff = iter->depth - curdepth;
240 // first save old parent and last
242 istack.push(pair< QTreeWidgetItem *, QTreeWidgetItem * >(parent,last));
243 item = (last ? new QTreeWidgetItem(last) : new QTreeWidgetItem(tocTW));
246 int diff = curdepth - iter->depth;
247 pair<QTreeWidgetItem *, QTreeWidgetItem * > top;
255 // insert it after the last one we processed
257 item = (last ? new QTreeWidgetItem(tocTW,last) : new QTreeWidgetItem(tocTW));
259 item = (last ? new QTreeWidgetItem(parent,last) : new QTreeWidgetItem(parent));
261 item->setText(0, toqstr(iter->str));
262 item->setOpen(iter->depth < depth_);
263 curdepth = iter->depth;