]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
Cosmetics.
[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 "GuiView.h"
17 #include "qt_helpers.h"
18 #include "TocModel.h"
19
20 #include "Buffer.h"
21 #include "FuncRequest.h"
22 #include "LyXFunc.h"
23
24 #include "support/debug.h"
25 #include "support/lassert.h"
26
27 #include <QHeaderView>
28 #include <QTimer>
29
30 #include <vector>
31
32 using namespace std;
33
34 namespace lyx {
35 namespace frontend {
36
37 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
38         : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
39 {
40         setupUi(this);
41
42         moveOutTB->setIcon(QIcon(":/images/promote.png"));
43         moveInTB->setIcon(QIcon(":/images/demote.png"));
44         moveUpTB->setIcon(QIcon(":/images/up.png"));
45         moveDownTB->setIcon(QIcon(":/images/down.png"));
46         updateTB->setIcon(QIcon(":/images/reload.png"));
47
48         // avoid flickering
49         tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
50
51         tocTV->showColumn(0);
52
53         // hide the pointless QHeader for now
54         // in the future, new columns may appear
55         // like labels, bookmarks, etc...
56         // tocTV->header()->hide();
57         tocTV->header()->setVisible(false);
58
59         // Only one item selected at a time.
60         tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
61
62         // The toc types combo won't change its model.
63         typeCO->setModel(gui_view_.tocModels().nameModel());
64
65         // Make sure the buttons are disabled when first shown without a loaded
66         // Buffer.
67         enableControls(false);
68
69         init(QString());
70 }
71
72
73 void TocWidget::on_tocTV_activated(QModelIndex const & index)
74 {
75         goTo(index);
76 }
77
78
79 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
80 {
81         goTo(index);
82         gui_view_.setFocus();
83 }
84
85
86 void TocWidget::goTo(QModelIndex const & index)
87 {
88         LYXERR(Debug::GUI, "goto " << index.row()
89                 << ", " << index.column());
90
91         gui_view_.tocModels().goTo(current_type_, index);
92 }
93
94
95 void TocWidget::on_updateTB_clicked()
96 {
97         // The backend update can take some time so we disable
98         // the controls while waiting.
99         enableControls(false);
100         gui_view_.tocModels().updateBackend();
101 }
102
103
104 void TocWidget::on_sortCB_stateChanged(int state)
105 {
106         gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
107         updateView();
108 }
109
110 void TocWidget::on_persistentCB_stateChanged(int state)
111 {
112         persistent_ = state == Qt::Checked;
113 }
114
115
116 /* FIXME (Ugras 17/11/06):
117 I have implemented a indexDepth function to get the model indices. In my
118 opinion, somebody should derive a new qvariant class for tocModelItem
119 which saves the string data and depth information. That will save the
120 depth calculation.  */
121
122 static int indexDepth(QModelIndex const & index, int depth = -1)
123 {
124         ++depth;
125         return index.parent() == QModelIndex()
126                 ? depth : indexDepth(index.parent(), depth);
127 }
128
129
130 void TocWidget::on_depthSL_valueChanged(int depth)
131 {
132         if (depth == depth_)
133                 return;
134         setTreeDepth(depth);
135         gui_view_.setFocus();
136 }
137
138
139 void TocWidget::setTreeDepth(int depth)
140 {
141         depth_ = depth;
142         if (!tocTV->model())
143                 return;
144
145 #if QT_VERSION >= 0x040300
146         // this should be faster than our own code below
147         if (depth == 0)
148                 tocTV->collapseAll();
149         else
150                 tocTV->expandToDepth(depth - 1);
151 #else
152         // expanding and then collapsing is probably better,
153         // but my qt 4.1.2 doesn't have expandAll()..
154         //tocTV->expandAll();
155         QModelIndexList indices = tocTV->model()->match(
156                 tocTV->model()->index(0, 0),
157                 Qt::DisplayRole, "*", -1,
158                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
159
160         int size = indices.size();
161         for (int i = 0; i < size; i++) {
162                 QModelIndex index = indices[i];
163                 tocTV->setExpanded(index, indexDepth(index) < depth_);
164         }
165 #endif
166 }
167
168
169 void TocWidget::on_typeCO_currentIndexChanged(int index)
170 {
171         current_type_ = typeCO->itemData(index).toString();
172         updateView();
173         gui_view_.setFocus();
174 }
175
176
177 void TocWidget::outline(int func_code)
178 {
179         enableControls(false);
180         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
181         if (list.isEmpty())
182                 return;
183         enableControls(false);
184         goTo(list[0]);
185         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
186         enableControls(true);
187         gui_view_.setFocus();
188 }
189
190
191 void TocWidget::on_moveUpTB_clicked()
192 {
193         outline(LFUN_OUTLINE_UP);
194 }
195
196
197 void TocWidget::on_moveDownTB_clicked()
198 {
199         outline(LFUN_OUTLINE_DOWN);
200 }
201
202
203 void TocWidget::on_moveInTB_clicked()
204 {
205         outline(LFUN_OUTLINE_IN);
206 }
207
208
209 void TocWidget::on_moveOutTB_clicked()
210 {
211         outline(LFUN_OUTLINE_OUT);
212 }
213
214
215 void TocWidget::select(QModelIndex const & index)
216 {
217         if (!index.isValid()) {
218                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
219                 return;
220         }
221
222         tocTV->scrollTo(index);
223         tocTV->clearSelection();
224         tocTV->setCurrentIndex(index);
225 }
226
227
228 /// Test if outlining operation is possible
229 static bool canOutline(QString const & type)
230 {
231         return type == "tableofcontents";
232 }
233
234
235 void TocWidget::enableControls(bool enable)
236 {
237         updateTB->setEnabled(enable);
238
239         if (!canOutline(current_type_))
240                 enable = false;
241
242         moveUpTB->setEnabled(enable);
243         moveDownTB->setEnabled(enable);
244         moveInTB->setEnabled(enable);
245         moveOutTB->setEnabled(enable);
246         if (!enable) {
247                 depthSL->setMaximum(0);
248                 depthSL->setValue(0);
249         }
250 }
251
252
253 /// Test if synchronized navigation is possible
254 static bool canNavigate(QString const & type)
255 {
256         // It is not possible to have synchronous navigation in a correctl
257         // and efficient way with the label type because Toc::item() do a linear
258         // seatch. Even if fixed, it might even not be desirable to do so if we 
259         // want to support drag&drop of labels and references.
260         return type != "label" && type != "change";
261 }
262
263
264 void TocWidget::updateView()
265 {
266         if (!gui_view_.view()) {
267                 enableControls(false);
268                 typeCO->setEnabled(false);
269                 tocTV->setModel(0);
270                 tocTV->setEnabled(false);
271                 return;
272         }
273         typeCO->setEnabled(true);
274         tocTV->setEnabled(false);
275         tocTV->setUpdatesEnabled(false);
276
277         QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
278         if (tocTV->model() != toc_model) {
279                 tocTV->setModel(toc_model);
280                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
281                 if (persistent_)
282                         setTreeDepth(depth_);
283         }
284
285         sortCB->blockSignals(true);
286         sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
287         sortCB->blockSignals(false);
288
289         persistentCB->setChecked(persistent_);
290
291         bool controls_enabled = toc_model && toc_model->rowCount() > 0
292                 && !gui_view_.buffer()->isReadonly();
293         enableControls(controls_enabled);
294
295         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
296         depthSL->setValue(depth_);
297         if (!persistent_)
298                 setTreeDepth(depth_);
299         if (canNavigate(current_type_))
300                 select(gui_view_.tocModels().currentIndex(current_type_));
301         tocTV->setEnabled(true);
302         tocTV->setUpdatesEnabled(true);
303 }
304
305
306 static QString decodeType(QString const & str)
307 {
308         QString type = str;
309         if (type.contains("tableofcontents")) {
310                 type = "tableofcontents";
311         } else if (type.contains("floatlist")) {
312                 if (type.contains("\"figure"))
313                         type = "figure";
314                 else if (type.contains("\"table"))
315                         type = "table";
316                 else if (type.contains("\"algorithm"))
317                         type = "algorithm";
318         }
319         return type;
320 }
321
322
323 void TocWidget::init(QString const & str)
324 {
325         int new_index;
326         if (str.isEmpty())
327                 new_index = typeCO->findData(current_type_);
328         else
329                 new_index = typeCO->findData(decodeType(str));
330
331         // If everything else fails, settle on the table of contents which is
332         // guaranted to exist.
333         if (new_index == -1) {
334                 current_type_ = "tableofcontents";
335                 new_index = typeCO->findData(current_type_);
336         } else {
337                 current_type_ = typeCO->itemData(new_index).toString();
338         }
339
340         typeCO->blockSignals(true);
341         typeCO->setCurrentIndex(new_index);
342         typeCO->blockSignals(false);
343 }
344
345 } // namespace frontend
346 } // namespace lyx
347
348 #include "moc_TocWidget.cpp"