]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QTocDialog.C
* src/frontends/qt4/QTocDialog.C
[lyx.git] / src / frontends / qt4 / QTocDialog.C
1 /**
2  * \file QTocDialog.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "QTocDialog.h"
15 #include "QToc.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18 #include "controllers/ControlToc.h"
19
20 #include "debug.h"
21
22 #include <QTreeWidget>
23 #include <QTreeWidgetItem>
24 #include <QPushButton>
25 #include <QCloseEvent>
26
27 #include <vector>
28 #include <string>
29 #include <stack>
30
31 using std::endl;
32 using std::pair;
33 using std::stack;
34 using std::vector;
35 using std::string;
36
37 namespace lyx {
38 namespace frontend {
39
40 QTocDialog::QTocDialog(QToc * form)
41         : form_(form), depth_(2)
42 {
43         setupUi(this);
44
45         // Manage the cancel/close button
46         form_->bcview().setCancel(closePB);
47
48         // disable sorting
49         tocTW->setSortingEnabled(false);
50         tocTW->setColumnCount(1);
51
52         // hide the pointless QHeader
53 //      QWidget * w = static_cast<QWidget*>(tocTW->child("list view header"));
54 //      if (w)
55 //              w->hide();
56
57 //      connect(closePB, SIGNAL(clicked()),
58 //              form, SLOT(slotClose()));
59 }
60
61
62 QTocDialog::~QTocDialog()
63 {
64         accept();
65 }
66
67 void QTocDialog::on_tocTW_currentItemChanged(QTreeWidgetItem * current,
68                                                                  QTreeWidgetItem * previous)
69 {
70         form_->select(fromqstr(current->text(0)));
71 }
72
73 void QTocDialog::on_closePB_clicked()
74 {
75         accept();
76 }
77
78 void QTocDialog::on_updatePB_clicked()
79 {
80         form_->update();
81 }
82
83 void QTocDialog::on_depthSL_valueChanged(int depth)
84 {
85         if (depth == depth_)
86                 return;
87
88         depth_ = depth;
89         updateToc(true);
90 }
91
92 void QTocDialog::on_typeCO_activated(int value)
93 {
94         updateToc();
95 }
96
97 void QTocDialog::on_moveUpPB_clicked()
98 {
99         enableButtons(false);
100         form_->moveUp();
101         enableButtons();
102 }
103
104 void QTocDialog::on_moveDownPB_clicked()
105 {
106         enableButtons(false);
107         form_->moveDown();
108         enableButtons();
109 }
110
111 void QTocDialog::on_moveInPB_clicked()
112 {
113         enableButtons(false);
114         form_->moveIn();
115         enableButtons();
116 }
117
118 void QTocDialog::on_moveOutPB_clicked()
119 {
120         enableButtons(false);
121         form_->moveOut();
122         enableButtons();
123 }
124
125 void QTocDialog::enableButtons(bool enable)
126 {
127         moveUpPB->setEnabled(enable);
128         moveDownPB->setEnabled(enable);
129         moveInPB->setEnabled(enable);
130         moveOutPB->setEnabled(enable);
131         updatePB->setEnabled(enable);
132 }
133
134 void QTocDialog::updateType()
135 {
136         typeCO->clear();
137
138         vector<string> const & choice = form_->controller().getTypes();
139         string const & type = toc::getType(form_->controller().params().getCmdName());
140
141         for (vector<string>::const_iterator it = choice.begin();
142                 it != choice.end(); ++it) {
143                 string const & guiname = form_->controller().getGuiName(*it);
144                 typeCO->insertItem(toqstr(guiname));
145                 if (*it == type) {
146                         typeCO->setCurrentItem(it - choice.begin());
147                         form_->setTitle(guiname);
148                 }
149         }
150 }
151
152 void QTocDialog::updateToc(bool newdepth)
153 {
154         vector<string> const & choice = form_->controller().getTypes();
155         string type;
156         if (!choice.empty())
157                 type = choice[typeCO->currentItem()];
158
159         toc::Toc const & contents = form_->controller().getContents(type);
160
161         // Check if all elements are the same.
162         if (!newdepth && form_->get_toclist() == contents) {
163                 return;
164         }
165
166         tocTW->clear();
167
168         form_->get_toclist() = contents;
169
170         if (form_->get_toclist().empty())
171                 return;
172
173         tocTW->setUpdatesEnabled(false);
174
175         QTreeWidgetItem * topLevelItem;
176
177         toc::Toc::const_iterator iter = form_->get_toclist().begin();
178
179         while (iter != form_->get_toclist().end()) {
180
181                 if (iter->depth == 1) {
182                         topLevelItem = new QTreeWidgetItem(tocTW);
183                         topLevelItem->setText(0, toqstr(iter->str));
184                         if (iter->depth > depth_)
185                                 tocTW->collapseItem(topLevelItem);
186                         else if (iter->depth <= depth_)
187                                 tocTW->expandItem(topLevelItem);
188
189                         lyxerr[Debug::GUI]
190                                 << "Table of contents\n"
191                                 << "Added Top Level item " << iter->str
192                                 << " at depth " << iter->depth
193                                 << endl;
194
195                         populateItem(topLevelItem, iter);
196                 }
197
198                 if (iter == form_->get_toclist().end())
199                         break;
200
201                 ++iter;
202         }
203
204         tocTW->setUpdatesEnabled(true);
205         tocTW->update();
206         form_->setTitle(fromqstr(typeCO->currentText()));
207         tocTW->show();
208 }
209
210 void QTocDialog::populateItem(QTreeWidgetItem * parentItem, toc::Toc::const_iterator & iter)
211 {
212         int curdepth = iter->depth + 1;
213         QTreeWidgetItem * item;
214
215         while (iter != form_->get_toclist().end()) {
216
217                 ++iter;
218
219                 if (iter == form_->get_toclist().end())
220                         break;
221
222                 if (iter->depth < curdepth) {
223                         --iter;
224                         return;
225                 }
226                 if (iter->depth > curdepth) {
227 //                      --iter;
228                         return;
229                 }
230
231                 item = new QTreeWidgetItem(parentItem);
232                 item->setText(0, toqstr(iter->str));
233
234                 if (iter->depth > depth_)
235                         tocTW->collapseItem(item);
236                 else if (iter->depth <= depth_)
237                         tocTW->expandItem(item);
238
239                 lyxerr[Debug::GUI]
240                         << "Table of contents: Added item " << iter->str
241                         << " at depth " << iter->depth
242                         << "  \", parent \""
243                         << fromqstr(parentItem->text(0)) << '"'
244                         << "expanded: " << tocTW->isItemExpanded(item)
245                         << endl;
246
247                 populateItem(item, iter);
248         }
249 }
250
251
252 void QTocDialog::closeEvent(QCloseEvent * e)
253 {
254         form_->slotWMHide();
255         e->accept();
256 }
257
258 } // namespace frontend
259 } // namespace lyx