]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
* TocWidget.cpp: disable sort option for TOC (bug 5983).
[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 "CutAndPaste.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
25 #include "LyXFunc.h"
26 #include "Menus.h"
27 #include "TocBackend.h"
28
29 #include "insets/InsetCommand.h"
30 #include "insets/InsetRef.h"
31
32 #include "support/debug.h"
33 #include "support/lassert.h"
34
35 #include <QHeaderView>
36 #include <QMenu>
37 #include <QTimer>
38
39 #include <vector>
40
41 using namespace std;
42
43 namespace lyx {
44 namespace frontend {
45
46 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
47         : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
48 {
49         setupUi(this);
50
51         moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
52         moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
53         moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
54         moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
55         updateTB->setIcon(QIcon(getPixmap("images/", "reload", "png")));
56
57         // avoid flickering
58         tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
59
60         tocTV->showColumn(0);
61
62         // hide the pointless QHeader for now
63         // in the future, new columns may appear
64         // like labels, bookmarks, etc...
65         // tocTV->header()->hide();
66         tocTV->header()->setVisible(false);
67
68         // Only one item selected at a time.
69         tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
70
71         // The toc types combo won't change its model.
72         typeCO->setModel(gui_view_.tocModels().nameModel());
73
74         // Make sure the buttons are disabled when first shown without a loaded
75         // Buffer.
76         enableControls(false);
77
78         // make us responsible for the context menu of the tabbar
79         setContextMenuPolicy(Qt::CustomContextMenu);
80         connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
81                 this, SLOT(showContextMenu(const QPoint &)));
82         connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
83                 this, SLOT(showContextMenu(const QPoint &)));
84
85         init(QString());
86 }
87
88
89 void TocWidget::showContextMenu(const QPoint & pos)
90 {
91         std::string name = "context-toc-" + fromqstr(current_type_);
92         QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
93         if (!menu)
94                 return; 
95         menu->exec(mapToGlobal(pos));
96 }
97
98
99 Inset * TocWidget::itemInset() const
100 {
101         QModelIndex const & index = tocTV->currentIndex();
102         TocItem const & item =
103                 gui_view_.tocModels().currentItem(current_type_, index);
104         DocIterator const & dit = item.dit();
105         
106         Inset * inset = 0;
107         if (current_type_ == "label" 
108                   || current_type_ == "graphics"
109                   || current_type_ == "citation"
110                   || current_type_ == "child")
111                 inset = dit.nextInset();
112
113         else if (current_type_ == "branch"
114                          || current_type_ == "index"
115                          || current_type_ == "change")
116                 inset = &dit.inset();
117
118         else if (current_type_ == "table" 
119                      || current_type_ == "listing"
120                      || current_type_ == "figure") {
121                 DocIterator tmp_dit(dit);
122                 tmp_dit.pop_back();
123                 inset = &tmp_dit.inset();
124         }
125         return inset;
126 }
127
128
129 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
130         FuncStatus & status) const
131 {
132         Inset * inset = itemInset();
133         FuncRequest tmpcmd(cmd);
134
135         QModelIndex const & index = tocTV->currentIndex();
136         TocItem const & item =
137                 gui_view_.tocModels().currentItem(current_type_, index);
138
139         switch (cmd.action)
140         {
141         case LFUN_CHANGE_ACCEPT:
142         case LFUN_CHANGE_REJECT:
143         case LFUN_OUTLINE_UP:
144         case LFUN_OUTLINE_DOWN:
145         case LFUN_OUTLINE_IN:
146         case LFUN_OUTLINE_OUT:
147         case LFUN_SECTION_SELECT:
148                 status.setEnabled(true);
149                 return true;
150
151         case LFUN_LABEL_COPY_AS_REF: {
152                 // For labels in math, we need to supply the label as a string
153                 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
154                 if (inset)
155                         return inset->getStatus(cur, label_copy, status);
156         }
157
158         default:
159                 if (inset)
160                         return inset->getStatus(cur, tmpcmd, status);
161         }
162
163         return false;
164 }
165
166
167 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
168 {
169         Inset * inset = itemInset();
170         FuncRequest tmpcmd(cmd);
171
172         QModelIndex const & index = tocTV->currentIndex();
173         TocItem const & item =
174                 gui_view_.tocModels().currentItem(current_type_, index);
175
176         switch (cmd.action)
177         {
178         case LFUN_CHANGE_ACCEPT:
179         case LFUN_CHANGE_REJECT:
180         case LFUN_SECTION_SELECT:
181                 dispatch(item.action());
182                 cur.dispatch(tmpcmd);
183                 break;
184
185         case LFUN_LABEL_COPY_AS_REF: {
186                 // For labels in math, we need to supply the label as a string
187                 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
188                 if (inset)
189                         inset->dispatch(cur, label_copy);
190                 break;
191         }
192         
193         case LFUN_OUTLINE_UP:
194         case LFUN_OUTLINE_DOWN:
195         case LFUN_OUTLINE_IN:
196         case LFUN_OUTLINE_OUT:
197                 outline(cmd.action);
198                 break;
199
200         default:
201                 if (inset)
202                         inset->dispatch(cur, tmpcmd);
203         }
204 }
205
206
207 void TocWidget::on_tocTV_activated(QModelIndex const & index)
208 {
209         goTo(index);
210 }
211
212
213 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
214 {
215         Qt::MouseButtons const button = QApplication::mouseButtons();
216         if (button & Qt::LeftButton) {
217                 goTo(index);
218                 gui_view_.setFocus();
219         }
220 }
221
222
223 void TocWidget::goTo(QModelIndex const & index)
224 {
225         LYXERR(Debug::GUI, "goto " << index.row()
226                 << ", " << index.column());
227
228         gui_view_.tocModels().goTo(current_type_, index);
229 }
230
231
232 void TocWidget::on_updateTB_clicked()
233 {
234         // The backend update can take some time so we disable
235         // the controls while waiting.
236         enableControls(false);
237         gui_view_.tocModels().updateBackend();
238 }
239
240
241 void TocWidget::on_sortCB_stateChanged(int state)
242 {
243         gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
244         updateView();
245 }
246
247
248 void TocWidget::on_persistentCB_stateChanged(int state)
249 {
250         persistent_ = state == Qt::Checked;
251 }
252
253
254 /* FIXME (Ugras 17/11/06):
255 I have implemented a indexDepth function to get the model indices. In my
256 opinion, somebody should derive a new qvariant class for tocModelItem
257 which saves the string data and depth information. That will save the
258 depth calculation.  */
259
260 static int indexDepth(QModelIndex const & index, int depth = -1)
261 {
262         ++depth;
263         return index.parent() == QModelIndex()
264                 ? depth : indexDepth(index.parent(), depth);
265 }
266
267
268 void TocWidget::on_depthSL_valueChanged(int depth)
269 {
270         if (depth == depth_)
271                 return;
272         setTreeDepth(depth);
273         gui_view_.setFocus();
274 }
275
276
277 void TocWidget::setTreeDepth(int depth)
278 {
279         depth_ = depth;
280         if (!tocTV->model())
281                 return;
282
283 #if QT_VERSION >= 0x040300
284         // this should be faster than our own code below
285         if (depth == 0)
286                 tocTV->collapseAll();
287         else
288                 tocTV->expandToDepth(depth - 1);
289 #else
290         // expanding and then collapsing is probably better,
291         // but my qt 4.1.2 doesn't have expandAll()..
292         //tocTV->expandAll();
293         QModelIndexList indices = tocTV->model()->match(
294                 tocTV->model()->index(0, 0),
295                 Qt::DisplayRole, "*", -1,
296                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
297
298         int size = indices.size();
299         for (int i = 0; i < size; i++) {
300                 QModelIndex index = indices[i];
301                 tocTV->setExpanded(index, indexDepth(index) < depth_);
302         }
303 #endif
304 }
305
306
307 void TocWidget::on_typeCO_currentIndexChanged(int index)
308 {
309         current_type_ = typeCO->itemData(index).toString();
310         updateView();
311         gui_view_.setFocus();
312 }
313
314
315 void TocWidget::outline(int func_code)
316 {
317         enableControls(false);
318         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
319         if (list.isEmpty())
320                 return;
321         enableControls(false);
322         goTo(list[0]);
323         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
324         enableControls(true);
325         gui_view_.setFocus();
326 }
327
328
329 void TocWidget::on_moveUpTB_clicked()
330 {
331         outline(LFUN_OUTLINE_UP);
332 }
333
334
335 void TocWidget::on_moveDownTB_clicked()
336 {
337         outline(LFUN_OUTLINE_DOWN);
338 }
339
340
341 void TocWidget::on_moveInTB_clicked()
342 {
343         outline(LFUN_OUTLINE_IN);
344 }
345
346
347 void TocWidget::on_moveOutTB_clicked()
348 {
349         outline(LFUN_OUTLINE_OUT);
350 }
351
352
353 void TocWidget::select(QModelIndex const & index)
354 {
355         if (!index.isValid()) {
356                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
357                 return;
358         }
359
360         tocTV->scrollTo(index);
361         tocTV->clearSelection();
362         tocTV->setCurrentIndex(index);
363 }
364
365
366 /// Test whether outlining operation is possible
367 static bool canOutline(QString const & type)
368 {
369         return type == "tableofcontents";
370 }
371
372
373 void TocWidget::enableControls(bool enable)
374 {
375         updateTB->setEnabled(enable);
376
377         if (!canOutline(current_type_))
378                 enable = false;
379
380         moveUpTB->setEnabled(enable);
381         moveDownTB->setEnabled(enable);
382         moveInTB->setEnabled(enable);
383         moveOutTB->setEnabled(enable);
384 }
385
386
387 /// Test whether synchronized navigation is possible
388 static bool canNavigate(QString const & type)
389 {
390         // It is not possible to have synchronous navigation in a correct
391         // and efficient way with the label and change type because Toc::item()
392         // does a linear search. Even when fixed, it might even not be desirable
393         // to do so if we want to support drag&drop of labels and references.
394         return type != "label" && type != "change";
395 }
396
397
398 /// Test whether sorting is possible
399 static bool isSortable(QString const & type)
400 {
401         return type != "tableofcontents";
402 }
403
404
405 void TocWidget::updateView()
406 {
407         if (!gui_view_.view()) {
408                 enableControls(false);
409                 typeCO->setEnabled(false);
410                 tocTV->setModel(0);
411                 tocTV->setEnabled(false);
412                 depthSL->setMaximum(0);
413                 depthSL->setValue(0);
414                 persistentCB->setEnabled(false);
415                 sortCB->setEnabled(false);
416                 depthSL->setEnabled(false);
417                 return;
418         }
419         sortCB->setEnabled(isSortable(current_type_));
420         depthSL->setEnabled(true);
421         typeCO->setEnabled(true);
422         tocTV->setEnabled(false);
423         tocTV->setUpdatesEnabled(false);
424
425         QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
426         if (tocTV->model() != toc_model) {
427                 tocTV->setModel(toc_model);
428                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
429                 if (persistent_)
430                         setTreeDepth(depth_);
431         }
432
433         sortCB->blockSignals(true);
434         sortCB->setChecked(isSortable(current_type_)
435                 && gui_view_.tocModels().isSorted(current_type_));
436         sortCB->blockSignals(false);
437
438         
439         bool const can_navigate_ = canNavigate(current_type_);
440         persistentCB->setEnabled(can_navigate_);
441
442         bool controls_enabled = toc_model && toc_model->rowCount() > 0
443                 && !gui_view_.buffer()->isReadonly();
444         enableControls(controls_enabled);
445
446         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
447         depthSL->setValue(depth_);
448         if (!persistent_ && can_navigate_)
449                 setTreeDepth(depth_);
450         if (can_navigate_) {
451                 persistentCB->setChecked(persistent_);
452                 select(gui_view_.tocModels().currentIndex(current_type_));
453         }
454         tocTV->setEnabled(true);
455         tocTV->setUpdatesEnabled(true);
456 }
457
458
459 static QString decodeType(QString const & str)
460 {
461         QString type = str;
462         if (type.contains("tableofcontents")) {
463                 type = "tableofcontents";
464         } else if (type.contains("floatlist")) {
465                 if (type.contains("\"figure"))
466                         type = "figure";
467                 else if (type.contains("\"table"))
468                         type = "table";
469                 else if (type.contains("\"algorithm"))
470                         type = "algorithm";
471         }
472         return type;
473 }
474
475
476 void TocWidget::init(QString const & str)
477 {
478         int new_index;
479         if (str.isEmpty())
480                 new_index = typeCO->findData(current_type_);
481         else
482                 new_index = typeCO->findData(decodeType(str));
483
484         // If everything else fails, settle on the table of contents which is
485         // guaranted to exist.
486         if (new_index == -1) {
487                 current_type_ = "tableofcontents";
488                 new_index = typeCO->findData(current_type_);
489         } else {
490                 current_type_ = typeCO->itemData(new_index).toString();
491         }
492
493         typeCO->blockSignals(true);
494         typeCO->setCurrentIndex(new_index);
495         typeCO->blockSignals(false);
496 }
497
498 } // namespace frontend
499 } // namespace lyx
500
501 #include "moc_TocWidget.cpp"