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"
24 #include "support/lassert.h"
26 #include <QHeaderView>
36 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
37 : QWidget(parent), depth_(0), gui_view_(gui_view)
41 moveOutTB->setIcon(QIcon(":/images/promote.png"));
42 moveInTB->setIcon(QIcon(":/images/demote.png"));
43 moveUpTB->setIcon(QIcon(":/images/up.png"));
44 moveDownTB->setIcon(QIcon(":/images/down.png"));
45 updateTB->setIcon(QIcon(":/images/reload.png"));
48 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
52 // hide the pointless QHeader for now
53 // in the future, new columns may appear
54 // like labels, bookmarks, etc...
55 // tocTV->header()->hide();
56 tocTV->header()->setVisible(false);
58 // Only one item selected at a time.
59 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
63 void TocWidget::on_tocTV_activated(QModelIndex const & index)
69 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
76 void TocWidget::goTo(QModelIndex const & index)
78 LYXERR(Debug::GUI, "goto " << index.row()
79 << ", " << index.column());
81 gui_view_.tocModels().goTo(typeCO->currentIndex(), index);
85 void TocWidget::on_updateTB_clicked()
87 // The backend update can take some time so we disable
88 // the controls while waiting.
89 enableControls(false);
90 gui_view_.tocModels().updateBackend();
94 /* FIXME (Ugras 17/11/06):
95 I have implemented a indexDepth function to get the model indices. In my
96 opinion, somebody should derive a new qvariant class for tocModelItem
97 which saves the string data and depth information. That will save the
100 static int indexDepth(QModelIndex const & index, int depth = -1)
103 return index.parent() == QModelIndex()
104 ? depth : indexDepth(index.parent(), depth);
108 void TocWidget::on_depthSL_valueChanged(int depth)
113 gui_view_.setFocus();
117 void TocWidget::setTreeDepth(int depth)
121 // expanding and then collapsing is probably better,
122 // but my qt 4.1.2 doesn't have expandAll()..
123 //tocTV->expandAll();
124 QModelIndexList indices = tocTV->model()->match(
125 tocTV->model()->index(0,0),
126 Qt::DisplayRole, "*", -1,
127 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
129 int size = indices.size();
130 for (int i = 0; i < size; i++) {
131 QModelIndex index = indices[i];
132 tocTV->setExpanded(index, indexDepth(index) < depth_);
137 void TocWidget::on_typeCO_currentIndexChanged(int)
140 gui_view_.setFocus();
144 void TocWidget::outline(int func_code)
146 enableControls(false);
147 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
150 enableControls(false);
152 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
153 enableControls(true);
154 gui_view_.setFocus();
158 void TocWidget::on_moveUpTB_clicked()
160 outline(LFUN_OUTLINE_UP);
164 void TocWidget::on_moveDownTB_clicked()
166 outline(LFUN_OUTLINE_DOWN);
170 void TocWidget::on_moveInTB_clicked()
172 outline(LFUN_OUTLINE_IN);
176 void TocWidget::on_moveOutTB_clicked()
178 outline(LFUN_OUTLINE_OUT);
182 void TocWidget::select(QModelIndex const & index)
184 if (!index.isValid()) {
185 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
189 tocTV->scrollTo(index);
190 tocTV->clearSelection();
191 tocTV->setCurrentIndex(index);
195 void TocWidget::enableControls(bool enable)
197 updateTB->setEnabled(enable);
199 if (!gui_view_.tocModels().canOutline(typeCO->currentIndex()))
202 moveUpTB->setEnabled(enable);
203 moveDownTB->setEnabled(enable);
204 moveInTB->setEnabled(enable);
205 moveOutTB->setEnabled(enable);
207 depthSL->setEnabled(enable);
211 void TocWidget::updateView()
213 LYXERR(Debug::GUI, "In TocWidget::updateView()");
215 setTreeDepth(depth_);
216 select(gui_view_.tocModels().currentIndex(typeCO->currentIndex()));
220 void TocWidget::init(QString const & str)
222 QStringList const & type_names = gui_view_.tocModels().typeNames();
223 if (type_names.isEmpty()) {
224 enableControls(false);
225 typeCO->setEnabled(false);
226 tocTV->setModel(new QStandardItemModel);
227 tocTV->setEnabled(false);
231 typeCO->setEnabled(true);
232 tocTV->setEnabled(true);
233 int selected_type = gui_view_.tocModels().decodeType(str);
235 QString const current_text = typeCO->currentText();
236 typeCO->blockSignals(true);
238 for (int i = 0; i != type_names.size(); ++i)
239 typeCO->addItem(type_names[i]);
241 typeCO->setCurrentIndex(selected_type);
243 int const new_index = typeCO->findText(current_text);
245 typeCO->setCurrentIndex(new_index);
247 typeCO->setCurrentIndex(selected_type);
250 typeCO->blockSignals(false);
252 // setTocModel produce QTreeView reset and setting depth again
253 // is needed. That must be done after all Qt updates are processed.
254 QTimer::singleShot(0, this, SLOT(updateView()));
258 void TocWidget::setTocModel()
260 int const toc_type = typeCO->currentIndex();
261 QStandardItemModel * toc_model = gui_view_.tocModels().model(toc_type);
262 LASSERT(toc_model, return);
264 if (tocTV->model() != toc_model) {
265 tocTV->setModel(toc_model);
266 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
269 bool controls_enabled = toc_model->rowCount() > 0;;
270 enableControls(controls_enabled);
272 if (controls_enabled) {
273 depthSL->setMaximum(gui_view_.tocModels().depth(toc_type));
274 depthSL->setValue(depth_);
278 } // namespace frontend
281 #include "TocWidget_moc.cpp"