]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
simplify GuiToc / TocWidget interaction. Much can still be simplified...
[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 "GuiToc.h"
17 #include "qt_helpers.h"
18
19 #include "support/debug.h"
20
21 #include <QHeaderView>
22
23 #include <vector>
24
25 using namespace std;
26
27 namespace lyx {
28 namespace frontend {
29
30 TocWidget::TocWidget(GuiToc & form, QWidget * parent)
31         : QWidget(parent), depth_(0), form_(form)
32 {
33         setupUi(this);
34
35         moveOutTB->setIcon(QIcon(":/images/promote.png"));
36         moveInTB->setIcon(QIcon(":/images/demote.png"));
37         moveUpTB->setIcon(QIcon(":/images/up.png"));
38         moveDownTB->setIcon(QIcon(":/images/down.png"));
39         updateTB->setIcon(QIcon(":/images/reload.png"));
40
41         // avoid flickering
42         tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
43
44         tocTV->showColumn(0);
45
46         // hide the pointless QHeader for now
47         // in the future, new columns may appear
48         // like labels, bookmarks, etc...
49         // tocTV->header()->hide();
50         tocTV->header()->setVisible(false);
51
52         // Only one item selected at a time.
53         tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
54 }
55
56
57 void TocWidget::selectionChanged(const QModelIndex & current,
58                                   const QModelIndex & /*previous*/)
59 {
60         LYXERR(Debug::GUI, "selectionChanged index " << current.row()
61                 << ", " << current.column());
62
63         form_.goTo(typeCO->currentIndex(), current);
64 }
65
66
67 void TocWidget::on_updateTB_clicked()
68 {
69         // The backend update can take some time so we disable
70         // the controls while waiting.
71         enableControls(false);
72         form_.updateBackend();
73 }
74
75 /* FIXME (Ugras 17/11/06):
76 I have implemented a getIndexDepth function to get the model indices. In my
77 opinion, somebody should derive a new qvariant class for tocModelItem
78 which saves the string data and depth information. that will save the
79 depth calculation.
80 */
81 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
82 {
83         ++depth;
84         return (index.parent() == QModelIndex())
85                 ? depth : getIndexDepth(index.parent(),depth);
86 }
87
88
89 void TocWidget::on_depthSL_valueChanged(int depth)
90 {
91         if (depth == depth_)
92                 return;
93         setTreeDepth(depth);
94 }
95
96
97 void TocWidget::setTreeDepth(int depth)
98 {
99         depth_ = depth;
100
101         // expanding and then collapsing is probably better,
102         // but my qt 4.1.2 doesn't have expandAll()..
103         //tocTV->expandAll();
104         QModelIndexList indices = tocTV->model()->match(
105                 tocTV->model()->index(0,0),
106                 Qt::DisplayRole, "*", -1,
107                 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
108
109         int size = indices.size();
110         for (int i = 0; i < size; i++) {
111                 QModelIndex index = indices[i];
112                 if (getIndexDepth(index) < depth_)
113                         tocTV->expand(index);
114                 else
115                         tocTV->collapse(index);
116         }
117 }
118
119 void TocWidget::on_typeCO_currentIndexChanged(int value)
120 {
121         setTocModel(value);
122 }
123
124
125 void TocWidget::on_moveUpTB_clicked()
126 {
127         enableControls(false);
128         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
129         if (!list.isEmpty()) {
130                 enableControls(false);
131                 form_.goTo(typeCO->currentIndex(), list[0]);
132                 form_.outlineUp();
133                 enableControls(true);
134         }
135 }
136
137
138 void TocWidget::on_moveDownTB_clicked()
139 {
140         enableControls(false);
141         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
142         if (!list.isEmpty()) {
143                 enableControls(false);
144                 form_.goTo(typeCO->currentIndex(), list[0]);
145                 form_.outlineDown();
146                 enableControls(true);
147         }
148 }
149
150
151 void TocWidget::on_moveInTB_clicked()
152 {
153         enableControls(false);
154         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
155         if (!list.isEmpty()) {
156                 enableControls(false);
157                 form_.goTo(typeCO->currentIndex(), list[0]);
158                 form_.outlineIn();
159                 enableControls(true);
160         }
161 }
162
163
164 void TocWidget::on_moveOutTB_clicked()
165 {
166         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
167         if (!list.isEmpty()) {
168                 enableControls(false);
169                 form_.goTo(typeCO->currentIndex(), list[0]);
170                 form_.outlineOut();
171                 enableControls(true);
172         }
173 }
174
175
176 void TocWidget::select(QModelIndex const & index)
177 {
178         if (!index.isValid()) {
179                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
180                 return;
181         }
182
183         disconnectSelectionModel();
184         tocTV->setCurrentIndex(index);
185         tocTV->scrollTo(index);
186         reconnectSelectionModel();
187 }
188
189
190 void TocWidget::enableControls(bool enable)
191 {
192         updateTB->setEnabled(enable);
193
194         if (!form_.canOutline(typeCO->currentIndex()))
195                 enable = false;
196
197         moveUpTB->setEnabled(enable);
198         moveDownTB->setEnabled(enable);
199         moveInTB->setEnabled(enable);
200         moveOutTB->setEnabled(enable);
201
202         depthSL->setEnabled(enable);
203 }
204
205
206 void TocWidget::updateView()
207 {
208         LYXERR(Debug::GUI, "In TocWidget::updateView()");
209         select(form_.currentIndex(typeCO->currentIndex()));
210 }
211
212
213 void TocWidget::updateGui(int selected_type)
214 {
215         vector<docstring> const & type_names = form_.typeNames();
216         if (type_names.empty()) {
217                 enableControls(false);
218                 typeCO->clear();
219                 tocTV->setModel(new QStandardItemModel);
220                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
221                 return;
222         }
223
224         QString current_text = typeCO->currentText();
225         typeCO->blockSignals(true);
226         typeCO->clear();
227         for (size_t i = 0; i != type_names.size(); ++i) {
228                 QString item = toqstr(type_names[i]);
229                 typeCO->addItem(item);
230         }
231         typeCO->setCurrentIndex(selected_type);
232         typeCO->blockSignals(false);
233
234         setTocModel(typeCO->currentIndex());
235 }
236
237
238 void TocWidget::setTocModel(size_t type)
239 {
240         bool controls_enabled = false;
241         QStandardItemModel * toc_model = form_.tocModel(type);
242         if (toc_model) {
243                 controls_enabled = toc_model->rowCount() > 0;
244                 tocTV->setModel(toc_model);
245                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
246         }
247
248         enableControls(controls_enabled);
249
250         reconnectSelectionModel();
251
252         if (controls_enabled) {
253                 depthSL->setMaximum(form_.getTocDepth(type));
254                 depthSL->setValue(depth_);
255         }
256
257         LYXERR(Debug::GUI, "In TocWidget::updateGui()");
258
259         select(form_.currentIndex(typeCO->currentIndex()));
260
261         if (toc_model) {
262                 LYXERR(Debug::GUI, "tocModel()->rowCount "
263                         << toc_model->rowCount()
264                         << "\nform_->tocModel()->columnCount "
265                         << toc_model->columnCount());
266         }
267 }
268
269
270 void TocWidget::reconnectSelectionModel()
271 {
272         connect(tocTV->selectionModel(),
273                 SIGNAL(currentChanged(const QModelIndex &,
274                        const QModelIndex &)),
275                 this,
276                 SLOT(selectionChanged(const QModelIndex &,
277                      const QModelIndex &)));
278 }
279
280 void TocWidget::disconnectSelectionModel()
281 {
282         disconnect(tocTV->selectionModel(),
283                 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
284                 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
285 }
286
287 } // namespace frontend
288 } // namespace lyx
289
290 #include "TocWidget_moc.cpp"