3 * This file is part of LyX, the document processor.
4 * Licence details can be found in the file COPYING.
7 * \author Abdelrazak Younes
9 * Full author contact details are available in file CREDITS.
14 #include "TocWidget.h"
17 #include "qt_helpers.h"
21 #include <QHeaderView>
22 #include <QPushButton>
23 #include <QTreeWidgetItem>
40 TocWidget::TocWidget(QToc * form, QWidget * parent)
41 : QWidget(parent), form_(form), depth_(0)
45 connect(form, SIGNAL(modelReset()),
49 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
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);
61 void TocWidget::selectionChanged(const QModelIndex & current,
62 const QModelIndex & /*previous*/)
65 << "selectionChanged index " << current.row()
66 << ", " << current.column()
69 form_->goTo(typeCO->currentIndex(), current);
73 void TocWidget::on_updatePB_clicked()
75 form_->updateBackend();
80 /* FIXME (Ugras 17/11/06):
81 I have implemented a getIndexDepth function to get the model indices. In my
82 opinion, somebody should derive a new qvariant class for tocModelItem
83 which saves the string data and depth information. that will save the
86 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
89 return (index.parent() == QModelIndex())? depth : getIndexDepth(index.parent(),depth);
93 void TocWidget::on_depthSL_valueChanged(int depth)
101 void TocWidget::setTreeDepth(int depth)
105 // expanding and then collapsing is probably better,
106 // but my qt 4.1.2 doesn't have expandAll()..
107 //tocTV->expandAll();
108 QModelIndexList indices = tocTV->model()->match(
109 tocTV->model()->index(0,0),
110 Qt::DisplayRole, "*", -1,
111 Qt::MatchWildcard|Qt::MatchRecursive);
113 int size = indices.size();
114 for (int i = 0; i < size; i++) {
115 QModelIndex index = indices[i];
116 if (getIndexDepth(index) < depth_)
117 tocTV->expand(index);
119 tocTV->collapse(index);
124 void TocWidget::on_typeCO_activated(int value)
130 void TocWidget::on_moveUpPB_clicked()
132 enableControls(false);
133 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
134 if (!list.isEmpty()) {
135 enableControls(false);
136 form_->goTo(typeCO->currentIndex(), list[0]);
138 enableControls(true);
143 void TocWidget::on_moveDownPB_clicked()
145 enableControls(false);
146 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
147 if (!list.isEmpty()) {
148 enableControls(false);
149 form_->goTo(typeCO->currentIndex(), list[0]);
150 form_->outlineDown();
151 enableControls(true);
156 void TocWidget::on_moveInPB_clicked()
158 enableControls(false);
159 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
160 if (!list.isEmpty()) {
161 enableControls(false);
162 form_->goTo(typeCO->currentIndex(), list[0]);
164 enableControls(true);
169 void TocWidget::on_moveOutPB_clicked()
171 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
172 if (!list.isEmpty()) {
173 enableControls(false);
174 form_->goTo(typeCO->currentIndex(), list[0]);
176 enableControls(true);
181 void TocWidget::select(QModelIndex const & index)
183 if (!index.isValid()) {
185 << "TocWidget::select(): QModelIndex is invalid!" << endl;
189 tocTV->selectionModel()->blockSignals(true);
190 tocTV->scrollTo(index);
191 tocTV->selectionModel()->setCurrentIndex(index,
192 QItemSelectionModel::ClearAndSelect);
193 tocTV->selectionModel()->blockSignals(false);
197 void TocWidget::enableControls(bool enable)
199 updatePB->setEnabled(enable);
201 if (!form_->canOutline(typeCO->currentIndex()))
204 moveUpPB->setEnabled(enable);
205 moveDownPB->setEnabled(enable);
206 moveInPB->setEnabled(enable);
207 moveOutPB->setEnabled(enable);
209 depthSL->setEnabled(enable);
213 void TocWidget::update()
215 LYXERR(Debug::GUI) << "In TocWidget::update()" << endl;
216 select(form_->getCurrentIndex(typeCO->currentIndex()));
221 void TocWidget::updateGui()
223 QStringListModel * type_model = form_->typeModel();
224 if (type_model->stringList().isEmpty()) {
225 enableControls(false);
226 typeCO->setModel(type_model);
227 tocTV->setModel(new QStandardItemModel);
228 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
232 QString current_text = typeCO->currentText();
233 typeCO->setModel(type_model);
234 int const current_type = typeCO->findText(current_text);
235 if (current_type != -1)
236 typeCO->setCurrentIndex(current_type);
238 typeCO->setCurrentIndex(form_->selectedType());
240 setTocModel(typeCO->currentIndex());
244 void TocWidget::setTocModel(size_t type)
246 bool controls_enabled = false;
247 QStandardItemModel * toc_model = form_->tocModel(type);
249 controls_enabled = toc_model->rowCount() > 0;
250 tocTV->setModel(toc_model);
251 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
254 enableControls(controls_enabled);
256 reconnectSelectionModel();
258 if (controls_enabled) {
259 depthSL->setMaximum(form_->getTocDepth(type));
260 depthSL->setValue(depth_);
263 LYXERR(Debug::GUI) << "In TocWidget::updateGui()" << endl;
265 select(form_->getCurrentIndex(typeCO->currentIndex()));
269 << "form_->tocModel()->rowCount "
270 << toc_model->rowCount()
271 << "\nform_->tocModel()->columnCount "
272 << toc_model->columnCount()
278 void TocWidget::reconnectSelectionModel()
280 connect(tocTV->selectionModel(),
281 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
282 this, SLOT(selectionChanged(const QModelIndex &, const QModelIndex &)));
285 } // namespace frontend
288 #include "TocWidget_moc.cpp"