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 inset = &dit.inset();
125 else if (current_type_ == "table"
126 || current_type_ == "listing"
127 || current_type_ == "figure") {
128 DocIterator tmp_dit(dit);
130 inset = &tmp_dit.inset();
136 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
137 FuncStatus & status) const
139 Inset * inset = itemInset();
140 FuncRequest tmpcmd(cmd);
142 QModelIndex const & index = tocTV->currentIndex();
143 TocItem const & item =
144 gui_view_.tocModels().currentItem(current_type_, index);
146 switch (cmd.action())
148 case LFUN_CHANGE_ACCEPT:
149 case LFUN_CHANGE_REJECT:
150 case LFUN_OUTLINE_UP:
151 case LFUN_OUTLINE_DOWN:
152 case LFUN_OUTLINE_IN:
153 case LFUN_OUTLINE_OUT:
154 case LFUN_SECTION_SELECT:
155 status.setEnabled(item.dit() != 0);
158 case LFUN_LABEL_COPY_AS_REFERENCE: {
159 // For labels in math, we need to supply the label as a string
160 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.asString());
162 return inset->getStatus(cur, label_copy, status);
167 return inset->getStatus(cur, tmpcmd, status);
174 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
176 Inset * inset = itemInset();
177 FuncRequest tmpcmd(cmd);
179 QModelIndex const & index = tocTV->currentIndex();
180 TocItem const & item =
181 gui_view_.tocModels().currentItem(current_type_, index);
183 // Start an undo group.
184 cur.beginUndoGroup();
186 switch (cmd.action())
188 case LFUN_CHANGE_ACCEPT:
189 case LFUN_CHANGE_REJECT:
190 dispatch(item.action());
191 cur.dispatch(tmpcmd);
194 case LFUN_SECTION_SELECT:
195 dispatch(item.action());
196 cur.dispatch(tmpcmd);
197 // necessary to get the selection drawn.
198 cur.buffer()->changed(true);
199 gui_view_.setFocus();
202 case LFUN_LABEL_COPY_AS_REFERENCE: {
203 // For labels in math, we need to supply the label as a string
204 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.asString());
206 inset->dispatch(cur, label_copy);
210 case LFUN_OUTLINE_UP:
211 case LFUN_OUTLINE_DOWN:
212 case LFUN_OUTLINE_IN:
213 case LFUN_OUTLINE_OUT:
214 outline(cmd.action());
219 inset->dispatch(cur, tmpcmd);
225 void TocWidget::on_tocTV_activated(QModelIndex const & index)
231 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
233 Qt::MouseButtons const button = QApplication::mouseButtons();
234 if (button & Qt::LeftButton) {
236 gui_view_.setFocus();
241 void TocWidget::goTo(QModelIndex const & index)
243 LYXERR(Debug::GUI, "goto " << index.row()
244 << ", " << index.column());
246 gui_view_.tocModels().goTo(current_type_, index);
250 void TocWidget::on_updateTB_clicked()
252 // The backend update can take some time so we disable
253 // the controls while waiting.
254 enableControls(false);
255 gui_view_.currentBufferView()->buffer().updateBuffer();
259 void TocWidget::on_sortCB_stateChanged(int state)
261 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
266 void TocWidget::on_persistentCB_stateChanged(int state)
268 persistent_ = state == Qt::Checked;
273 /* FIXME (Ugras 17/11/06):
274 I have implemented a indexDepth function to get the model indices. In my
275 opinion, somebody should derive a new qvariant class for tocModelItem
276 which saves the string data and depth information. That will save the
277 depth calculation. */
279 static int indexDepth(QModelIndex const & index, int depth = -1)
282 return index.parent() == QModelIndex()
283 ? depth : indexDepth(index.parent(), depth);
287 void TocWidget::on_depthSL_valueChanged(int depth)
292 gui_view_.setFocus();
296 void TocWidget::setTreeDepth(int depth)
303 tocTV->collapseAll();
305 tocTV->expandToDepth(depth - 1);
309 void TocWidget::on_typeCO_currentIndexChanged(int index)
313 current_type_ = typeCO->itemData(index).toString();
315 if (typeCO->hasFocus())
316 gui_view_.setFocus();
320 void TocWidget::outline(FuncCode func_code)
322 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
325 enableControls(false);
327 dispatch(FuncRequest(func_code));
328 enableControls(true);
329 gui_view_.setFocus();
333 void TocWidget::on_moveUpTB_clicked()
335 outline(LFUN_OUTLINE_UP);
339 void TocWidget::on_moveDownTB_clicked()
341 outline(LFUN_OUTLINE_DOWN);
345 void TocWidget::on_moveInTB_clicked()
347 outline(LFUN_OUTLINE_IN);
351 void TocWidget::on_moveOutTB_clicked()
353 outline(LFUN_OUTLINE_OUT);
357 void TocWidget::select(QModelIndex const & index)
359 if (!index.isValid()) {
360 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
364 tocTV->scrollTo(index);
365 tocTV->clearSelection();
366 tocTV->setCurrentIndex(index);
370 void TocWidget::enableControls(bool enable)
372 updateTB->setEnabled(enable);
377 moveUpTB->setEnabled(enable);
378 moveDownTB->setEnabled(enable);
379 moveInTB->setEnabled(enable);
380 moveOutTB->setEnabled(enable);
384 void TocWidget::updateView()
386 // Enable if you dont want the delaying business, cf #7138.
387 #ifndef DELAY_UPDATE_VIEW
391 // already scheduled?
392 if (update_delay_ == -1)
394 QTimer::singleShot(update_delay_, this, SLOT(updateViewForce()));
395 // Subtler optimization for having the delay more UI invisible.
396 // We trigger update immediately for sparse editation actions,
397 // i.e. there was no editation/cursor movement in last 2 sec.
398 // At worst there will be +1 redraw after 2s in a such "calm" mode.
399 if (update_delay_ != 0)
404 void TocWidget::updateViewForce()
406 update_delay_ = 2000;
407 if (!gui_view_.documentBufferView()) {
409 depthSL->setMaximum(0);
410 depthSL->setValue(0);
415 bool const is_sortable = isSortable();
416 sortCB->setEnabled(is_sortable);
417 bool focus_ = tocTV->hasFocus();
418 tocTV->setEnabled(false);
419 tocTV->setUpdatesEnabled(false);
421 QAbstractItemModel * toc_model =
422 gui_view_.tocModels().model(current_type_);
423 if (tocTV->model() != toc_model) {
424 tocTV->setModel(toc_model);
425 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
427 setTreeDepth(depth_);
430 sortCB->blockSignals(true);
431 sortCB->setChecked(is_sortable
432 && gui_view_.tocModels().isSorted(current_type_));
433 sortCB->blockSignals(false);
435 bool const can_navigate_ = canNavigate();
436 persistentCB->setEnabled(can_navigate_);
438 bool controls_enabled = toc_model && toc_model->rowCount() > 0
439 && !gui_view_.documentBufferView()->buffer().isReadonly();
440 enableControls(controls_enabled);
442 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
443 depthSL->setValue(depth_);
444 if (!persistent_ && can_navigate_)
445 setTreeDepth(depth_);
447 persistentCB->setChecked(persistent_);
448 select(gui_view_.tocModels().currentIndex(current_type_));
451 tocTV->setEnabled(true);
452 tocTV->setUpdatesEnabled(true);
458 void TocWidget::filterContents()
463 QModelIndexList indices = tocTV->model()->match(
464 tocTV->model()->index(0, 0),
465 Qt::DisplayRole, "*", -1,
466 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
468 int size = indices.size();
469 for (int i = 0; i < size; i++) {
470 QModelIndex index = indices[i];
472 index.data().toString().contains(
473 filterLE->text(), Qt::CaseInsensitive);
474 tocTV->setRowHidden(index.row(), index.parent(), !matches);
476 // recursively unhide parents of unhidden children
477 for (int i = size - 1; i >= 0; i--) {
478 QModelIndex index = indices[i];
479 if (!tocTV->isRowHidden(index.row(), index.parent())
480 && index.parent() != QModelIndex())
481 tocTV->setRowHidden(index.parent().row(),
482 index.parent().parent(), false);
487 static QString decodeType(QString const & str)
490 if (type.contains("tableofcontents"))
491 type = "tableofcontents";
492 else if (type.contains("lstlistoflistings"))
494 else if (type.contains("floatlist")) {
495 if (type.contains("\"figure"))
497 else if (type.contains("\"table"))
499 else if (type.contains("\"algorithm"))
506 void TocWidget::init(QString const & str)
510 new_index = typeCO->findData(current_type_);
512 new_index = typeCO->findData(decodeType(str));
514 // If everything else fails, settle on the table of contents which is
515 // guaranteed to exist.
516 if (new_index == -1) {
517 current_type_ = "tableofcontents";
518 new_index = typeCO->findData(current_type_);
520 current_type_ = typeCO->itemData(new_index).toString();
523 typeCO->blockSignals(true);
524 typeCO->setCurrentIndex(new_index);
525 typeCO->blockSignals(false);
527 // no delay when the whole outliner is reseted.
531 } // namespace frontend
534 #include "moc_TocWidget.cpp"