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