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