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