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