]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocWidget.cpp
add a FIXME.
[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         // FIXME: It would be nice to return the focus to the work area in this
71         // case. But we need access to the GuiView!
72 }
73
74
75 void TocWidget::goTo(QModelIndex const & index)
76 {
77         LYXERR(Debug::GUI, "goto " << index.row()
78                 << ", " << index.column());
79
80         models_.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         models_.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 void TocWidget::on_typeCO_currentIndexChanged(int value)
137 {
138         setTocModel(value);
139 }
140
141
142 void TocWidget::on_moveUpTB_clicked()
143 {
144         enableControls(false);
145         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
146         if (!list.isEmpty()) {
147                 enableControls(false);
148                 goTo(list[0]);
149                 dispatch(FuncRequest(LFUN_OUTLINE_UP));
150                 enableControls(true);
151         }
152 }
153
154
155 void TocWidget::on_moveDownTB_clicked()
156 {
157         enableControls(false);
158         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
159         if (!list.isEmpty()) {
160                 enableControls(false);
161                 goTo(list[0]);
162                 dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
163                 enableControls(true);
164         }
165 }
166
167
168 void TocWidget::on_moveInTB_clicked()
169 {
170         enableControls(false);
171         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
172         if (!list.isEmpty()) {
173                 enableControls(false);
174                 goTo(list[0]);
175                 dispatch(FuncRequest(LFUN_OUTLINE_IN));
176                 enableControls(true);
177         }
178 }
179
180
181 void TocWidget::on_moveOutTB_clicked()
182 {
183         QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
184         if (!list.isEmpty()) {
185                 enableControls(false);
186                 goTo(list[0]);
187                 dispatch(FuncRequest(LFUN_OUTLINE_OUT));
188                 enableControls(true);
189         }
190 }
191
192
193 void TocWidget::select(QModelIndex const & index)
194 {
195         if (!index.isValid()) {
196                 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
197                 return;
198         }
199
200         tocTV->scrollTo(index);
201         tocTV->clearSelection();
202         tocTV->setCurrentIndex(index);
203 }
204
205
206 void TocWidget::enableControls(bool enable)
207 {
208         updateTB->setEnabled(enable);
209
210         if (!models_.canOutline(typeCO->currentIndex()))
211                 enable = false;
212
213         moveUpTB->setEnabled(enable);
214         moveDownTB->setEnabled(enable);
215         moveInTB->setEnabled(enable);
216         moveOutTB->setEnabled(enable);
217
218         depthSL->setEnabled(enable);
219 }
220
221
222 void TocWidget::updateView()
223 {
224         LYXERR(Debug::GUI, "In TocWidget::updateView()");
225         setTreeDepth(depth_);
226         select(models_.currentIndex(typeCO->currentIndex()));
227 }
228
229
230 void TocWidget::init(QString const & str)
231 {
232         QStringList const & type_names = models_.typeNames();
233         if (type_names.isEmpty()) {
234                 enableControls(false);
235                 typeCO->clear();
236                 tocTV->setModel(new QStandardItemModel);
237                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
238                 return;
239         }
240
241         int selected_type = models_.decodeType(str);
242
243         QString const current_text = typeCO->currentText();
244         typeCO->blockSignals(true);
245         typeCO->clear();
246         for (int i = 0; i != type_names.size(); ++i)
247                 typeCO->addItem(type_names[i]);
248         if (!str.isEmpty())
249                 typeCO->setCurrentIndex(selected_type);
250         else {
251                 int const new_index = typeCO->findText(current_text);
252                 if (new_index != -1)
253                         typeCO->setCurrentIndex(new_index);
254                 else
255                         typeCO->setCurrentIndex(selected_type);
256         }
257
258         typeCO->blockSignals(false);
259
260         setTocModel(typeCO->currentIndex());
261 }
262
263
264 void TocWidget::setTocModel(size_t type)
265 {
266         bool controls_enabled = false;
267         QStandardItemModel * toc_model = models_.model(type);
268         if (toc_model) {
269                 controls_enabled = toc_model->rowCount() > 0;
270                 tocTV->setModel(toc_model);
271                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
272                 LYXERR(Debug::GUI, "tocModel()->rowCount "
273                         << toc_model->rowCount()
274                         << "\nform_->tocModel()->columnCount "
275                         << toc_model->columnCount());
276         }
277
278         enableControls(controls_enabled);
279
280         if (controls_enabled) {
281                 depthSL->setMaximum(models_.depth(type));
282                 depthSL->setValue(depth_);
283         }
284
285         // setTocModel produce QTreeView reset and setting depth again
286         // is needed. That must be done after all Qt updates are processed.
287         QTimer::singleShot(0, this, SLOT(updateView()));
288 }
289
290 } // namespace frontend
291 } // namespace lyx
292
293 #include "TocWidget_moc.cpp"