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")));
59 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
63 // hide the pointless QHeader for now
64 // in the future, new columns may appear
65 // like labels, bookmarks, etc...
66 // tocTV->header()->hide();
67 tocTV->header()->setVisible(false);
69 // Only one item selected at a time.
70 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
73 // The toc types combo won't change its model.
74 typeCO->setModel(gui_view_.tocModels().nameModel());
76 // Make sure the buttons are disabled when first shown without a loaded
78 enableControls(false);
80 // make us responsible for the context menu of the tabbar
81 setContextMenuPolicy(Qt::CustomContextMenu);
82 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
83 this, SLOT(showContextMenu(const QPoint &)));
84 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
85 this, SLOT(showContextMenu(const QPoint &)));
86 connect(filterLE, SIGNAL(textEdited(QString)),
87 this, SLOT(filterContents()));
89 // setting the update timer
90 timer_->setSingleShot(true);
91 connect(timer_, SIGNAL(timeout()), this, SLOT(finishUpdateView()));
97 void TocWidget::showContextMenu(const QPoint & pos)
99 std::string name = "context-toc-" + fromqstr(current_type_);
100 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
103 menu->exec(mapToGlobal(pos));
107 Inset * TocWidget::itemInset() const
109 QModelIndex const & index = tocTV->currentIndex();
110 TocItem const & item =
111 gui_view_.tocModels().currentItem(current_type_, index);
112 DocIterator const & dit = item.dit();
115 if (current_type_ == "label"
116 || current_type_ == "graphics"
117 || current_type_ == "citation"
118 || current_type_ == "child")
119 inset = dit.nextInset();
121 else if (current_type_ == "branch"
122 || current_type_ == "index"
123 || current_type_ == "change"
124 || current_type_ == "table"
125 || current_type_ == "listing"
126 || current_type_ == "figure")
127 inset = &dit.inset();
133 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
134 FuncStatus & status) const
136 Inset * inset = itemInset();
137 FuncRequest tmpcmd(cmd);
139 QModelIndex const & index = tocTV->currentIndex();
140 TocItem const & item =
141 gui_view_.tocModels().currentItem(current_type_, index);
143 switch (cmd.action())
145 case LFUN_CHANGE_ACCEPT:
146 case LFUN_CHANGE_REJECT:
147 case LFUN_OUTLINE_UP:
148 case LFUN_OUTLINE_DOWN:
149 case LFUN_OUTLINE_IN:
150 case LFUN_OUTLINE_OUT:
151 case LFUN_SECTION_SELECT:
152 status.setEnabled(item.dit() != 0);
155 case LFUN_LABEL_COPY_AS_REFERENCE: {
156 // For labels in math, we need to supply the label as a string
157 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
159 return inset->getStatus(cur, label_copy, status);
165 return inset->getStatus(cur, tmpcmd, status);
172 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
174 Inset * inset = itemInset();
175 FuncRequest tmpcmd(cmd);
177 QModelIndex const & index = tocTV->currentIndex();
178 TocItem const & item =
179 gui_view_.tocModels().currentItem(current_type_, index);
181 // Start an undo group.
182 cur.beginUndoGroup();
184 switch (cmd.action())
186 case LFUN_CHANGE_ACCEPT:
187 case LFUN_CHANGE_REJECT:
188 dispatch(item.action());
189 cur.dispatch(tmpcmd);
192 case LFUN_SECTION_SELECT:
193 dispatch(item.action());
194 cur.dispatch(tmpcmd);
195 // necessary to get the selection drawn.
196 cur.buffer()->changed(true);
197 gui_view_.setFocus();
200 case LFUN_LABEL_COPY_AS_REFERENCE: {
201 // For labels in math, we need to supply the label as a string
202 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REFERENCE, item.str());
204 inset->dispatch(cur, label_copy);
208 case LFUN_OUTLINE_UP:
209 case LFUN_OUTLINE_DOWN:
210 case LFUN_OUTLINE_IN:
211 case LFUN_OUTLINE_OUT:
212 outline(cmd.action());
217 inset->dispatch(cur, tmpcmd);
223 void TocWidget::on_tocTV_activated(QModelIndex const & index)
229 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
231 Qt::MouseButtons const button = QApplication::mouseButtons();
232 if (button & Qt::LeftButton) {
234 gui_view_.setFocus();
235 gui_view_.activateWindow();
240 void TocWidget::goTo(QModelIndex const & index)
242 LYXERR(Debug::GUI, "goto " << index.row()
243 << ", " << index.column());
245 gui_view_.tocModels().goTo(current_type_, index);
249 void TocWidget::on_updateTB_clicked()
251 // The backend update can take some time so we disable
252 // the controls while waiting.
253 enableControls(false);
254 gui_view_.currentBufferView()->buffer().updateBuffer();
258 void TocWidget::on_sortCB_stateChanged(int state)
260 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
265 void TocWidget::on_persistentCB_stateChanged(int state)
267 persistent_ = state == Qt::Checked;
272 /* FIXME (Ugras 17/11/06):
273 I have implemented a indexDepth function to get the model indices. In my
274 opinion, somebody should derive a new qvariant class for tocModelItem
275 which saves the string data and depth information. That will save the
276 depth calculation. */
278 static int indexDepth(QModelIndex const & index, int depth = -1)
281 return index.parent() == QModelIndex()
282 ? depth : indexDepth(index.parent(), depth);
286 void TocWidget::on_depthSL_valueChanged(int depth)
291 gui_view_.setFocus();
295 void TocWidget::setTreeDepth(int depth)
302 tocTV->collapseAll();
304 tocTV->expandToDepth(depth - 1);
308 void TocWidget::on_typeCO_currentIndexChanged(int index)
312 current_type_ = typeCO->itemData(index).toString();
314 if (typeCO->hasFocus())
315 gui_view_.setFocus();
319 void TocWidget::outline(FuncCode func_code)
321 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
324 enableControls(false);
326 dispatch(FuncRequest(func_code));
327 enableControls(true);
328 gui_view_.setFocus();
332 void TocWidget::on_moveUpTB_clicked()
334 outline(LFUN_OUTLINE_UP);
338 void TocWidget::on_moveDownTB_clicked()
340 outline(LFUN_OUTLINE_DOWN);
344 void TocWidget::on_moveInTB_clicked()
346 outline(LFUN_OUTLINE_IN);
350 void TocWidget::on_moveOutTB_clicked()
352 outline(LFUN_OUTLINE_OUT);
356 void TocWidget::select(QModelIndex const & index)
358 if (!index.isValid()) {
359 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
363 tocTV->scrollTo(index);
364 tocTV->clearSelection();
365 tocTV->setCurrentIndex(index);
369 void TocWidget::enableControls(bool enable)
371 updateTB->setEnabled(enable);
376 moveUpTB->setEnabled(enable);
377 moveDownTB->setEnabled(enable);
378 moveInTB->setEnabled(enable);
379 moveOutTB->setEnabled(enable);
383 void TocWidget::updateView()
385 if (!gui_view_.documentBufferView()) {
387 depthSL->setMaximum(0);
388 depthSL->setValue(0);
393 bool const is_sortable = isSortable();
394 sortCB->setEnabled(is_sortable);
395 bool focus = tocTV->hasFocus();
396 tocTV->setEnabled(false);
397 tocTV->setUpdatesEnabled(false);
399 QAbstractItemModel * toc_model =
400 gui_view_.tocModels().model(current_type_);
401 if (tocTV->model() != toc_model) {
402 tocTV->setModel(toc_model);
403 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
405 setTreeDepth(depth_);
408 sortCB->blockSignals(true);
409 sortCB->setChecked(is_sortable
410 && gui_view_.tocModels().isSorted(current_type_));
411 sortCB->blockSignals(false);
413 persistentCB->setEnabled(canNavigate());
415 bool controls_enabled = toc_model && toc_model->rowCount() > 0
416 && !gui_view_.documentBufferView()->buffer().isReadonly();
417 enableControls(controls_enabled);
419 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
420 depthSL->setValue(depth_);
421 tocTV->setEnabled(true);
422 tocTV->setUpdatesEnabled(true);
426 // Expensive operations are on a timer. We finish the update immediately
427 // for sparse edition actions, i.e. there was no edition/cursor movement
428 // recently, then every 300ms.
429 if (!timer_->isActive()) {
436 void TocWidget::updateViewNow()
443 void TocWidget::finishUpdateView()
445 // Profiling shows that this is the expensive stuff in the context of typing
446 // text and moving with arrows (still five times less than updateToolbars in
447 // my tests with a medium-sized document, however this grows linearly in the
448 // size of the document). For bigger operations, this is negligible, and
449 // outweighted by TocModels::reset() anyway.
452 setTreeDepth(depth_);
453 persistentCB->setChecked(persistent_);
454 select(gui_view_.tocModels().currentIndex(current_type_));
460 void TocWidget::filterContents()
465 QModelIndexList indices = tocTV->model()->match(
466 tocTV->model()->index(0, 0),
467 Qt::DisplayRole, "*", -1,
468 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
470 int size = indices.size();
471 for (int i = 0; i < size; i++) {
472 QModelIndex index = indices[i];
474 index.data().toString().contains(
475 filterLE->text(), Qt::CaseInsensitive);
476 tocTV->setRowHidden(index.row(), index.parent(), !matches);
478 // recursively unhide parents of unhidden children
479 for (int i = size - 1; i >= 0; i--) {
480 QModelIndex index = indices[i];
481 if (!tocTV->isRowHidden(index.row(), index.parent())
482 && index.parent() != QModelIndex())
483 tocTV->setRowHidden(index.parent().row(),
484 index.parent().parent(), false);
489 static QString decodeType(QString const & str)
492 if (type.contains("tableofcontents"))
493 type = "tableofcontents";
494 else if (type.contains("lstlistoflistings"))
496 else if (type.contains("floatlist")) {
497 if (type.contains("\"figure"))
499 else if (type.contains("\"table"))
501 else if (type.contains("\"algorithm"))
508 void TocWidget::init(QString const & str)
512 new_index = typeCO->findData(current_type_);
514 new_index = typeCO->findData(decodeType(str));
516 // If everything else fails, settle on the table of contents which is
517 // guaranteed to exist.
518 if (new_index == -1) {
519 current_type_ = "tableofcontents";
520 new_index = typeCO->findData(current_type_);
522 current_type_ = typeCO->itemData(new_index).toString();
525 typeCO->blockSignals(true);
526 typeCO->setCurrentIndex(new_index);
527 typeCO->blockSignals(false);
531 } // namespace frontend
534 #include "moc_TocWidget.cpp"