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