]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
Some dialog consistency work:
[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         setTreeDepth();
211         select(form_.currentIndex(typeCO->currentIndex()));
212 }
213
214
215 void TocWidget::updateGui(int selected_type)
216 {
217         QStringList const & type_names = form_.typeNames();
218         if (type_names.isEmpty()) {
219                 enableControls(false);
220                 typeCO->clear();
221                 tocTV->setModel(new QStandardItemModel);
222                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
223                 return;
224         }
225
226         QString const current_text = typeCO->currentText();
227         typeCO->blockSignals(true);
228         typeCO->clear();
229         for (int i = 0; i != type_names.size(); ++i)
230                 typeCO->addItem(type_names[i]);
231         if (selected_type != -1)
232                 typeCO->setCurrentIndex(selected_type);
233         else {
234                 int const new_index = typeCO->findText(current_text);
235                 if (new_index != -1)
236                         typeCO->setCurrentIndex(new_index);
237         }
238
239         typeCO->blockSignals(false);
240
241         setTocModel(typeCO->currentIndex());
242
243         // setTocModel produce QTreeView reset and setting depth again
244         // is needed. That must be done after all Qt updates are processed.
245         QTimer::singleShot(0, this, SLOT(updateView()));
246 }
247
248
249 void TocWidget::setTocModel(size_t type)
250 {
251         bool controls_enabled = false;
252         QStandardItemModel * toc_model = form_.tocModel(type);
253         if (toc_model) {
254                 controls_enabled = toc_model->rowCount() > 0;
255                 tocTV->setModel(toc_model);
256                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
257         }
258
259         enableControls(controls_enabled);
260
261         reconnectSelectionModel();
262
263         if (controls_enabled) {
264                 depthSL->setMaximum(form_.getTocDepth(type));
265                 depthSL->setValue(depth_);
266         }
267
268         LYXERR(Debug::GUI, "In TocWidget::updateGui()");
269
270         select(form_.currentIndex(typeCO->currentIndex()));
271
272         if (toc_model) {
273                 LYXERR(Debug::GUI, "tocModel()->rowCount "
274                         << toc_model->rowCount()
275                         << "\nform_->tocModel()->columnCount "
276                         << toc_model->columnCount());
277         }
278 }
279
280
281 void TocWidget::reconnectSelectionModel()
282 {
283         connect(tocTV->selectionModel(),
284                 SIGNAL(currentChanged(const QModelIndex &,
285                        const QModelIndex &)),
286                 this,
287                 SLOT(selectionChanged(const QModelIndex &,
288                      const QModelIndex &)));
289 }
290
291 void TocWidget::disconnectSelectionModel()
292 {
293         disconnect(tocTV->selectionModel(),
294                 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
295                 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
296 }
297
298 } // namespace frontend
299 } // namespace lyx
300
301 #include "TocWidget_moc.cpp"