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