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);
137 case LFUN_CHANGE_ACCEPT:
138 case LFUN_CHANGE_REJECT:
139 case LFUN_OUTLINE_UP:
140 case LFUN_OUTLINE_DOWN:
141 case LFUN_OUTLINE_IN:
142 case LFUN_OUTLINE_OUT:
143 status.setEnabled(true);
148 return inset->getStatus(cur, tmpcmd, status);
155 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
157 Inset * inset = itemInset();
158 FuncRequest tmpcmd(cmd);
160 QModelIndex const & index = tocTV->currentIndex();
161 TocItem const & item =
162 gui_view_.tocModels().currentItem(current_type_, index);
166 case LFUN_CHANGE_ACCEPT:
167 case LFUN_CHANGE_REJECT:
168 dispatch(item.action());
169 cur.dispatch(tmpcmd);
172 case LFUN_OUTLINE_UP:
173 case LFUN_OUTLINE_DOWN:
174 case LFUN_OUTLINE_IN:
175 case LFUN_OUTLINE_OUT:
181 inset->dispatch(cur, tmpcmd);
186 void TocWidget::on_tocTV_activated(QModelIndex const & index)
192 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
194 Qt::MouseButtons const button = QApplication::mouseButtons();
195 if (button & Qt::LeftButton) {
197 gui_view_.setFocus();
202 void TocWidget::goTo(QModelIndex const & index)
204 LYXERR(Debug::GUI, "goto " << index.row()
205 << ", " << index.column());
207 gui_view_.tocModels().goTo(current_type_, index);
211 void TocWidget::on_updateTB_clicked()
213 // The backend update can take some time so we disable
214 // the controls while waiting.
215 enableControls(false);
216 gui_view_.tocModels().updateBackend();
220 void TocWidget::on_sortCB_stateChanged(int state)
222 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
226 void TocWidget::on_persistentCB_stateChanged(int state)
228 persistent_ = state == Qt::Checked;
232 /* FIXME (Ugras 17/11/06):
233 I have implemented a indexDepth function to get the model indices. In my
234 opinion, somebody should derive a new qvariant class for tocModelItem
235 which saves the string data and depth information. That will save the
236 depth calculation. */
238 static int indexDepth(QModelIndex const & index, int depth = -1)
241 return index.parent() == QModelIndex()
242 ? depth : indexDepth(index.parent(), depth);
246 void TocWidget::on_depthSL_valueChanged(int depth)
251 gui_view_.setFocus();
255 void TocWidget::setTreeDepth(int depth)
261 #if QT_VERSION >= 0x040300
262 // this should be faster than our own code below
264 tocTV->collapseAll();
266 tocTV->expandToDepth(depth - 1);
268 // expanding and then collapsing is probably better,
269 // but my qt 4.1.2 doesn't have expandAll()..
270 //tocTV->expandAll();
271 QModelIndexList indices = tocTV->model()->match(
272 tocTV->model()->index(0, 0),
273 Qt::DisplayRole, "*", -1,
274 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
276 int size = indices.size();
277 for (int i = 0; i < size; i++) {
278 QModelIndex index = indices[i];
279 tocTV->setExpanded(index, indexDepth(index) < depth_);
285 void TocWidget::on_typeCO_currentIndexChanged(int index)
287 current_type_ = typeCO->itemData(index).toString();
289 gui_view_.setFocus();
293 void TocWidget::outline(int func_code)
295 enableControls(false);
296 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
299 enableControls(false);
301 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
302 enableControls(true);
303 gui_view_.setFocus();
307 void TocWidget::on_moveUpTB_clicked()
309 outline(LFUN_OUTLINE_UP);
313 void TocWidget::on_moveDownTB_clicked()
315 outline(LFUN_OUTLINE_DOWN);
319 void TocWidget::on_moveInTB_clicked()
321 outline(LFUN_OUTLINE_IN);
325 void TocWidget::on_moveOutTB_clicked()
327 outline(LFUN_OUTLINE_OUT);
331 void TocWidget::select(QModelIndex const & index)
333 if (!index.isValid()) {
334 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
338 tocTV->scrollTo(index);
339 tocTV->clearSelection();
340 tocTV->setCurrentIndex(index);
344 /// Test whether outlining operation is possible
345 static bool canOutline(QString const & type)
347 return type == "tableofcontents";
351 void TocWidget::enableControls(bool enable)
353 updateTB->setEnabled(enable);
355 if (!canOutline(current_type_))
358 moveUpTB->setEnabled(enable);
359 moveDownTB->setEnabled(enable);
360 moveInTB->setEnabled(enable);
361 moveOutTB->setEnabled(enable);
365 /// Test whether synchronized navigation is possible
366 static bool canNavigate(QString const & type)
368 // It is not possible to have synchronous navigation in a correct
369 // and efficient way with the label and change type because Toc::item()
370 // does a linear search. Even when fixed, it might even not be desirable
371 // to do so if we want to support drag&drop of labels and references.
372 return type != "label" && type != "change";
376 void TocWidget::updateView()
378 if (!gui_view_.view()) {
379 enableControls(false);
380 typeCO->setEnabled(false);
382 tocTV->setEnabled(false);
383 depthSL->setMaximum(0);
384 depthSL->setValue(0);
385 persistentCB->setEnabled(false);
386 sortCB->setEnabled(false);
387 depthSL->setEnabled(false);
390 sortCB->setEnabled(true);
391 depthSL->setEnabled(true);
392 typeCO->setEnabled(true);
393 tocTV->setEnabled(false);
394 tocTV->setUpdatesEnabled(false);
396 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
397 if (tocTV->model() != toc_model) {
398 tocTV->setModel(toc_model);
399 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
401 setTreeDepth(depth_);
404 sortCB->blockSignals(true);
405 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
406 sortCB->blockSignals(false);
409 bool const can_navigate_ = canNavigate(current_type_);
410 persistentCB->setEnabled(can_navigate_);
412 bool controls_enabled = toc_model && toc_model->rowCount() > 0
413 && !gui_view_.buffer()->isReadonly();
414 enableControls(controls_enabled);
416 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
417 depthSL->setValue(depth_);
418 if (!persistent_ && can_navigate_)
419 setTreeDepth(depth_);
421 persistentCB->setChecked(persistent_);
422 select(gui_view_.tocModels().currentIndex(current_type_));
424 tocTV->setEnabled(true);
425 tocTV->setUpdatesEnabled(true);
429 static QString decodeType(QString const & str)
432 if (type.contains("tableofcontents")) {
433 type = "tableofcontents";
434 } else if (type.contains("floatlist")) {
435 if (type.contains("\"figure"))
437 else if (type.contains("\"table"))
439 else if (type.contains("\"algorithm"))
446 void TocWidget::init(QString const & str)
450 new_index = typeCO->findData(current_type_);
452 new_index = typeCO->findData(decodeType(str));
454 // If everything else fails, settle on the table of contents which is
455 // guaranted to exist.
456 if (new_index == -1) {
457 current_type_ = "tableofcontents";
458 new_index = typeCO->findData(current_type_);
460 current_type_ = typeCO->itemData(new_index).toString();
463 typeCO->blockSignals(true);
464 typeCO->setCurrentIndex(new_index);
465 typeCO->blockSignals(false);
468 } // namespace frontend
471 #include "moc_TocWidget.cpp"