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