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