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 moveOutTB->setIcon(QIcon(":/images/promote.png"));
36 moveInTB->setIcon(QIcon(":/images/demote.png"));
37 moveUpTB->setIcon(QIcon(":/images/up.png"));
38 moveDownTB->setIcon(QIcon(":/images/down.png"));
39 updateTB->setIcon(QIcon(":/images/reload.png"));
42 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
46 // hide the pointless QHeader for now
47 // in the future, new columns may appear
48 // like labels, bookmarks, etc...
49 // tocTV->header()->hide();
50 tocTV->header()->setVisible(false);
52 // Only one item selected at a time.
53 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
57 void TocWidget::selectionChanged(const QModelIndex & current,
58 const QModelIndex & /*previous*/)
60 LYXERR(Debug::GUI, "selectionChanged index " << current.row()
61 << ", " << current.column());
63 form_.goTo(typeCO->currentIndex(), current);
67 void TocWidget::on_updateTB_clicked()
69 // The backend update can take some time so we disable
70 // the controls while waiting.
71 enableControls(false);
72 form_.updateBackend();
75 /* FIXME (Ugras 17/11/06):
76 I have implemented a getIndexDepth function to get the model indices. In my
77 opinion, somebody should derive a new qvariant class for tocModelItem
78 which saves the string data and depth information. that will save the
81 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
84 return (index.parent() == QModelIndex())
85 ? depth : getIndexDepth(index.parent(),depth);
89 void TocWidget::on_depthSL_valueChanged(int depth)
97 void TocWidget::setTreeDepth(int depth)
101 // expanding and then collapsing is probably better,
102 // but my qt 4.1.2 doesn't have expandAll()..
103 //tocTV->expandAll();
104 QModelIndexList indices = tocTV->model()->match(
105 tocTV->model()->index(0,0),
106 Qt::DisplayRole, "*", -1,
107 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
109 int size = indices.size();
110 for (int i = 0; i < size; i++) {
111 QModelIndex index = indices[i];
112 if (getIndexDepth(index) < depth_)
113 tocTV->expand(index);
115 tocTV->collapse(index);
119 void TocWidget::on_typeCO_currentIndexChanged(int value)
125 void TocWidget::on_moveUpTB_clicked()
127 enableControls(false);
128 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
129 if (!list.isEmpty()) {
130 enableControls(false);
131 form_.goTo(typeCO->currentIndex(), list[0]);
133 enableControls(true);
138 void TocWidget::on_moveDownTB_clicked()
140 enableControls(false);
141 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
142 if (!list.isEmpty()) {
143 enableControls(false);
144 form_.goTo(typeCO->currentIndex(), list[0]);
146 enableControls(true);
151 void TocWidget::on_moveInTB_clicked()
153 enableControls(false);
154 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
155 if (!list.isEmpty()) {
156 enableControls(false);
157 form_.goTo(typeCO->currentIndex(), list[0]);
159 enableControls(true);
164 void TocWidget::on_moveOutTB_clicked()
166 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
167 if (!list.isEmpty()) {
168 enableControls(false);
169 form_.goTo(typeCO->currentIndex(), list[0]);
171 enableControls(true);
176 void TocWidget::select(QModelIndex const & index)
178 if (!index.isValid()) {
179 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
183 disconnectSelectionModel();
184 tocTV->setCurrentIndex(index);
185 tocTV->scrollTo(index);
186 reconnectSelectionModel();
190 void TocWidget::enableControls(bool enable)
192 updateTB->setEnabled(enable);
194 if (!form_.canOutline(typeCO->currentIndex()))
197 moveUpTB->setEnabled(enable);
198 moveDownTB->setEnabled(enable);
199 moveInTB->setEnabled(enable);
200 moveOutTB->setEnabled(enable);
202 depthSL->setEnabled(enable);
206 void TocWidget::updateView()
208 LYXERR(Debug::GUI, "In TocWidget::updateView()");
209 select(form_.currentIndex(typeCO->currentIndex()));
213 void TocWidget::updateGui(int selected_type)
215 vector<docstring> const & type_names = form_.typeNames();
216 if (type_names.empty()) {
217 enableControls(false);
219 tocTV->setModel(new QStandardItemModel);
220 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
224 QString const current_text = typeCO->currentText();
225 typeCO->blockSignals(true);
227 for (size_t i = 0; i != type_names.size(); ++i) {
228 QString item = toqstr(type_names[i]);
229 typeCO->addItem(item);
231 if (selected_type != -1)
232 typeCO->setCurrentIndex(selected_type);
234 int const new_index = typeCO->findText(current_text);
236 typeCO->setCurrentIndex(new_index);
239 typeCO->blockSignals(false);
241 setTocModel(typeCO->currentIndex());
245 void TocWidget::setTocModel(size_t type)
247 bool controls_enabled = false;
248 QStandardItemModel * toc_model = form_.tocModel(type);
250 controls_enabled = toc_model->rowCount() > 0;
251 tocTV->setModel(toc_model);
252 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
255 enableControls(controls_enabled);
257 reconnectSelectionModel();
259 if (controls_enabled) {
260 depthSL->setMaximum(form_.getTocDepth(type));
261 depthSL->setValue(depth_);
264 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
266 select(form_.currentIndex(typeCO->currentIndex()));
269 LYXERR(Debug::GUI, "tocModel()->rowCount "
270 << toc_model->rowCount()
271 << "\nform_->tocModel()->columnCount "
272 << toc_model->columnCount());
277 void TocWidget::reconnectSelectionModel()
279 connect(tocTV->selectionModel(),
280 SIGNAL(currentChanged(const QModelIndex &,
281 const QModelIndex &)),
283 SLOT(selectionChanged(const QModelIndex &,
284 const QModelIndex &)));
287 void TocWidget::disconnectSelectionModel()
289 disconnect(tocTV->selectionModel(),
290 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
291 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
294 } // namespace frontend
297 #include "TocWidget_moc.cpp"