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