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 vector<docstring> const & type_names = form_.typeNames();
217 if (type_names.empty()) {
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 (size_t i = 0; i != type_names.size(); ++i) {
229 QString item = toqstr(type_names[i]);
230 typeCO->addItem(item);
232 if (selected_type != -1)
233 typeCO->setCurrentIndex(selected_type);
235 int const new_index = typeCO->findText(current_text);
237 typeCO->setCurrentIndex(new_index);
240 typeCO->blockSignals(false);
242 setTocModel(typeCO->currentIndex());
244 // setTocModel produce QTreeView reset and setting depth again
245 // is needed. That must be done after all Qt updates are processed.
246 QTimer::singleShot(0, this, SLOT(setTreeDepth()));
250 void TocWidget::setTocModel(size_t type)
252 bool controls_enabled = false;
253 QStandardItemModel * toc_model = form_.tocModel(type);
255 controls_enabled = toc_model->rowCount() > 0;
256 tocTV->setModel(toc_model);
257 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
260 enableControls(controls_enabled);
262 reconnectSelectionModel();
264 if (controls_enabled) {
265 depthSL->setMaximum(form_.getTocDepth(type));
266 depthSL->setValue(depth_);
269 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
271 select(form_.currentIndex(typeCO->currentIndex()));
274 LYXERR(Debug::GUI, "tocModel()->rowCount "
275 << toc_model->rowCount()
276 << "\nform_->tocModel()->columnCount "
277 << toc_model->columnCount());
282 void TocWidget::reconnectSelectionModel()
284 connect(tocTV->selectionModel(),
285 SIGNAL(currentChanged(const QModelIndex &,
286 const QModelIndex &)),
288 SLOT(selectionChanged(const QModelIndex &,
289 const QModelIndex &)));
292 void TocWidget::disconnectSelectionModel()
294 disconnect(tocTV->selectionModel(),
295 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
296 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
299 } // namespace frontend
302 #include "TocWidget_moc.cpp"