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 void TocWidget::enableControls(bool enable)
375 updateTB->setEnabled(enable);
380 moveUpTB->setEnabled(enable);
381 moveDownTB->setEnabled(enable);
382 moveInTB->setEnabled(enable);
383 moveOutTB->setEnabled(enable);
387 void TocWidget::updateView()
389 if (!gui_view_.documentBufferView()) {
390 enableControls(false);
391 typeCO->setEnabled(false);
393 tocTV->setEnabled(false);
394 depthSL->setMaximum(0);
395 depthSL->setValue(0);
396 persistentCB->setEnabled(false);
397 sortCB->setEnabled(false);
398 depthSL->setEnabled(false);
401 bool const is_sortable = isSortable();
402 sortCB->setEnabled(is_sortable);
403 depthSL->setEnabled(true);
404 typeCO->setEnabled(true);
405 tocTV->setEnabled(false);
406 tocTV->setUpdatesEnabled(false);
408 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
409 if (tocTV->model() != toc_model) {
410 tocTV->setModel(toc_model);
411 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
413 setTreeDepth(depth_);
416 sortCB->blockSignals(true);
417 sortCB->setChecked(is_sortable
418 && gui_view_.tocModels().isSorted(current_type_));
419 sortCB->blockSignals(false);
421 bool const can_navigate_ = canNavigate();
422 persistentCB->setEnabled(can_navigate_);
424 bool controls_enabled = toc_model && toc_model->rowCount() > 0
425 && !gui_view_.documentBufferView()->buffer().isReadonly();
426 enableControls(controls_enabled);
428 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
429 depthSL->setValue(depth_);
430 if (!persistent_ && can_navigate_)
431 setTreeDepth(depth_);
433 persistentCB->setChecked(persistent_);
434 select(gui_view_.tocModels().currentIndex(current_type_));
437 tocTV->setEnabled(true);
438 tocTV->setUpdatesEnabled(true);
442 void TocWidget::filterContents()
444 QModelIndexList indices = tocTV->model()->match(
445 tocTV->model()->index(0, 0),
446 Qt::DisplayRole, "*", -1,
447 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
449 int size = indices.size();
450 for (int i = 0; i < size; i++) {
451 QModelIndex index = indices[i];
453 index.data().toString().contains(
454 filterLE->text(), Qt::CaseSensitive);
455 tocTV->setRowHidden(index.row(), index.parent(), !matches);
457 // recursively unhide parents of unhidden children
458 for (int i = size - 1; i >= 0; i--) {
459 QModelIndex index = indices[i];
460 if (!tocTV->isRowHidden(index.row(), index.parent())
461 && index.parent() != QModelIndex())
462 tocTV->setRowHidden(index.parent().row(),
463 index.parent().parent(), false);
468 static QString decodeType(QString const & str)
471 if (type.contains("tableofcontents")) {
472 type = "tableofcontents";
473 } else if (type.contains("floatlist")) {
474 if (type.contains("\"figure"))
476 else if (type.contains("\"table"))
478 else if (type.contains("\"algorithm"))
485 void TocWidget::init(QString const & str)
489 new_index = typeCO->findData(current_type_);
491 new_index = typeCO->findData(decodeType(str));
493 // If everything else fails, settle on the table of contents which is
494 // guaranted to exist.
495 if (new_index == -1) {
496 current_type_ = "tableofcontents";
497 new_index = typeCO->findData(current_type_);
499 current_type_ = typeCO->itemData(new_index).toString();
502 typeCO->blockSignals(true);
503 typeCO->setCurrentIndex(new_index);
504 typeCO->blockSignals(false);
507 } // namespace frontend
510 #include "moc_TocWidget.cpp"