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)
181 Inset * inset = itemInset();
182 FuncRequest tmpcmd(cmd);
184 QModelIndex const & index = tocTV->currentIndex();
185 TocItem const & item =
186 gui_view_.tocModels().currentItem(current_type_, index);
188 // Start an undo group.
189 cur.beginUndoGroup();
191 switch (cmd.action())
193 case LFUN_CHANGE_ACCEPT:
194 case LFUN_CHANGE_REJECT:
195 dispatch(item.action());
196 cur.dispatch(tmpcmd);
199 case LFUN_SECTION_SELECT:
200 dispatch(item.action());
201 cur.dispatch(tmpcmd);
202 // necessary to get the selection drawn.
203 cur.buffer()->changed(true);
204 gui_view_.setFocus();
207 case LFUN_LABEL_COPY_AS_REFERENCE: {
208 // For labels in math, we need to supply the label as a string
209 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
211 inset->dispatch(cur, label_copy);
215 case LFUN_OUTLINE_UP:
216 case LFUN_OUTLINE_DOWN:
217 case LFUN_OUTLINE_IN:
218 case LFUN_OUTLINE_OUT:
219 outline(cmd.action());
224 inset->dispatch(cur, tmpcmd);
230 void TocWidget::on_tocTV_activated(QModelIndex const & index)
236 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
238 Qt::MouseButtons const button = QApplication::mouseButtons();
239 if (button & Qt::LeftButton) {
241 gui_view_.setFocus();
242 gui_view_.activateWindow();
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 if (!gui_view_.documentBufferView()) {
394 depthSL->setMaximum(0);
395 depthSL->setValue(0);
400 bool const is_sortable = isSortable();
401 sortCB->setEnabled(is_sortable);
402 bool focus = tocTV->hasFocus();
403 tocTV->setEnabled(false);
404 tocTV->setUpdatesEnabled(false);
406 QAbstractItemModel * toc_model =
407 gui_view_.tocModels().model(current_type_);
408 if (tocTV->model() != toc_model) {
409 tocTV->setModel(toc_model);
410 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
412 setTreeDepth(depth_);
415 sortCB->blockSignals(true);
416 sortCB->setChecked(is_sortable
417 && gui_view_.tocModels().isSorted(current_type_));
418 sortCB->blockSignals(false);
420 persistentCB->setEnabled(canNavigate());
422 bool controls_enabled = toc_model && toc_model->rowCount() > 0
423 && !gui_view_.documentBufferView()->buffer().isReadonly();
424 enableControls(controls_enabled);
426 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
427 depthSL->setValue(depth_);
428 tocTV->setEnabled(true);
429 tocTV->setUpdatesEnabled(true);
433 // Expensive operations are on a timer. We finish the update immediately
434 // for sparse edition actions, i.e. there was no edition/cursor movement
435 // recently, then every 300ms.
436 if (!timer_->isActive()) {
443 void TocWidget::updateViewNow()
450 void TocWidget::finishUpdateView()
452 // Profiling shows that this is the expensive stuff in the context of typing
453 // text and moving with arrows. For bigger operations, this is negligible,
454 // and outweighted by TocModels::reset() anyway.
457 setTreeDepth(depth_);
458 persistentCB->setChecked(persistent_);
459 // select the item at current cursor location
460 if (gui_view_.documentBufferView()) {
461 DocIterator const & dit = gui_view_.documentBufferView()->cursor();
462 select(gui_view_.tocModels().currentIndex(current_type_, dit));
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);
540 } // namespace frontend
543 #include "moc_TocWidget.cpp"