]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/QTocDialog.C
* qt_helpers.h:
[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         ++depth;
93         return (index.parent() == QModelIndex())? depth : getIndexDepth(index.parent(),depth);
94 }
95
96
97 void QTocDialog::on_depthSL_valueChanged(int depth)
98 {
99         if (depth == depth_)
100                 return;
101         setTreeDepth(depth);
102 }
103
104
105 void QTocDialog::setTreeDepth(int depth)
106 {
107         if(depth!=-1)
108                 depth_ = depth;
109 //      tocTV->expandAll(); //expanding and then collapsing is probably better, but my qt 4.1.2 doesn't have expandAll()..
110         QModelIndexList indices = 
111                 form_->tocModel()->match(form_->tocModel()->index(0,0),
112                                         Qt::DisplayRole, "*", -1, 
113                                         Qt::MatchWildcard|Qt::MatchRecursive);
114         Q_FOREACH (QModelIndex index, indices) { // I had to use Q_FOREACH instead of foreach
115                 if(getIndexDepth(index) < depth_) // because compile flag -DQT_NO_KEYWORDS doesn't allow me..
116                         tocTV->expand(index); 
117                 else
118                         tocTV->collapse(index); 
119         }
120 }
121
122
123 void QTocDialog::on_typeCO_activated(int value)
124 {
125         form_->setTocModel(value);
126         tocTV->setModel(form_->tocModel());
127         reconnectSelectionModel();
128         enableButtons();
129 }
130
131
132 void QTocDialog::on_moveUpPB_clicked()
133 {
134         enableButtons(false);
135         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
136         form_->goTo(index);
137         form_->outlineUp();
138         update();
139 }
140
141
142 void QTocDialog::on_moveDownPB_clicked()
143 {
144         enableButtons(false);
145         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
146         form_->goTo(index);
147         form_->outlineDown();
148         update();
149 }
150
151
152 void QTocDialog::on_moveInPB_clicked()
153 {
154         enableButtons(false);
155         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
156         form_->goTo(index);
157         form_->outlineIn();
158         update();
159 }
160
161
162 void QTocDialog::on_moveOutPB_clicked()
163 {
164         enableButtons(false);
165         QModelIndex index = tocTV->selectionModel()->selectedIndexes()[0];
166         form_->goTo(index);
167         form_->outlineOut();
168         update();
169 }
170
171
172 void QTocDialog::select(QModelIndex const & index)
173 {
174 //      tocTV->setModel(form_->tocModel());
175
176         if (!index.isValid()) {
177                 lyxerr[Debug::GUI]
178                         << "QTocDialog::select(): QModelIndex is invalid!" << endl;
179                 return;
180         }
181
182         tocTV->scrollTo(index);
183         tocTV->selectionModel()->select(index, QItemSelectionModel::Select);
184 }
185
186
187 void QTocDialog::enableButtons(bool enable)
188 {
189         updatePB->setEnabled(enable);
190
191         if (!form_->canOutline())
192                 enable = false;
193
194         moveUpPB->setEnabled(enable);
195         moveDownPB->setEnabled(enable);
196         moveInPB->setEnabled(enable);
197         moveOutPB->setEnabled(enable);
198 }
199
200
201 void QTocDialog::update()
202 {
203         form_->updateToc();
204         updateGui();
205 }
206
207
208 void QTocDialog::updateGui()
209 {
210         QStringListModel * type_model = form_->typeModel();
211         if (type_model->stringList().isEmpty())
212         {
213                 enableButtons();
214                 typeCO->setModel(type_model);
215                 tocTV->setModel(new QStandardItemModel);
216                 return;
217         }
218
219         typeCO->setModel(type_model);
220         typeCO->setCurrentIndex(form_->getType());
221
222         if (form_->tocModel())
223                 tocTV->setModel(form_->tocModel());
224         tocTV->showColumn(0);
225         // hide the pointless QHeader for now
226         // in the future, new columns may appear
227         // like labels, bookmarks, etc...
228         // tocTV->header()->hide();
229         tocTV->header()->setVisible(false);
230         enableButtons();
231
232         reconnectSelectionModel();
233         depthSL->setMaximum(form_->getTocDepth());
234         setTreeDepth();
235         select(form_->getCurrentIndex());
236
237         lyxerr[Debug::GUI]
238                 << "form_->tocModel()->rowCount " << form_->tocModel()->rowCount()
239                 << "\nform_->tocModel()->columnCount " << form_->tocModel()->columnCount()
240                 << endl;
241 //      setTitle(form_->guiname())
242 }
243
244
245 void QTocDialog::reconnectSelectionModel()
246 {
247         connect(tocTV->selectionModel(),
248                 SIGNAL(currentChanged(const QModelIndex &,
249                         const QModelIndex &)),
250                 this, SLOT(selectionChanged(const QModelIndex &,
251                         const QModelIndex &)));
252 }
253
254
255 void QTocDialog::apply()
256 {
257         // Nothing to do here... for now.
258         // Ideas welcome... (Abdel, 17042006)
259 }
260
261
262 void QTocDialog::hide()
263 {
264         accept();
265 }
266
267
268 void QTocDialog::show()
269 {
270         form_->update();
271         QDialog::show();
272 }
273
274
275 bool QTocDialog::isVisible() const
276 {
277         return QDialog::isVisible();
278 }
279
280
281 } // namespace frontend
282 } // namespace lyx
283
284 #include "QTocDialog_moc.cpp"