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