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);
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()));
93 void TocWidget::showContextMenu(const QPoint & pos)
95 std::string name = "context-toc-" + fromqstr(current_type_);
96 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
99 menu->exec(mapToGlobal(pos));
103 Inset * TocWidget::itemInset() const
105 QModelIndex const & index = tocTV->currentIndex();
106 TocItem const & item =
107 gui_view_.tocModels().currentItem(current_type_, index);
108 DocIterator const & dit = item.dit();
111 if (current_type_ == "label"
112 || current_type_ == "graphics"
113 || current_type_ == "citation"
114 || current_type_ == "child")
115 inset = dit.nextInset();
117 else if (current_type_ == "branch"
118 || current_type_ == "index"
119 || current_type_ == "change")
120 inset = &dit.inset();
122 else if (current_type_ == "table"
123 || current_type_ == "listing"
124 || current_type_ == "figure") {
125 DocIterator tmp_dit(dit);
127 inset = &tmp_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(true);
155 case LFUN_LABEL_COPY_AS_REF: {
156 // For labels in math, we need to supply the label as a string
157 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
159 return inset->getStatus(cur, label_copy, status);
164 return inset->getStatus(cur, tmpcmd, status);
171 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
173 Inset * inset = itemInset();
174 FuncRequest tmpcmd(cmd);
176 QModelIndex const & index = tocTV->currentIndex();
177 TocItem const & item =
178 gui_view_.tocModels().currentItem(current_type_, index);
180 // Start an undo group.
181 cur.beginUndoGroup();
183 switch (cmd.action())
185 case LFUN_CHANGE_ACCEPT:
186 case LFUN_CHANGE_REJECT:
187 case LFUN_SECTION_SELECT:
188 dispatch(item.action());
189 cur.dispatch(tmpcmd);
192 case LFUN_LABEL_COPY_AS_REF: {
193 // For labels in math, we need to supply the label as a string
194 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
196 inset->dispatch(cur, label_copy);
200 case LFUN_OUTLINE_UP:
201 case LFUN_OUTLINE_DOWN:
202 case LFUN_OUTLINE_IN:
203 case LFUN_OUTLINE_OUT:
204 outline(cmd.action());
209 inset->dispatch(cur, tmpcmd);
215 void TocWidget::on_tocTV_activated(QModelIndex const & index)
221 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
223 Qt::MouseButtons const button = QApplication::mouseButtons();
224 if (button & Qt::LeftButton) {
226 gui_view_.setFocus();
231 void TocWidget::goTo(QModelIndex const & index)
233 LYXERR(Debug::GUI, "goto " << index.row()
234 << ", " << index.column());
236 gui_view_.tocModels().goTo(current_type_, index);
240 void TocWidget::on_updateTB_clicked()
242 // The backend update can take some time so we disable
243 // the controls while waiting.
244 enableControls(false);
245 gui_view_.tocModels().updateBackend();
249 void TocWidget::on_sortCB_stateChanged(int state)
251 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
256 void TocWidget::on_persistentCB_stateChanged(int state)
258 persistent_ = state == Qt::Checked;
262 /* FIXME (Ugras 17/11/06):
263 I have implemented a indexDepth function to get the model indices. In my
264 opinion, somebody should derive a new qvariant class for tocModelItem
265 which saves the string data and depth information. That will save the
266 depth calculation. */
268 static int indexDepth(QModelIndex const & index, int depth = -1)
271 return index.parent() == QModelIndex()
272 ? depth : indexDepth(index.parent(), depth);
276 void TocWidget::on_depthSL_valueChanged(int depth)
281 gui_view_.setFocus();
285 void TocWidget::setTreeDepth(int depth)
291 #if QT_VERSION >= 0x040300
292 // this should be faster than our own code below
294 tocTV->collapseAll();
296 tocTV->expandToDepth(depth - 1);
298 // expanding and then collapsing is probably better,
299 // but my qt 4.1.2 doesn't have expandAll()..
300 //tocTV->expandAll();
301 QModelIndexList indices = tocTV->model()->match(
302 tocTV->model()->index(0, 0),
303 Qt::DisplayRole, "*", -1,
304 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
306 int size = indices.size();
307 for (int i = 0; i < size; i++) {
308 QModelIndex index = indices[i];
309 tocTV->setExpanded(index, indexDepth(index) < depth_);
315 void TocWidget::on_typeCO_currentIndexChanged(int index)
317 current_type_ = typeCO->itemData(index).toString();
319 if (typeCO->hasFocus())
320 gui_view_.setFocus();
324 void TocWidget::outline(FuncCode func_code)
326 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
329 enableControls(false);
331 dispatch(FuncRequest(func_code));
332 enableControls(true);
333 gui_view_.setFocus();
337 void TocWidget::on_moveUpTB_clicked()
339 outline(LFUN_OUTLINE_UP);
343 void TocWidget::on_moveDownTB_clicked()
345 outline(LFUN_OUTLINE_DOWN);
349 void TocWidget::on_moveInTB_clicked()
351 outline(LFUN_OUTLINE_IN);
355 void TocWidget::on_moveOutTB_clicked()
357 outline(LFUN_OUTLINE_OUT);
361 void TocWidget::select(QModelIndex const & index)
363 if (!index.isValid()) {
364 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
368 tocTV->scrollTo(index);
369 tocTV->clearSelection();
370 tocTV->setCurrentIndex(index);
374 void TocWidget::enableControls(bool enable)
376 updateTB->setEnabled(enable);
381 moveUpTB->setEnabled(enable);
382 moveDownTB->setEnabled(enable);
383 moveInTB->setEnabled(enable);
384 moveOutTB->setEnabled(enable);
388 void TocWidget::updateView()
390 if (!gui_view_.documentBufferView()) {
391 enableControls(false);
392 typeCO->setEnabled(false);
394 tocTV->setEnabled(false);
395 depthSL->setMaximum(0);
396 depthSL->setValue(0);
397 persistentCB->setEnabled(false);
398 sortCB->setEnabled(false);
399 depthSL->setEnabled(false);
402 bool const is_sortable = isSortable();
403 sortCB->setEnabled(is_sortable);
404 depthSL->setEnabled(true);
405 typeCO->setEnabled(true);
406 bool focus_ = tocTV->hasFocus();
407 tocTV->setEnabled(false);
408 tocTV->setUpdatesEnabled(false);
410 QAbstractItemModel * toc_model =
411 gui_view_.tocModels().model(current_type_);
412 if (tocTV->model() != toc_model) {
413 tocTV->setModel(toc_model);
414 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
416 setTreeDepth(depth_);
419 sortCB->blockSignals(true);
420 sortCB->setChecked(is_sortable
421 && gui_view_.tocModels().isSorted(current_type_));
422 sortCB->blockSignals(false);
424 bool const can_navigate_ = canNavigate();
425 persistentCB->setEnabled(can_navigate_);
427 bool controls_enabled = toc_model && toc_model->rowCount() > 0
428 && !gui_view_.documentBufferView()->buffer().isReadonly();
429 enableControls(controls_enabled);
431 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
432 depthSL->setValue(depth_);
433 if (!persistent_ && can_navigate_)
434 setTreeDepth(depth_);
436 persistentCB->setChecked(persistent_);
437 select(gui_view_.tocModels().currentIndex(current_type_));
440 tocTV->setEnabled(true);
441 tocTV->setUpdatesEnabled(true);
447 void TocWidget::filterContents()
452 QModelIndexList indices = tocTV->model()->match(
453 tocTV->model()->index(0, 0),
454 Qt::DisplayRole, "*", -1,
455 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
457 int size = indices.size();
458 for (int i = 0; i < size; i++) {
459 QModelIndex index = indices[i];
461 index.data().toString().contains(
462 filterLE->text(), Qt::CaseInsensitive);
463 tocTV->setRowHidden(index.row(), index.parent(), !matches);
465 // recursively unhide parents of unhidden children
466 for (int i = size - 1; i >= 0; i--) {
467 QModelIndex index = indices[i];
468 if (!tocTV->isRowHidden(index.row(), index.parent())
469 && index.parent() != QModelIndex())
470 tocTV->setRowHidden(index.parent().row(),
471 index.parent().parent(), false);
476 static QString decodeType(QString const & str)
479 if (type.contains("tableofcontents")) {
480 type = "tableofcontents";
481 } else if (type.contains("floatlist")) {
482 if (type.contains("\"figure"))
484 else if (type.contains("\"table"))
486 else if (type.contains("\"algorithm"))
493 void TocWidget::init(QString const & str)
497 new_index = typeCO->findData(current_type_);
499 new_index = typeCO->findData(decodeType(str));
501 // If everything else fails, settle on the table of contents which is
502 // guaranted to exist.
503 if (new_index == -1) {
504 current_type_ = "tableofcontents";
505 new_index = typeCO->findData(current_type_);
507 current_type_ = typeCO->itemData(new_index).toString();
510 typeCO->blockSignals(true);
511 typeCO->setCurrentIndex(new_index);
512 typeCO->blockSignals(false);
515 } // namespace frontend
518 #include "moc_TocWidget.cpp"