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)
70 // FIXME: It would be nice to return the focus to the work area in this
71 // case. But we need access to the GuiView!
75 void TocWidget::goTo(QModelIndex const & index)
77 LYXERR(Debug::GUI, "goto " << index.row()
78 << ", " << index.column());
80 models_.goTo(typeCO->currentIndex(), index);
84 void TocWidget::on_updateTB_clicked()
86 // The backend update can take some time so we disable
87 // the controls while waiting.
88 enableControls(false);
89 models_.updateBackend();
92 /* FIXME (Ugras 17/11/06):
93 I have implemented a getIndexDepth function to get the model indices. In my
94 opinion, somebody should derive a new qvariant class for tocModelItem
95 which saves the string data and depth information. that will save the
98 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
101 return (index.parent() == QModelIndex())
102 ? depth : getIndexDepth(index.parent(),depth);
106 void TocWidget::on_depthSL_valueChanged(int depth)
114 void TocWidget::setTreeDepth(int depth)
118 // expanding and then collapsing is probably better,
119 // but my qt 4.1.2 doesn't have expandAll()..
120 //tocTV->expandAll();
121 QModelIndexList indices = tocTV->model()->match(
122 tocTV->model()->index(0,0),
123 Qt::DisplayRole, "*", -1,
124 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
126 int size = indices.size();
127 for (int i = 0; i < size; i++) {
128 QModelIndex index = indices[i];
129 if (getIndexDepth(index) < depth_)
130 tocTV->expand(index);
132 tocTV->collapse(index);
136 void TocWidget::on_typeCO_currentIndexChanged(int value)
142 void TocWidget::on_moveUpTB_clicked()
144 enableControls(false);
145 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
146 if (!list.isEmpty()) {
147 enableControls(false);
149 dispatch(FuncRequest(LFUN_OUTLINE_UP));
150 enableControls(true);
155 void TocWidget::on_moveDownTB_clicked()
157 enableControls(false);
158 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
159 if (!list.isEmpty()) {
160 enableControls(false);
162 dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
163 enableControls(true);
168 void TocWidget::on_moveInTB_clicked()
170 enableControls(false);
171 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
172 if (!list.isEmpty()) {
173 enableControls(false);
175 dispatch(FuncRequest(LFUN_OUTLINE_IN));
176 enableControls(true);
181 void TocWidget::on_moveOutTB_clicked()
183 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
184 if (!list.isEmpty()) {
185 enableControls(false);
187 dispatch(FuncRequest(LFUN_OUTLINE_OUT));
188 enableControls(true);
193 void TocWidget::select(QModelIndex const & index)
195 if (!index.isValid()) {
196 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
200 tocTV->scrollTo(index);
201 tocTV->clearSelection();
202 tocTV->setCurrentIndex(index);
206 void TocWidget::enableControls(bool enable)
208 updateTB->setEnabled(enable);
210 if (!models_.canOutline(typeCO->currentIndex()))
213 moveUpTB->setEnabled(enable);
214 moveDownTB->setEnabled(enable);
215 moveInTB->setEnabled(enable);
216 moveOutTB->setEnabled(enable);
218 depthSL->setEnabled(enable);
222 void TocWidget::updateView()
224 LYXERR(Debug::GUI, "In TocWidget::updateView()");
225 setTreeDepth(depth_);
226 select(models_.currentIndex(typeCO->currentIndex()));
230 void TocWidget::init(QString const & str)
232 QStringList const & type_names = models_.typeNames();
233 if (type_names.isEmpty()) {
234 enableControls(false);
236 tocTV->setModel(new QStandardItemModel);
237 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
241 int selected_type = models_.decodeType(str);
243 QString const current_text = typeCO->currentText();
244 typeCO->blockSignals(true);
246 for (int i = 0; i != type_names.size(); ++i)
247 typeCO->addItem(type_names[i]);
249 typeCO->setCurrentIndex(selected_type);
251 int const new_index = typeCO->findText(current_text);
253 typeCO->setCurrentIndex(new_index);
255 typeCO->setCurrentIndex(selected_type);
258 typeCO->blockSignals(false);
260 setTocModel(typeCO->currentIndex());
264 void TocWidget::setTocModel(size_t type)
266 bool controls_enabled = false;
267 QStandardItemModel * toc_model = models_.model(type);
269 controls_enabled = toc_model->rowCount() > 0;
270 tocTV->setModel(toc_model);
271 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
272 LYXERR(Debug::GUI, "tocModel()->rowCount "
273 << toc_model->rowCount()
274 << "\nform_->tocModel()->columnCount "
275 << toc_model->columnCount());
278 enableControls(controls_enabled);
280 if (controls_enabled) {
281 depthSL->setMaximum(models_.depth(type));
282 depthSL->setValue(depth_);
285 // setTocModel produce QTreeView reset and setting depth again
286 // is needed. That must be done after all Qt updates are processed.
287 QTimer::singleShot(0, this, SLOT(updateView()));
290 } // namespace frontend
293 #include "TocWidget_moc.cpp"