]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QTocDialog.C
* LyXView::updateInset(): schedule a redraw instead of redraw immediately.
[lyx.git] / src / frontends / qt4 / QTocDialog.C
1 /**
2  * \file QTocDialog.C
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 "QTocDialog.h"
15 #include "QToc.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18 #include "controllers/ControlToc.h"
19
20 #include "debug.h"
21
22 #include <QTreeWidgetItem>
23 #include <QPushButton>
24 #include <QCloseEvent>
25 #include <QHeaderView>
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 namespace frontend {
40
41 QTocDialog::QTocDialog(Dialog & dialog, QToc * form)
42         : Dialog::View(dialog, _("Toc")), form_(form), depth_(2)
43 {
44         setupUi(this);
45
46         updateGui();
47
48         connect(tocTV->selectionModel(),
49                 SIGNAL(currentChanged(const QModelIndex &,
50                         const QModelIndex &)),
51                 this, SLOT(selectionChanged(const QModelIndex &,
52                         const QModelIndex &)));
53 }
54
55
56 QTocDialog::~QTocDialog()
57 {
58         accept();
59 }
60
61
62 void QTocDialog::selectionChanged(const QModelIndex & current,
63                                   const QModelIndex & /*previous*/)
64 {
65         lyxerr[Debug::GUI]
66                 << "selectionChanged index " << current.row()
67                 << ", " << current.column()
68                 << endl;
69
70         form_->goTo(current);
71 }
72
73
74 void QTocDialog::on_closePB_clicked()
75 {
76         accept();
77 }
78
79
80 void QTocDialog::on_updatePB_clicked()
81 {
82         update();
83 }
84
85 /* FIXME (Ugras 17/11/06):
86 I have implemented a getIndexDepth function to get the model indices. In my
87 opinion, somebody should derive a new qvariant class for tocModelItem
88 which saves the string data and depth information. that will save the
89 depth calculation.
90 */
91 int QTocDialog::getIndexDepth(QModelIndex const & index, int depth)
92 {
93         ++depth;
94         return (index.parent() == QModelIndex())? depth : getIndexDepth(index.parent(),depth);
95 }
96
97
98 void QTocDialog::on_depthSL_valueChanged(int depth)
99 {
100         if (depth == depth_)
101                 return;
102         setTreeDepth(depth);
103 }
104
105
106 void QTocDialog::setTreeDepth(int depth)
107 {
108         if(depth!=-1)
109                 depth_ = depth;
110
111         // expanding and then collapsing is probably better, 
112         // but my qt 4.1.2 doesn't have expandAll()..
113         //tocTV->expandAll(); 
114         QModelIndexList indices = 
115                 form_->tocModel()->match(form_->tocModel()->index(0,0),
116                                         Qt::DisplayRole, "*", -1, 
117                                         Qt::MatchWildcard|Qt::MatchRecursive);
118         
119         int size = indices.size();
120         for (int i = 0; i < size; i++) {
121                 QModelIndex index = indices[i];
122                 if (getIndexDepth(index) < depth_) 
123                         tocTV->expand(index); 
124                 else
125                         tocTV->collapse(index); 
126         }
127 }
128
129
130 void QTocDialog::on_typeCO_activated(int value)
131 {
132         form_->setTocModel(value);
133         tocTV->setModel(form_->tocModel());
134         reconnectSelectionModel();
135         enableButtons();
136         update();
137 }
138
139
140 void QTocDialog::on_moveUpPB_clicked()
141 {
142         enableButtons(false);
143         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
144         form_->goTo(index);
145         form_->outlineUp();
146         update();
147 }
148
149
150 void QTocDialog::on_moveDownPB_clicked()
151 {
152         enableButtons(false);
153         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
154         form_->goTo(index);
155         form_->outlineDown();
156         update();
157 }
158
159
160 void QTocDialog::on_moveInPB_clicked()
161 {
162         enableButtons(false);
163         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
164         form_->goTo(index);
165         form_->outlineIn();
166         update();
167 }
168
169
170 void QTocDialog::on_moveOutPB_clicked()
171 {
172         enableButtons(false);
173         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
174         form_->goTo(index);
175         form_->outlineOut();
176         update();
177 }
178
179
180 void QTocDialog::select(QModelIndex const & index)
181 {
182 //      tocTV->setModel(form_->tocModel());
183
184         if (!index.isValid()) {
185                 lyxerr[Debug::GUI]
186                         << "QTocDialog::select(): QModelIndex is invalid!" << endl;
187                 return;
188         }
189
190         tocTV->scrollTo(index);
191         tocTV->selectionModel()->select(index, QItemSelectionModel::Select);
192 }
193
194
195 void QTocDialog::enableButtons(bool enable)
196 {
197         updatePB->setEnabled(enable);
198
199         if (!form_->canOutline())
200                 enable = false;
201
202         moveUpPB->setEnabled(enable);
203         moveDownPB->setEnabled(enable);
204         moveInPB->setEnabled(enable);
205         moveOutPB->setEnabled(enable);
206 }
207
208
209 void QTocDialog::update()
210 {
211         form_->updateToc();
212         updateGui();
213 }
214
215
216 void QTocDialog::updateGui()
217 {
218         QStringListModel * type_model = form_->typeModel();
219         if (type_model->stringList().isEmpty()) {
220                 enableButtons();
221                 typeCO->setModel(type_model);
222                 tocTV->setModel(new QStandardItemModel);
223                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
224                 depthSL->setEnabled(false);
225                 return;
226         }
227
228         typeCO->setModel(type_model);
229         typeCO->setCurrentIndex(form_->getType());
230
231         if (form_->tocModel()) {
232                 tocTV->setModel(form_->tocModel());
233                 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
234         }
235         // avoid flickering
236         tocTV-> setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
237         tocTV->showColumn(0);
238         // hide the pointless QHeader for now
239         // in the future, new columns may appear
240         // like labels, bookmarks, etc...
241         // tocTV->header()->hide();
242         tocTV->header()->setVisible(false);
243         enableButtons();
244
245         reconnectSelectionModel();
246         depthSL->setEnabled(true);
247         depthSL->setMaximum(form_->getTocDepth());
248         setTreeDepth();
249         select(form_->getCurrentIndex());
250
251         lyxerr[Debug::GUI]
252                 << "form_->tocModel()->rowCount " << form_->tocModel()->rowCount()
253                 << "\nform_->tocModel()->columnCount " << form_->tocModel()->columnCount()
254                 << endl;
255 //      setTitle(form_->guiname())
256 }
257
258
259 void QTocDialog::reconnectSelectionModel()
260 {
261         connect(tocTV->selectionModel(),
262                 SIGNAL(currentChanged(const QModelIndex &,
263                         const QModelIndex &)),
264                 this, SLOT(selectionChanged(const QModelIndex &,
265                         const QModelIndex &)));
266 }
267
268
269 void QTocDialog::apply()
270 {
271         // Nothing to do here... for now.
272         // Ideas welcome... (Abdel, 17042006)
273 }
274
275
276 void QTocDialog::hide()
277 {
278         accept();
279 }
280
281
282 void QTocDialog::show()
283 {
284         form_->update();
285         QDialog::show();
286 }
287
288
289 bool QTocDialog::isVisible() const
290 {
291         return QDialog::isVisible();
292 }
293
294
295 } // namespace frontend
296 } // namespace lyx
297
298 #include "QTocDialog_moc.cpp"