]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
3edc839f54286bab4f1bf5bb384e7e86b6c28511
[lyx.git] / src / frontends / qt4 / TocWidget.cpp
1 /**
2  * \file TocWidget.cpp
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 "TocWidget.h"
15
16 #include "QToc.h"
17 #include "qt_helpers.h"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
20
21 #include "debug.h"
22
23 #include <QHeaderView>
24 #include <QPushButton>
25 #include <QTreeWidgetItem>
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
38 namespace lyx {
39
40 using support::FileName;
41 using support::libFileSearch;
42         
43 namespace frontend {
44
45 TocWidget::TocWidget(QToc * form, QWidget * parent)
46         : QWidget(parent), form_(form), depth_(0)
47 {
48         setupUi(this);
49
50         connect(form, SIGNAL(modelReset()),
51                 SLOT(updateGui()));
52         
53         FileName icon_path = libFileSearch("images", "promote.xpm");
54         moveOutTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
55         icon_path = libFileSearch("images", "demote.xpm");
56         moveInTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
57         icon_path = libFileSearch("images", "up.xpm");
58         moveUpTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
59         icon_path = libFileSearch("images", "down.xpm");
60         moveDownTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
61         icon_path = libFileSearch("images", "reload.xpm");
62         updateTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
63                 
64         // avoid flickering
65         tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
66
67         tocTV->showColumn(0);
68
69         // hide the pointless QHeader for now
70         // in the future, new columns may appear
71         // like labels, bookmarks, etc...
72         // tocTV->header()->hide();
73         tocTV->header()->setVisible(false);
74
75         // Only one item selected at a time.
76         tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
77 }
78
79
80 void TocWidget::selectionChanged(const QModelIndex & current,
81                                   const QModelIndex & /*previous*/)
82 {
83         LYXERR(Debug::GUI)
84                 << "selectionChanged index " << current.row()
85                 << ", " << current.column()
86                 << endl;
87
88         form_->goTo(typeCO->currentIndex(), current);
89 }
90
91
92 void TocWidget::on_updateTB_clicked()
93 {
94         // The backend update can take some time so we disable
95         // the controls while waiting.
96         enableControls(false);
97         form_->updateBackend();
98 }
99
100 /* FIXME (Ugras 17/11/06):
101 I have implemented a getIndexDepth function to get the model indices. In my
102 opinion, somebody should derive a new qvariant class for tocModelItem
103 which saves the string data and depth information. that will save the
104 depth calculation.
105 */
106 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
107 {
108         ++depth;
109         return (index.parent() ==
110                 QModelIndex())? depth : getIndexDepth(index.parent(),depth);
111 }
112
113
114 void TocWidget::on_depthSL_valueChanged(int depth)
115 {
116         if (depth == depth_)
117                 return;
118         setTreeDepth(depth);
119 }
120
121
122 void TocWidget::setTreeDepth(int depth)
123 {
124         depth_ = depth;
125
126         // expanding and then collapsing is probably better, 
127         // but my qt 4.1.2 doesn't have expandAll()..
128         //tocTV->expandAll(); 
129         QModelIndexList indices = tocTV->model()->match(
130                 tocTV->model()->index(0,0),
131                 Qt::DisplayRole, "*", -1, 
132                 Qt::MatchWildcard|Qt::MatchRecursive);
133         
134         int size = indices.size();
135         for (int i = 0; i < size; i++) {
136                 QModelIndex index = indices[i];
137                 if (getIndexDepth(index) < depth_) 
138                         tocTV->expand(index); 
139                 else
140                         tocTV->collapse(index); 
141         }
142 }
143
144 void TocWidget::on_typeCO_currentIndexChanged(int value)
145 {
146         setTocModel(value);
147 }
148
149
150 void TocWidget::on_moveUpTB_clicked()
151 {
152         enableControls(false);
153         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
154         if (!list.isEmpty()) {
155                 enableControls(false);
156                 form_->goTo(typeCO->currentIndex(), list[0]);
157                 form_->outlineUp();
158                 enableControls(true);
159         }
160 }
161
162
163 void TocWidget::on_moveDownTB_clicked()
164 {
165         enableControls(false);
166         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
167         if (!list.isEmpty()) {
168                 enableControls(false);
169                 form_->goTo(typeCO->currentIndex(), list[0]);
170                 form_->outlineDown();
171                 enableControls(true);
172         }
173 }
174
175
176 void TocWidget::on_moveInTB_clicked()
177 {
178         enableControls(false);
179         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
180         if (!list.isEmpty()) {
181                 enableControls(false);
182                 form_->goTo(typeCO->currentIndex(), list[0]);
183                 form_->outlineIn();
184                 enableControls(true);
185         }
186 }
187
188
189 void TocWidget::on_moveOutTB_clicked()
190 {
191         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
192         if (!list.isEmpty()) {
193                 enableControls(false);
194                 form_->goTo(typeCO->currentIndex(), list[0]);
195                 form_->outlineOut();
196                 enableControls(true);
197         }
198 }
199
200
201 void TocWidget::select(QModelIndex const & index)
202 {
203         if (!index.isValid()) {
204                 LYXERR(Debug::GUI)
205                         << "TocWidget::select(): QModelIndex is invalid!" << endl;
206                 return;
207         }
208
209         // FIXME: The TocBackend infrastructure is not ready for LOF and LOT
210         // This is because a proper ParConstIterator is not constructed in
211         // InsetCaption::addToToc()
212         if(!form_->canOutline(typeCO->currentIndex()))
213                 return;
214
215         disconnectSelectionModel();
216         tocTV->setCurrentIndex(index);
217         tocTV->scrollTo(index);
218         reconnectSelectionModel();
219 }
220
221
222 void TocWidget::enableControls(bool enable)
223 {
224         updateTB->setEnabled(enable);
225
226         if (!form_->canOutline(typeCO->currentIndex()))
227                 enable = false;
228
229         moveUpTB->setEnabled(enable);
230         moveDownTB->setEnabled(enable);
231         moveInTB->setEnabled(enable);
232         moveOutTB->setEnabled(enable);
233
234         depthSL->setEnabled(enable);
235 }
236
237
238 void TocWidget::update()
239 {
240         LYXERR(Debug::GUI) << "In TocWidget::update()" << endl;
241         select(form_->getCurrentIndex(typeCO->currentIndex()));
242         QWidget::update();
243 }
244
245
246 void TocWidget::updateGui()
247 {
248         vector<docstring> const & type_names = form_->typeNames();
249         if (type_names.empty()) {
250                 enableControls(false);
251                 typeCO->clear();
252                 tocTV->setModel(new QStandardItemModel);
253                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
254                 return;
255         }
256
257         QString current_text = typeCO->currentText();
258         lyxerr << "current_text " << fromqstr(current_text) << endl;
259         typeCO->blockSignals(true);
260         typeCO->clear();
261         int current_type = -1;
262         for (size_t i = 0; i != type_names.size(); ++i) {
263                 QString item = toqstr(type_names[i]);
264                 typeCO->addItem(item);
265                 if (item == current_text)
266                         current_type = i;
267         }
268         if (current_type != -1)
269                 typeCO->setCurrentIndex(current_type);
270         else
271                 typeCO->setCurrentIndex(form_->selectedType());
272         typeCO->blockSignals(false);
273
274         setTocModel(typeCO->currentIndex());
275 }
276
277
278 void TocWidget::setTocModel(size_t type)
279 {
280         bool controls_enabled = false;
281         QStandardItemModel * toc_model = form_->tocModel(type);
282         if (toc_model) {
283                 controls_enabled = toc_model->rowCount() > 0;
284                 tocTV->setModel(toc_model);
285                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
286         }
287
288         enableControls(controls_enabled);
289
290         reconnectSelectionModel();
291
292         if (controls_enabled) {
293                 depthSL->setMaximum(form_->getTocDepth(type));
294                 depthSL->setValue(depth_);
295         }
296
297         LYXERR(Debug::GUI) << "In TocWidget::updateGui()" << endl;
298
299         select(form_->getCurrentIndex(typeCO->currentIndex()));
300
301         if (toc_model) {
302                 LYXERR(Debug::GUI)
303                 << "form_->tocModel()->rowCount " 
304                         << toc_model->rowCount()
305                         << "\nform_->tocModel()->columnCount "
306                         << toc_model->columnCount()
307                         << endl;
308         }
309 }
310
311
312 void TocWidget::reconnectSelectionModel()
313 {
314         connect(tocTV->selectionModel(),
315                 SIGNAL(currentChanged(const QModelIndex &,
316                        const QModelIndex &)),
317                 this,
318                 SLOT(selectionChanged(const QModelIndex &,
319                      const QModelIndex &)));
320 }
321
322 void TocWidget::disconnectSelectionModel()
323 {
324         disconnect(tocTV->selectionModel(),
325                    SIGNAL(currentChanged(const QModelIndex &, 
326                           const QModelIndex &)),
327                    this,
328                    SLOT(selectionChanged(const QModelIndex &,
329                         const QModelIndex &)));
330 }
331
332 } // namespace frontend
333 } // namespace lyx
334
335 #include "TocWidget_moc.cpp"