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"
19 #include "support/debug.h"
21 #include <QHeaderView>
31 TocWidget::TocWidget(GuiToc & form, QWidget * parent)
32 : QWidget(parent), depth_(0), form_(form)
36 moveOutTB->setIcon(QIcon(":/images/promote.png"));
37 moveInTB->setIcon(QIcon(":/images/demote.png"));
38 moveUpTB->setIcon(QIcon(":/images/up.png"));
39 moveDownTB->setIcon(QIcon(":/images/down.png"));
40 updateTB->setIcon(QIcon(":/images/reload.png"));
43 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
47 // hide the pointless QHeader for now
48 // in the future, new columns may appear
49 // like labels, bookmarks, etc...
50 // tocTV->header()->hide();
51 tocTV->header()->setVisible(false);
53 // Only one item selected at a time.
54 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
58 void TocWidget::selectionChanged(const QModelIndex & current,
59 const QModelIndex & /*previous*/)
61 LYXERR(Debug::GUI, "selectionChanged index " << current.row()
62 << ", " << current.column());
64 form_.goTo(typeCO->currentIndex(), current);
68 void TocWidget::on_updateTB_clicked()
70 // The backend update can take some time so we disable
71 // the controls while waiting.
72 enableControls(false);
73 form_.updateBackend();
76 /* FIXME (Ugras 17/11/06):
77 I have implemented a getIndexDepth function to get the model indices. In my
78 opinion, somebody should derive a new qvariant class for tocModelItem
79 which saves the string data and depth information. that will save the
82 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
85 return (index.parent() == QModelIndex())
86 ? depth : getIndexDepth(index.parent(),depth);
90 void TocWidget::on_depthSL_valueChanged(int depth)
98 void TocWidget::setTreeDepth(int depth)
102 // expanding and then collapsing is probably better,
103 // but my qt 4.1.2 doesn't have expandAll()..
104 //tocTV->expandAll();
105 QModelIndexList indices = tocTV->model()->match(
106 tocTV->model()->index(0,0),
107 Qt::DisplayRole, "*", -1,
108 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
110 int size = indices.size();
111 for (int i = 0; i < size; i++) {
112 QModelIndex index = indices[i];
113 if (getIndexDepth(index) < depth_)
114 tocTV->expand(index);
116 tocTV->collapse(index);
120 void TocWidget::on_typeCO_currentIndexChanged(int value)
126 void TocWidget::on_moveUpTB_clicked()
128 enableControls(false);
129 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
130 if (!list.isEmpty()) {
131 enableControls(false);
132 form_.goTo(typeCO->currentIndex(), list[0]);
134 enableControls(true);
139 void TocWidget::on_moveDownTB_clicked()
141 enableControls(false);
142 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
143 if (!list.isEmpty()) {
144 enableControls(false);
145 form_.goTo(typeCO->currentIndex(), list[0]);
147 enableControls(true);
152 void TocWidget::on_moveInTB_clicked()
154 enableControls(false);
155 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
156 if (!list.isEmpty()) {
157 enableControls(false);
158 form_.goTo(typeCO->currentIndex(), list[0]);
160 enableControls(true);
165 void TocWidget::on_moveOutTB_clicked()
167 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
168 if (!list.isEmpty()) {
169 enableControls(false);
170 form_.goTo(typeCO->currentIndex(), list[0]);
172 enableControls(true);
177 void TocWidget::select(QModelIndex const & index)
179 if (!index.isValid()) {
180 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
184 disconnectSelectionModel();
185 tocTV->setCurrentIndex(index);
186 tocTV->scrollTo(index);
187 reconnectSelectionModel();
191 void TocWidget::enableControls(bool enable)
193 updateTB->setEnabled(enable);
195 if (!form_.canOutline(typeCO->currentIndex()))
198 moveUpTB->setEnabled(enable);
199 moveDownTB->setEnabled(enable);
200 moveInTB->setEnabled(enable);
201 moveOutTB->setEnabled(enable);
203 depthSL->setEnabled(enable);
207 void TocWidget::updateView()
209 LYXERR(Debug::GUI, "In TocWidget::updateView()");
210 select(form_.currentIndex(typeCO->currentIndex()));
214 void TocWidget::updateGui(int selected_type)
216 QStringList const & type_names = form_.typeNames();
217 if (type_names.isEmpty()) {
218 enableControls(false);
220 tocTV->setModel(new QStandardItemModel);
221 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
225 QString const current_text = typeCO->currentText();
226 typeCO->blockSignals(true);
228 for (int i = 0; i != type_names.size(); ++i)
229 typeCO->addItem(type_names[i]);
230 if (selected_type != -1)
231 typeCO->setCurrentIndex(selected_type);
233 int const new_index = typeCO->findText(current_text);
235 typeCO->setCurrentIndex(new_index);
238 typeCO->blockSignals(false);
240 setTocModel(typeCO->currentIndex());
242 // setTocModel produce QTreeView reset and setting depth again
243 // is needed. That must be done after all Qt updates are processed.
244 QTimer::singleShot(0, this, SLOT(setTreeDepth()));
248 void TocWidget::setTocModel(size_t type)
250 bool controls_enabled = false;
251 QStandardItemModel * toc_model = form_.tocModel(type);
253 controls_enabled = toc_model->rowCount() > 0;
254 tocTV->setModel(toc_model);
255 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
258 enableControls(controls_enabled);
260 reconnectSelectionModel();
262 if (controls_enabled) {
263 depthSL->setMaximum(form_.getTocDepth(type));
264 depthSL->setValue(depth_);
267 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
269 select(form_.currentIndex(typeCO->currentIndex()));
272 LYXERR(Debug::GUI, "tocModel()->rowCount "
273 << toc_model->rowCount()
274 << "\nform_->tocModel()->columnCount "
275 << toc_model->columnCount());
280 void TocWidget::reconnectSelectionModel()
282 connect(tocTV->selectionModel(),
283 SIGNAL(currentChanged(const QModelIndex &,
284 const QModelIndex &)),
286 SLOT(selectionChanged(const QModelIndex &,
287 const QModelIndex &)));
290 void TocWidget::disconnectSelectionModel()
292 disconnect(tocTV->selectionModel(),
293 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
294 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
297 } // namespace frontend
300 #include "TocWidget_moc.cpp"