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