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()");
211 select(form_.currentIndex(typeCO->currentIndex()));
215 void TocWidget::updateGui(int selected_type)
217 QStringList const & type_names = form_.typeNames();
218 if (type_names.isEmpty()) {
219 enableControls(false);
221 tocTV->setModel(new QStandardItemModel);
222 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
226 QString const current_text = typeCO->currentText();
227 typeCO->blockSignals(true);
229 for (int i = 0; i != type_names.size(); ++i)
230 typeCO->addItem(type_names[i]);
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());
243 // setTocModel produce QTreeView reset and setting depth again
244 // is needed. That must be done after all Qt updates are processed.
245 QTimer::singleShot(0, this, SLOT(updateView()));
249 void TocWidget::setTocModel(size_t type)
251 bool controls_enabled = false;
252 QStandardItemModel * toc_model = form_.tocModel(type);
254 controls_enabled = toc_model->rowCount() > 0;
255 tocTV->setModel(toc_model);
256 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
259 enableControls(controls_enabled);
261 reconnectSelectionModel();
263 if (controls_enabled) {
264 depthSL->setMaximum(form_.getTocDepth(type));
265 depthSL->setValue(depth_);
268 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
270 select(form_.currentIndex(typeCO->currentIndex()));
273 LYXERR(Debug::GUI, "tocModel()->rowCount "
274 << toc_model->rowCount()
275 << "\nform_->tocModel()->columnCount "
276 << toc_model->columnCount());
281 void TocWidget::reconnectSelectionModel()
283 connect(tocTV->selectionModel(),
284 SIGNAL(currentChanged(const QModelIndex &,
285 const QModelIndex &)),
287 SLOT(selectionChanged(const QModelIndex &,
288 const QModelIndex &)));
291 void TocWidget::disconnectSelectionModel()
293 disconnect(tocTV->selectionModel(),
294 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
295 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
298 } // namespace frontend
301 #include "TocWidget_moc.cpp"