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 "CutAndPaste.h"
23 #include "FuncRequest.h"
24 #include "FuncStatus.h"
27 #include "TocBackend.h"
29 #include "insets/InsetCommand.h"
30 #include "insets/InsetRef.h"
32 #include "support/debug.h"
33 #include "support/lassert.h"
35 #include <QHeaderView>
46 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
47 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
51 moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
52 moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
53 moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
54 moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
55 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "png")));
58 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
62 // hide the pointless QHeader for now
63 // in the future, new columns may appear
64 // like labels, bookmarks, etc...
65 // tocTV->header()->hide();
66 tocTV->header()->setVisible(false);
68 // Only one item selected at a time.
69 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
71 // The toc types combo won't change its model.
72 typeCO->setModel(gui_view_.tocModels().nameModel());
74 // Make sure the buttons are disabled when first shown without a loaded
76 enableControls(false);
78 // make us responsible for the context menu of the tabbar
79 setContextMenuPolicy(Qt::CustomContextMenu);
80 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
81 this, SLOT(showContextMenu(const QPoint &)));
82 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
83 this, SLOT(showContextMenu(const QPoint &)));
89 void TocWidget::showContextMenu(const QPoint & pos)
91 std::string name = "context-toc-" + fromqstr(current_type_);
92 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
95 menu->exec(mapToGlobal(pos));
99 Inset * TocWidget::itemInset() const
101 QModelIndex const & index = tocTV->currentIndex();
102 TocItem const & item =
103 gui_view_.tocModels().currentItem(current_type_, index);
104 DocIterator const & dit = item.dit();
107 if (current_type_ == "label"
108 || current_type_ == "graphics"
109 || current_type_ == "citation"
110 || current_type_ == "child")
111 inset = dit.nextInset();
113 else if (current_type_ == "branch"
114 || current_type_ == "index"
115 || current_type_ == "change")
116 inset = &dit.inset();
118 else if (current_type_ == "table"
119 || current_type_ == "listing"
120 || current_type_ == "figure") {
121 DocIterator tmp_dit(dit);
123 inset = &tmp_dit.inset();
129 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
130 FuncStatus & status) const
132 Inset * inset = itemInset();
133 FuncRequest tmpcmd(cmd);
135 QModelIndex const & index = tocTV->currentIndex();
136 TocItem const & item =
137 gui_view_.tocModels().currentItem(current_type_, index);
141 case LFUN_CHANGE_ACCEPT:
142 case LFUN_CHANGE_REJECT:
143 case LFUN_OUTLINE_UP:
144 case LFUN_OUTLINE_DOWN:
145 case LFUN_OUTLINE_IN:
146 case LFUN_OUTLINE_OUT:
147 case LFUN_SECTION_SELECT:
148 status.setEnabled(true);
151 case LFUN_LABEL_COPY_AS_REF: {
152 // For labels in math, we need to supply the label as a string
153 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
155 return inset->getStatus(cur, label_copy, status);
160 return inset->getStatus(cur, tmpcmd, status);
167 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
169 Inset * inset = itemInset();
170 FuncRequest tmpcmd(cmd);
172 QModelIndex const & index = tocTV->currentIndex();
173 TocItem const & item =
174 gui_view_.tocModels().currentItem(current_type_, index);
178 case LFUN_CHANGE_ACCEPT:
179 case LFUN_CHANGE_REJECT:
180 case LFUN_SECTION_SELECT:
181 dispatch(item.action());
182 cur.dispatch(tmpcmd);
185 case LFUN_LABEL_COPY_AS_REF: {
186 // For labels in math, we need to supply the label as a string
187 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
189 inset->dispatch(cur, label_copy);
193 case LFUN_OUTLINE_UP:
194 case LFUN_OUTLINE_DOWN:
195 case LFUN_OUTLINE_IN:
196 case LFUN_OUTLINE_OUT:
202 inset->dispatch(cur, tmpcmd);
207 void TocWidget::on_tocTV_activated(QModelIndex const & index)
213 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
215 Qt::MouseButtons const button = QApplication::mouseButtons();
216 if (button & Qt::LeftButton) {
218 gui_view_.setFocus();
223 void TocWidget::goTo(QModelIndex const & index)
225 LYXERR(Debug::GUI, "goto " << index.row()
226 << ", " << index.column());
228 gui_view_.tocModels().goTo(current_type_, index);
232 void TocWidget::on_updateTB_clicked()
234 // The backend update can take some time so we disable
235 // the controls while waiting.
236 enableControls(false);
237 gui_view_.tocModels().updateBackend();
241 void TocWidget::on_sortCB_stateChanged(int state)
243 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
247 void TocWidget::on_persistentCB_stateChanged(int state)
249 persistent_ = state == Qt::Checked;
253 /* FIXME (Ugras 17/11/06):
254 I have implemented a indexDepth function to get the model indices. In my
255 opinion, somebody should derive a new qvariant class for tocModelItem
256 which saves the string data and depth information. That will save the
257 depth calculation. */
259 static int indexDepth(QModelIndex const & index, int depth = -1)
262 return index.parent() == QModelIndex()
263 ? depth : indexDepth(index.parent(), depth);
267 void TocWidget::on_depthSL_valueChanged(int depth)
272 gui_view_.setFocus();
276 void TocWidget::setTreeDepth(int depth)
282 #if QT_VERSION >= 0x040300
283 // this should be faster than our own code below
285 tocTV->collapseAll();
287 tocTV->expandToDepth(depth - 1);
289 // expanding and then collapsing is probably better,
290 // but my qt 4.1.2 doesn't have expandAll()..
291 //tocTV->expandAll();
292 QModelIndexList indices = tocTV->model()->match(
293 tocTV->model()->index(0, 0),
294 Qt::DisplayRole, "*", -1,
295 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
297 int size = indices.size();
298 for (int i = 0; i < size; i++) {
299 QModelIndex index = indices[i];
300 tocTV->setExpanded(index, indexDepth(index) < depth_);
306 void TocWidget::on_typeCO_currentIndexChanged(int index)
308 current_type_ = typeCO->itemData(index).toString();
310 gui_view_.setFocus();
314 void TocWidget::outline(int func_code)
316 enableControls(false);
317 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
320 enableControls(false);
322 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
323 enableControls(true);
324 gui_view_.setFocus();
328 void TocWidget::on_moveUpTB_clicked()
330 outline(LFUN_OUTLINE_UP);
334 void TocWidget::on_moveDownTB_clicked()
336 outline(LFUN_OUTLINE_DOWN);
340 void TocWidget::on_moveInTB_clicked()
342 outline(LFUN_OUTLINE_IN);
346 void TocWidget::on_moveOutTB_clicked()
348 outline(LFUN_OUTLINE_OUT);
352 void TocWidget::select(QModelIndex const & index)
354 if (!index.isValid()) {
355 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
359 tocTV->scrollTo(index);
360 tocTV->clearSelection();
361 tocTV->setCurrentIndex(index);
365 /// Test whether outlining operation is possible
366 static bool canOutline(QString const & type)
368 return type == "tableofcontents";
372 void TocWidget::enableControls(bool enable)
374 updateTB->setEnabled(enable);
376 if (!canOutline(current_type_))
379 moveUpTB->setEnabled(enable);
380 moveDownTB->setEnabled(enable);
381 moveInTB->setEnabled(enable);
382 moveOutTB->setEnabled(enable);
386 /// Test whether synchronized navigation is possible
387 static bool canNavigate(QString const & type)
389 // It is not possible to have synchronous navigation in a correct
390 // and efficient way with the label and change type because Toc::item()
391 // does a linear search. Even when fixed, it might even not be desirable
392 // to do so if we want to support drag&drop of labels and references.
393 return type != "label" && type != "change";
397 void TocWidget::updateView()
399 if (!gui_view_.view()) {
400 enableControls(false);
401 typeCO->setEnabled(false);
403 tocTV->setEnabled(false);
404 depthSL->setMaximum(0);
405 depthSL->setValue(0);
406 persistentCB->setEnabled(false);
407 sortCB->setEnabled(false);
408 depthSL->setEnabled(false);
411 sortCB->setEnabled(true);
412 depthSL->setEnabled(true);
413 typeCO->setEnabled(true);
414 tocTV->setEnabled(false);
415 tocTV->setUpdatesEnabled(false);
417 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
418 if (tocTV->model() != toc_model) {
419 tocTV->setModel(toc_model);
420 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
422 setTreeDepth(depth_);
425 sortCB->blockSignals(true);
426 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
427 sortCB->blockSignals(false);
430 bool const can_navigate_ = canNavigate(current_type_);
431 persistentCB->setEnabled(can_navigate_);
433 bool controls_enabled = toc_model && toc_model->rowCount() > 0
434 && !gui_view_.buffer()->isReadonly();
435 enableControls(controls_enabled);
437 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
438 depthSL->setValue(depth_);
439 if (!persistent_ && can_navigate_)
440 setTreeDepth(depth_);
442 persistentCB->setChecked(persistent_);
443 select(gui_view_.tocModels().currentIndex(current_type_));
445 tocTV->setEnabled(true);
446 tocTV->setUpdatesEnabled(true);
450 static QString decodeType(QString const & str)
453 if (type.contains("tableofcontents")) {
454 type = "tableofcontents";
455 } else if (type.contains("floatlist")) {
456 if (type.contains("\"figure"))
458 else if (type.contains("\"table"))
460 else if (type.contains("\"algorithm"))
467 void TocWidget::init(QString const & str)
471 new_index = typeCO->findData(current_type_);
473 new_index = typeCO->findData(decodeType(str));
475 // If everything else fails, settle on the table of contents which is
476 // guaranted to exist.
477 if (new_index == -1) {
478 current_type_ = "tableofcontents";
479 new_index = typeCO->findData(current_type_);
481 current_type_ = typeCO->itemData(new_index).toString();
484 typeCO->blockSignals(true);
485 typeCO->setCurrentIndex(new_index);
486 typeCO->blockSignals(false);
489 } // namespace frontend
492 #include "moc_TocWidget.cpp"