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