]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
TOC context menu (part 4)
[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 "LyXFunc.h"
25 #include "Menus.h"
26 #include "TocBackend.h"
27
28 #include "insets/InsetCommand.h"
29 #include "insets/InsetRef.h"
30
31 #include "support/debug.h"
32 #include "support/lassert.h"
33
34 #include <QHeaderView>
35 #include <QMenu>
36 #include <QTimer>
37
38 #include <vector>
39
40 using namespace std;
41
42 namespace lyx {
43 namespace frontend {
44
45 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
46         : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
47 {
48         setupUi(this);
49
50         moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
51         moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
52         moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
53         moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
54         updateTB->setIcon(QIcon(getPixmap("images/", "reload", "png")));
55
56         // avoid flickering
57         tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
58
59         tocTV->showColumn(0);
60
61         // hide the pointless QHeader for now
62         // in the future, new columns may appear
63         // like labels, bookmarks, etc...
64         // tocTV->header()->hide();
65         tocTV->header()->setVisible(false);
66
67         // Only one item selected at a time.
68         tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
69
70         // The toc types combo won't change its model.
71         typeCO->setModel(gui_view_.tocModels().nameModel());
72
73         // Make sure the buttons are disabled when first shown without a loaded
74         // Buffer.
75         enableControls(false);
76
77         // make us responsible for the context menu of the tabbar
78         setContextMenuPolicy(Qt::CustomContextMenu);
79         connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
80                 this, SLOT(showContextMenu(const QPoint &)));
81         connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
82                 this, SLOT(showContextMenu(const QPoint &)));
83
84         init(QString());
85 }
86
87
88 void TocWidget::showContextMenu(const QPoint & pos)
89 {
90         std::string name = "context-toc-" + fromqstr(current_type_);
91         QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
92         if (!menu)
93                 return; 
94         menu->exec(mapToGlobal(pos));
95 }
96
97
98 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
99 {
100         QModelIndex const & index = tocTV->currentIndex();
101         TocItem const & item =
102                 gui_view_.tocModels().currentItem(current_type_, index);
103         DocIterator const & dit = item.dit();
104         
105         Inset * inset = 0;
106         if (current_type_ == "label")
107                 inset = dit.nextInset();
108
109         if (inset)
110                 inset->dispatch(cur, FuncRequest(cmd));
111 }
112
113
114 void TocWidget::on_tocTV_activated(QModelIndex const & index)
115 {
116         goTo(index);
117 }
118
119
120 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
121 {
122         Qt::MouseButtons const button = QApplication::mouseButtons();
123         if (button & Qt::LeftButton) {
124                 goTo(index);
125                 gui_view_.setFocus();
126         }
127 }
128
129
130 void TocWidget::goTo(QModelIndex const & index)
131 {
132         LYXERR(Debug::GUI, "goto " << index.row()
133                 << ", " << index.column());
134
135         gui_view_.tocModels().goTo(current_type_, index);
136 }
137
138
139 void TocWidget::on_updateTB_clicked()
140 {
141         // The backend update can take some time so we disable
142         // the controls while waiting.
143         enableControls(false);
144         gui_view_.tocModels().updateBackend();
145 }
146
147
148 void TocWidget::on_sortCB_stateChanged(int state)
149 {
150         gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
151         updateView();
152 }
153
154 void TocWidget::on_persistentCB_stateChanged(int state)
155 {
156         persistent_ = state == Qt::Checked;
157 }
158
159
160 /* FIXME (Ugras 17/11/06):
161 I have implemented a indexDepth function to get the model indices. In my
162 opinion, somebody should derive a new qvariant class for tocModelItem
163 which saves the string data and depth information. That will save the
164 depth calculation.  */
165
166 static int indexDepth(QModelIndex const & index, int depth = -1)
167 {
168         ++depth;
169         return index.parent() == QModelIndex()
170                 ? depth : indexDepth(index.parent(), depth);
171 }
172
173
174 void TocWidget::on_depthSL_valueChanged(int depth)
175 {
176         if (depth == depth_)
177                 return;
178         setTreeDepth(depth);
179         gui_view_.setFocus();
180 }
181
182
183 void TocWidget::setTreeDepth(int depth)
184 {
185         depth_ = depth;
186         if (!tocTV->model())
187                 return;
188
189 #if QT_VERSION >= 0x040300
190         // this should be faster than our own code below
191         if (depth == 0)
192                 tocTV->collapseAll();
193         else
194                 tocTV->expandToDepth(depth - 1);
195 #else
196         // expanding and then collapsing is probably better,
197         // but my qt 4.1.2 doesn't have expandAll()..
198         //tocTV->expandAll();
199         QModelIndexList indices = tocTV->model()->match(
200                 tocTV->model()->index(0, 0),
201                 Qt::DisplayRole, "*", -1,
202                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
203
204         int size = indices.size();
205         for (int i = 0; i < size; i++) {
206                 QModelIndex index = indices[i];
207                 tocTV->setExpanded(index, indexDepth(index) < depth_);
208         }
209 #endif
210 }
211
212
213 void TocWidget::on_typeCO_currentIndexChanged(int index)
214 {
215         current_type_ = typeCO->itemData(index).toString();
216         updateView();
217         gui_view_.setFocus();
218 }
219
220
221 void TocWidget::outline(int func_code)
222 {
223         enableControls(false);
224         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
225         if (list.isEmpty())
226                 return;
227         enableControls(false);
228         goTo(list[0]);
229         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
230         enableControls(true);
231         gui_view_.setFocus();
232 }
233
234
235 void TocWidget::on_moveUpTB_clicked()
236 {
237         outline(LFUN_OUTLINE_UP);
238 }
239
240
241 void TocWidget::on_moveDownTB_clicked()
242 {
243         outline(LFUN_OUTLINE_DOWN);
244 }
245
246
247 void TocWidget::on_moveInTB_clicked()
248 {
249         outline(LFUN_OUTLINE_IN);
250 }
251
252
253 void TocWidget::on_moveOutTB_clicked()
254 {
255         outline(LFUN_OUTLINE_OUT);
256 }
257
258
259 void TocWidget::select(QModelIndex const & index)
260 {
261         if (!index.isValid()) {
262                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
263                 return;
264         }
265
266         tocTV->scrollTo(index);
267         tocTV->clearSelection();
268         tocTV->setCurrentIndex(index);
269 }
270
271
272 /// Test if outlining operation is possible
273 static bool canOutline(QString const & type)
274 {
275         return type == "tableofcontents";
276 }
277
278
279 void TocWidget::enableControls(bool enable)
280 {
281         updateTB->setEnabled(enable);
282
283         if (!canOutline(current_type_))
284                 enable = false;
285
286         moveUpTB->setEnabled(enable);
287         moveDownTB->setEnabled(enable);
288         moveInTB->setEnabled(enable);
289         moveOutTB->setEnabled(enable);
290         if (!enable) {
291                 depthSL->setMaximum(0);
292                 depthSL->setValue(0);
293         }
294 }
295
296
297 /// Test if synchronized navigation is possible
298 static bool canNavigate(QString const & type)
299 {
300         // It is not possible to have synchronous navigation in a correctl
301         // and efficient way with the label type because Toc::item() do a linear
302         // seatch. Even if fixed, it might even not be desirable to do so if we 
303         // want to support drag&drop of labels and references.
304         return type != "label" && type != "change";
305 }
306
307
308 void TocWidget::updateView()
309 {
310         if (!gui_view_.view()) {
311                 enableControls(false);
312                 typeCO->setEnabled(false);
313                 tocTV->setModel(0);
314                 tocTV->setEnabled(false);
315                 return;
316         }
317         typeCO->setEnabled(true);
318         tocTV->setEnabled(false);
319         tocTV->setUpdatesEnabled(false);
320
321         QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
322         if (tocTV->model() != toc_model) {
323                 tocTV->setModel(toc_model);
324                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
325                 if (persistent_)
326                         setTreeDepth(depth_);
327         }
328
329         sortCB->blockSignals(true);
330         sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
331         sortCB->blockSignals(false);
332
333         persistentCB->setChecked(persistent_);
334
335         bool controls_enabled = toc_model && toc_model->rowCount() > 0
336                 && !gui_view_.buffer()->isReadonly();
337         enableControls(controls_enabled);
338
339         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
340         depthSL->setValue(depth_);
341         if (!persistent_)
342                 setTreeDepth(depth_);
343         if (canNavigate(current_type_))
344                 select(gui_view_.tocModels().currentIndex(current_type_));
345         tocTV->setEnabled(true);
346         tocTV->setUpdatesEnabled(true);
347 }
348
349
350 static QString decodeType(QString const & str)
351 {
352         QString type = str;
353         if (type.contains("tableofcontents")) {
354                 type = "tableofcontents";
355         } else if (type.contains("floatlist")) {
356                 if (type.contains("\"figure"))
357                         type = "figure";
358                 else if (type.contains("\"table"))
359                         type = "table";
360                 else if (type.contains("\"algorithm"))
361                         type = "algorithm";
362         }
363         return type;
364 }
365
366
367 void TocWidget::init(QString const & str)
368 {
369         int new_index;
370         if (str.isEmpty())
371                 new_index = typeCO->findData(current_type_);
372         else
373                 new_index = typeCO->findData(decodeType(str));
374
375         // If everything else fails, settle on the table of contents which is
376         // guaranted to exist.
377         if (new_index == -1) {
378                 current_type_ = "tableofcontents";
379                 new_index = typeCO->findData(current_type_);
380         } else {
381                 current_type_ = typeCO->itemData(new_index).toString();
382         }
383
384         typeCO->blockSignals(true);
385         typeCO->setCurrentIndex(new_index);
386         typeCO->blockSignals(false);
387 }
388
389 } // namespace frontend
390 } // namespace lyx
391
392 #include "moc_TocWidget.cpp"