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>
42 #define DELAY_UPDATE_VIEW
49 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
50 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view), update_delay_(0)
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")));
62 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
66 // hide the pointless QHeader for now
67 // in the future, new columns may appear
68 // like labels, bookmarks, etc...
69 // tocTV->header()->hide();
70 tocTV->header()->setVisible(false);
72 // Only one item selected at a time.
73 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
76 // The toc types combo won't change its model.
77 typeCO->setModel(gui_view_.tocModels().nameModel());
79 // Make sure the buttons are disabled when first shown without a loaded
81 enableControls(false);
83 // make us responsible for the context menu of the tabbar
84 setContextMenuPolicy(Qt::CustomContextMenu);
85 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
86 this, SLOT(showContextMenu(const QPoint &)));
87 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
88 this, SLOT(showContextMenu(const QPoint &)));
89 connect(filterLE, SIGNAL(textEdited(QString)),
90 this, SLOT(filterContents()));
96 void TocWidget::showContextMenu(const QPoint & pos)
98 std::string name = "context-toc-" + fromqstr(current_type_);
99 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
102 menu->exec(mapToGlobal(pos));
106 Inset * TocWidget::itemInset() const
108 QModelIndex const & index = tocTV->currentIndex();
109 TocItem const & item =
110 gui_view_.tocModels().currentItem(current_type_, index);
111 DocIterator const & dit = item.dit();
114 if (current_type_ == "label"
115 || current_type_ == "graphics"
116 || current_type_ == "citation"
117 || current_type_ == "child")
118 inset = dit.nextInset();
120 else if (current_type_ == "branch"
121 || current_type_ == "index"
122 || current_type_ == "change"
123 || current_type_ == "table"
124 || current_type_ == "listing"
125 || current_type_ == "figure")
126 inset = &dit.inset();
132 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
133 FuncStatus & status) const
135 Inset * inset = itemInset();
136 FuncRequest tmpcmd(cmd);
138 QModelIndex const & index = tocTV->currentIndex();
139 TocItem const & item =
140 gui_view_.tocModels().currentItem(current_type_, index);
142 switch (cmd.action())
144 case LFUN_CHANGE_ACCEPT:
145 case LFUN_CHANGE_REJECT:
146 case LFUN_OUTLINE_UP:
147 case LFUN_OUTLINE_DOWN:
148 case LFUN_OUTLINE_IN:
149 case LFUN_OUTLINE_OUT:
150 case LFUN_SECTION_SELECT:
151 status.setEnabled(item.dit() != 0);
154 case LFUN_LABEL_COPY_AS_REFERENCE: {
155 // For labels in math, we need to supply the label as a string
156 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
158 return inset->getStatus(cur, label_copy, status);
164 return inset->getStatus(cur, tmpcmd, status);
171 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
173 Inset * inset = itemInset();
174 FuncRequest tmpcmd(cmd);
176 QModelIndex const & index = tocTV->currentIndex();
177 TocItem const & item =
178 gui_view_.tocModels().currentItem(current_type_, index);
180 // Start an undo group.
181 cur.beginUndoGroup();
183 switch (cmd.action())
185 case LFUN_CHANGE_ACCEPT:
186 case LFUN_CHANGE_REJECT:
187 dispatch(item.action());
188 cur.dispatch(tmpcmd);
191 case LFUN_SECTION_SELECT:
192 dispatch(item.action());
193 cur.dispatch(tmpcmd);
194 // necessary to get the selection drawn.
195 cur.buffer()->changed(true);
196 gui_view_.setFocus();
199 case LFUN_LABEL_COPY_AS_REFERENCE: {
200 // For labels in math, we need to supply the label as a string
201 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
203 inset->dispatch(cur, label_copy);
207 case LFUN_OUTLINE_UP:
208 case LFUN_OUTLINE_DOWN:
209 case LFUN_OUTLINE_IN:
210 case LFUN_OUTLINE_OUT:
211 outline(cmd.action());
216 inset->dispatch(cur, tmpcmd);
222 void TocWidget::on_tocTV_activated(QModelIndex const & index)
228 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
230 Qt::MouseButtons const button = QApplication::mouseButtons();
231 if (button & Qt::LeftButton) {
233 gui_view_.setFocus();
238 void TocWidget::goTo(QModelIndex const & index)
240 LYXERR(Debug::GUI, "goto " << index.row()
241 << ", " << index.column());
243 gui_view_.tocModels().goTo(current_type_, index);
247 void TocWidget::on_updateTB_clicked()
249 // The backend update can take some time so we disable
250 // the controls while waiting.
251 enableControls(false);
252 gui_view_.currentBufferView()->buffer().updateBuffer();
256 void TocWidget::on_sortCB_stateChanged(int state)
258 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
263 void TocWidget::on_persistentCB_stateChanged(int state)
265 persistent_ = state == Qt::Checked;
270 /* FIXME (Ugras 17/11/06):
271 I have implemented a indexDepth function to get the model indices. In my
272 opinion, somebody should derive a new qvariant class for tocModelItem
273 which saves the string data and depth information. That will save the
274 depth calculation. */
276 static int indexDepth(QModelIndex const & index, int depth = -1)
279 return index.parent() == QModelIndex()
280 ? depth : indexDepth(index.parent(), depth);
284 void TocWidget::on_depthSL_valueChanged(int depth)
289 gui_view_.setFocus();
293 void TocWidget::setTreeDepth(int depth)
300 tocTV->collapseAll();
302 tocTV->expandToDepth(depth - 1);
306 void TocWidget::on_typeCO_currentIndexChanged(int index)
310 current_type_ = typeCO->itemData(index).toString();
312 if (typeCO->hasFocus())
313 gui_view_.setFocus();
317 void TocWidget::outline(FuncCode func_code)
319 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
322 enableControls(false);
324 dispatch(FuncRequest(func_code));
325 enableControls(true);
326 gui_view_.setFocus();
330 void TocWidget::on_moveUpTB_clicked()
332 outline(LFUN_OUTLINE_UP);
336 void TocWidget::on_moveDownTB_clicked()
338 outline(LFUN_OUTLINE_DOWN);
342 void TocWidget::on_moveInTB_clicked()
344 outline(LFUN_OUTLINE_IN);
348 void TocWidget::on_moveOutTB_clicked()
350 outline(LFUN_OUTLINE_OUT);
354 void TocWidget::select(QModelIndex const & index)
356 if (!index.isValid()) {
357 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
361 tocTV->scrollTo(index);
362 tocTV->clearSelection();
363 tocTV->setCurrentIndex(index);
367 void TocWidget::enableControls(bool enable)
369 updateTB->setEnabled(enable);
374 moveUpTB->setEnabled(enable);
375 moveDownTB->setEnabled(enable);
376 moveInTB->setEnabled(enable);
377 moveOutTB->setEnabled(enable);
381 void TocWidget::updateView()
383 // Enable if you dont want the delaying business, cf #7138.
384 #ifndef DELAY_UPDATE_VIEW
388 // already scheduled?
389 if (update_delay_ == -1)
391 QTimer::singleShot(update_delay_, this, SLOT(updateViewForce()));
392 // Subtler optimization for having the delay more UI invisible.
393 // We trigger update immediately for sparse editation actions,
394 // i.e. there was no editation/cursor movement in last 2 sec.
395 // At worst there will be +1 redraw after 2s in a such "calm" mode.
396 if (update_delay_ != 0)
401 void TocWidget::updateViewForce()
403 update_delay_ = 2000;
404 if (!gui_view_.documentBufferView()) {
406 depthSL->setMaximum(0);
407 depthSL->setValue(0);
412 bool const is_sortable = isSortable();
413 sortCB->setEnabled(is_sortable);
414 bool focus_ = tocTV->hasFocus();
415 tocTV->setEnabled(false);
416 tocTV->setUpdatesEnabled(false);
418 QAbstractItemModel * toc_model =
419 gui_view_.tocModels().model(current_type_);
420 if (tocTV->model() != toc_model) {
421 tocTV->setModel(toc_model);
422 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
424 setTreeDepth(depth_);
427 sortCB->blockSignals(true);
428 sortCB->setChecked(is_sortable
429 && gui_view_.tocModels().isSorted(current_type_));
430 sortCB->blockSignals(false);
432 bool const can_navigate_ = canNavigate();
433 persistentCB->setEnabled(can_navigate_);
435 bool controls_enabled = toc_model && toc_model->rowCount() > 0
436 && !gui_view_.documentBufferView()->buffer().isReadonly();
437 enableControls(controls_enabled);
439 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
440 depthSL->setValue(depth_);
441 if (!persistent_ && can_navigate_)
442 setTreeDepth(depth_);
444 persistentCB->setChecked(persistent_);
445 select(gui_view_.tocModels().currentIndex(current_type_));
448 tocTV->setEnabled(true);
449 tocTV->setUpdatesEnabled(true);
455 void TocWidget::filterContents()
460 QModelIndexList indices = tocTV->model()->match(
461 tocTV->model()->index(0, 0),
462 Qt::DisplayRole, "*", -1,
463 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
465 int size = indices.size();
466 for (int i = 0; i < size; i++) {
467 QModelIndex index = indices[i];
469 index.data().toString().contains(
470 filterLE->text(), Qt::CaseInsensitive);
471 tocTV->setRowHidden(index.row(), index.parent(), !matches);
473 // recursively unhide parents of unhidden children
474 for (int i = size - 1; i >= 0; i--) {
475 QModelIndex index = indices[i];
476 if (!tocTV->isRowHidden(index.row(), index.parent())
477 && index.parent() != QModelIndex())
478 tocTV->setRowHidden(index.parent().row(),
479 index.parent().parent(), false);
484 static QString decodeType(QString const & str)
487 if (type.contains("tableofcontents"))
488 type = "tableofcontents";
489 else if (type.contains("lstlistoflistings"))
491 else if (type.contains("floatlist")) {
492 if (type.contains("\"figure"))
494 else if (type.contains("\"table"))
496 else if (type.contains("\"algorithm"))
503 void TocWidget::init(QString const & str)
507 new_index = typeCO->findData(current_type_);
509 new_index = typeCO->findData(decodeType(str));
511 // If everything else fails, settle on the table of contents which is
512 // guaranteed to exist.
513 if (new_index == -1) {
514 current_type_ = "tableofcontents";
515 new_index = typeCO->findData(current_type_);
517 current_type_ = typeCO->itemData(new_index).toString();
520 typeCO->blockSignals(true);
521 typeCO->setCurrentIndex(new_index);
522 typeCO->blockSignals(false);
524 // no delay when the whole outliner is reseted.
528 } // namespace frontend
531 #include "moc_TocWidget.cpp"