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();
249 gui_view_.activateWindow();
254 void TocWidget::goTo(QModelIndex const & index)
256 LYXERR(Debug::GUI, "goto " << index.row()
257 << ", " << index.column());
259 gui_view_.tocModels().goTo(current_type_, index);
263 void TocWidget::on_updateTB_clicked()
265 // The backend update can take some time so we disable
266 // the controls while waiting.
267 enableControls(false);
268 gui_view_.currentBufferView()->buffer().updateBuffer();
272 void TocWidget::on_sortCB_stateChanged(int state)
274 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
279 void TocWidget::on_persistentCB_stateChanged(int state)
281 persistent_ = state == Qt::Checked;
286 /* FIXME (Ugras 17/11/06):
287 I have implemented a indexDepth function to get the model indices. In my
288 opinion, somebody should derive a new qvariant class for tocModelItem
289 which saves the string data and depth information. That will save the
290 depth calculation. */
292 static int indexDepth(QModelIndex const & index, int depth = -1)
295 return index.parent() == QModelIndex()
296 ? depth : indexDepth(index.parent(), depth);
300 void TocWidget::on_depthSL_valueChanged(int depth)
305 gui_view_.setFocus();
309 void TocWidget::setTreeDepth(int depth)
316 tocTV->collapseAll();
318 tocTV->expandToDepth(depth - 1);
322 void TocWidget::on_typeCO_currentIndexChanged(int index)
326 current_type_ = typeCO->itemData(index).toString();
328 if (typeCO->hasFocus())
329 gui_view_.setFocus();
333 void TocWidget::outline(FuncCode func_code)
335 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
338 enableControls(false);
340 dispatch(FuncRequest(func_code));
341 enableControls(true);
342 gui_view_.setFocus();
346 void TocWidget::on_moveUpTB_clicked()
348 outline(LFUN_OUTLINE_UP);
352 void TocWidget::on_moveDownTB_clicked()
354 outline(LFUN_OUTLINE_DOWN);
358 void TocWidget::on_moveInTB_clicked()
360 outline(LFUN_OUTLINE_IN);
364 void TocWidget::on_moveOutTB_clicked()
366 outline(LFUN_OUTLINE_OUT);
370 void TocWidget::select(QModelIndex const & index)
372 if (!index.isValid()) {
373 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
377 tocTV->scrollTo(index);
378 tocTV->clearSelection();
379 tocTV->setCurrentIndex(index);
383 void TocWidget::enableControls(bool enable)
385 updateTB->setEnabled(enable);
390 moveUpTB->setEnabled(enable);
391 moveDownTB->setEnabled(enable);
392 moveInTB->setEnabled(enable);
393 moveOutTB->setEnabled(enable);
397 void TocWidget::updateView()
399 // Subtler optimization for having the delay more UI invisible.
400 // We trigger update immediately for sparse editation actions,
401 // i.e. there was no editation/cursor movement in last 2 sec.
402 // At worst there will be +1 redraw after 2s in a such "calm" mode.
403 if (!update_timer_long_->isActive())
404 update_timer_short_->start();
405 // resets the timer to trigger after 2s
406 update_timer_long_->start();
410 void TocWidget::updateViewNow()
412 update_timer_long_->stop();
413 update_timer_short_->start();
417 void TocWidget::realUpdateView()
419 if (!gui_view_.documentBufferView()) {
421 depthSL->setMaximum(0);
422 depthSL->setValue(0);
427 bool const is_sortable = isSortable();
428 sortCB->setEnabled(is_sortable);
429 bool focus_ = tocTV->hasFocus();
430 tocTV->setEnabled(false);
431 tocTV->setUpdatesEnabled(false);
433 QAbstractItemModel * toc_model =
434 gui_view_.tocModels().model(current_type_);
435 if (tocTV->model() != toc_model) {
436 tocTV->setModel(toc_model);
437 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
439 setTreeDepth(depth_);
442 sortCB->blockSignals(true);
443 sortCB->setChecked(is_sortable
444 && gui_view_.tocModels().isSorted(current_type_));
445 sortCB->blockSignals(false);
447 bool const can_navigate_ = canNavigate();
448 persistentCB->setEnabled(can_navigate_);
450 bool controls_enabled = toc_model && toc_model->rowCount() > 0
451 && !gui_view_.documentBufferView()->buffer().isReadonly();
452 enableControls(controls_enabled);
454 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
455 depthSL->setValue(depth_);
456 if (!persistent_ && can_navigate_)
457 setTreeDepth(depth_);
459 persistentCB->setChecked(persistent_);
460 select(gui_view_.tocModels().currentIndex(current_type_));
463 tocTV->setEnabled(true);
464 tocTV->setUpdatesEnabled(true);
470 void TocWidget::checkModelChanged()
472 if (!gui_view_.documentBufferView() ||
473 gui_view_.tocModels().model(current_type_) != tocTV->model())
478 void TocWidget::filterContents()
483 QModelIndexList indices = tocTV->model()->match(
484 tocTV->model()->index(0, 0),
485 Qt::DisplayRole, "*", -1,
486 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
488 int size = indices.size();
489 for (int i = 0; i < size; i++) {
490 QModelIndex index = indices[i];
492 index.data().toString().contains(
493 filterLE->text(), Qt::CaseInsensitive);
494 tocTV->setRowHidden(index.row(), index.parent(), !matches);
496 // recursively unhide parents of unhidden children
497 for (int i = size - 1; i >= 0; i--) {
498 QModelIndex index = indices[i];
499 if (!tocTV->isRowHidden(index.row(), index.parent())
500 && index.parent() != QModelIndex())
501 tocTV->setRowHidden(index.parent().row(),
502 index.parent().parent(), false);
507 static QString decodeType(QString const & str)
510 if (type.contains("tableofcontents"))
511 type = "tableofcontents";
512 else if (type.contains("lstlistoflistings"))
514 else if (type.contains("floatlist")) {
515 if (type.contains("\"figure"))
517 else if (type.contains("\"table"))
519 else if (type.contains("\"algorithm"))
526 void TocWidget::init(QString const & str)
530 new_index = typeCO->findData(current_type_);
532 new_index = typeCO->findData(decodeType(str));
534 // If everything else fails, settle on the table of contents which is
535 // guaranteed to exist.
536 if (new_index == -1) {
537 current_type_ = "tableofcontents";
538 new_index = typeCO->findData(current_type_);
540 current_type_ = typeCO->itemData(new_index).toString();
543 typeCO->blockSignals(true);
544 typeCO->setCurrentIndex(new_index);
545 typeCO->blockSignals(false);
548 } // namespace frontend
551 #include "moc_TocWidget.cpp"