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