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