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 "FuncRequest.h"
22 #include "support/debug.h"
24 #include <QHeaderView>
34 TocWidget::TocWidget(TocModels & models, QWidget * parent)
35 : QWidget(parent), depth_(0), models_(models)
39 moveOutTB->setIcon(QIcon(":/images/promote.png"));
40 moveInTB->setIcon(QIcon(":/images/demote.png"));
41 moveUpTB->setIcon(QIcon(":/images/up.png"));
42 moveDownTB->setIcon(QIcon(":/images/down.png"));
43 updateTB->setIcon(QIcon(":/images/reload.png"));
46 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
50 // hide the pointless QHeader for now
51 // in the future, new columns may appear
52 // like labels, bookmarks, etc...
53 // tocTV->header()->hide();
54 tocTV->header()->setVisible(false);
56 // Only one item selected at a time.
57 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
61 void TocWidget::on_tocTV_activated(QModelIndex const & index)
67 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
73 void TocWidget::goTo(QModelIndex const & index)
75 LYXERR(Debug::GUI, "goto " << index.row()
76 << ", " << index.column());
78 models_.goTo(typeCO->currentIndex(), index);
82 void TocWidget::on_updateTB_clicked()
84 // The backend update can take some time so we disable
85 // the controls while waiting.
86 enableControls(false);
87 models_.updateBackend();
90 /* FIXME (Ugras 17/11/06):
91 I have implemented a getIndexDepth function to get the model indices. In my
92 opinion, somebody should derive a new qvariant class for tocModelItem
93 which saves the string data and depth information. that will save the
96 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
99 return (index.parent() == QModelIndex())
100 ? depth : getIndexDepth(index.parent(),depth);
104 void TocWidget::on_depthSL_valueChanged(int depth)
112 void TocWidget::setTreeDepth(int depth)
116 // expanding and then collapsing is probably better,
117 // but my qt 4.1.2 doesn't have expandAll()..
118 //tocTV->expandAll();
119 QModelIndexList indices = tocTV->model()->match(
120 tocTV->model()->index(0,0),
121 Qt::DisplayRole, "*", -1,
122 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
124 int size = indices.size();
125 for (int i = 0; i < size; i++) {
126 QModelIndex index = indices[i];
127 if (getIndexDepth(index) < depth_)
128 tocTV->expand(index);
130 tocTV->collapse(index);
134 void TocWidget::on_typeCO_currentIndexChanged(int value)
140 void TocWidget::on_moveUpTB_clicked()
142 enableControls(false);
143 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
144 if (!list.isEmpty()) {
145 enableControls(false);
147 dispatch(FuncRequest(LFUN_OUTLINE_UP));
148 enableControls(true);
153 void TocWidget::on_moveDownTB_clicked()
155 enableControls(false);
156 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
157 if (!list.isEmpty()) {
158 enableControls(false);
160 dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
161 enableControls(true);
166 void TocWidget::on_moveInTB_clicked()
168 enableControls(false);
169 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
170 if (!list.isEmpty()) {
171 enableControls(false);
173 dispatch(FuncRequest(LFUN_OUTLINE_IN));
174 enableControls(true);
179 void TocWidget::on_moveOutTB_clicked()
181 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
182 if (!list.isEmpty()) {
183 enableControls(false);
185 dispatch(FuncRequest(LFUN_OUTLINE_OUT));
186 enableControls(true);
191 void TocWidget::select(QModelIndex const & index)
193 if (!index.isValid()) {
194 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
198 tocTV->scrollTo(index);
199 tocTV->clearSelection();
200 tocTV->setCurrentIndex(index);
204 void TocWidget::enableControls(bool enable)
206 updateTB->setEnabled(enable);
208 if (!models_.canOutline(typeCO->currentIndex()))
211 moveUpTB->setEnabled(enable);
212 moveDownTB->setEnabled(enable);
213 moveInTB->setEnabled(enable);
214 moveOutTB->setEnabled(enable);
216 depthSL->setEnabled(enable);
220 void TocWidget::updateView()
222 LYXERR(Debug::GUI, "In TocWidget::updateView()");
223 setTreeDepth(depth_);
224 select(models_.currentIndex(typeCO->currentIndex()));
228 void TocWidget::init(QString const & str)
230 QStringList const & type_names = models_.typeNames();
231 if (type_names.isEmpty()) {
232 enableControls(false);
234 tocTV->setModel(new QStandardItemModel);
235 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
239 int selected_type = models_.decodeType(str);
241 QString const current_text = typeCO->currentText();
242 typeCO->blockSignals(true);
244 for (int i = 0; i != type_names.size(); ++i)
245 typeCO->addItem(type_names[i]);
247 typeCO->setCurrentIndex(selected_type);
249 int const new_index = typeCO->findText(current_text);
251 typeCO->setCurrentIndex(new_index);
253 typeCO->setCurrentIndex(selected_type);
256 typeCO->blockSignals(false);
258 setTocModel(typeCO->currentIndex());
262 void TocWidget::setTocModel(size_t type)
264 bool controls_enabled = false;
265 QStandardItemModel * toc_model = models_.model(type);
267 controls_enabled = toc_model->rowCount() > 0;
268 tocTV->setModel(toc_model);
269 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
270 LYXERR(Debug::GUI, "tocModel()->rowCount "
271 << toc_model->rowCount()
272 << "\nform_->tocModel()->columnCount "
273 << toc_model->columnCount());
276 enableControls(controls_enabled);
278 if (controls_enabled) {
279 depthSL->setMaximum(models_.depth(type));
280 depthSL->setValue(depth_);
283 // setTocModel produce QTreeView reset and setting depth again
284 // is needed. That must be done after all Qt updates are processed.
285 QTimer::singleShot(0, this, SLOT(updateView()));
288 } // namespace frontend
291 #include "TocWidget_moc.cpp"