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);
61 // The toc types combo won't change its model.
62 typeCO->setModel(gui_view_.tocModels().nameModel());
66 void TocWidget::on_tocTV_activated(QModelIndex const & index)
72 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
79 void TocWidget::goTo(QModelIndex const & index)
81 LYXERR(Debug::GUI, "goto " << index.row()
82 << ", " << index.column());
84 gui_view_.tocModels().goTo(current_type_, index);
88 void TocWidget::on_updateTB_clicked()
90 // The backend update can take some time so we disable
91 // the controls while waiting.
92 enableControls(false);
93 gui_view_.tocModels().updateBackend();
97 /* FIXME (Ugras 17/11/06):
98 I have implemented a indexDepth function to get the model indices. In my
99 opinion, somebody should derive a new qvariant class for tocModelItem
100 which saves the string data and depth information. That will save the
101 depth calculation. */
103 static int indexDepth(QModelIndex const & index, int depth = -1)
106 return index.parent() == QModelIndex()
107 ? depth : indexDepth(index.parent(), depth);
111 void TocWidget::on_depthSL_valueChanged(int depth)
116 gui_view_.setFocus();
120 void TocWidget::setTreeDepth(int depth)
126 // expanding and then collapsing is probably better,
127 // but my qt 4.1.2 doesn't have expandAll()..
128 //tocTV->expandAll();
129 QModelIndexList indices = tocTV->model()->match(
130 tocTV->model()->index(0,0),
131 Qt::DisplayRole, "*", -1,
132 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
134 int size = indices.size();
135 for (int i = 0; i < size; i++) {
136 QModelIndex index = indices[i];
137 tocTV->setExpanded(index, indexDepth(index) < depth_);
142 void TocWidget::on_typeCO_currentIndexChanged(int index)
144 current_type_ = typeCO->itemData(index).toString();
146 gui_view_.setFocus();
150 void TocWidget::outline(int func_code)
152 enableControls(false);
153 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
156 enableControls(false);
158 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
159 enableControls(true);
160 gui_view_.setFocus();
164 void TocWidget::on_moveUpTB_clicked()
166 outline(LFUN_OUTLINE_UP);
170 void TocWidget::on_moveDownTB_clicked()
172 outline(LFUN_OUTLINE_DOWN);
176 void TocWidget::on_moveInTB_clicked()
178 outline(LFUN_OUTLINE_IN);
182 void TocWidget::on_moveOutTB_clicked()
184 outline(LFUN_OUTLINE_OUT);
188 void TocWidget::select(QModelIndex const & index)
190 if (!index.isValid()) {
191 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
195 tocTV->scrollTo(index);
196 tocTV->clearSelection();
197 tocTV->setCurrentIndex(index);
201 /// Test if outlining operation is possible
202 static bool canOutline(QString const & type)
204 return type == "tableofcontents";
208 void TocWidget::enableControls(bool enable)
210 updateTB->setEnabled(enable);
212 if (!canOutline(current_type_))
215 moveUpTB->setEnabled(enable);
216 moveDownTB->setEnabled(enable);
217 moveInTB->setEnabled(enable);
218 moveOutTB->setEnabled(enable);
220 depthSL->setEnabled(enable);
224 void TocWidget::updateView()
226 LYXERR(Debug::GUI, "In TocWidget::updateView()");
228 setTreeDepth(depth_);
229 select(gui_view_.tocModels().currentIndex(current_type_));
233 static QString decodeType(QString const & str)
236 if (type.contains("tableofcontents")) {
237 type = "tableofcontents";
238 } else if (type.contains("floatlist")) {
239 if (type.contains("\"figure"))
241 else if (type.contains("\"table"))
243 else if (type.contains("\"algorithm"))
250 void TocWidget::init(QString const & str)
252 if (!gui_view_.view()) {
253 enableControls(false);
254 typeCO->setEnabled(false);
256 tocTV->setEnabled(false);
260 typeCO->setEnabled(true);
261 tocTV->setEnabled(true);
263 typeCO->blockSignals(true);
267 new_index = typeCO->findData(current_type_);
269 new_index = typeCO->findData(decodeType(str));
271 // If everything else fails, settle on the table of contents which is
272 // guaranted to exist.
273 if (new_index == -1) {
274 current_type_ = "tableofcontents";
275 new_index = typeCO->findData(current_type_);
278 typeCO->setCurrentIndex(new_index);
279 typeCO->blockSignals(false);
283 void TocWidget::setTocModel()
285 QStandardItemModel * toc_model = gui_view_.tocModels().model(current_type_);
287 if (tocTV->model() != toc_model) {
288 tocTV->setModel(toc_model);
289 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
292 bool controls_enabled = toc_model && toc_model->rowCount() > 0;;
293 enableControls(controls_enabled);
295 if (controls_enabled) {
296 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
297 depthSL->setValue(depth_);
301 } // namespace frontend
304 #include "TocWidget_moc.cpp"