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