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