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/", "promote", "png")));
56 moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
57 moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
58 moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
59 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "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(true);
158 case LFUN_LABEL_COPY_AS_REF: {
159 // For labels in math, we need to supply the label as a string
160 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, 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 case LFUN_SECTION_SELECT:
191 dispatch(item.action());
192 cur.dispatch(tmpcmd);
195 case LFUN_LABEL_COPY_AS_REF: {
196 // For labels in math, we need to supply the label as a string
197 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
199 inset->dispatch(cur, label_copy);
203 case LFUN_OUTLINE_UP:
204 case LFUN_OUTLINE_DOWN:
205 case LFUN_OUTLINE_IN:
206 case LFUN_OUTLINE_OUT:
207 outline(cmd.action());
212 inset->dispatch(cur, tmpcmd);
218 void TocWidget::on_tocTV_activated(QModelIndex const & index)
224 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
226 Qt::MouseButtons const button = QApplication::mouseButtons();
227 if (button & Qt::LeftButton) {
229 gui_view_.setFocus();
234 void TocWidget::goTo(QModelIndex const & index)
236 LYXERR(Debug::GUI, "goto " << index.row()
237 << ", " << index.column());
239 gui_view_.tocModels().goTo(current_type_, index);
243 void TocWidget::on_updateTB_clicked()
245 // The backend update can take some time so we disable
246 // the controls while waiting.
247 enableControls(false);
248 gui_view_.currentBufferView()->buffer().updateBuffer();
252 void TocWidget::on_sortCB_stateChanged(int state)
254 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
259 void TocWidget::on_persistentCB_stateChanged(int state)
261 persistent_ = state == Qt::Checked;
265 /* FIXME (Ugras 17/11/06):
266 I have implemented a indexDepth function to get the model indices. In my
267 opinion, somebody should derive a new qvariant class for tocModelItem
268 which saves the string data and depth information. That will save the
269 depth calculation. */
271 static int indexDepth(QModelIndex const & index, int depth = -1)
274 return index.parent() == QModelIndex()
275 ? depth : indexDepth(index.parent(), depth);
279 void TocWidget::on_depthSL_valueChanged(int depth)
284 gui_view_.setFocus();
288 void TocWidget::setTreeDepth(int depth)
295 tocTV->collapseAll();
297 tocTV->expandToDepth(depth - 1);
301 void TocWidget::on_typeCO_currentIndexChanged(int index)
305 current_type_ = typeCO->itemData(index).toString();
307 if (typeCO->hasFocus())
308 gui_view_.setFocus();
312 void TocWidget::outline(FuncCode func_code)
314 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
317 enableControls(false);
319 dispatch(FuncRequest(func_code));
320 enableControls(true);
321 gui_view_.setFocus();
325 void TocWidget::on_moveUpTB_clicked()
327 outline(LFUN_OUTLINE_UP);
331 void TocWidget::on_moveDownTB_clicked()
333 outline(LFUN_OUTLINE_DOWN);
337 void TocWidget::on_moveInTB_clicked()
339 outline(LFUN_OUTLINE_IN);
343 void TocWidget::on_moveOutTB_clicked()
345 outline(LFUN_OUTLINE_OUT);
349 void TocWidget::select(QModelIndex const & index)
351 if (!index.isValid()) {
352 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
356 tocTV->scrollTo(index);
357 tocTV->clearSelection();
358 tocTV->setCurrentIndex(index);
362 void TocWidget::enableControls(bool enable)
364 updateTB->setEnabled(enable);
369 moveUpTB->setEnabled(enable);
370 moveDownTB->setEnabled(enable);
371 moveInTB->setEnabled(enable);
372 moveOutTB->setEnabled(enable);
376 void TocWidget::updateView()
378 // Enable if you dont want the delaying business, cf #7138.
379 #ifndef DELAY_UPDATE_VIEW
383 // already scheduled?
384 if (update_delay_ == -1)
386 QTimer::singleShot(update_delay_, this, SLOT(updateViewForce()));
387 // Subtler optimization for having the delay more UI invisible.
388 // We trigger update immediately for sparse editation actions,
389 // i.e. there was no editation/cursor movement in last 2 sec.
390 // At worst there will be +1 redraw after 2s in a such "calm" mode.
391 if (update_delay_ != 0)
396 void TocWidget::updateViewForce()
398 update_delay_ = 2000;
399 if (!gui_view_.documentBufferView()) {
401 depthSL->setMaximum(0);
402 depthSL->setValue(0);
407 bool const is_sortable = isSortable();
408 sortCB->setEnabled(is_sortable);
409 bool focus_ = tocTV->hasFocus();
410 tocTV->setEnabled(false);
411 tocTV->setUpdatesEnabled(false);
413 QAbstractItemModel * toc_model =
414 gui_view_.tocModels().model(current_type_);
415 if (tocTV->model() != toc_model) {
416 tocTV->setModel(toc_model);
417 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
419 setTreeDepth(depth_);
422 sortCB->blockSignals(true);
423 sortCB->setChecked(is_sortable
424 && gui_view_.tocModels().isSorted(current_type_));
425 sortCB->blockSignals(false);
427 bool const can_navigate_ = canNavigate();
428 persistentCB->setEnabled(can_navigate_);
430 bool controls_enabled = toc_model && toc_model->rowCount() > 0
431 && !gui_view_.documentBufferView()->buffer().isReadonly();
432 enableControls(controls_enabled);
434 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
435 depthSL->setValue(depth_);
436 if (!persistent_ && can_navigate_)
437 setTreeDepth(depth_);
439 persistentCB->setChecked(persistent_);
440 select(gui_view_.tocModels().currentIndex(current_type_));
443 tocTV->setEnabled(true);
444 tocTV->setUpdatesEnabled(true);
450 void TocWidget::filterContents()
455 QModelIndexList indices = tocTV->model()->match(
456 tocTV->model()->index(0, 0),
457 Qt::DisplayRole, "*", -1,
458 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
460 int size = indices.size();
461 for (int i = 0; i < size; i++) {
462 QModelIndex index = indices[i];
464 index.data().toString().contains(
465 filterLE->text(), Qt::CaseInsensitive);
466 tocTV->setRowHidden(index.row(), index.parent(), !matches);
468 // recursively unhide parents of unhidden children
469 for (int i = size - 1; i >= 0; i--) {
470 QModelIndex index = indices[i];
471 if (!tocTV->isRowHidden(index.row(), index.parent())
472 && index.parent() != QModelIndex())
473 tocTV->setRowHidden(index.parent().row(),
474 index.parent().parent(), false);
479 static QString decodeType(QString const & str)
482 if (type.contains("tableofcontents"))
483 type = "tableofcontents";
484 else if (type.contains("lstlistoflistings"))
486 else if (type.contains("floatlist")) {
487 if (type.contains("\"figure"))
489 else if (type.contains("\"table"))
491 else if (type.contains("\"algorithm"))
498 void TocWidget::init(QString const & str)
502 new_index = typeCO->findData(current_type_);
504 new_index = typeCO->findData(decodeType(str));
506 // If everything else fails, settle on the table of contents which is
507 // guaranteed to exist.
508 if (new_index == -1) {
509 current_type_ = "tableofcontents";
510 new_index = typeCO->findData(current_type_);
512 current_type_ = typeCO->itemData(new_index).toString();
515 typeCO->blockSignals(true);
516 typeCO->setCurrentIndex(new_index);
517 typeCO->blockSignals(false);
519 // no delay when the whole outliner is reseted.
523 } // namespace frontend
526 #include "moc_TocWidget.cpp"