]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
acf6ecb39cb42c479fb15d7370eed73c95cd7ea9
[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         switch (cmd.action)
136         {
137         case LFUN_CHANGE_ACCEPT:
138         case LFUN_CHANGE_REJECT:
139         case LFUN_OUTLINE_UP:
140         case LFUN_OUTLINE_DOWN:
141         case LFUN_OUTLINE_IN:
142         case LFUN_OUTLINE_OUT:
143                 status.setEnabled(true);
144                 return true;
145
146         default:
147                 if (inset)
148                         return inset->getStatus(cur, tmpcmd, status);
149         }
150
151         return false;
152 }
153
154
155 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
156 {
157         Inset * inset = itemInset();
158         FuncRequest tmpcmd(cmd);
159
160         QModelIndex const & index = tocTV->currentIndex();
161         TocItem const & item =
162                 gui_view_.tocModels().currentItem(current_type_, index);
163
164         switch (cmd.action)
165         {
166         case LFUN_CHANGE_ACCEPT:
167         case LFUN_CHANGE_REJECT:
168                 dispatch(item.action());
169                 cur.dispatch(tmpcmd);
170                 break;
171         
172         case LFUN_OUTLINE_UP:
173         case LFUN_OUTLINE_DOWN:
174         case LFUN_OUTLINE_IN:
175         case LFUN_OUTLINE_OUT:
176                 outline(cmd.action);
177                 break;
178
179         default:
180                 if (inset)
181                         inset->dispatch(cur, tmpcmd);
182         }
183 }
184
185
186 void TocWidget::on_tocTV_activated(QModelIndex const & index)
187 {
188         goTo(index);
189 }
190
191
192 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
193 {
194         Qt::MouseButtons const button = QApplication::mouseButtons();
195         if (button & Qt::LeftButton) {
196                 goTo(index);
197                 gui_view_.setFocus();
198         }
199 }
200
201
202 void TocWidget::goTo(QModelIndex const & index)
203 {
204         LYXERR(Debug::GUI, "goto " << index.row()
205                 << ", " << index.column());
206
207         gui_view_.tocModels().goTo(current_type_, index);
208 }
209
210
211 void TocWidget::on_updateTB_clicked()
212 {
213         // The backend update can take some time so we disable
214         // the controls while waiting.
215         enableControls(false);
216         gui_view_.tocModels().updateBackend();
217 }
218
219
220 void TocWidget::on_sortCB_stateChanged(int state)
221 {
222         gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
223         updateView();
224 }
225
226 void TocWidget::on_persistentCB_stateChanged(int state)
227 {
228         persistent_ = state == Qt::Checked;
229 }
230
231
232 /* FIXME (Ugras 17/11/06):
233 I have implemented a indexDepth function to get the model indices. In my
234 opinion, somebody should derive a new qvariant class for tocModelItem
235 which saves the string data and depth information. That will save the
236 depth calculation.  */
237
238 static int indexDepth(QModelIndex const & index, int depth = -1)
239 {
240         ++depth;
241         return index.parent() == QModelIndex()
242                 ? depth : indexDepth(index.parent(), depth);
243 }
244
245
246 void TocWidget::on_depthSL_valueChanged(int depth)
247 {
248         if (depth == depth_)
249                 return;
250         setTreeDepth(depth);
251         gui_view_.setFocus();
252 }
253
254
255 void TocWidget::setTreeDepth(int depth)
256 {
257         depth_ = depth;
258         if (!tocTV->model())
259                 return;
260
261 #if QT_VERSION >= 0x040300
262         // this should be faster than our own code below
263         if (depth == 0)
264                 tocTV->collapseAll();
265         else
266                 tocTV->expandToDepth(depth - 1);
267 #else
268         // expanding and then collapsing is probably better,
269         // but my qt 4.1.2 doesn't have expandAll()..
270         //tocTV->expandAll();
271         QModelIndexList indices = tocTV->model()->match(
272                 tocTV->model()->index(0, 0),
273                 Qt::DisplayRole, "*", -1,
274                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
275
276         int size = indices.size();
277         for (int i = 0; i < size; i++) {
278                 QModelIndex index = indices[i];
279                 tocTV->setExpanded(index, indexDepth(index) < depth_);
280         }
281 #endif
282 }
283
284
285 void TocWidget::on_typeCO_currentIndexChanged(int index)
286 {
287         current_type_ = typeCO->itemData(index).toString();
288         updateView();
289         gui_view_.setFocus();
290 }
291
292
293 void TocWidget::outline(int func_code)
294 {
295         enableControls(false);
296         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
297         if (list.isEmpty())
298                 return;
299         enableControls(false);
300         goTo(list[0]);
301         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
302         enableControls(true);
303         gui_view_.setFocus();
304 }
305
306
307 void TocWidget::on_moveUpTB_clicked()
308 {
309         outline(LFUN_OUTLINE_UP);
310 }
311
312
313 void TocWidget::on_moveDownTB_clicked()
314 {
315         outline(LFUN_OUTLINE_DOWN);
316 }
317
318
319 void TocWidget::on_moveInTB_clicked()
320 {
321         outline(LFUN_OUTLINE_IN);
322 }
323
324
325 void TocWidget::on_moveOutTB_clicked()
326 {
327         outline(LFUN_OUTLINE_OUT);
328 }
329
330
331 void TocWidget::select(QModelIndex const & index)
332 {
333         if (!index.isValid()) {
334                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
335                 return;
336         }
337
338         tocTV->scrollTo(index);
339         tocTV->clearSelection();
340         tocTV->setCurrentIndex(index);
341 }
342
343
344 /// Test whether outlining operation is possible
345 static bool canOutline(QString const & type)
346 {
347         return type == "tableofcontents";
348 }
349
350
351 void TocWidget::enableControls(bool enable)
352 {
353         updateTB->setEnabled(enable);
354
355         if (!canOutline(current_type_))
356                 enable = false;
357
358         moveUpTB->setEnabled(enable);
359         moveDownTB->setEnabled(enable);
360         moveInTB->setEnabled(enable);
361         moveOutTB->setEnabled(enable);
362 }
363
364
365 /// Test whether synchronized navigation is possible
366 static bool canNavigate(QString const & type)
367 {
368         // It is not possible to have synchronous navigation in a correct
369         // and efficient way with the label and change type because Toc::item()
370         // does a linear search. Even when fixed, it might even not be desirable
371         // to do so if we want to support drag&drop of labels and references.
372         return type != "label" && type != "change";
373 }
374
375
376 void TocWidget::updateView()
377 {
378         if (!gui_view_.view()) {
379                 enableControls(false);
380                 typeCO->setEnabled(false);
381                 tocTV->setModel(0);
382                 tocTV->setEnabled(false);
383                 depthSL->setMaximum(0);
384                 depthSL->setValue(0);
385                 persistentCB->setEnabled(false);
386                 sortCB->setEnabled(false);
387                 depthSL->setEnabled(false);
388                 return;
389         }
390         sortCB->setEnabled(true);
391         depthSL->setEnabled(true);
392         typeCO->setEnabled(true);
393         tocTV->setEnabled(false);
394         tocTV->setUpdatesEnabled(false);
395
396         QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
397         if (tocTV->model() != toc_model) {
398                 tocTV->setModel(toc_model);
399                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
400                 if (persistent_)
401                         setTreeDepth(depth_);
402         }
403
404         sortCB->blockSignals(true);
405         sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
406         sortCB->blockSignals(false);
407
408         
409         bool const can_navigate_ = canNavigate(current_type_);
410         persistentCB->setEnabled(can_navigate_);
411
412         bool controls_enabled = toc_model && toc_model->rowCount() > 0
413                 && !gui_view_.buffer()->isReadonly();
414         enableControls(controls_enabled);
415
416         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
417         depthSL->setValue(depth_);
418         if (!persistent_ && can_navigate_)
419                 setTreeDepth(depth_);
420         if (can_navigate_) {
421                 persistentCB->setChecked(persistent_);
422                 select(gui_view_.tocModels().currentIndex(current_type_));
423         }
424         tocTV->setEnabled(true);
425         tocTV->setUpdatesEnabled(true);
426 }
427
428
429 static QString decodeType(QString const & str)
430 {
431         QString type = str;
432         if (type.contains("tableofcontents")) {
433                 type = "tableofcontents";
434         } else if (type.contains("floatlist")) {
435                 if (type.contains("\"figure"))
436                         type = "figure";
437                 else if (type.contains("\"table"))
438                         type = "table";
439                 else if (type.contains("\"algorithm"))
440                         type = "algorithm";
441         }
442         return type;
443 }
444
445
446 void TocWidget::init(QString const & str)
447 {
448         int new_index;
449         if (str.isEmpty())
450                 new_index = typeCO->findData(current_type_);
451         else
452                 new_index = typeCO->findData(decodeType(str));
453
454         // If everything else fails, settle on the table of contents which is
455         // guaranted to exist.
456         if (new_index == -1) {
457                 current_type_ = "tableofcontents";
458                 new_index = typeCO->findData(current_type_);
459         } else {
460                 current_type_ = typeCO->itemData(new_index).toString();
461         }
462
463         typeCO->blockSignals(true);
464         typeCO->setCurrentIndex(new_index);
465         typeCO->blockSignals(false);
466 }
467
468 } // namespace frontend
469 } // namespace lyx
470
471 #include "moc_TocWidget.cpp"