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