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)
319 current_type_ = typeCO->itemData(index).toString();
321 if (typeCO->hasFocus())
322 gui_view_.setFocus();
326 void TocWidget::outline(FuncCode func_code)
328 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
331 enableControls(false);
333 dispatch(FuncRequest(func_code));
334 enableControls(true);
335 gui_view_.setFocus();
339 void TocWidget::on_moveUpTB_clicked()
341 outline(LFUN_OUTLINE_UP);
345 void TocWidget::on_moveDownTB_clicked()
347 outline(LFUN_OUTLINE_DOWN);
351 void TocWidget::on_moveInTB_clicked()
353 outline(LFUN_OUTLINE_IN);
357 void TocWidget::on_moveOutTB_clicked()
359 outline(LFUN_OUTLINE_OUT);
363 void TocWidget::select(QModelIndex const & index)
365 if (!index.isValid()) {
366 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
370 tocTV->scrollTo(index);
371 tocTV->clearSelection();
372 tocTV->setCurrentIndex(index);
376 void TocWidget::enableControls(bool enable)
378 updateTB->setEnabled(enable);
383 moveUpTB->setEnabled(enable);
384 moveDownTB->setEnabled(enable);
385 moveInTB->setEnabled(enable);
386 moveOutTB->setEnabled(enable);
390 void TocWidget::updateView()
392 if (!gui_view_.documentBufferView()) {
394 depthSL->setMaximum(0);
395 depthSL->setValue(0);
400 bool const is_sortable = isSortable();
401 sortCB->setEnabled(is_sortable);
402 bool focus_ = tocTV->hasFocus();
403 tocTV->setEnabled(false);
404 tocTV->setUpdatesEnabled(false);
406 QAbstractItemModel * toc_model =
407 gui_view_.tocModels().model(current_type_);
408 if (tocTV->model() != toc_model) {
409 tocTV->setModel(toc_model);
410 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
412 setTreeDepth(depth_);
415 sortCB->blockSignals(true);
416 sortCB->setChecked(is_sortable
417 && gui_view_.tocModels().isSorted(current_type_));
418 sortCB->blockSignals(false);
420 bool const can_navigate_ = canNavigate();
421 persistentCB->setEnabled(can_navigate_);
423 bool controls_enabled = toc_model && toc_model->rowCount() > 0
424 && !gui_view_.documentBufferView()->buffer().isReadonly();
425 enableControls(controls_enabled);
427 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
428 depthSL->setValue(depth_);
429 if (!persistent_ && can_navigate_)
430 setTreeDepth(depth_);
432 persistentCB->setChecked(persistent_);
433 select(gui_view_.tocModels().currentIndex(current_type_));
436 tocTV->setEnabled(true);
437 tocTV->setUpdatesEnabled(true);
443 void TocWidget::filterContents()
448 QModelIndexList indices = tocTV->model()->match(
449 tocTV->model()->index(0, 0),
450 Qt::DisplayRole, "*", -1,
451 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
453 int size = indices.size();
454 for (int i = 0; i < size; i++) {
455 QModelIndex index = indices[i];
457 index.data().toString().contains(
458 filterLE->text(), Qt::CaseInsensitive);
459 tocTV->setRowHidden(index.row(), index.parent(), !matches);
461 // recursively unhide parents of unhidden children
462 for (int i = size - 1; i >= 0; i--) {
463 QModelIndex index = indices[i];
464 if (!tocTV->isRowHidden(index.row(), index.parent())
465 && index.parent() != QModelIndex())
466 tocTV->setRowHidden(index.parent().row(),
467 index.parent().parent(), false);
472 static QString decodeType(QString const & str)
475 if (type.contains("tableofcontents")) {
476 type = "tableofcontents";
477 } else if (type.contains("floatlist")) {
478 if (type.contains("\"figure"))
480 else if (type.contains("\"table"))
482 else if (type.contains("\"algorithm"))
489 void TocWidget::init(QString const & str)
493 new_index = typeCO->findData(current_type_);
495 new_index = typeCO->findData(decodeType(str));
497 // If everything else fails, settle on the table of contents which is
498 // guaranteed to exist.
499 if (new_index == -1) {
500 current_type_ = "tableofcontents";
501 new_index = typeCO->findData(current_type_);
503 current_type_ = typeCO->itemData(new_index).toString();
506 typeCO->blockSignals(true);
507 typeCO->setCurrentIndex(new_index);
508 typeCO->blockSignals(false);
511 } // namespace frontend
514 #include "moc_TocWidget.cpp"