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"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
23 #include <QHeaderView>
24 #include <QPushButton>
25 #include <QTreeWidgetItem>
40 using support::FileName;
41 using support::libFileSearch;
45 TocWidget::TocWidget(QToc * form, QWidget * parent)
46 : QWidget(parent), form_(form), depth_(0)
50 connect(form, SIGNAL(modelReset()),
53 FileName icon_path = libFileSearch("images", "promote.xpm");
54 moveOutTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
55 icon_path = libFileSearch("images", "demote.xpm");
56 moveInTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
57 icon_path = libFileSearch("images", "up.xpm");
58 moveUpTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
59 icon_path = libFileSearch("images", "down.xpm");
60 moveDownTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
61 icon_path = libFileSearch("images", "reload.xpm");
62 updateTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
65 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
69 // hide the pointless QHeader for now
70 // in the future, new columns may appear
71 // like labels, bookmarks, etc...
72 // tocTV->header()->hide();
73 tocTV->header()->setVisible(false);
75 // Only one item selected at a time.
76 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
80 void TocWidget::selectionChanged(const QModelIndex & current,
81 const QModelIndex & /*previous*/)
84 << "selectionChanged index " << current.row()
85 << ", " << current.column()
88 form_->goTo(typeCO->currentIndex(), current);
92 void TocWidget::on_updateTB_clicked()
94 form_->updateBackend();
99 /* FIXME (Ugras 17/11/06):
100 I have implemented a getIndexDepth function to get the model indices. In my
101 opinion, somebody should derive a new qvariant class for tocModelItem
102 which saves the string data and depth information. that will save the
105 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
108 return (index.parent() == QModelIndex())? depth : getIndexDepth(index.parent(),depth);
112 void TocWidget::on_depthSL_valueChanged(int depth)
120 void TocWidget::setTreeDepth(int depth)
124 // expanding and then collapsing is probably better,
125 // but my qt 4.1.2 doesn't have expandAll()..
126 //tocTV->expandAll();
127 QModelIndexList indices = tocTV->model()->match(
128 tocTV->model()->index(0,0),
129 Qt::DisplayRole, "*", -1,
130 Qt::MatchWildcard|Qt::MatchRecursive);
132 int size = indices.size();
133 for (int i = 0; i < size; i++) {
134 QModelIndex index = indices[i];
135 if (getIndexDepth(index) < depth_)
136 tocTV->expand(index);
138 tocTV->collapse(index);
143 void TocWidget::on_typeCO_activated(int value)
149 void TocWidget::on_moveUpTB_clicked()
151 enableControls(false);
152 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
153 if (!list.isEmpty()) {
154 enableControls(false);
155 form_->goTo(typeCO->currentIndex(), list[0]);
157 enableControls(true);
162 void TocWidget::on_moveDownTB_clicked()
164 enableControls(false);
165 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
166 if (!list.isEmpty()) {
167 enableControls(false);
168 form_->goTo(typeCO->currentIndex(), list[0]);
169 form_->outlineDown();
170 enableControls(true);
175 void TocWidget::on_moveInTB_clicked()
177 enableControls(false);
178 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
179 if (!list.isEmpty()) {
180 enableControls(false);
181 form_->goTo(typeCO->currentIndex(), list[0]);
183 enableControls(true);
188 void TocWidget::on_moveOutTB_clicked()
190 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
191 if (!list.isEmpty()) {
192 enableControls(false);
193 form_->goTo(typeCO->currentIndex(), list[0]);
195 enableControls(true);
200 void TocWidget::select(QModelIndex const & index)
202 if (!index.isValid()) {
204 << "TocWidget::select(): QModelIndex is invalid!" << endl;
208 tocTV->selectionModel()->blockSignals(true);
209 tocTV->selectionModel()->clear();
210 tocTV->scrollTo(index);
211 tocTV->selectionModel()->setCurrentIndex(index,
212 QItemSelectionModel::ClearAndSelect);
213 tocTV->selectionModel()->blockSignals(false);
217 void TocWidget::enableControls(bool enable)
219 updateTB->setEnabled(enable);
221 if (!form_->canOutline(typeCO->currentIndex()))
224 moveUpTB->setEnabled(enable);
225 moveDownTB->setEnabled(enable);
226 moveInTB->setEnabled(enable);
227 moveOutTB->setEnabled(enable);
229 depthSL->setEnabled(enable);
233 void TocWidget::update()
235 LYXERR(Debug::GUI) << "In TocWidget::update()" << endl;
236 select(form_->getCurrentIndex(typeCO->currentIndex()));
241 void TocWidget::updateGui()
243 QStringListModel * type_model = form_->typeModel();
244 if (type_model->stringList().isEmpty()) {
245 enableControls(false);
246 typeCO->setModel(type_model);
247 tocTV->setModel(new QStandardItemModel);
248 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
252 QString current_text = typeCO->currentText();
253 typeCO->setModel(type_model);
254 int const current_type = typeCO->findText(current_text);
255 if (current_type != -1)
256 typeCO->setCurrentIndex(current_type);
258 typeCO->setCurrentIndex(form_->selectedType());
260 setTocModel(typeCO->currentIndex());
264 void TocWidget::setTocModel(size_t type)
266 bool controls_enabled = false;
267 QStandardItemModel * toc_model = form_->tocModel(type);
269 controls_enabled = toc_model->rowCount() > 0;
270 tocTV->setModel(toc_model);
271 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
274 enableControls(controls_enabled);
276 reconnectSelectionModel();
278 if (controls_enabled) {
279 depthSL->setMaximum(form_->getTocDepth(type));
280 depthSL->setValue(depth_);
283 LYXERR(Debug::GUI) << "In TocWidget::updateGui()" << endl;
285 select(form_->getCurrentIndex(typeCO->currentIndex()));
289 << "form_->tocModel()->rowCount "
290 << toc_model->rowCount()
291 << "\nform_->tocModel()->columnCount "
292 << toc_model->columnCount()
298 void TocWidget::reconnectSelectionModel()
300 connect(tocTV->selectionModel(),
301 SIGNAL(currentChanged(const QModelIndex &, const QModelIndex &)),
302 this, SLOT(selectionChanged(const QModelIndex &, const QModelIndex &)));
305 } // namespace frontend
308 #include "TocWidget_moc.cpp"