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