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>
47 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
48 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
52 moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
53 moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
54 moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
55 moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
56 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "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);
72 // The toc types combo won't change its model.
73 typeCO->setModel(gui_view_.tocModels().nameModel());
75 // Make sure the buttons are disabled when first shown without a loaded
77 enableControls(false);
79 // make us responsible for the context menu of the tabbar
80 setContextMenuPolicy(Qt::CustomContextMenu);
81 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
82 this, SLOT(showContextMenu(const QPoint &)));
83 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
84 this, SLOT(showContextMenu(const QPoint &)));
85 connect(filterLE, SIGNAL(textEdited(QString)),
86 this, SLOT(filterContents()));
92 void TocWidget::showContextMenu(const QPoint & pos)
94 std::string name = "context-toc-" + fromqstr(current_type_);
95 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
98 menu->exec(mapToGlobal(pos));
102 Inset * TocWidget::itemInset() const
104 QModelIndex const & index = tocTV->currentIndex();
105 TocItem const & item =
106 gui_view_.tocModels().currentItem(current_type_, index);
107 DocIterator const & dit = item.dit();
110 if (current_type_ == "label"
111 || current_type_ == "graphics"
112 || current_type_ == "citation"
113 || current_type_ == "child")
114 inset = dit.nextInset();
116 else if (current_type_ == "branch"
117 || current_type_ == "index"
118 || current_type_ == "change")
119 inset = &dit.inset();
121 else if (current_type_ == "table"
122 || current_type_ == "listing"
123 || current_type_ == "figure") {
124 DocIterator tmp_dit(dit);
126 inset = &tmp_dit.inset();
132 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
133 FuncStatus & status) const
135 Inset * inset = itemInset();
136 FuncRequest tmpcmd(cmd);
138 QModelIndex const & index = tocTV->currentIndex();
139 TocItem const & item =
140 gui_view_.tocModels().currentItem(current_type_, index);
144 case LFUN_CHANGE_ACCEPT:
145 case LFUN_CHANGE_REJECT:
146 case LFUN_OUTLINE_UP:
147 case LFUN_OUTLINE_DOWN:
148 case LFUN_OUTLINE_IN:
149 case LFUN_OUTLINE_OUT:
150 case LFUN_SECTION_SELECT:
151 status.setEnabled(true);
154 case LFUN_LABEL_COPY_AS_REF: {
155 // For labels in math, we need to supply the label as a string
156 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
158 return inset->getStatus(cur, label_copy, status);
163 return inset->getStatus(cur, tmpcmd, status);
170 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
172 Inset * inset = itemInset();
173 FuncRequest tmpcmd(cmd);
175 QModelIndex const & index = tocTV->currentIndex();
176 TocItem const & item =
177 gui_view_.tocModels().currentItem(current_type_, index);
179 // Start an undo group.
180 cur.beginUndoGroup();
184 case LFUN_CHANGE_ACCEPT:
185 case LFUN_CHANGE_REJECT:
186 case LFUN_SECTION_SELECT:
187 dispatch(item.action());
188 cur.dispatch(tmpcmd);
191 case LFUN_LABEL_COPY_AS_REF: {
192 // For labels in math, we need to supply the label as a string
193 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
195 inset->dispatch(cur, label_copy);
199 case LFUN_OUTLINE_UP:
200 case LFUN_OUTLINE_DOWN:
201 case LFUN_OUTLINE_IN:
202 case LFUN_OUTLINE_OUT:
208 inset->dispatch(cur, tmpcmd);
214 void TocWidget::on_tocTV_activated(QModelIndex const & index)
220 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
222 Qt::MouseButtons const button = QApplication::mouseButtons();
223 if (button & Qt::LeftButton) {
225 gui_view_.setFocus();
230 void TocWidget::goTo(QModelIndex const & index)
232 LYXERR(Debug::GUI, "goto " << index.row()
233 << ", " << index.column());
235 gui_view_.tocModels().goTo(current_type_, index);
239 void TocWidget::on_updateTB_clicked()
241 // The backend update can take some time so we disable
242 // the controls while waiting.
243 enableControls(false);
244 gui_view_.tocModels().updateBackend();
248 void TocWidget::on_sortCB_stateChanged(int state)
250 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
255 void TocWidget::on_persistentCB_stateChanged(int state)
257 persistent_ = state == Qt::Checked;
261 /* FIXME (Ugras 17/11/06):
262 I have implemented a indexDepth function to get the model indices. In my
263 opinion, somebody should derive a new qvariant class for tocModelItem
264 which saves the string data and depth information. That will save the
265 depth calculation. */
267 static int indexDepth(QModelIndex const & index, int depth = -1)
270 return index.parent() == QModelIndex()
271 ? depth : indexDepth(index.parent(), depth);
275 void TocWidget::on_depthSL_valueChanged(int depth)
280 gui_view_.setFocus();
284 void TocWidget::setTreeDepth(int depth)
290 #if QT_VERSION >= 0x040300
291 // this should be faster than our own code below
293 tocTV->collapseAll();
295 tocTV->expandToDepth(depth - 1);
297 // expanding and then collapsing is probably better,
298 // but my qt 4.1.2 doesn't have expandAll()..
299 //tocTV->expandAll();
300 QModelIndexList indices = tocTV->model()->match(
301 tocTV->model()->index(0, 0),
302 Qt::DisplayRole, "*", -1,
303 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
305 int size = indices.size();
306 for (int i = 0; i < size; i++) {
307 QModelIndex index = indices[i];
308 tocTV->setExpanded(index, indexDepth(index) < depth_);
314 void TocWidget::on_typeCO_currentIndexChanged(int index)
316 current_type_ = typeCO->itemData(index).toString();
318 gui_view_.setFocus();
322 void TocWidget::outline(int func_code)
324 enableControls(false);
325 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
328 enableControls(false);
330 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
331 enableControls(true);
332 gui_view_.setFocus();
336 void TocWidget::on_moveUpTB_clicked()
338 outline(LFUN_OUTLINE_UP);
342 void TocWidget::on_moveDownTB_clicked()
344 outline(LFUN_OUTLINE_DOWN);
348 void TocWidget::on_moveInTB_clicked()
350 outline(LFUN_OUTLINE_IN);
354 void TocWidget::on_moveOutTB_clicked()
356 outline(LFUN_OUTLINE_OUT);
360 void TocWidget::select(QModelIndex const & index)
362 if (!index.isValid()) {
363 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
367 tocTV->scrollTo(index);
368 tocTV->clearSelection();
369 tocTV->setCurrentIndex(index);
373 /// Test whether outlining operation is possible
374 static bool canOutline(QString const & type)
376 return type == "tableofcontents";
380 void TocWidget::enableControls(bool enable)
382 updateTB->setEnabled(enable);
384 if (!canOutline(current_type_))
387 moveUpTB->setEnabled(enable);
388 moveDownTB->setEnabled(enable);
389 moveInTB->setEnabled(enable);
390 moveOutTB->setEnabled(enable);
394 /// Test whether synchronized navigation is possible
395 static bool canNavigate(QString const & type)
397 // It is not possible to have synchronous navigation in a correct
398 // and efficient way with the label and change type because Toc::item()
399 // does a linear search. Even when fixed, it might even not be desirable
400 // to do so if we want to support drag&drop of labels and references.
401 return type != "label" && type != "change";
405 /// Test whether sorting is possible
406 static bool isSortable(QString const & type)
408 return type != "tableofcontents";
412 void TocWidget::updateView()
414 if (!gui_view_.documentBufferView()) {
415 enableControls(false);
416 typeCO->setEnabled(false);
418 tocTV->setEnabled(false);
419 depthSL->setMaximum(0);
420 depthSL->setValue(0);
421 persistentCB->setEnabled(false);
422 sortCB->setEnabled(false);
423 depthSL->setEnabled(false);
426 sortCB->setEnabled(isSortable(current_type_));
427 depthSL->setEnabled(true);
428 typeCO->setEnabled(true);
429 tocTV->setEnabled(false);
430 tocTV->setUpdatesEnabled(false);
432 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
433 if (tocTV->model() != toc_model) {
434 tocTV->setModel(toc_model);
435 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
437 setTreeDepth(depth_);
440 sortCB->blockSignals(true);
441 sortCB->setChecked(isSortable(current_type_)
442 && gui_view_.tocModels().isSorted(current_type_));
443 sortCB->blockSignals(false);
445 bool const can_navigate_ = canNavigate(current_type_);
446 persistentCB->setEnabled(can_navigate_);
448 bool controls_enabled = toc_model && toc_model->rowCount() > 0
449 && !gui_view_.documentBufferView()->buffer().isReadonly();
450 enableControls(controls_enabled);
452 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
453 depthSL->setValue(depth_);
454 if (!persistent_ && can_navigate_)
455 setTreeDepth(depth_);
457 persistentCB->setChecked(persistent_);
458 select(gui_view_.tocModels().currentIndex(current_type_));
461 tocTV->setEnabled(true);
462 tocTV->setUpdatesEnabled(true);
466 void TocWidget::filterContents()
468 QModelIndexList indices = tocTV->model()->match(
469 tocTV->model()->index(0, 0),
470 Qt::DisplayRole, "*", -1,
471 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
473 int size = indices.size();
474 for (int i = 0; i < size; i++) {
475 QModelIndex index = indices[i];
477 index.data().toString().contains(
478 filterLE->text(), Qt::CaseSensitive);
479 tocTV->setRowHidden(index.row(), index.parent(), !matches);
481 // recursively unhide parents of unhidden children
482 for (int i = size - 1; i >= 0; i--) {
483 QModelIndex index = indices[i];
484 if (!tocTV->isRowHidden(index.row(), index.parent())
485 && index.parent() != QModelIndex())
486 tocTV->setRowHidden(index.parent().row(),
487 index.parent().parent(), false);
492 static QString decodeType(QString const & str)
495 if (type.contains("tableofcontents")) {
496 type = "tableofcontents";
497 } else if (type.contains("floatlist")) {
498 if (type.contains("\"figure"))
500 else if (type.contains("\"table"))
502 else if (type.contains("\"algorithm"))
509 void TocWidget::init(QString const & str)
513 new_index = typeCO->findData(current_type_);
515 new_index = typeCO->findData(decodeType(str));
517 // If everything else fails, settle on the table of contents which is
518 // guaranted to exist.
519 if (new_index == -1) {
520 current_type_ = "tableofcontents";
521 new_index = typeCO->findData(current_type_);
523 current_type_ = typeCO->itemData(new_index).toString();
526 typeCO->blockSignals(true);
527 typeCO->setCurrentIndex(new_index);
528 typeCO->blockSignals(false);
531 } // namespace frontend
534 #include "moc_TocWidget.cpp"