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 status.setEnabled(true);
144 return inset->getStatus(cur, tmpcmd, status);
151 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
153 Inset * inset = itemInset();
154 FuncRequest tmpcmd(cmd);
156 QModelIndex const & index = tocTV->currentIndex();
157 TocItem const & item =
158 gui_view_.tocModels().currentItem(current_type_, index);
162 case LFUN_CHANGE_ACCEPT:
163 case LFUN_CHANGE_REJECT:
164 dispatch(item.action());
165 cur.dispatch(tmpcmd);
170 inset->dispatch(cur, tmpcmd);
175 void TocWidget::on_tocTV_activated(QModelIndex const & index)
181 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
183 Qt::MouseButtons const button = QApplication::mouseButtons();
184 if (button & Qt::LeftButton) {
186 gui_view_.setFocus();
191 void TocWidget::goTo(QModelIndex const & index)
193 LYXERR(Debug::GUI, "goto " << index.row()
194 << ", " << index.column());
196 gui_view_.tocModels().goTo(current_type_, index);
200 void TocWidget::on_updateTB_clicked()
202 // The backend update can take some time so we disable
203 // the controls while waiting.
204 enableControls(false);
205 gui_view_.tocModels().updateBackend();
209 void TocWidget::on_sortCB_stateChanged(int state)
211 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
215 void TocWidget::on_persistentCB_stateChanged(int state)
217 persistent_ = state == Qt::Checked;
221 /* FIXME (Ugras 17/11/06):
222 I have implemented a indexDepth function to get the model indices. In my
223 opinion, somebody should derive a new qvariant class for tocModelItem
224 which saves the string data and depth information. That will save the
225 depth calculation. */
227 static int indexDepth(QModelIndex const & index, int depth = -1)
230 return index.parent() == QModelIndex()
231 ? depth : indexDepth(index.parent(), depth);
235 void TocWidget::on_depthSL_valueChanged(int depth)
240 gui_view_.setFocus();
244 void TocWidget::setTreeDepth(int depth)
250 #if QT_VERSION >= 0x040300
251 // this should be faster than our own code below
253 tocTV->collapseAll();
255 tocTV->expandToDepth(depth - 1);
257 // expanding and then collapsing is probably better,
258 // but my qt 4.1.2 doesn't have expandAll()..
259 //tocTV->expandAll();
260 QModelIndexList indices = tocTV->model()->match(
261 tocTV->model()->index(0, 0),
262 Qt::DisplayRole, "*", -1,
263 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
265 int size = indices.size();
266 for (int i = 0; i < size; i++) {
267 QModelIndex index = indices[i];
268 tocTV->setExpanded(index, indexDepth(index) < depth_);
274 void TocWidget::on_typeCO_currentIndexChanged(int index)
276 current_type_ = typeCO->itemData(index).toString();
278 gui_view_.setFocus();
282 void TocWidget::outline(int func_code)
284 enableControls(false);
285 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
288 enableControls(false);
290 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
291 enableControls(true);
292 gui_view_.setFocus();
296 void TocWidget::on_moveUpTB_clicked()
298 outline(LFUN_OUTLINE_UP);
302 void TocWidget::on_moveDownTB_clicked()
304 outline(LFUN_OUTLINE_DOWN);
308 void TocWidget::on_moveInTB_clicked()
310 outline(LFUN_OUTLINE_IN);
314 void TocWidget::on_moveOutTB_clicked()
316 outline(LFUN_OUTLINE_OUT);
320 void TocWidget::select(QModelIndex const & index)
322 if (!index.isValid()) {
323 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
327 tocTV->scrollTo(index);
328 tocTV->clearSelection();
329 tocTV->setCurrentIndex(index);
333 /// Test whether outlining operation is possible
334 static bool canOutline(QString const & type)
336 return type == "tableofcontents";
340 void TocWidget::enableControls(bool enable)
342 updateTB->setEnabled(enable);
344 if (!canOutline(current_type_))
347 moveUpTB->setEnabled(enable);
348 moveDownTB->setEnabled(enable);
349 moveInTB->setEnabled(enable);
350 moveOutTB->setEnabled(enable);
354 /// Test whether synchronized navigation is possible
355 static bool canNavigate(QString const & type)
357 // It is not possible to have synchronous navigation in a correct
358 // and efficient way with the label and change type because Toc::item()
359 // does a linear search. Even when fixed, it might even not be desirable
360 // to do so if we want to support drag&drop of labels and references.
361 return type != "label" && type != "change";
365 void TocWidget::updateView()
367 if (!gui_view_.view()) {
368 enableControls(false);
369 typeCO->setEnabled(false);
371 tocTV->setEnabled(false);
372 depthSL->setMaximum(0);
373 depthSL->setValue(0);
374 persistentCB->setEnabled(false);
375 sortCB->setEnabled(false);
376 depthSL->setEnabled(false);
379 sortCB->setEnabled(true);
380 depthSL->setEnabled(true);
381 typeCO->setEnabled(true);
382 tocTV->setEnabled(false);
383 tocTV->setUpdatesEnabled(false);
385 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
386 if (tocTV->model() != toc_model) {
387 tocTV->setModel(toc_model);
388 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
390 setTreeDepth(depth_);
393 sortCB->blockSignals(true);
394 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
395 sortCB->blockSignals(false);
398 bool const can_navigate_ = canNavigate(current_type_);
399 persistentCB->setEnabled(can_navigate_);
401 bool controls_enabled = toc_model && toc_model->rowCount() > 0
402 && !gui_view_.buffer()->isReadonly();
403 enableControls(controls_enabled);
405 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
406 depthSL->setValue(depth_);
407 if (!persistent_ && can_navigate_)
408 setTreeDepth(depth_);
410 persistentCB->setChecked(persistent_);
411 select(gui_view_.tocModels().currentIndex(current_type_));
413 tocTV->setEnabled(true);
414 tocTV->setUpdatesEnabled(true);
418 static QString decodeType(QString const & str)
421 if (type.contains("tableofcontents")) {
422 type = "tableofcontents";
423 } else if (type.contains("floatlist")) {
424 if (type.contains("\"figure"))
426 else if (type.contains("\"table"))
428 else if (type.contains("\"algorithm"))
435 void TocWidget::init(QString const & str)
439 new_index = typeCO->findData(current_type_);
441 new_index = typeCO->findData(decodeType(str));
443 // If everything else fails, settle on the table of contents which is
444 // guaranted to exist.
445 if (new_index == -1) {
446 current_type_ = "tableofcontents";
447 new_index = typeCO->findData(current_type_);
449 current_type_ = typeCO->itemData(new_index).toString();
452 typeCO->blockSignals(true);
453 typeCO->setCurrentIndex(new_index);
454 typeCO->blockSignals(false);
457 } // namespace frontend
460 #include "moc_TocWidget.cpp"