]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
1194cc8d5d9458e78784ed81220bf70f9680b64e
[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 "GuiView.h"
17 #include "qt_helpers.h"
18 #include "TocModel.h"
19
20 #include "Buffer.h"
21 #include "FuncRequest.h"
22 #include "LyXFunc.h"
23
24 #include "support/debug.h"
25 #include "support/lassert.h"
26
27 #include <QHeaderView>
28 #include <QTimer>
29
30 #include <vector>
31
32 using namespace std;
33
34 namespace lyx {
35 namespace frontend {
36
37 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
38         : QWidget(parent), depth_(0), gui_view_(gui_view)
39 {
40         setupUi(this);
41
42         moveOutTB->setIcon(QIcon(":/images/promote.png"));
43         moveInTB->setIcon(QIcon(":/images/demote.png"));
44         moveUpTB->setIcon(QIcon(":/images/up.png"));
45         moveDownTB->setIcon(QIcon(":/images/down.png"));
46         updateTB->setIcon(QIcon(":/images/reload.png"));
47
48         // avoid flickering
49         tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
50
51         tocTV->showColumn(0);
52
53         // hide the pointless QHeader for now
54         // in the future, new columns may appear
55         // like labels, bookmarks, etc...
56         // tocTV->header()->hide();
57         tocTV->header()->setVisible(false);
58
59         // Only one item selected at a time.
60         tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
61
62         // The toc types combo won't change its model.
63         typeCO->setModel(gui_view_.tocModels().nameModel());
64 }
65
66
67 void TocWidget::on_tocTV_activated(QModelIndex const & index)
68 {
69         goTo(index);
70 }
71
72
73 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
74 {
75         goTo(index);
76         gui_view_.setFocus();
77 }
78
79
80 void TocWidget::goTo(QModelIndex const & index)
81 {
82         LYXERR(Debug::GUI, "goto " << index.row()
83                 << ", " << index.column());
84
85         gui_view_.tocModels().goTo(current_type_, index);
86 }
87
88
89 void TocWidget::on_updateTB_clicked()
90 {
91         // The backend update can take some time so we disable
92         // the controls while waiting.
93         enableControls(false);
94         gui_view_.tocModels().updateBackend();
95 }
96
97
98 /* FIXME (Ugras 17/11/06):
99 I have implemented a indexDepth function to get the model indices. In my
100 opinion, somebody should derive a new qvariant class for tocModelItem
101 which saves the string data and depth information. That will save the
102 depth calculation.  */
103
104 static int indexDepth(QModelIndex const & index, int depth = -1)
105 {
106         ++depth;
107         return index.parent() == QModelIndex()
108                 ? depth : indexDepth(index.parent(), depth);
109 }
110
111
112 void TocWidget::on_depthSL_valueChanged(int depth)
113 {
114         if (depth == depth_)
115                 return;
116         setTreeDepth(depth);
117         gui_view_.setFocus();
118 }
119
120
121 void TocWidget::setTreeDepth(int depth)
122 {
123         depth_ = depth;
124         if (!tocTV->model())
125                 return;
126
127         // expanding and then collapsing is probably better,
128         // but my qt 4.1.2 doesn't have expandAll()..
129         //tocTV->expandAll();
130         QModelIndexList indices = tocTV->model()->match(
131                 tocTV->model()->index(0,0),
132                 Qt::DisplayRole, "*", -1,
133                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
134
135         int size = indices.size();
136         for (int i = 0; i < size; i++) {
137                 QModelIndex index = indices[i];
138                 tocTV->setExpanded(index, indexDepth(index) < depth_);
139         }
140 }
141
142
143 void TocWidget::on_typeCO_currentIndexChanged(int index)
144 {
145         current_type_ = typeCO->itemData(index).toString();
146         updateView();
147         gui_view_.setFocus();
148 }
149
150
151 void TocWidget::outline(int func_code)
152 {
153         enableControls(false);
154         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
155         if (list.isEmpty())
156                 return;
157         enableControls(false);
158         goTo(list[0]);
159         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
160         enableControls(true);
161         gui_view_.setFocus();
162 }
163
164
165 void TocWidget::on_moveUpTB_clicked()
166 {
167         outline(LFUN_OUTLINE_UP);
168 }
169
170
171 void TocWidget::on_moveDownTB_clicked()
172 {
173         outline(LFUN_OUTLINE_DOWN);
174 }
175
176
177 void TocWidget::on_moveInTB_clicked()
178 {
179         outline(LFUN_OUTLINE_IN);
180 }
181
182
183 void TocWidget::on_moveOutTB_clicked()
184 {
185         outline(LFUN_OUTLINE_OUT);
186 }
187
188
189 void TocWidget::select(QModelIndex const & index)
190 {
191         if (!index.isValid()) {
192                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
193                 return;
194         }
195
196         tocTV->scrollTo(index);
197         tocTV->clearSelection();
198         tocTV->setCurrentIndex(index);
199 }
200
201
202 /// Test if outlining operation is possible
203 static bool canOutline(QString const & type)
204 {
205         return type == "tableofcontents";
206 }
207
208
209 void TocWidget::enableControls(bool enable)
210 {
211         updateTB->setEnabled(enable);
212
213         if (!canOutline(current_type_))
214                 enable = false;
215
216         moveUpTB->setEnabled(enable);
217         moveDownTB->setEnabled(enable);
218         moveInTB->setEnabled(enable);
219         moveOutTB->setEnabled(enable);
220 }
221
222
223 /// Test if synchronized navigation is possible
224 static bool canNavigate(QString const & type)
225 {
226         // It is not possible to have synchronous navigation in a correctl
227         // and efficient way with the label type because Toc::item() do a linear
228         // seatch. Even if fixed, it might even not be desirable to do so if we 
229         // want to support drag&drop of labels and references.
230         return type != "label";
231 }
232
233
234 void TocWidget::updateView()
235 {
236         if (!gui_view_.view()) {
237                 enableControls(false);
238                 typeCO->setEnabled(false);
239                 tocTV->setModel(0);
240                 tocTV->setEnabled(false);
241                 return;
242         }
243         typeCO->setEnabled(true);
244         tocTV->setEnabled(true);
245
246         QStandardItemModel * toc_model = gui_view_.tocModels().model(current_type_);    
247         if (tocTV->model() != toc_model) {
248                 tocTV->setModel(toc_model);
249                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
250         }
251         bool controls_enabled = toc_model && toc_model->rowCount() > 0
252                 && !gui_view_.buffer()->isReadonly();
253         enableControls(controls_enabled);
254
255         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
256         depthSL->setValue(depth_);
257         setTreeDepth(depth_);
258         if (canNavigate(current_type_))
259                 select(gui_view_.tocModels().currentIndex(current_type_));
260 }
261
262
263 static QString decodeType(QString const & str)
264 {
265         QString type = str;
266         if (type.contains("tableofcontents")) {
267                 type = "tableofcontents";
268         } else if (type.contains("floatlist")) {
269                 if (type.contains("\"figure"))
270                         type = "figure";
271                 else if (type.contains("\"table"))
272                         type = "table";
273                 else if (type.contains("\"algorithm"))
274                         type = "algorithm";
275         }
276         return type;
277 }
278
279
280 void TocWidget::init(QString const & str)
281 {
282         int new_index;
283         if (str.isEmpty())
284                 new_index = typeCO->findData(current_type_);
285         else
286                 new_index = typeCO->findData(decodeType(str));
287
288         // If everything else fails, settle on the table of contents which is
289         // guaranted to exist.
290         if (new_index == -1) {
291                 current_type_ = "tableofcontents";
292                 new_index = typeCO->findData(current_type_);
293         }
294
295         typeCO->blockSignals(true);
296         typeCO->setCurrentIndex(new_index);
297         typeCO->blockSignals(false);
298 }
299
300 } // namespace frontend
301 } // namespace lyx
302
303 #include "TocWidget_moc.cpp"