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