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