]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
Oops.. compile fix.
[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         // expanding and then collapsing is probably better,
146         // but my qt 4.1.2 doesn't have expandAll()..
147         //tocTV->expandAll();
148         QModelIndexList indices = tocTV->model()->match(
149                 tocTV->model()->index(0, 0),
150                 Qt::DisplayRole, "*", -1,
151                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
152
153         int size = indices.size();
154         for (int i = 0; i < size; i++) {
155                 QModelIndex index = indices[i];
156                 tocTV->setExpanded(index, indexDepth(index) < depth_);
157         }
158 }
159
160
161 void TocWidget::on_typeCO_currentIndexChanged(int index)
162 {
163         current_type_ = typeCO->itemData(index).toString();
164         updateView();
165         gui_view_.setFocus();
166 }
167
168
169 void TocWidget::outline(int func_code)
170 {
171         enableControls(false);
172         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
173         if (list.isEmpty())
174                 return;
175         enableControls(false);
176         goTo(list[0]);
177         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
178         enableControls(true);
179         gui_view_.setFocus();
180 }
181
182
183 void TocWidget::on_moveUpTB_clicked()
184 {
185         outline(LFUN_OUTLINE_UP);
186 }
187
188
189 void TocWidget::on_moveDownTB_clicked()
190 {
191         outline(LFUN_OUTLINE_DOWN);
192 }
193
194
195 void TocWidget::on_moveInTB_clicked()
196 {
197         outline(LFUN_OUTLINE_IN);
198 }
199
200
201 void TocWidget::on_moveOutTB_clicked()
202 {
203         outline(LFUN_OUTLINE_OUT);
204 }
205
206
207 void TocWidget::select(QModelIndex const & index)
208 {
209         if (!index.isValid()) {
210                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
211                 return;
212         }
213
214         tocTV->scrollTo(index);
215         tocTV->clearSelection();
216         tocTV->setCurrentIndex(index);
217 }
218
219
220 /// Test if outlining operation is possible
221 static bool canOutline(QString const & type)
222 {
223         return type == "tableofcontents";
224 }
225
226
227 void TocWidget::enableControls(bool enable)
228 {
229         updateTB->setEnabled(enable);
230         sortCB->setEnabled(enable);
231
232         if (!canOutline(current_type_))
233                 enable = false;
234
235         moveUpTB->setEnabled(enable);
236         moveDownTB->setEnabled(enable);
237         moveInTB->setEnabled(enable);
238         moveOutTB->setEnabled(enable);
239         persistentCB->setEnabled(enable);
240         if (!enable) {
241                 depthSL->setMaximum(0);
242                 depthSL->setValue(0);
243         }
244 }
245
246
247 /// Test if synchronized navigation is possible
248 static bool canNavigate(QString const & type)
249 {
250         // It is not possible to have synchronous navigation in a correctl
251         // and efficient way with the label type because Toc::item() do a linear
252         // seatch. Even if fixed, it might even not be desirable to do so if we 
253         // want to support drag&drop of labels and references.
254         return type != "label" && type != "change";
255 }
256
257
258 void TocWidget::updateView()
259 {
260         if (!gui_view_.view()) {
261                 enableControls(false);
262                 typeCO->setEnabled(false);
263                 tocTV->setModel(0);
264                 tocTV->setEnabled(false);
265                 return;
266         }
267         typeCO->setEnabled(true);
268         tocTV->setEnabled(false);
269         tocTV->setUpdatesEnabled(false);
270
271         QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);    
272         if (tocTV->model() != toc_model) {
273                 tocTV->setModel(toc_model);
274                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
275                 if (persistent_) setTreeDepth(depth_);
276         }
277
278         sortCB->blockSignals(true);
279         sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
280         sortCB->blockSignals(false);
281
282         persistentCB->setChecked(persistent_);
283
284         bool controls_enabled = toc_model && toc_model->rowCount() > 0
285                 && !gui_view_.buffer()->isReadonly();
286         enableControls(controls_enabled);
287
288         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
289         depthSL->setValue(depth_);
290         if (!persistent_) setTreeDepth(depth_);
291         if (canNavigate(current_type_))
292                 select(gui_view_.tocModels().currentIndex(current_type_));
293         tocTV->setEnabled(true);
294         tocTV->setUpdatesEnabled(true);
295 }
296
297
298 static QString decodeType(QString const & str)
299 {
300         QString type = str;
301         if (type.contains("tableofcontents")) {
302                 type = "tableofcontents";
303         } else if (type.contains("floatlist")) {
304                 if (type.contains("\"figure"))
305                         type = "figure";
306                 else if (type.contains("\"table"))
307                         type = "table";
308                 else if (type.contains("\"algorithm"))
309                         type = "algorithm";
310         }
311         return type;
312 }
313
314
315 void TocWidget::init(QString const & str)
316 {
317         int new_index;
318         if (str.isEmpty())
319                 new_index = typeCO->findData(current_type_);
320         else
321                 new_index = typeCO->findData(decodeType(str));
322
323         // If everything else fails, settle on the table of contents which is
324         // guaranted to exist.
325         if (new_index == -1) {
326                 current_type_ = "tableofcontents";
327                 new_index = typeCO->findData(current_type_);
328         } else {
329                 current_type_ = typeCO->itemData(new_index).toString();
330         }
331
332         typeCO->blockSignals(true);
333         typeCO->setCurrentIndex(new_index);
334         typeCO->blockSignals(false);
335 }
336
337 } // namespace frontend
338 } // namespace lyx
339
340 #include "moc_TocWidget.cpp"