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