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