]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
Typos.
[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
70
71 void TocWidget::on_tocTV_activated(QModelIndex const & index)
72 {
73         goTo(index);
74 }
75
76
77 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
78 {
79         goTo(index);
80         gui_view_.setFocus();
81 }
82
83
84 void TocWidget::goTo(QModelIndex const & index)
85 {
86         LYXERR(Debug::GUI, "goto " << index.row()
87                 << ", " << index.column());
88
89         gui_view_.tocModels().goTo(current_type_, index);
90 }
91
92
93 void TocWidget::on_updateTB_clicked()
94 {
95         // The backend update can take some time so we disable
96         // the controls while waiting.
97         enableControls(false);
98         gui_view_.tocModels().updateBackend();
99 }
100
101
102 void TocWidget::on_sortCB_stateChanged(int state)
103 {
104         gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
105         updateView();
106 }
107
108 void TocWidget::on_persistentCB_stateChanged(int state)
109 {
110         persistent_ = state == Qt::Checked;
111 }
112
113
114 /* FIXME (Ugras 17/11/06):
115 I have implemented a indexDepth function to get the model indices. In my
116 opinion, somebody should derive a new qvariant class for tocModelItem
117 which saves the string data and depth information. That will save the
118 depth calculation.  */
119
120 static int indexDepth(QModelIndex const & index, int depth = -1)
121 {
122         ++depth;
123         return index.parent() == QModelIndex()
124                 ? depth : indexDepth(index.parent(), depth);
125 }
126
127
128 void TocWidget::on_depthSL_valueChanged(int depth)
129 {
130         if (depth == depth_)
131                 return;
132         setTreeDepth(depth);
133         gui_view_.setFocus();
134 }
135
136
137 void TocWidget::setTreeDepth(int depth)
138 {
139         depth_ = depth;
140         if (!tocTV->model())
141                 return;
142
143         // expanding and then collapsing is probably better,
144         // but my qt 4.1.2 doesn't have expandAll()..
145         //tocTV->expandAll();
146         QModelIndexList indices = tocTV->model()->match(
147                 tocTV->model()->index(0, 0),
148                 Qt::DisplayRole, "*", -1,
149                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
150
151         int size = indices.size();
152         for (int i = 0; i < size; i++) {
153                 QModelIndex index = indices[i];
154                 tocTV->setExpanded(index, indexDepth(index) < depth_);
155         }
156 }
157
158
159 void TocWidget::on_typeCO_currentIndexChanged(int index)
160 {
161         current_type_ = typeCO->itemData(index).toString();
162         updateView();
163         gui_view_.setFocus();
164 }
165
166
167 void TocWidget::outline(int func_code)
168 {
169         enableControls(false);
170         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
171         if (list.isEmpty())
172                 return;
173         enableControls(false);
174         goTo(list[0]);
175         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
176         enableControls(true);
177         gui_view_.setFocus();
178 }
179
180
181 void TocWidget::on_moveUpTB_clicked()
182 {
183         outline(LFUN_OUTLINE_UP);
184 }
185
186
187 void TocWidget::on_moveDownTB_clicked()
188 {
189         outline(LFUN_OUTLINE_DOWN);
190 }
191
192
193 void TocWidget::on_moveInTB_clicked()
194 {
195         outline(LFUN_OUTLINE_IN);
196 }
197
198
199 void TocWidget::on_moveOutTB_clicked()
200 {
201         outline(LFUN_OUTLINE_OUT);
202 }
203
204
205 void TocWidget::select(QModelIndex const & index)
206 {
207         if (!index.isValid()) {
208                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
209                 return;
210         }
211
212         tocTV->scrollTo(index);
213         tocTV->clearSelection();
214         tocTV->setCurrentIndex(index);
215 }
216
217
218 /// Test if outlining operation is possible
219 static bool canOutline(QString const & type)
220 {
221         return type == "tableofcontents";
222 }
223
224
225 void TocWidget::enableControls(bool enable)
226 {
227         updateTB->setEnabled(enable);
228         sortCB->setEnabled(enable);
229
230         if (!canOutline(current_type_))
231                 enable = false;
232
233         moveUpTB->setEnabled(enable);
234         moveDownTB->setEnabled(enable);
235         moveInTB->setEnabled(enable);
236         moveOutTB->setEnabled(enable);
237         persistentCB->setEnabled(enable);
238         if (!enable) {
239                 depthSL->setMaximum(0);
240                 depthSL->setValue(0);
241         }
242 }
243
244
245 /// Test if synchronized navigation is possible
246 static bool canNavigate(QString const & type)
247 {
248         // It is not possible to have synchronous navigation in a correctl
249         // and efficient way with the label type because Toc::item() do a linear
250         // seatch. Even if fixed, it might even not be desirable to do so if we 
251         // want to support drag&drop of labels and references.
252         return type != "label" && type != "change";
253 }
254
255
256 void TocWidget::updateView()
257 {
258         if (!gui_view_.view()) {
259                 enableControls(false);
260                 typeCO->setEnabled(false);
261                 tocTV->setModel(0);
262                 tocTV->setEnabled(false);
263                 return;
264         }
265         typeCO->setEnabled(true);
266         tocTV->setEnabled(false);
267         tocTV->setUpdatesEnabled(false);
268
269         QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);    
270         if (tocTV->model() != toc_model) {
271                 tocTV->setModel(toc_model);
272                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
273                 if (persistent_) setTreeDepth(depth_);
274         }
275
276         sortCB->blockSignals(true);
277         sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
278         sortCB->blockSignals(false);
279
280         persistentCB->setChecked(persistent_);
281
282         bool controls_enabled = toc_model && toc_model->rowCount() > 0
283                 && !gui_view_.buffer()->isReadonly();
284         enableControls(controls_enabled);
285
286         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
287         depthSL->setValue(depth_);
288         if (!persistent_) setTreeDepth(depth_);
289         if (canNavigate(current_type_))
290                 select(gui_view_.tocModels().currentIndex(current_type_));
291         tocTV->setEnabled(true);
292         tocTV->setUpdatesEnabled(true);
293 }
294
295
296 static QString decodeType(QString const & str)
297 {
298         QString type = str;
299         if (type.contains("tableofcontents")) {
300                 type = "tableofcontents";
301         } else if (type.contains("floatlist")) {
302                 if (type.contains("\"figure"))
303                         type = "figure";
304                 else if (type.contains("\"table"))
305                         type = "table";
306                 else if (type.contains("\"algorithm"))
307                         type = "algorithm";
308         }
309         return type;
310 }
311
312
313 void TocWidget::init(QString const & str)
314 {
315         int new_index;
316         if (str.isEmpty())
317                 new_index = typeCO->findData(current_type_);
318         else
319                 new_index = typeCO->findData(decodeType(str));
320
321         // If everything else fails, settle on the table of contents which is
322         // guaranted to exist.
323         if (new_index == -1) {
324                 current_type_ = "tableofcontents";
325                 new_index = typeCO->findData(current_type_);
326         }
327
328         typeCO->blockSignals(true);
329         typeCO->setCurrentIndex(new_index);
330         typeCO->blockSignals(false);
331 }
332
333 } // namespace frontend
334 } // namespace lyx
335
336 #include "TocWidget_moc.cpp"