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"
16 #include "GuiApplication.h"
18 #include "qt_helpers.h"
22 #include "CutAndPaste.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
27 #include "TocBackend.h"
29 #include "insets/InsetCommand.h"
30 #include "insets/InsetRef.h"
32 #include "support/debug.h"
33 #include "support/lassert.h"
35 #include <QHeaderView>
46 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
47 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
51 moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
52 moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
53 moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
54 moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
55 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "png")));
58 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
62 // hide the pointless QHeader for now
63 // in the future, new columns may appear
64 // like labels, bookmarks, etc...
65 // tocTV->header()->hide();
66 tocTV->header()->setVisible(false);
68 // Only one item selected at a time.
69 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
71 // The toc types combo won't change its model.
72 typeCO->setModel(gui_view_.tocModels().nameModel());
74 // Make sure the buttons are disabled when first shown without a loaded
76 enableControls(false);
78 // make us responsible for the context menu of the tabbar
79 setContextMenuPolicy(Qt::CustomContextMenu);
80 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
81 this, SLOT(showContextMenu(const QPoint &)));
82 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
83 this, SLOT(showContextMenu(const QPoint &)));
89 void TocWidget::showContextMenu(const QPoint & pos)
91 std::string name = "context-toc-" + fromqstr(current_type_);
92 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
95 menu->exec(mapToGlobal(pos));
99 Inset * TocWidget::itemInset() const
101 QModelIndex const & index = tocTV->currentIndex();
102 TocItem const & item =
103 gui_view_.tocModels().currentItem(current_type_, index);
104 DocIterator const & dit = item.dit();
107 if (current_type_ == "label"
108 || current_type_ == "graphics"
109 || current_type_ == "citation"
110 || current_type_ == "child")
111 inset = dit.nextInset();
113 else if (current_type_ == "branch"
114 || current_type_ == "index")
115 inset = &dit.inset();
117 else if (current_type_ == "table"
118 || current_type_ == "listing"
119 || current_type_ == "figure") {
120 DocIterator tmp_dit(dit);
122 inset = &tmp_dit.inset();
128 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
129 FuncStatus & status) const
131 Inset * inset = itemInset();
133 FuncRequest tmpcmd(cmd);
135 return inset->getStatus(cur, tmpcmd, status);
140 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
142 Inset * inset = itemInset();
144 FuncRequest tmpcmd(cmd);
146 inset->dispatch(cur, tmpcmd);
150 void TocWidget::on_tocTV_activated(QModelIndex const & index)
156 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
158 Qt::MouseButtons const button = QApplication::mouseButtons();
159 if (button & Qt::LeftButton) {
161 gui_view_.setFocus();
166 void TocWidget::goTo(QModelIndex const & index)
168 LYXERR(Debug::GUI, "goto " << index.row()
169 << ", " << index.column());
171 gui_view_.tocModels().goTo(current_type_, index);
175 void TocWidget::on_updateTB_clicked()
177 // The backend update can take some time so we disable
178 // the controls while waiting.
179 enableControls(false);
180 gui_view_.tocModels().updateBackend();
184 void TocWidget::on_sortCB_stateChanged(int state)
186 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
190 void TocWidget::on_persistentCB_stateChanged(int state)
192 persistent_ = state == Qt::Checked;
196 /* FIXME (Ugras 17/11/06):
197 I have implemented a indexDepth function to get the model indices. In my
198 opinion, somebody should derive a new qvariant class for tocModelItem
199 which saves the string data and depth information. That will save the
200 depth calculation. */
202 static int indexDepth(QModelIndex const & index, int depth = -1)
205 return index.parent() == QModelIndex()
206 ? depth : indexDepth(index.parent(), depth);
210 void TocWidget::on_depthSL_valueChanged(int depth)
215 gui_view_.setFocus();
219 void TocWidget::setTreeDepth(int depth)
225 #if QT_VERSION >= 0x040300
226 // this should be faster than our own code below
228 tocTV->collapseAll();
230 tocTV->expandToDepth(depth - 1);
232 // expanding and then collapsing is probably better,
233 // but my qt 4.1.2 doesn't have expandAll()..
234 //tocTV->expandAll();
235 QModelIndexList indices = tocTV->model()->match(
236 tocTV->model()->index(0, 0),
237 Qt::DisplayRole, "*", -1,
238 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
240 int size = indices.size();
241 for (int i = 0; i < size; i++) {
242 QModelIndex index = indices[i];
243 tocTV->setExpanded(index, indexDepth(index) < depth_);
249 void TocWidget::on_typeCO_currentIndexChanged(int index)
251 current_type_ = typeCO->itemData(index).toString();
253 gui_view_.setFocus();
257 void TocWidget::outline(int func_code)
259 enableControls(false);
260 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
263 enableControls(false);
265 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
266 enableControls(true);
267 gui_view_.setFocus();
271 void TocWidget::on_moveUpTB_clicked()
273 outline(LFUN_OUTLINE_UP);
277 void TocWidget::on_moveDownTB_clicked()
279 outline(LFUN_OUTLINE_DOWN);
283 void TocWidget::on_moveInTB_clicked()
285 outline(LFUN_OUTLINE_IN);
289 void TocWidget::on_moveOutTB_clicked()
291 outline(LFUN_OUTLINE_OUT);
295 void TocWidget::select(QModelIndex const & index)
297 if (!index.isValid()) {
298 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
302 tocTV->scrollTo(index);
303 tocTV->clearSelection();
304 tocTV->setCurrentIndex(index);
308 /// Test if outlining operation is possible
309 static bool canOutline(QString const & type)
311 return type == "tableofcontents";
315 void TocWidget::enableControls(bool enable)
317 updateTB->setEnabled(enable);
319 if (!canOutline(current_type_))
322 moveUpTB->setEnabled(enable);
323 moveDownTB->setEnabled(enable);
324 moveInTB->setEnabled(enable);
325 moveOutTB->setEnabled(enable);
327 depthSL->setMaximum(0);
328 depthSL->setValue(0);
333 /// Test if synchronized navigation is possible
334 static bool canNavigate(QString const & type)
336 // It is not possible to have synchronous navigation in a correctl
337 // and efficient way with the label type because Toc::item() do a linear
338 // seatch. Even if fixed, it might even not be desirable to do so if we
339 // want to support drag&drop of labels and references.
340 return type != "label" && type != "change";
344 void TocWidget::updateView()
346 if (!gui_view_.view()) {
347 enableControls(false);
348 typeCO->setEnabled(false);
350 tocTV->setEnabled(false);
353 typeCO->setEnabled(true);
354 tocTV->setEnabled(false);
355 tocTV->setUpdatesEnabled(false);
357 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
358 if (tocTV->model() != toc_model) {
359 tocTV->setModel(toc_model);
360 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
362 setTreeDepth(depth_);
365 sortCB->blockSignals(true);
366 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
367 sortCB->blockSignals(false);
369 persistentCB->setChecked(persistent_);
371 bool controls_enabled = toc_model && toc_model->rowCount() > 0
372 && !gui_view_.buffer()->isReadonly();
373 enableControls(controls_enabled);
375 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
376 depthSL->setValue(depth_);
378 setTreeDepth(depth_);
379 if (canNavigate(current_type_))
380 select(gui_view_.tocModels().currentIndex(current_type_));
381 tocTV->setEnabled(true);
382 tocTV->setUpdatesEnabled(true);
386 static QString decodeType(QString const & str)
389 if (type.contains("tableofcontents")) {
390 type = "tableofcontents";
391 } else if (type.contains("floatlist")) {
392 if (type.contains("\"figure"))
394 else if (type.contains("\"table"))
396 else if (type.contains("\"algorithm"))
403 void TocWidget::init(QString const & str)
407 new_index = typeCO->findData(current_type_);
409 new_index = typeCO->findData(decodeType(str));
411 // If everything else fails, settle on the table of contents which is
412 // guaranted to exist.
413 if (new_index == -1) {
414 current_type_ = "tableofcontents";
415 new_index = typeCO->findData(current_type_);
417 current_type_ = typeCO->itemData(new_index).toString();
420 typeCO->blockSignals(true);
421 typeCO->setCurrentIndex(new_index);
422 typeCO->blockSignals(false);
425 } // namespace frontend
428 #include "moc_TocWidget.cpp"