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))
54 moveOutTB->setIcon(QIcon(getPixmap("images/", "outline-out", "svgz,png")));
55 moveInTB->setIcon(QIcon(getPixmap("images/", "outline-in", "svgz,png")));
56 moveUpTB->setIcon(QIcon(getPixmap("images/", "outline-up", "svgz,png")));
57 moveDownTB->setIcon(QIcon(getPixmap("images/", "outline-down", "svgz,png")));
58 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "svgz,png")));
61 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
65 // hide the pointless QHeader for now
66 // in the future, new columns may appear
67 // like labels, bookmarks, etc...
68 // tocTV->header()->hide();
69 tocTV->header()->setVisible(false);
71 // Only one item selected at a time.
72 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
75 // The toc types combo won't change its model.
76 typeCO->setModel(gui_view_.tocModels().nameModel());
78 // Make sure the buttons are disabled when first shown without a loaded
80 enableControls(false);
82 // make us responsible for the context menu of the tabbar
83 setContextMenuPolicy(Qt::CustomContextMenu);
84 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
85 this, SLOT(showContextMenu(const QPoint &)));
86 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
87 this, SLOT(showContextMenu(const QPoint &)));
88 connect(filterLE, SIGNAL(textEdited(QString)),
89 this, SLOT(filterContents()));
91 // setting the update timer
92 update_timer_short_->setSingleShot(true);
93 update_timer_long_->setSingleShot(true);
94 update_timer_short_->setInterval(0);
95 update_timer_long_->setInterval(2000);
96 connect(update_timer_short_, SIGNAL(timeout()),
97 this, SLOT(realUpdateView()));
98 connect(update_timer_long_, SIGNAL(timeout()),
99 this, SLOT(realUpdateView()));
105 void TocWidget::showContextMenu(const QPoint & pos)
107 std::string name = "context-toc-" + fromqstr(current_type_);
108 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
111 menu->exec(mapToGlobal(pos));
115 Inset * TocWidget::itemInset() const
117 QModelIndex const & index = tocTV->currentIndex();
118 TocItem const & item =
119 gui_view_.tocModels().currentItem(current_type_, index);
120 DocIterator const & dit = item.dit();
123 if (current_type_ == "label"
124 || current_type_ == "graphics"
125 || current_type_ == "citation"
126 || current_type_ == "child")
127 inset = dit.nextInset();
129 else if (current_type_ == "branch"
130 || current_type_ == "index"
131 || current_type_ == "change"
132 || current_type_ == "table"
133 || current_type_ == "listing"
134 || current_type_ == "figure")
135 inset = &dit.inset();
141 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
142 FuncStatus & status) const
144 Inset * inset = itemInset();
145 FuncRequest tmpcmd(cmd);
147 QModelIndex const & index = tocTV->currentIndex();
148 TocItem const & item =
149 gui_view_.tocModels().currentItem(current_type_, index);
151 switch (cmd.action())
153 case LFUN_CHANGE_ACCEPT:
154 case LFUN_CHANGE_REJECT:
155 case LFUN_OUTLINE_UP:
156 case LFUN_OUTLINE_DOWN:
157 case LFUN_OUTLINE_IN:
158 case LFUN_OUTLINE_OUT:
159 case LFUN_SECTION_SELECT:
160 status.setEnabled(item.dit() != 0);
163 case LFUN_LABEL_COPY_AS_REFERENCE: {
164 // For labels in math, we need to supply the label as a string
165 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
167 return inset->getStatus(cur, label_copy, status);
173 return inset->getStatus(cur, tmpcmd, status);
180 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
182 Inset * inset = itemInset();
183 FuncRequest tmpcmd(cmd);
185 QModelIndex const & index = tocTV->currentIndex();
186 TocItem const & item =
187 gui_view_.tocModels().currentItem(current_type_, index);
189 // Start an undo group.
190 cur.beginUndoGroup();
192 switch (cmd.action())
194 case LFUN_CHANGE_ACCEPT:
195 case LFUN_CHANGE_REJECT:
196 dispatch(item.action());
197 cur.dispatch(tmpcmd);
200 case LFUN_SECTION_SELECT:
201 dispatch(item.action());
202 cur.dispatch(tmpcmd);
203 // necessary to get the selection drawn.
204 cur.buffer()->changed(true);
205 gui_view_.setFocus();
208 case LFUN_LABEL_COPY_AS_REFERENCE: {
209 // For labels in math, we need to supply the label as a string
210 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
212 inset->dispatch(cur, label_copy);
216 case LFUN_OUTLINE_UP:
217 case LFUN_OUTLINE_DOWN:
218 case LFUN_OUTLINE_IN:
219 case LFUN_OUTLINE_OUT:
220 outline(cmd.action());
225 inset->dispatch(cur, tmpcmd);
231 void TocWidget::on_tocTV_activated(QModelIndex const & index)
237 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
239 Qt::MouseButtons const button = QApplication::mouseButtons();
240 if (button & Qt::LeftButton) {
242 gui_view_.setFocus();
247 void TocWidget::goTo(QModelIndex const & index)
249 LYXERR(Debug::GUI, "goto " << index.row()
250 << ", " << index.column());
252 gui_view_.tocModels().goTo(current_type_, index);
256 void TocWidget::on_updateTB_clicked()
258 // The backend update can take some time so we disable
259 // the controls while waiting.
260 enableControls(false);
261 gui_view_.currentBufferView()->buffer().updateBuffer();
265 void TocWidget::on_sortCB_stateChanged(int state)
267 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
272 void TocWidget::on_persistentCB_stateChanged(int state)
274 persistent_ = state == Qt::Checked;
279 /* FIXME (Ugras 17/11/06):
280 I have implemented a indexDepth function to get the model indices. In my
281 opinion, somebody should derive a new qvariant class for tocModelItem
282 which saves the string data and depth information. That will save the
283 depth calculation. */
285 static int indexDepth(QModelIndex const & index, int depth = -1)
288 return index.parent() == QModelIndex()
289 ? depth : indexDepth(index.parent(), depth);
293 void TocWidget::on_depthSL_valueChanged(int depth)
298 gui_view_.setFocus();
302 void TocWidget::setTreeDepth(int depth)
309 tocTV->collapseAll();
311 tocTV->expandToDepth(depth - 1);
315 void TocWidget::on_typeCO_currentIndexChanged(int index)
319 current_type_ = typeCO->itemData(index).toString();
321 if (typeCO->hasFocus())
322 gui_view_.setFocus();
326 void TocWidget::outline(FuncCode func_code)
328 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
331 enableControls(false);
333 dispatch(FuncRequest(func_code));
334 enableControls(true);
335 gui_view_.setFocus();
339 void TocWidget::on_moveUpTB_clicked()
341 outline(LFUN_OUTLINE_UP);
345 void TocWidget::on_moveDownTB_clicked()
347 outline(LFUN_OUTLINE_DOWN);
351 void TocWidget::on_moveInTB_clicked()
353 outline(LFUN_OUTLINE_IN);
357 void TocWidget::on_moveOutTB_clicked()
359 outline(LFUN_OUTLINE_OUT);
363 void TocWidget::select(QModelIndex const & index)
365 if (!index.isValid()) {
366 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
370 tocTV->scrollTo(index);
371 tocTV->clearSelection();
372 tocTV->setCurrentIndex(index);
376 void TocWidget::enableControls(bool enable)
378 updateTB->setEnabled(enable);
383 moveUpTB->setEnabled(enable);
384 moveDownTB->setEnabled(enable);
385 moveInTB->setEnabled(enable);
386 moveOutTB->setEnabled(enable);
390 void TocWidget::updateView()
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_timer_long_->isActive())
397 update_timer_short_->start();
398 // resets the timer to trigger after 2s
399 update_timer_long_->start();
402 void TocWidget::updateViewNow()
404 update_timer_long_->stop();
405 update_timer_short_->start();
408 void TocWidget::realUpdateView()
410 if (!gui_view_.documentBufferView()) {
412 depthSL->setMaximum(0);
413 depthSL->setValue(0);
418 bool const is_sortable = isSortable();
419 sortCB->setEnabled(is_sortable);
420 bool focus_ = tocTV->hasFocus();
421 tocTV->setEnabled(false);
422 tocTV->setUpdatesEnabled(false);
424 QAbstractItemModel * toc_model =
425 gui_view_.tocModels().model(current_type_);
426 if (tocTV->model() != toc_model) {
427 tocTV->setModel(toc_model);
428 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
430 setTreeDepth(depth_);
433 sortCB->blockSignals(true);
434 sortCB->setChecked(is_sortable
435 && gui_view_.tocModels().isSorted(current_type_));
436 sortCB->blockSignals(false);
438 bool const can_navigate_ = canNavigate();
439 persistentCB->setEnabled(can_navigate_);
441 bool controls_enabled = toc_model && toc_model->rowCount() > 0
442 && !gui_view_.documentBufferView()->buffer().isReadonly();
443 enableControls(controls_enabled);
445 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
446 depthSL->setValue(depth_);
447 if (!persistent_ && can_navigate_)
448 setTreeDepth(depth_);
450 persistentCB->setChecked(persistent_);
451 select(gui_view_.tocModels().currentIndex(current_type_));
454 tocTV->setEnabled(true);
455 tocTV->setUpdatesEnabled(true);
461 void TocWidget::checkModelChanged()
463 if (!gui_view_.documentBufferView() ||
464 gui_view_.tocModels().model(current_type_) != tocTV->model())
469 void TocWidget::filterContents()
474 QModelIndexList indices = tocTV->model()->match(
475 tocTV->model()->index(0, 0),
476 Qt::DisplayRole, "*", -1,
477 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
479 int size = indices.size();
480 for (int i = 0; i < size; i++) {
481 QModelIndex index = indices[i];
483 index.data().toString().contains(
484 filterLE->text(), Qt::CaseInsensitive);
485 tocTV->setRowHidden(index.row(), index.parent(), !matches);
487 // recursively unhide parents of unhidden children
488 for (int i = size - 1; i >= 0; i--) {
489 QModelIndex index = indices[i];
490 if (!tocTV->isRowHidden(index.row(), index.parent())
491 && index.parent() != QModelIndex())
492 tocTV->setRowHidden(index.parent().row(),
493 index.parent().parent(), false);
498 static QString decodeType(QString const & str)
501 if (type.contains("tableofcontents"))
502 type = "tableofcontents";
503 else if (type.contains("lstlistoflistings"))
505 else if (type.contains("floatlist")) {
506 if (type.contains("\"figure"))
508 else if (type.contains("\"table"))
510 else if (type.contains("\"algorithm"))
517 void TocWidget::init(QString const & str)
521 new_index = typeCO->findData(current_type_);
523 new_index = typeCO->findData(decodeType(str));
525 // If everything else fails, settle on the table of contents which is
526 // guaranteed to exist.
527 if (new_index == -1) {
528 current_type_ = "tableofcontents";
529 new_index = typeCO->findData(current_type_);
531 current_type_ = typeCO->itemData(new_index).toString();
534 typeCO->blockSignals(true);
535 typeCO->setCurrentIndex(new_index);
536 typeCO->blockSignals(false);
539 } // namespace frontend
542 #include "moc_TocWidget.cpp"