]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
9aa2c07b78b7bbe44ed6bc6ad1de1a0350663e95
[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 void TocWidget::updateView()
224 {
225         if (!gui_view_.view()) {
226                 enableControls(false);
227                 typeCO->setEnabled(false);
228                 tocTV->setModel(0);
229                 tocTV->setEnabled(false);
230                 return;
231         }
232         typeCO->setEnabled(true);
233         tocTV->setEnabled(true);
234
235         QStandardItemModel * toc_model = gui_view_.tocModels().model(current_type_);    
236         if (tocTV->model() != toc_model) {
237                 tocTV->setModel(toc_model);
238                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
239         }
240         bool controls_enabled = toc_model && toc_model->rowCount() > 0
241                 && !gui_view_.buffer()->isReadonly();
242         enableControls(controls_enabled);
243
244         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
245         depthSL->setValue(depth_);
246         setTreeDepth(depth_);
247         select(gui_view_.tocModels().currentIndex(current_type_));
248 }
249
250
251 static QString decodeType(QString const & str)
252 {
253         QString type = str;
254         if (type.contains("tableofcontents")) {
255                 type = "tableofcontents";
256         } else if (type.contains("floatlist")) {
257                 if (type.contains("\"figure"))
258                         type = "figure";
259                 else if (type.contains("\"table"))
260                         type = "table";
261                 else if (type.contains("\"algorithm"))
262                         type = "algorithm";
263         }
264         return type;
265 }
266
267
268 void TocWidget::init(QString const & str)
269 {
270         int new_index;
271         if (str.isEmpty())
272                 new_index = typeCO->findData(current_type_);
273         else
274                 new_index = typeCO->findData(decodeType(str));
275
276         // If everything else fails, settle on the table of contents which is
277         // guaranted to exist.
278         if (new_index == -1) {
279                 current_type_ = "tableofcontents";
280                 new_index = typeCO->findData(current_type_);
281         }
282
283         typeCO->blockSignals(true);
284         typeCO->setCurrentIndex(new_index);
285         typeCO->blockSignals(false);
286 }
287
288 } // namespace frontend
289 } // namespace lyx
290
291 #include "TocWidget_moc.cpp"