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 &)));
90 void TocWidget::showContextMenu(const QPoint & pos)
92 std::string name = "context-toc-" + fromqstr(current_type_);
93 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
96 menu->exec(mapToGlobal(pos));
100 Inset * TocWidget::itemInset() const
102 QModelIndex const & index = tocTV->currentIndex();
103 TocItem const & item =
104 gui_view_.tocModels().currentItem(current_type_, index);
105 DocIterator const & dit = item.dit();
108 if (current_type_ == "label"
109 || current_type_ == "graphics"
110 || current_type_ == "citation"
111 || current_type_ == "child")
112 inset = dit.nextInset();
114 else if (current_type_ == "branch"
115 || current_type_ == "index"
116 || current_type_ == "change")
117 inset = &dit.inset();
119 else if (current_type_ == "table"
120 || current_type_ == "listing"
121 || current_type_ == "figure") {
122 DocIterator tmp_dit(dit);
124 inset = &tmp_dit.inset();
130 bool TocWidget::getStatus(Cursor & cur, FuncRequest const & cmd,
131 FuncStatus & status) const
133 Inset * inset = itemInset();
134 FuncRequest tmpcmd(cmd);
136 QModelIndex const & index = tocTV->currentIndex();
137 TocItem const & item =
138 gui_view_.tocModels().currentItem(current_type_, index);
142 case LFUN_CHANGE_ACCEPT:
143 case LFUN_CHANGE_REJECT:
144 case LFUN_OUTLINE_UP:
145 case LFUN_OUTLINE_DOWN:
146 case LFUN_OUTLINE_IN:
147 case LFUN_OUTLINE_OUT:
148 case LFUN_SECTION_SELECT:
149 status.setEnabled(true);
152 case LFUN_LABEL_COPY_AS_REF: {
153 // For labels in math, we need to supply the label as a string
154 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
156 return inset->getStatus(cur, label_copy, status);
161 return inset->getStatus(cur, tmpcmd, status);
168 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
170 Inset * inset = itemInset();
171 FuncRequest tmpcmd(cmd);
173 QModelIndex const & index = tocTV->currentIndex();
174 TocItem const & item =
175 gui_view_.tocModels().currentItem(current_type_, index);
179 case LFUN_CHANGE_ACCEPT:
180 case LFUN_CHANGE_REJECT:
181 case LFUN_SECTION_SELECT:
182 dispatch(item.action());
183 cur.dispatch(tmpcmd);
186 case LFUN_LABEL_COPY_AS_REF: {
187 // For labels in math, we need to supply the label as a string
188 FuncRequest label_copy(LFUN_LABEL_COPY_AS_REF, item.asString());
190 inset->dispatch(cur, label_copy);
194 case LFUN_OUTLINE_UP:
195 case LFUN_OUTLINE_DOWN:
196 case LFUN_OUTLINE_IN:
197 case LFUN_OUTLINE_OUT:
203 inset->dispatch(cur, tmpcmd);
208 void TocWidget::on_tocTV_activated(QModelIndex const & index)
214 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
216 Qt::MouseButtons const button = QApplication::mouseButtons();
217 if (button & Qt::LeftButton) {
219 gui_view_.setFocus();
224 void TocWidget::goTo(QModelIndex const & index)
226 LYXERR(Debug::GUI, "goto " << index.row()
227 << ", " << index.column());
229 gui_view_.tocModels().goTo(current_type_, index);
233 void TocWidget::on_updateTB_clicked()
235 // The backend update can take some time so we disable
236 // the controls while waiting.
237 enableControls(false);
238 gui_view_.tocModels().updateBackend();
242 void TocWidget::on_sortCB_stateChanged(int state)
244 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
249 void TocWidget::on_persistentCB_stateChanged(int state)
251 persistent_ = state == Qt::Checked;
255 /* FIXME (Ugras 17/11/06):
256 I have implemented a indexDepth function to get the model indices. In my
257 opinion, somebody should derive a new qvariant class for tocModelItem
258 which saves the string data and depth information. That will save the
259 depth calculation. */
261 static int indexDepth(QModelIndex const & index, int depth = -1)
264 return index.parent() == QModelIndex()
265 ? depth : indexDepth(index.parent(), depth);
269 void TocWidget::on_depthSL_valueChanged(int depth)
274 gui_view_.setFocus();
278 void TocWidget::setTreeDepth(int depth)
284 #if QT_VERSION >= 0x040300
285 // this should be faster than our own code below
287 tocTV->collapseAll();
289 tocTV->expandToDepth(depth - 1);
291 // expanding and then collapsing is probably better,
292 // but my qt 4.1.2 doesn't have expandAll()..
293 //tocTV->expandAll();
294 QModelIndexList indices = tocTV->model()->match(
295 tocTV->model()->index(0, 0),
296 Qt::DisplayRole, "*", -1,
297 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
299 int size = indices.size();
300 for (int i = 0; i < size; i++) {
301 QModelIndex index = indices[i];
302 tocTV->setExpanded(index, indexDepth(index) < depth_);
308 void TocWidget::on_typeCO_currentIndexChanged(int index)
310 current_type_ = typeCO->itemData(index).toString();
312 gui_view_.setFocus();
316 void TocWidget::outline(int func_code)
318 enableControls(false);
319 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
322 enableControls(false);
324 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
325 enableControls(true);
326 gui_view_.setFocus();
330 void TocWidget::on_moveUpTB_clicked()
332 outline(LFUN_OUTLINE_UP);
336 void TocWidget::on_moveDownTB_clicked()
338 outline(LFUN_OUTLINE_DOWN);
342 void TocWidget::on_moveInTB_clicked()
344 outline(LFUN_OUTLINE_IN);
348 void TocWidget::on_moveOutTB_clicked()
350 outline(LFUN_OUTLINE_OUT);
354 void TocWidget::select(QModelIndex const & index)
356 if (!index.isValid()) {
357 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
361 tocTV->scrollTo(index);
362 tocTV->clearSelection();
363 tocTV->setCurrentIndex(index);
367 /// Test whether outlining operation is possible
368 static bool canOutline(QString const & type)
370 return type == "tableofcontents";
374 void TocWidget::enableControls(bool enable)
376 updateTB->setEnabled(enable);
378 if (!canOutline(current_type_))
381 moveUpTB->setEnabled(enable);
382 moveDownTB->setEnabled(enable);
383 moveInTB->setEnabled(enable);
384 moveOutTB->setEnabled(enable);
388 /// Test whether synchronized navigation is possible
389 static bool canNavigate(QString const & type)
391 // It is not possible to have synchronous navigation in a correct
392 // and efficient way with the label and change type because Toc::item()
393 // does a linear search. Even when fixed, it might even not be desirable
394 // to do so if we want to support drag&drop of labels and references.
395 return type != "label" && type != "change";
399 /// Test whether sorting is possible
400 static bool isSortable(QString const & type)
402 return type != "tableofcontents";
406 void TocWidget::updateView()
408 if (!gui_view_.documentBufferView()) {
409 enableControls(false);
410 typeCO->setEnabled(false);
412 tocTV->setEnabled(false);
413 depthSL->setMaximum(0);
414 depthSL->setValue(0);
415 persistentCB->setEnabled(false);
416 sortCB->setEnabled(false);
417 depthSL->setEnabled(false);
420 sortCB->setEnabled(isSortable(current_type_));
421 depthSL->setEnabled(true);
422 typeCO->setEnabled(true);
423 tocTV->setEnabled(false);
424 tocTV->setUpdatesEnabled(false);
426 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
427 if (tocTV->model() != toc_model) {
428 tocTV->setModel(toc_model);
429 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
431 setTreeDepth(depth_);
434 sortCB->blockSignals(true);
435 sortCB->setChecked(isSortable(current_type_)
436 && gui_view_.tocModels().isSorted(current_type_));
437 sortCB->blockSignals(false);
440 bool const can_navigate_ = canNavigate(current_type_);
441 persistentCB->setEnabled(can_navigate_);
443 bool controls_enabled = toc_model && toc_model->rowCount() > 0
444 && !gui_view_.documentBufferView()->buffer().isReadonly();
445 enableControls(controls_enabled);
447 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
448 depthSL->setValue(depth_);
449 if (!persistent_ && can_navigate_)
450 setTreeDepth(depth_);
452 persistentCB->setChecked(persistent_);
453 select(gui_view_.tocModels().currentIndex(current_type_));
455 tocTV->setEnabled(true);
456 tocTV->setUpdatesEnabled(true);
460 static QString decodeType(QString const & str)
463 if (type.contains("tableofcontents")) {
464 type = "tableofcontents";
465 } else if (type.contains("floatlist")) {
466 if (type.contains("\"figure"))
468 else if (type.contains("\"table"))
470 else if (type.contains("\"algorithm"))
477 void TocWidget::init(QString const & str)
481 new_index = typeCO->findData(current_type_);
483 new_index = typeCO->findData(decodeType(str));
485 // If everything else fails, settle on the table of contents which is
486 // guaranted to exist.
487 if (new_index == -1) {
488 current_type_ = "tableofcontents";
489 new_index = typeCO->findData(current_type_);
491 current_type_ = typeCO->itemData(new_index).toString();
494 typeCO->blockSignals(true);
495 typeCO->setCurrentIndex(new_index);
496 typeCO->blockSignals(false);
499 } // namespace frontend
502 #include "moc_TocWidget.cpp"