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 "BufferView.h"
23 #include "CutAndPaste.h"
24 #include "FuncRequest.h"
25 #include "FuncStatus.h"
28 #include "TocBackend.h"
30 #include "insets/InsetCommand.h"
31 #include "insets/InsetRef.h"
33 #include "support/debug.h"
34 #include "support/lassert.h"
36 #include <QHeaderView>
46 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
47 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view),
48 update_timer_short_(new QTimer(this)),
49 update_timer_long_(new QTimer(this))
53 moveOutTB->setIcon(QIcon(getPixmap("images/", "outline-out", "svgz,png")));
54 moveInTB->setIcon(QIcon(getPixmap("images/", "outline-in", "svgz,png")));
55 moveUpTB->setIcon(QIcon(getPixmap("images/", "outline-up", "svgz,png")));
56 moveDownTB->setIcon(QIcon(getPixmap("images/", "outline-down", "svgz,png")));
57 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "svgz,png")));
60 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
64 // hide the pointless QHeader for now
65 // in the future, new columns may appear
66 // like labels, bookmarks, etc...
67 // tocTV->header()->hide();
68 tocTV->header()->setVisible(false);
70 // Only one item selected at a time.
71 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
74 // The toc types combo won't change its model.
75 typeCO->setModel(gui_view_.tocModels().nameModel());
77 // Make sure the buttons are disabled when first shown without a loaded
79 enableControls(false);
81 // make us responsible for the context menu of the tabbar
82 setContextMenuPolicy(Qt::CustomContextMenu);
83 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
84 this, SLOT(showContextMenu(const QPoint &)));
85 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
86 this, SLOT(showContextMenu(const QPoint &)));
87 connect(filterLE, SIGNAL(textEdited(QString)),
88 this, SLOT(filterContents()));
90 // setting the update timer
91 update_timer_short_->setSingleShot(true);
92 update_timer_long_->setSingleShot(true);
93 update_timer_short_->setInterval(0);
94 update_timer_long_->setInterval(2000);
95 connect(update_timer_short_, SIGNAL(timeout()),
96 this, SLOT(realUpdateView()));
97 connect(update_timer_long_, SIGNAL(timeout()),
98 this, SLOT(realUpdateView()));
100 // fix #9826: Outline disclosure of subsection content disappears one second
101 // after doubleclicking content item.
102 // This is only meant as a workaround. See #6675 for more general issues
103 // regarding unwanted collapse of the tree view.
104 connect(tocTV, SIGNAL(expanded(const QModelIndex &)),
105 update_timer_long_, SLOT(stop()));
111 void TocWidget::showContextMenu(const QPoint & pos)
113 std::string name = "context-toc-" + fromqstr(current_type_);
114 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
117 menu->exec(mapToGlobal(pos));
121 Inset * TocWidget::itemInset() const
123 QModelIndex const & index = tocTV->currentIndex();
124 TocItem const & item =
125 gui_view_.tocModels().currentItem(current_type_, index);
126 DocIterator const & dit = item.dit();
129 if (current_type_ == "label"
130 || current_type_ == "graphics"
131 || current_type_ == "citation"
132 || current_type_ == "child")
133 inset = dit.nextInset();
135 else if (current_type_ == "branch"
136 || current_type_ == "index"
137 || current_type_ == "change"
138 || current_type_ == "table"
139 || current_type_ == "listing"
140 || current_type_ == "figure")
141 inset = &dit.inset();
147 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
148 FuncStatus & status) const
150 Inset * inset = itemInset();
151 FuncRequest tmpcmd(cmd);
153 QModelIndex const & index = tocTV->currentIndex();
154 TocItem const & item =
155 gui_view_.tocModels().currentItem(current_type_, index);
157 switch (cmd.action())
159 case LFUN_CHANGE_ACCEPT:
160 case LFUN_CHANGE_REJECT:
161 case LFUN_OUTLINE_UP:
162 case LFUN_OUTLINE_DOWN:
163 case LFUN_OUTLINE_IN:
164 case LFUN_OUTLINE_OUT:
165 case LFUN_SECTION_SELECT:
166 status.setEnabled(item.dit() != 0);
169 case LFUN_LABEL_COPY_AS_REFERENCE: {
170 // For labels in math, we need to supply the label as a string
171 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
173 return inset->getStatus(cur, label_copy, status);
179 return inset->getStatus(cur, tmpcmd, status);
186 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
188 Inset * inset = itemInset();
189 FuncRequest tmpcmd(cmd);
191 QModelIndex const & index = tocTV->currentIndex();
192 TocItem const & item =
193 gui_view_.tocModels().currentItem(current_type_, index);
195 // Start an undo group.
196 cur.beginUndoGroup();
198 switch (cmd.action())
200 case LFUN_CHANGE_ACCEPT:
201 case LFUN_CHANGE_REJECT:
202 dispatch(item.action());
203 cur.dispatch(tmpcmd);
206 case LFUN_SECTION_SELECT:
207 dispatch(item.action());
208 cur.dispatch(tmpcmd);
209 // necessary to get the selection drawn.
210 cur.buffer()->changed(true);
211 gui_view_.setFocus();
214 case LFUN_LABEL_COPY_AS_REFERENCE: {
215 // For labels in math, we need to supply the label as a string
216 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
218 inset->dispatch(cur, label_copy);
222 case LFUN_OUTLINE_UP:
223 case LFUN_OUTLINE_DOWN:
224 case LFUN_OUTLINE_IN:
225 case LFUN_OUTLINE_OUT:
226 outline(cmd.action());
231 inset->dispatch(cur, tmpcmd);
237 void TocWidget::on_tocTV_activated(QModelIndex const & index)
243 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
245 Qt::MouseButtons const button = QApplication::mouseButtons();
246 if (button & Qt::LeftButton) {
248 gui_view_.setFocus();
253 void TocWidget::goTo(QModelIndex const & index)
255 LYXERR(Debug::GUI, "goto " << index.row()
256 << ", " << index.column());
258 gui_view_.tocModels().goTo(current_type_, index);
262 void TocWidget::on_updateTB_clicked()
264 // The backend update can take some time so we disable
265 // the controls while waiting.
266 enableControls(false);
267 gui_view_.currentBufferView()->buffer().updateBuffer();
271 void TocWidget::on_sortCB_stateChanged(int state)
273 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
278 void TocWidget::on_persistentCB_stateChanged(int state)
280 persistent_ = state == Qt::Checked;
285 /* FIXME (Ugras 17/11/06):
286 I have implemented a indexDepth function to get the model indices. In my
287 opinion, somebody should derive a new qvariant class for tocModelItem
288 which saves the string data and depth information. That will save the
289 depth calculation. */
291 static int indexDepth(QModelIndex const & index, int depth = -1)
294 return index.parent() == QModelIndex()
295 ? depth : indexDepth(index.parent(), depth);
299 void TocWidget::on_depthSL_valueChanged(int depth)
304 gui_view_.setFocus();
308 void TocWidget::setTreeDepth(int depth)
315 tocTV->collapseAll();
317 tocTV->expandToDepth(depth - 1);
321 void TocWidget::on_typeCO_currentIndexChanged(int index)
325 current_type_ = typeCO->itemData(index).toString();
327 if (typeCO->hasFocus())
328 gui_view_.setFocus();
332 void TocWidget::outline(FuncCode func_code)
334 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
337 enableControls(false);
339 dispatch(FuncRequest(func_code));
340 enableControls(true);
341 gui_view_.setFocus();
345 void TocWidget::on_moveUpTB_clicked()
347 outline(LFUN_OUTLINE_UP);
351 void TocWidget::on_moveDownTB_clicked()
353 outline(LFUN_OUTLINE_DOWN);
357 void TocWidget::on_moveInTB_clicked()
359 outline(LFUN_OUTLINE_IN);
363 void TocWidget::on_moveOutTB_clicked()
365 outline(LFUN_OUTLINE_OUT);
369 void TocWidget::select(QModelIndex const & index)
371 if (!index.isValid()) {
372 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
376 tocTV->scrollTo(index);
377 tocTV->clearSelection();
378 tocTV->setCurrentIndex(index);
382 void TocWidget::enableControls(bool enable)
384 updateTB->setEnabled(enable);
389 moveUpTB->setEnabled(enable);
390 moveDownTB->setEnabled(enable);
391 moveInTB->setEnabled(enable);
392 moveOutTB->setEnabled(enable);
396 void TocWidget::updateView()
398 // Subtler optimization for having the delay more UI invisible.
399 // We trigger update immediately for sparse editation actions,
400 // i.e. there was no editation/cursor movement in last 2 sec.
401 // At worst there will be +1 redraw after 2s in a such "calm" mode.
402 if (!update_timer_long_->isActive())
403 update_timer_short_->start();
404 // resets the timer to trigger after 2s
405 update_timer_long_->start();
409 void TocWidget::updateViewNow()
411 update_timer_long_->stop();
412 update_timer_short_->start();
416 void TocWidget::realUpdateView()
418 if (!gui_view_.documentBufferView()) {
420 depthSL->setMaximum(0);
421 depthSL->setValue(0);
426 bool const is_sortable = isSortable();
427 sortCB->setEnabled(is_sortable);
428 bool focus_ = tocTV->hasFocus();
429 tocTV->setEnabled(false);
430 tocTV->setUpdatesEnabled(false);
432 QAbstractItemModel * toc_model =
433 gui_view_.tocModels().model(current_type_);
434 if (tocTV->model() != toc_model) {
435 tocTV->setModel(toc_model);
436 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
438 setTreeDepth(depth_);
441 sortCB->blockSignals(true);
442 sortCB->setChecked(is_sortable
443 && gui_view_.tocModels().isSorted(current_type_));
444 sortCB->blockSignals(false);
446 bool const can_navigate_ = canNavigate();
447 persistentCB->setEnabled(can_navigate_);
449 bool controls_enabled = toc_model && toc_model->rowCount() > 0
450 && !gui_view_.documentBufferView()->buffer().isReadonly();
451 enableControls(controls_enabled);
453 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
454 depthSL->setValue(depth_);
455 if (!persistent_ && can_navigate_)
456 setTreeDepth(depth_);
458 persistentCB->setChecked(persistent_);
459 select(gui_view_.tocModels().currentIndex(current_type_));
462 tocTV->setEnabled(true);
463 tocTV->setUpdatesEnabled(true);
469 void TocWidget::checkModelChanged()
471 if (!gui_view_.documentBufferView() ||
472 gui_view_.tocModels().model(current_type_) != tocTV->model())
477 void TocWidget::filterContents()
482 QModelIndexList indices = tocTV->model()->match(
483 tocTV->model()->index(0, 0),
484 Qt::DisplayRole, "*", -1,
485 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
487 int size = indices.size();
488 for (int i = 0; i < size; i++) {
489 QModelIndex index = indices[i];
491 index.data().toString().contains(
492 filterLE->text(), Qt::CaseInsensitive);
493 tocTV->setRowHidden(index.row(), index.parent(), !matches);
495 // recursively unhide parents of unhidden children
496 for (int i = size - 1; i >= 0; i--) {
497 QModelIndex index = indices[i];
498 if (!tocTV->isRowHidden(index.row(), index.parent())
499 && index.parent() != QModelIndex())
500 tocTV->setRowHidden(index.parent().row(),
501 index.parent().parent(), false);
506 static QString decodeType(QString const & str)
509 if (type.contains("tableofcontents"))
510 type = "tableofcontents";
511 else if (type.contains("lstlistoflistings"))
513 else if (type.contains("floatlist")) {
514 if (type.contains("\"figure"))
516 else if (type.contains("\"table"))
518 else if (type.contains("\"algorithm"))
525 void TocWidget::init(QString const & str)
529 new_index = typeCO->findData(current_type_);
531 new_index = typeCO->findData(decodeType(str));
533 // If everything else fails, settle on the table of contents which is
534 // guaranteed to exist.
535 if (new_index == -1) {
536 current_type_ = "tableofcontents";
537 new_index = typeCO->findData(current_type_);
539 current_type_ = typeCO->itemData(new_index).toString();
542 typeCO->blockSignals(true);
543 typeCO->setCurrentIndex(new_index);
544 typeCO->blockSignals(false);
547 } // namespace frontend
550 #include "moc_TocWidget.cpp"