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