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 timer_(new QTimer(this))
52 moveOutTB->setIcon(QIcon(getPixmap("images/", "outline-out", "svgz,png")));
53 moveInTB->setIcon(QIcon(getPixmap("images/", "outline-in", "svgz,png")));
54 moveUpTB->setIcon(QIcon(getPixmap("images/", "outline-up", "svgz,png")));
55 moveDownTB->setIcon(QIcon(getPixmap("images/", "outline-down", "svgz,png")));
56 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "svgz,png")));
58 QSize icon_size = gui_view.iconSize();
59 moveOutTB->setIconSize(icon_size);
60 moveInTB->setIconSize(icon_size);
61 moveUpTB->setIconSize(icon_size);
62 moveDownTB->setIconSize(icon_size);
63 updateTB->setIconSize(icon_size);
66 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
70 // hide the pointless QHeader for now
71 // in the future, new columns may appear
72 // like labels, bookmarks, etc...
73 // tocTV->header()->hide();
74 tocTV->header()->setVisible(false);
76 // Only one item selected at a time.
77 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
80 // The toc types combo won't change its model.
81 typeCO->setModel(gui_view_.tocModels().nameModel());
83 // Make sure the buttons are disabled when first shown without a loaded
85 enableControls(false);
87 // make us responsible for the context menu of the tabbar
88 setContextMenuPolicy(Qt::CustomContextMenu);
89 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
90 this, SLOT(showContextMenu(const QPoint &)));
91 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
92 this, SLOT(showContextMenu(const QPoint &)));
93 connect(filterLE, SIGNAL(textEdited(QString)),
94 this, SLOT(filterContents()));
96 // setting the update timer
97 timer_->setSingleShot(true);
98 connect(timer_, SIGNAL(timeout()), this, SLOT(finishUpdateView()));
104 void TocWidget::showContextMenu(const QPoint & pos)
106 std::string name = "context-toc-" + fromqstr(current_type_);
107 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
110 menu->exec(mapToGlobal(pos));
114 Inset * TocWidget::itemInset() const
116 QModelIndex const & index = tocTV->currentIndex();
117 TocItem const & item =
118 gui_view_.tocModels().currentItem(current_type_, index);
119 DocIterator const & dit = item.dit();
122 if (current_type_ == "label"
123 || current_type_ == "graphics"
124 || current_type_ == "citation"
125 || current_type_ == "child")
126 inset = dit.nextInset();
128 else if (current_type_ == "branch"
129 || current_type_ == "index"
130 || current_type_ == "change"
131 || current_type_ == "table"
132 || current_type_ == "listing"
133 || current_type_ == "figure")
134 inset = &dit.inset();
140 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
141 FuncStatus & status) const
143 Inset * inset = itemInset();
144 FuncRequest tmpcmd(cmd);
146 QModelIndex const & index = tocTV->currentIndex();
147 TocItem const & item =
148 gui_view_.tocModels().currentItem(current_type_, index);
150 switch (cmd.action())
152 case LFUN_CHANGE_ACCEPT:
153 case LFUN_CHANGE_REJECT:
154 case LFUN_OUTLINE_UP:
155 case LFUN_OUTLINE_DOWN:
156 case LFUN_OUTLINE_IN:
157 case LFUN_OUTLINE_OUT:
158 case LFUN_SECTION_SELECT:
159 status.setEnabled((bool)item.dit());
162 case LFUN_LABEL_COPY_AS_REFERENCE: {
163 // For labels in math, we need to supply the label as a string
164 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
166 return inset->getStatus(cur, label_copy, status);
172 return inset->getStatus(cur, tmpcmd, status);
179 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)
240 Qt::MouseButtons const button = QApplication::mouseButtons();
241 if (button & Qt::LeftButton) {
243 gui_view_.setFocus();
244 gui_view_.activateWindow();
249 void TocWidget::goTo(QModelIndex const & index)
251 LYXERR(Debug::GUI, "goto " << index.row()
252 << ", " << index.column());
254 sendDispatch(gui_view_.tocModels().goTo(current_type_, index));
258 void TocWidget::on_updateTB_clicked()
260 // The backend update can take some time so we disable
261 // the controls while waiting.
262 enableControls(false);
263 gui_view_.currentBufferView()->buffer().updateBuffer();
267 void TocWidget::on_sortCB_stateChanged(int state)
269 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
274 void TocWidget::on_persistentCB_stateChanged(int state)
276 persistent_ = state == Qt::Checked;
281 /* FIXME (Ugras 17/11/06):
282 I have implemented a indexDepth function to get the model indices. In my
283 opinion, somebody should derive a new qvariant class for tocModelItem
284 which saves the string data and depth information. That will save the
285 depth calculation. */
287 static int indexDepth(QModelIndex const & index, int depth = -1)
290 return index.parent() == QModelIndex()
291 ? depth : indexDepth(index.parent(), depth);
295 void TocWidget::on_depthSL_valueChanged(int depth)
300 gui_view_.setFocus();
304 void TocWidget::setTreeDepth(int depth)
311 tocTV->collapseAll();
313 tocTV->expandToDepth(depth - 1);
317 void TocWidget::on_typeCO_currentIndexChanged(int index)
322 current_type_ = typeCO->itemData(index).toString();
324 if (typeCO->hasFocus())
325 gui_view_.setFocus();
329 void TocWidget::outline(FuncCode func_code)
331 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
335 //if another window is active, this attempt will fail,
336 //but it will work at least for the second attempt
337 gui_view_.activateWindow();
339 enableControls(false);
341 sendDispatch(FuncRequest(func_code));
342 enableControls(true);
343 gui_view_.setFocus();
347 void TocWidget::sendDispatch(FuncRequest fr)
350 fr.setViewOrigin(&gui_view_);
351 DispatchResult dr=dispatch(fr);
353 gui_view_.message(dr.message());
357 void TocWidget::on_moveUpTB_clicked()
359 outline(LFUN_OUTLINE_UP);
363 void TocWidget::on_moveDownTB_clicked()
365 outline(LFUN_OUTLINE_DOWN);
369 void TocWidget::on_moveInTB_clicked()
371 outline(LFUN_OUTLINE_IN);
375 void TocWidget::on_moveOutTB_clicked()
377 outline(LFUN_OUTLINE_OUT);
381 void TocWidget::select(QModelIndex const & index)
383 if (!index.isValid()) {
384 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
388 tocTV->scrollTo(index);
389 tocTV->clearSelection();
390 tocTV->setCurrentIndex(index);
394 void TocWidget::enableControls(bool enable)
396 updateTB->setEnabled(enable);
401 moveUpTB->setEnabled(enable);
402 moveDownTB->setEnabled(enable);
403 moveInTB->setEnabled(enable);
404 moveOutTB->setEnabled(enable);
408 void TocWidget::updateView()
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 persistentCB->setEnabled(canNavigate());
440 bool controls_enabled = toc_model && toc_model->rowCount() > 0
441 && !gui_view_.documentBufferView()->buffer().isReadonly();
442 enableControls(controls_enabled);
444 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
445 depthSL->setValue(depth_);
446 tocTV->setEnabled(true);
447 tocTV->setUpdatesEnabled(true);
451 // Expensive operations are on a timer. We finish the update immediately
452 // for sparse edition actions, i.e. there was no edition/cursor movement
453 // recently, then every 300ms.
454 if (!timer_->isActive()) {
461 void TocWidget::updateViewNow()
468 void TocWidget::finishUpdateView()
470 // Profiling shows that this is the expensive stuff in the context of typing
471 // text and moving with arrows. For bigger operations, this is negligible,
472 // and outweighted by TocModels::reset() anyway.
475 setTreeDepth(depth_);
476 persistentCB->setChecked(persistent_);
477 // select the item at current cursor location
478 if (gui_view_.documentBufferView()) {
479 DocIterator const & dit = gui_view_.documentBufferView()->cursor();
480 select(gui_view_.tocModels().currentIndex(current_type_, dit));
487 void TocWidget::filterContents()
492 QModelIndexList indices = tocTV->model()->match(
493 tocTV->model()->index(0, 0),
494 Qt::DisplayRole, "*", -1,
495 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
497 int size = indices.size();
498 for (int i = 0; i < size; i++) {
499 QModelIndex index = indices[i];
501 index.data().toString().contains(
502 filterLE->text(), Qt::CaseInsensitive);
503 tocTV->setRowHidden(index.row(), index.parent(), !matches);
505 // recursively unhide parents of unhidden children
506 for (int i = size - 1; i >= 0; i--) {
507 QModelIndex index = indices[i];
508 if (!tocTV->isRowHidden(index.row(), index.parent())
509 && index.parent() != QModelIndex())
510 tocTV->setRowHidden(index.parent().row(),
511 index.parent().parent(), false);
516 static QString decodeType(QString const & str)
519 if (type.contains("tableofcontents"))
520 type = "tableofcontents";
521 else if (type.contains("lstlistoflistings"))
523 else if (type.contains("floatlist")) {
524 if (type.contains("\"figure"))
526 else if (type.contains("\"table"))
528 else if (type.contains("\"algorithm"))
535 void TocWidget::init(QString const & str)
539 new_index = typeCO->findData(current_type_);
541 new_index = typeCO->findData(decodeType(str));
543 // If everything else fails, settle on the table of contents which is
544 // guaranteed to exist.
545 if (new_index == -1) {
546 current_type_ = "tableofcontents";
547 new_index = typeCO->findData(current_type_);
549 current_type_ = typeCO->itemData(new_index).toString();
552 typeCO->blockSignals(true);
553 typeCO->setCurrentIndex(new_index);
554 typeCO->blockSignals(false);
558 } // namespace frontend
561 #include "moc_TocWidget.cpp"