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"
20 #include "FancyLineEdit.h"
23 #include "BufferView.h"
25 #include "CutAndPaste.h"
26 #include "FuncRequest.h"
27 #include "FuncStatus.h"
30 #include "TocBackend.h"
32 #include "insets/InsetCommand.h"
33 #include "insets/InsetRef.h"
35 #include "support/debug.h"
36 #include "support/lassert.h"
38 #include <QHeaderView>
49 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
50 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view),
51 timer_(new QTimer(this))
55 moveOutTB->setIcon(QIcon(getPixmap("images/", "outline-out", "svgz,png")));
56 moveInTB->setIcon(QIcon(getPixmap("images/", "outline-in", "svgz,png")));
57 moveUpTB->setIcon(QIcon(getPixmap("images/", "outline-up", "svgz,png")));
58 moveDownTB->setIcon(QIcon(getPixmap("images/", "outline-down", "svgz,png")));
59 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "svgz,png")));
61 QSize icon_size = gui_view.iconSize();
62 moveOutTB->setIconSize(icon_size);
63 moveInTB->setIconSize(icon_size);
64 moveUpTB->setIconSize(icon_size);
65 moveDownTB->setIconSize(icon_size);
66 updateTB->setIconSize(icon_size);
69 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
73 // hide the pointless QHeader for now
74 // in the future, new columns may appear
75 // like labels, bookmarks, etc...
76 // tocTV->header()->hide();
77 tocTV->header()->setVisible(false);
79 // Only one item selected at a time.
80 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
82 // The toc types combo won't change its model.
83 typeCO->setModel(gui_view_.tocModels().nameModel());
86 filter_ = new FancyLineEdit(this);
87 filter_->setClearButton(true);
88 filter_->setPlaceholderText(qt_("All items"));
89 filterBarL->addWidget(filter_, 0);
90 filterLA->setBuddy(filter_);
91 setFocusProxy(filter_);
93 // Make sure the buttons are disabled when first shown without a loaded
95 enableControls(false);
97 // make us responsible for the context menu of the tabbar
98 setContextMenuPolicy(Qt::CustomContextMenu);
99 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
100 this, SLOT(showContextMenu(const QPoint &)));
101 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
102 this, SLOT(showContextMenu(const QPoint &)));
103 connect(filter_, SIGNAL(textEdited(QString)),
104 this, SLOT(filterContents()));
105 #if (QT_VERSION < 0x050000)
106 connect(filter_, SIGNAL(downPressed()),
107 tocTV, SLOT(setFocus()));
109 connect(filter_, &FancyLineEdit::downPressed,
110 tocTV, [this](){ focusAndHighlight(tocTV); });
112 connect(activeFilterCO, SIGNAL(activated(int)),
113 this, SLOT(filterContents()));
115 // setting the update timer
116 timer_->setSingleShot(true);
117 connect(timer_, SIGNAL(timeout()), this, SLOT(finishUpdateView()));
123 void TocWidget::showContextMenu(const QPoint & pos)
125 std::string name = "context-toc-" + fromqstr(current_type_);
126 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
129 menu->exec(mapToGlobal(pos));
133 Inset * TocWidget::itemInset() const
135 QModelIndex const & index = tocTV->currentIndex();
136 TocItem const & item =
137 gui_view_.tocModels().currentItem(current_type_, index);
138 DocIterator const & dit = item.dit();
140 Inset * inset = nullptr;
141 if (current_type_ == "label"
142 || current_type_ == "graphics"
143 || current_type_ == "citation"
144 || current_type_ == "child")
145 inset = dit.nextInset();
147 else if (current_type_ == "branch"
148 || current_type_ == "index"
149 || current_type_ == "change"
150 || current_type_ == "table"
151 || current_type_ == "listing"
152 || current_type_ == "figure")
153 inset = &dit.inset();
159 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
160 FuncStatus & status) const
162 Inset * inset = itemInset();
163 FuncRequest tmpcmd(cmd);
165 QModelIndex const & index = tocTV->currentIndex();
166 TocItem const & item =
167 gui_view_.tocModels().currentItem(current_type_, index);
169 switch (cmd.action())
171 case LFUN_CHANGE_ACCEPT:
172 case LFUN_CHANGE_REJECT:
173 case LFUN_OUTLINE_UP:
174 case LFUN_OUTLINE_DOWN:
175 case LFUN_OUTLINE_IN:
176 case LFUN_OUTLINE_OUT:
177 case LFUN_SECTION_SELECT:
178 status.setEnabled((bool)item.dit());
181 case LFUN_LABEL_COPY_AS_REFERENCE: {
182 // For labels in math, we need to supply the label as a string
183 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
185 return inset->getStatus(cur, label_copy, status);
191 return inset->getStatus(cur, tmpcmd, status);
198 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd,
202 Inset * inset = itemInset();
204 QModelIndex const & index = tocTV->currentIndex();
205 TocItem const & item =
206 gui_view_.tocModels().currentItem(current_type_, index);
208 // Start an undo group.
209 cur.beginUndoGroup();
211 switch (cmd.action())
213 case LFUN_CHANGE_ACCEPT:
214 case LFUN_CHANGE_REJECT: {
215 // The action is almost always LYX_UNKNOWN_ACTION, which will
216 // have the effect of moving the cursor to the location of
217 // the change. (See TocItem::action.)
218 dispatch(item.action());
219 // If we do not reset the origin, then the request will be sent back
220 // here, and we are in an infinite loop. But we need the dispatch
221 // machinery to clean up for us, if the cursor is in an inset that
222 // will be deleted. See bug #10316.
223 FuncRequest tmpcmd(cmd);
224 tmpcmd.setOrigin(FuncRequest::INTERNAL);
226 dr.forceBufferUpdate();
230 case LFUN_SECTION_SELECT:
231 dispatch(item.action());
233 // necessary to get the selection drawn.
234 cur.buffer()->changed(true);
235 gui_view_.setFocus();
238 case LFUN_LABEL_COPY_AS_REFERENCE: {
239 // For labels in math, we need to supply the label as a string
240 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
242 inset->dispatch(cur, label_copy);
246 case LFUN_OUTLINE_UP:
247 case LFUN_OUTLINE_DOWN:
248 case LFUN_OUTLINE_IN:
249 case LFUN_OUTLINE_OUT:
250 outline(cmd.action());
254 FuncRequest tmpcmd(cmd);
256 inset->dispatch(cur, tmpcmd);
263 void TocWidget::on_tocTV_activated(QModelIndex const & index)
269 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
272 Qt::MouseButtons const button = QApplication::mouseButtons();
273 if (button & Qt::LeftButton) {
275 gui_view_.setFocus();
276 gui_view_.activateWindow();
281 void TocWidget::goTo(QModelIndex const & index)
283 LYXERR(Debug::GUI, "goto " << index.row()
284 << ", " << index.column());
286 sendDispatch(gui_view_.tocModels().goTo(current_type_, index));
290 void TocWidget::on_updateTB_clicked()
292 // The backend update can take some time so we disable
293 // the controls while waiting.
294 enableControls(false);
295 gui_view_.currentBufferView()->buffer().updateBuffer();
299 void TocWidget::on_sortCB_stateChanged(int state)
301 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
306 void TocWidget::on_persistentCB_stateChanged(int state)
308 persistent_ = state == Qt::Checked;
313 /* FIXME (Ugras 17/11/06):
314 I have implemented a indexDepth function to get the model indices. In my
315 opinion, somebody should derive a new qvariant class for tocModelItem
316 which saves the string data and depth information. That will save the
317 depth calculation. */
319 static int indexDepth(QModelIndex const & index, int depth = -1)
322 return index.parent() == QModelIndex()
323 ? depth : indexDepth(index.parent(), depth);
327 void TocWidget::on_depthSL_valueChanged(int depth)
332 gui_view_.setFocus();
336 void TocWidget::setTreeDepth(int depth)
343 tocTV->collapseAll();
345 tocTV->expandToDepth(depth - 1);
349 void TocWidget::on_typeCO_currentIndexChanged(int index)
354 current_type_ = typeCO->itemData(index).toString();
356 if (typeCO->hasFocus())
357 gui_view_.setFocus();
361 void TocWidget::outline(FuncCode func_code)
363 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
367 //if another window is active, this attempt will fail,
368 //but it will work at least for the second attempt
369 gui_view_.activateWindow();
371 enableControls(false);
373 sendDispatch(FuncRequest(func_code));
374 enableControls(true);
375 gui_view_.setFocus();
379 void TocWidget::sendDispatch(FuncRequest fr)
382 fr.setViewOrigin(&gui_view_);
383 DispatchResult const & dr = dispatch(fr);
385 gui_view_.message(dr.message());
389 void TocWidget::on_moveUpTB_clicked()
391 outline(LFUN_OUTLINE_UP);
395 void TocWidget::on_moveDownTB_clicked()
397 outline(LFUN_OUTLINE_DOWN);
401 void TocWidget::on_moveInTB_clicked()
403 outline(LFUN_OUTLINE_IN);
407 void TocWidget::on_moveOutTB_clicked()
409 outline(LFUN_OUTLINE_OUT);
413 void TocWidget::select(QModelIndex const & index)
415 if (!index.isValid()) {
416 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
420 tocTV->scrollTo(index);
421 tocTV->clearSelection();
422 tocTV->setCurrentIndex(index);
426 void TocWidget::enableControls(bool enable)
428 updateTB->setEnabled(enable);
433 moveUpTB->setEnabled(enable);
434 moveDownTB->setEnabled(enable);
435 moveInTB->setEnabled(enable);
436 moveOutTB->setEnabled(enable);
440 void TocWidget::updateView()
442 if (!gui_view_.documentBufferView()) {
443 tocTV->setModel(nullptr);
444 depthSL->setMaximum(0);
445 depthSL->setValue(0);
450 bool const is_sortable = isSortable();
451 sortCB->setEnabled(is_sortable);
452 bool focus = tocTV->hasFocus();
453 tocTV->setEnabled(false);
454 tocTV->setUpdatesEnabled(false);
456 QAbstractItemModel * toc_model =
457 gui_view_.tocModels().model(current_type_);
458 if (tocTV->model() != toc_model) {
459 tocTV->setModel(toc_model);
460 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
462 setTreeDepth(depth_);
465 sortCB->blockSignals(true);
466 sortCB->setChecked(is_sortable
467 && gui_view_.tocModels().isSorted(current_type_));
468 sortCB->blockSignals(false);
470 persistentCB->setEnabled(canNavigate());
472 bool controls_enabled = toc_model && toc_model->rowCount() > 0
473 && !gui_view_.documentBufferView()->buffer().isReadonly();
474 enableControls(controls_enabled);
476 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
477 depthSL->setValue(depth_);
478 tocTV->setEnabled(true);
479 tocTV->setUpdatesEnabled(true);
483 // Expensive operations are on a timer. We finish the update immediately
484 // for sparse edition actions, i.e. there was no edition/cursor movement
485 // recently, then every 300ms.
486 if (!timer_->isActive()) {
493 void TocWidget::updateViewNow()
500 void TocWidget::finishUpdateView()
502 // Profiling shows that this is the expensive stuff in the context of typing
503 // text and moving with arrows. For bigger operations, this is negligible,
504 // and outweighted by TocModels::reset() anyway.
507 setTreeDepth(depth_);
508 persistentCB->setChecked(persistent_);
509 // select the item at current cursor location
510 if (gui_view_.documentBufferView()) {
511 DocIterator const & dit = gui_view_.documentBufferView()->cursor();
512 select(gui_view_.tocModels().currentIndex(current_type_, dit));
519 void TocWidget::filterContents()
524 QModelIndexList indices = tocTV->model()->match(
525 tocTV->model()->index(0, 0),
526 Qt::DisplayRole, "*", -1,
527 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
529 bool const show_active =
530 activeFilterCO->currentIndex() != 2;
531 bool const show_inactive =
532 activeFilterCO->currentIndex() != 1;
534 int size = indices.size();
535 for (int i = 0; i < size; i++) {
536 QModelIndex index = indices[i];
538 index.data().toString().contains(
539 filter_->text(), Qt::CaseInsensitive);
540 TocItem const & item =
541 gui_view_.tocModels().currentItem(current_type_, index);
542 matches &= (show_active && item.isOutput()) || (show_inactive && !item.isOutput());
543 tocTV->setRowHidden(index.row(), index.parent(), !matches);
545 // recursively unhide parents of unhidden children
546 for (int i = size - 1; i >= 0; i--) {
547 QModelIndex index = indices[i];
548 if (!tocTV->isRowHidden(index.row(), index.parent())
549 && index.parent() != QModelIndex())
550 tocTV->setRowHidden(index.parent().row(),
551 index.parent().parent(), false);
556 static QString decodeType(QString const & str)
559 if (type.contains("tableofcontents"))
560 type = "tableofcontents";
561 else if (type.contains("lstlistoflistings"))
563 else if (type.contains("floatlist")) {
564 if (type.contains("\"figure"))
566 else if (type.contains("\"table"))
568 else if (type.contains("\"algorithm"))
575 void TocWidget::init(QString const & str)
579 new_index = typeCO->findData(current_type_);
581 new_index = typeCO->findData(decodeType(str));
583 // If everything else fails, settle on the table of contents which is
584 // guaranteed to exist.
585 if (new_index == -1) {
586 current_type_ = "tableofcontents";
587 new_index = typeCO->findData(current_type_);
589 current_type_ = typeCO->itemData(new_index).toString();
592 typeCO->blockSignals(true);
593 typeCO->setCurrentIndex(new_index);
594 typeCO->blockSignals(false);
598 } // namespace frontend
601 #include "moc_TocWidget.cpp"