]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
fix critical part of http://bugzilla.lyx.org/show_bug.cgi?id=5082
[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         // Make sure the buttons are disabled when first shown without a loaded
66         // Buffer.
67         enableControls(false);
68 }
69
70
71 void TocWidget::on_tocTV_activated(QModelIndex const & index)
72 {
73         goTo(index);
74 }
75
76
77 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
78 {
79         goTo(index);
80         gui_view_.setFocus();
81 }
82
83
84 void TocWidget::goTo(QModelIndex const & index)
85 {
86         LYXERR(Debug::GUI, "goto " << index.row()
87                 << ", " << index.column());
88
89         gui_view_.tocModels().goTo(current_type_, index);
90 }
91
92
93 void TocWidget::on_updateTB_clicked()
94 {
95         // The backend update can take some time so we disable
96         // the controls while waiting.
97         enableControls(false);
98         gui_view_.tocModels().updateBackend();
99 }
100
101
102 /* FIXME (Ugras 17/11/06):
103 I have implemented a indexDepth function to get the model indices. In my
104 opinion, somebody should derive a new qvariant class for tocModelItem
105 which saves the string data and depth information. That will save the
106 depth calculation.  */
107
108 static int indexDepth(QModelIndex const & index, int depth = -1)
109 {
110         ++depth;
111         return index.parent() == QModelIndex()
112                 ? depth : indexDepth(index.parent(), depth);
113 }
114
115
116 void TocWidget::on_depthSL_valueChanged(int depth)
117 {
118         if (depth == depth_)
119                 return;
120         setTreeDepth(depth);
121         gui_view_.setFocus();
122 }
123
124
125 void TocWidget::setTreeDepth(int depth)
126 {
127         depth_ = depth;
128         if (!tocTV->model())
129                 return;
130
131         // expanding and then collapsing is probably better,
132         // but my qt 4.1.2 doesn't have expandAll()..
133         //tocTV->expandAll();
134         QModelIndexList indices = tocTV->model()->match(
135                 tocTV->model()->index(0,0),
136                 Qt::DisplayRole, "*", -1,
137                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
138
139         int size = indices.size();
140         for (int i = 0; i < size; i++) {
141                 QModelIndex index = indices[i];
142                 tocTV->setExpanded(index, indexDepth(index) < depth_);
143         }
144 }
145
146
147 void TocWidget::on_typeCO_currentIndexChanged(int index)
148 {
149         current_type_ = typeCO->itemData(index).toString();
150         updateView();
151         gui_view_.setFocus();
152 }
153
154
155 void TocWidget::outline(int func_code)
156 {
157         enableControls(false);
158         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
159         if (list.isEmpty())
160                 return;
161         enableControls(false);
162         goTo(list[0]);
163         dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
164         enableControls(true);
165         gui_view_.setFocus();
166 }
167
168
169 void TocWidget::on_moveUpTB_clicked()
170 {
171         outline(LFUN_OUTLINE_UP);
172 }
173
174
175 void TocWidget::on_moveDownTB_clicked()
176 {
177         outline(LFUN_OUTLINE_DOWN);
178 }
179
180
181 void TocWidget::on_moveInTB_clicked()
182 {
183         outline(LFUN_OUTLINE_IN);
184 }
185
186
187 void TocWidget::on_moveOutTB_clicked()
188 {
189         outline(LFUN_OUTLINE_OUT);
190 }
191
192
193 void TocWidget::select(QModelIndex const & index)
194 {
195         if (!index.isValid()) {
196                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
197                 return;
198         }
199
200         tocTV->scrollTo(index);
201         tocTV->clearSelection();
202         tocTV->setCurrentIndex(index);
203 }
204
205
206 /// Test if outlining operation is possible
207 static bool canOutline(QString const & type)
208 {
209         return type == "tableofcontents";
210 }
211
212
213 void TocWidget::enableControls(bool enable)
214 {
215         updateTB->setEnabled(enable);
216
217         if (!canOutline(current_type_))
218                 enable = false;
219
220         moveUpTB->setEnabled(enable);
221         moveDownTB->setEnabled(enable);
222         moveInTB->setEnabled(enable);
223         moveOutTB->setEnabled(enable);
224 }
225
226
227 /// Test if synchronized navigation is possible
228 static bool canNavigate(QString const & type)
229 {
230         // It is not possible to have synchronous navigation in a correctl
231         // and efficient way with the label type because Toc::item() do a linear
232         // seatch. Even if fixed, it might even not be desirable to do so if we 
233         // want to support drag&drop of labels and references.
234         return type != "label";
235 }
236
237
238 void TocWidget::updateView()
239 {
240         if (!gui_view_.view()) {
241                 enableControls(false);
242                 typeCO->setEnabled(false);
243                 tocTV->setModel(0);
244                 tocTV->setEnabled(false);
245                 return;
246         }
247         typeCO->setEnabled(true);
248         tocTV->setEnabled(true);
249
250         QStandardItemModel * toc_model = gui_view_.tocModels().model(current_type_);    
251         if (tocTV->model() != toc_model) {
252                 tocTV->setModel(toc_model);
253                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
254         }
255         bool controls_enabled = toc_model && toc_model->rowCount() > 0
256                 && !gui_view_.buffer()->isReadonly();
257         enableControls(controls_enabled);
258
259         depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
260         depthSL->setValue(depth_);
261         setTreeDepth(depth_);
262         if (canNavigate(current_type_))
263                 select(gui_view_.tocModels().currentIndex(current_type_));
264 }
265
266
267 static QString decodeType(QString const & str)
268 {
269         QString type = str;
270         if (type.contains("tableofcontents")) {
271                 type = "tableofcontents";
272         } else if (type.contains("floatlist")) {
273                 if (type.contains("\"figure"))
274                         type = "figure";
275                 else if (type.contains("\"table"))
276                         type = "table";
277                 else if (type.contains("\"algorithm"))
278                         type = "algorithm";
279         }
280         return type;
281 }
282
283
284 void TocWidget::init(QString const & str)
285 {
286         int new_index;
287         if (str.isEmpty())
288                 new_index = typeCO->findData(current_type_);
289         else
290                 new_index = typeCO->findData(decodeType(str));
291
292         // If everything else fails, settle on the table of contents which is
293         // guaranted to exist.
294         if (new_index == -1) {
295                 current_type_ = "tableofcontents";
296                 new_index = typeCO->findData(current_type_);
297         }
298
299         typeCO->blockSignals(true);
300         typeCO->setCurrentIndex(new_index);
301         typeCO->blockSignals(false);
302 }
303
304 } // namespace frontend
305 } // namespace lyx
306
307 #include "TocWidget_moc.cpp"