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"
20 #include "FuncRequest.h"
23 #include "support/debug.h"
25 #include <QHeaderView>
35 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
36 : QWidget(parent), depth_(0), gui_view_(gui_view)
40 moveOutTB->setIcon(QIcon(":/images/promote.png"));
41 moveInTB->setIcon(QIcon(":/images/demote.png"));
42 moveUpTB->setIcon(QIcon(":/images/up.png"));
43 moveDownTB->setIcon(QIcon(":/images/down.png"));
44 updateTB->setIcon(QIcon(":/images/reload.png"));
47 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
51 // hide the pointless QHeader for now
52 // in the future, new columns may appear
53 // like labels, bookmarks, etc...
54 // tocTV->header()->hide();
55 tocTV->header()->setVisible(false);
57 // Only one item selected at a time.
58 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
62 void TocWidget::on_tocTV_activated(QModelIndex const & index)
68 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
75 void TocWidget::goTo(QModelIndex const & index)
77 LYXERR(Debug::GUI, "goto " << index.row()
78 << ", " << index.column());
80 gui_view_.tocModels().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 gui_view_.tocModels().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)
111 gui_view_.setFocus();
115 void TocWidget::setTreeDepth(int depth)
119 // expanding and then collapsing is probably better,
120 // but my qt 4.1.2 doesn't have expandAll()..
121 //tocTV->expandAll();
122 QModelIndexList indices = tocTV->model()->match(
123 tocTV->model()->index(0,0),
124 Qt::DisplayRole, "*", -1,
125 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
127 int size = indices.size();
128 for (int i = 0; i < size; i++) {
129 QModelIndex index = indices[i];
130 tocTV->setExpanded(index, getIndexDepth(index) < depth_);
135 void TocWidget::on_typeCO_currentIndexChanged(int value)
138 gui_view_.setFocus();
142 void TocWidget::outline(int func_code)
144 enableControls(false);
145 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
148 enableControls(false);
150 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
151 enableControls(true);
152 gui_view_.setFocus();
156 void TocWidget::on_moveUpTB_clicked()
158 outline(LFUN_OUTLINE_UP);
162 void TocWidget::on_moveDownTB_clicked()
164 outline(LFUN_OUTLINE_DOWN);
168 void TocWidget::on_moveInTB_clicked()
170 outline(LFUN_OUTLINE_IN);
174 void TocWidget::on_moveOutTB_clicked()
176 outline(LFUN_OUTLINE_OUT);
180 void TocWidget::select(QModelIndex const & index)
182 if (!index.isValid()) {
183 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
187 tocTV->scrollTo(index);
188 tocTV->clearSelection();
189 tocTV->setCurrentIndex(index);
193 void TocWidget::enableControls(bool enable)
195 updateTB->setEnabled(enable);
197 if (!gui_view_.tocModels().canOutline(typeCO->currentIndex()))
200 moveUpTB->setEnabled(enable);
201 moveDownTB->setEnabled(enable);
202 moveInTB->setEnabled(enable);
203 moveOutTB->setEnabled(enable);
205 depthSL->setEnabled(enable);
209 void TocWidget::updateView()
211 LYXERR(Debug::GUI, "In TocWidget::updateView()");
212 setTreeDepth(depth_);
213 select(gui_view_.tocModels().currentIndex(typeCO->currentIndex()));
217 void TocWidget::init(QString const & str)
219 QStringList const & type_names = gui_view_.tocModels().typeNames();
220 if (type_names.isEmpty()) {
221 enableControls(false);
223 tocTV->setModel(new QStandardItemModel);
224 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
228 int selected_type = gui_view_.tocModels().decodeType(str);
230 QString const current_text = typeCO->currentText();
231 typeCO->blockSignals(true);
233 for (int i = 0; i != type_names.size(); ++i)
234 typeCO->addItem(type_names[i]);
236 typeCO->setCurrentIndex(selected_type);
238 int const new_index = typeCO->findText(current_text);
240 typeCO->setCurrentIndex(new_index);
242 typeCO->setCurrentIndex(selected_type);
245 typeCO->blockSignals(false);
247 setTocModel(typeCO->currentIndex());
251 void TocWidget::setTocModel(size_t type)
253 bool controls_enabled = false;
254 QStandardItemModel * toc_model = gui_view_.tocModels().model(type);
256 controls_enabled = toc_model->rowCount() > 0;
257 tocTV->setModel(toc_model);
258 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
259 LYXERR(Debug::GUI, "tocModel()->rowCount "
260 << toc_model->rowCount()
261 << "\nform_->tocModel()->columnCount "
262 << toc_model->columnCount());
265 enableControls(controls_enabled);
267 if (controls_enabled) {
268 depthSL->setMaximum(gui_view_.tocModels().depth(type));
269 depthSL->setValue(depth_);
272 // setTocModel produce QTreeView reset and setting depth again
273 // is needed. That must be done after all Qt updates are processed.
274 QTimer::singleShot(0, this, SLOT(updateView()));
277 } // namespace frontend
280 #include "TocWidget_moc.cpp"