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 enableButtons(false);
133 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
134 if (!list.isEmpty()) {
135 enableButtons(false);
136 form_->goTo(typeCO->currentIndex(), list[0]);
143 void TocWidget::on_moveDownPB_clicked()
145 enableButtons(false);
146 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
147 if (!list.isEmpty()) {
148 enableButtons(false);
149 form_->goTo(typeCO->currentIndex(), list[0]);
150 form_->outlineDown();
156 void TocWidget::on_moveInPB_clicked()
158 enableButtons(false);
159 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
160 if (!list.isEmpty()) {
161 enableButtons(false);
162 form_->goTo(typeCO->currentIndex(), list[0]);
169 void TocWidget::on_moveOutPB_clicked()
171 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
172 if (!list.isEmpty()) {
173 enableButtons(false);
174 form_->goTo(typeCO->currentIndex(), list[0]);
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::enableButtons(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);
211 void TocWidget::update()
213 lyxerr[Debug::GUI] << "In TocWidget::update()" << endl;
214 select(form_->getCurrentIndex(typeCO->currentIndex()));
219 void TocWidget::updateGui()
221 QStringListModel * type_model = form_->typeModel();
222 if (type_model->stringList().isEmpty()) {
223 enableButtons(false);
224 typeCO->setModel(type_model);
225 tocTV->setModel(new QStandardItemModel);
226 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
227 depthSL->setEnabled(false);
231 QString current_text = typeCO->currentText();
232 typeCO->setModel(type_model);
233 int const current_type = typeCO->findText(current_text);
234 if (current_type != -1)
235 typeCO->setCurrentIndex(current_type);
237 typeCO->setCurrentIndex(form_->selectedType());
239 setTocModel(typeCO->currentIndex());
243 void TocWidget::setTocModel(size_t type)
245 bool buttons_enabled = false;
246 QStandardItemModel * toc_model = form_->tocModel(type);
248 buttons_enabled = toc_model->rowCount() > 0;
249 tocTV->setModel(toc_model);
250 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
253 enableButtons(buttons_enabled);
255 reconnectSelectionModel();
256 depthSL->setEnabled(true);
257 depthSL->setMaximum(form_->getTocDepth(type));
258 depthSL->setValue(depth_);
260 lyxerr[Debug::GUI] << "In TocWidget::updateGui()" << endl;
262 select(form_->getCurrentIndex(typeCO->currentIndex()));
266 << "form_->tocModel()->rowCount "
267 << toc_model->rowCount()
268 << "\nform_->tocModel()->columnCount "
269 << toc_model->columnCount()
275 void TocWidget::reconnectSelectionModel()
277 connect(tocTV->selectionModel(),
278 SIGNAL(currentChanged(const QModelIndex &,
279 const QModelIndex &)),
280 this, SLOT(selectionChanged(const QModelIndex &,
281 const QModelIndex &)));
284 } // namespace frontend
287 #include "TocWidget_moc.cpp"