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>
30 TocWidget::TocWidget(GuiToc & form, QWidget * parent)
31 : QWidget(parent), depth_(0), form_(form)
35 connect(&form_, SIGNAL(modelReset()), SLOT(updateGui()));
37 moveOutTB->setIcon(QIcon(":/images/promote.png"));
38 moveInTB->setIcon(QIcon(":/images/demote.png"));
39 moveUpTB->setIcon(QIcon(":/images/up.png"));
40 moveDownTB->setIcon(QIcon(":/images/down.png"));
41 updateTB->setIcon(QIcon(":/images/reload.png"));
44 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
48 // hide the pointless QHeader for now
49 // in the future, new columns may appear
50 // like labels, bookmarks, etc...
51 // tocTV->header()->hide();
52 tocTV->header()->setVisible(false);
54 // Only one item selected at a time.
55 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
59 void TocWidget::selectionChanged(const QModelIndex & current,
60 const QModelIndex & /*previous*/)
62 LYXERR(Debug::GUI, "selectionChanged index " << current.row()
63 << ", " << current.column());
65 form_.goTo(typeCO->currentIndex(), current);
69 void TocWidget::on_updateTB_clicked()
71 // The backend update can take some time so we disable
72 // the controls while waiting.
73 enableControls(false);
74 form_.updateBackend();
77 /* FIXME (Ugras 17/11/06):
78 I have implemented a getIndexDepth function to get the model indices. In my
79 opinion, somebody should derive a new qvariant class for tocModelItem
80 which saves the string data and depth information. that will save the
83 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
86 return (index.parent() == QModelIndex())
87 ? depth : getIndexDepth(index.parent(),depth);
91 void TocWidget::on_depthSL_valueChanged(int depth)
99 void TocWidget::setTreeDepth(int depth)
103 // expanding and then collapsing is probably better,
104 // but my qt 4.1.2 doesn't have expandAll()..
105 //tocTV->expandAll();
106 QModelIndexList indices = tocTV->model()->match(
107 tocTV->model()->index(0,0),
108 Qt::DisplayRole, "*", -1,
109 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
111 int size = indices.size();
112 for (int i = 0; i < size; i++) {
113 QModelIndex index = indices[i];
114 if (getIndexDepth(index) < depth_)
115 tocTV->expand(index);
117 tocTV->collapse(index);
121 void TocWidget::on_typeCO_currentIndexChanged(int value)
127 void TocWidget::on_moveUpTB_clicked()
129 enableControls(false);
130 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
131 if (!list.isEmpty()) {
132 enableControls(false);
133 form_.goTo(typeCO->currentIndex(), list[0]);
135 enableControls(true);
140 void TocWidget::on_moveDownTB_clicked()
142 enableControls(false);
143 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
144 if (!list.isEmpty()) {
145 enableControls(false);
146 form_.goTo(typeCO->currentIndex(), list[0]);
148 enableControls(true);
153 void TocWidget::on_moveInTB_clicked()
155 enableControls(false);
156 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
157 if (!list.isEmpty()) {
158 enableControls(false);
159 form_.goTo(typeCO->currentIndex(), list[0]);
161 enableControls(true);
166 void TocWidget::on_moveOutTB_clicked()
168 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
169 if (!list.isEmpty()) {
170 enableControls(false);
171 form_.goTo(typeCO->currentIndex(), list[0]);
173 enableControls(true);
178 void TocWidget::select(QModelIndex const & index)
180 if (!index.isValid()) {
181 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
185 disconnectSelectionModel();
186 tocTV->setCurrentIndex(index);
187 tocTV->scrollTo(index);
188 reconnectSelectionModel();
192 void TocWidget::enableControls(bool enable)
194 updateTB->setEnabled(enable);
196 if (!form_.canOutline(typeCO->currentIndex()))
199 moveUpTB->setEnabled(enable);
200 moveDownTB->setEnabled(enable);
201 moveInTB->setEnabled(enable);
202 moveOutTB->setEnabled(enable);
204 depthSL->setEnabled(enable);
208 void TocWidget::updateView()
210 LYXERR(Debug::GUI, "In TocWidget::updateView()");
211 select(form_.currentIndex(typeCO->currentIndex()));
215 void TocWidget::updateGui()
217 vector<docstring> const & type_names = form_.typeNames();
218 if (type_names.empty()) {
219 enableControls(false);
221 tocTV->setModel(new QStandardItemModel);
222 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
226 QString current_text = typeCO->currentText();
227 //lyxerr << "current_text " << fromqstr(current_text) << endl;
228 typeCO->blockSignals(true);
230 int current_type = -1;
231 for (size_t i = 0; i != type_names.size(); ++i) {
232 QString item = toqstr(type_names[i]);
233 typeCO->addItem(item);
234 if (item == current_text)
237 if (current_type != -1)
238 typeCO->setCurrentIndex(current_type);
240 typeCO->setCurrentIndex(form_.selectedType());
241 typeCO->blockSignals(false);
243 setTocModel(typeCO->currentIndex());
247 void TocWidget::setTocModel(size_t type)
249 bool controls_enabled = false;
250 QStandardItemModel * toc_model = form_.tocModel(type);
252 controls_enabled = toc_model->rowCount() > 0;
253 tocTV->setModel(toc_model);
254 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
257 enableControls(controls_enabled);
259 reconnectSelectionModel();
261 if (controls_enabled) {
262 depthSL->setMaximum(form_.getTocDepth(type));
263 depthSL->setValue(depth_);
266 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
268 select(form_.currentIndex(typeCO->currentIndex()));
271 LYXERR(Debug::GUI, "tocModel()->rowCount "
272 << toc_model->rowCount()
273 << "\nform_->tocModel()->columnCount "
274 << toc_model->columnCount());
279 void TocWidget::reconnectSelectionModel()
281 connect(tocTV->selectionModel(),
282 SIGNAL(currentChanged(const QModelIndex &,
283 const QModelIndex &)),
285 SLOT(selectionChanged(const QModelIndex &,
286 const QModelIndex &)));
289 void TocWidget::disconnectSelectionModel()
291 disconnect(tocTV->selectionModel(),
292 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
293 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
296 } // namespace frontend
299 #include "TocWidget_moc.cpp"