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"
26 #include "TocBackend.h"
28 #include "insets/InsetCommand.h"
29 #include "insets/InsetRef.h"
31 #include "support/debug.h"
32 #include "support/lassert.h"
34 #include <QHeaderView>
45 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
46 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
50 moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
51 moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
52 moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
53 moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
54 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "png")));
57 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
61 // hide the pointless QHeader for now
62 // in the future, new columns may appear
63 // like labels, bookmarks, etc...
64 // tocTV->header()->hide();
65 tocTV->header()->setVisible(false);
67 // Only one item selected at a time.
68 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
70 // The toc types combo won't change its model.
71 typeCO->setModel(gui_view_.tocModels().nameModel());
73 // Make sure the buttons are disabled when first shown without a loaded
75 enableControls(false);
77 // make us responsible for the context menu of the tabbar
78 setContextMenuPolicy(Qt::CustomContextMenu);
79 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
80 this, SLOT(showContextMenu(const QPoint &)));
81 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
82 this, SLOT(showContextMenu(const QPoint &)));
88 void TocWidget::showContextMenu(const QPoint & pos)
90 std::string name = "context-toc-" + fromqstr(current_type_);
91 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
94 menu->exec(mapToGlobal(pos));
98 void TocWidget::doDispatch(Cursor & cur, FuncRequest const & cmd)
100 QModelIndex const & index = tocTV->currentIndex();
101 TocItem const & item =
102 gui_view_.tocModels().currentItem(current_type_, index);
103 DocIterator const & dit = item.dit();
106 if (current_type_ == "label"
107 || current_type_ == "graphics"
108 || current_type_ == "citation")
109 inset = dit.nextInset();
111 else if (current_type_ == "branch")
112 inset = &dit.inset();
114 else if (current_type_ == "table" || current_type_ == "figure") {
115 DocIterator tmp_dit(dit);
117 inset = &tmp_dit.inset();
120 FuncRequest tmpcmd(cmd);
122 inset->dispatch(cur, tmpcmd);
126 void TocWidget::on_tocTV_activated(QModelIndex const & index)
132 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
134 Qt::MouseButtons const button = QApplication::mouseButtons();
135 if (button & Qt::LeftButton) {
137 gui_view_.setFocus();
142 void TocWidget::goTo(QModelIndex const & index)
144 LYXERR(Debug::GUI, "goto " << index.row()
145 << ", " << index.column());
147 gui_view_.tocModels().goTo(current_type_, index);
151 void TocWidget::on_updateTB_clicked()
153 // The backend update can take some time so we disable
154 // the controls while waiting.
155 enableControls(false);
156 gui_view_.tocModels().updateBackend();
160 void TocWidget::on_sortCB_stateChanged(int state)
162 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
166 void TocWidget::on_persistentCB_stateChanged(int state)
168 persistent_ = state == Qt::Checked;
172 /* FIXME (Ugras 17/11/06):
173 I have implemented a indexDepth function to get the model indices. In my
174 opinion, somebody should derive a new qvariant class for tocModelItem
175 which saves the string data and depth information. That will save the
176 depth calculation. */
178 static int indexDepth(QModelIndex const & index, int depth = -1)
181 return index.parent() == QModelIndex()
182 ? depth : indexDepth(index.parent(), depth);
186 void TocWidget::on_depthSL_valueChanged(int depth)
191 gui_view_.setFocus();
195 void TocWidget::setTreeDepth(int depth)
201 #if QT_VERSION >= 0x040300
202 // this should be faster than our own code below
204 tocTV->collapseAll();
206 tocTV->expandToDepth(depth - 1);
208 // expanding and then collapsing is probably better,
209 // but my qt 4.1.2 doesn't have expandAll()..
210 //tocTV->expandAll();
211 QModelIndexList indices = tocTV->model()->match(
212 tocTV->model()->index(0, 0),
213 Qt::DisplayRole, "*", -1,
214 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
216 int size = indices.size();
217 for (int i = 0; i < size; i++) {
218 QModelIndex index = indices[i];
219 tocTV->setExpanded(index, indexDepth(index) < depth_);
225 void TocWidget::on_typeCO_currentIndexChanged(int index)
227 current_type_ = typeCO->itemData(index).toString();
229 gui_view_.setFocus();
233 void TocWidget::outline(int func_code)
235 enableControls(false);
236 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
239 enableControls(false);
241 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
242 enableControls(true);
243 gui_view_.setFocus();
247 void TocWidget::on_moveUpTB_clicked()
249 outline(LFUN_OUTLINE_UP);
253 void TocWidget::on_moveDownTB_clicked()
255 outline(LFUN_OUTLINE_DOWN);
259 void TocWidget::on_moveInTB_clicked()
261 outline(LFUN_OUTLINE_IN);
265 void TocWidget::on_moveOutTB_clicked()
267 outline(LFUN_OUTLINE_OUT);
271 void TocWidget::select(QModelIndex const & index)
273 if (!index.isValid()) {
274 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
278 tocTV->scrollTo(index);
279 tocTV->clearSelection();
280 tocTV->setCurrentIndex(index);
284 /// Test if outlining operation is possible
285 static bool canOutline(QString const & type)
287 return type == "tableofcontents";
291 void TocWidget::enableControls(bool enable)
293 updateTB->setEnabled(enable);
295 if (!canOutline(current_type_))
298 moveUpTB->setEnabled(enable);
299 moveDownTB->setEnabled(enable);
300 moveInTB->setEnabled(enable);
301 moveOutTB->setEnabled(enable);
303 depthSL->setMaximum(0);
304 depthSL->setValue(0);
309 /// Test if synchronized navigation is possible
310 static bool canNavigate(QString const & type)
312 // It is not possible to have synchronous navigation in a correctl
313 // and efficient way with the label type because Toc::item() do a linear
314 // seatch. Even if fixed, it might even not be desirable to do so if we
315 // want to support drag&drop of labels and references.
316 return type != "label" && type != "change";
320 void TocWidget::updateView()
322 if (!gui_view_.view()) {
323 enableControls(false);
324 typeCO->setEnabled(false);
326 tocTV->setEnabled(false);
329 typeCO->setEnabled(true);
330 tocTV->setEnabled(false);
331 tocTV->setUpdatesEnabled(false);
333 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
334 if (tocTV->model() != toc_model) {
335 tocTV->setModel(toc_model);
336 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
338 setTreeDepth(depth_);
341 sortCB->blockSignals(true);
342 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
343 sortCB->blockSignals(false);
345 persistentCB->setChecked(persistent_);
347 bool controls_enabled = toc_model && toc_model->rowCount() > 0
348 && !gui_view_.buffer()->isReadonly();
349 enableControls(controls_enabled);
351 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
352 depthSL->setValue(depth_);
354 setTreeDepth(depth_);
355 if (canNavigate(current_type_))
356 select(gui_view_.tocModels().currentIndex(current_type_));
357 tocTV->setEnabled(true);
358 tocTV->setUpdatesEnabled(true);
362 static QString decodeType(QString const & str)
365 if (type.contains("tableofcontents")) {
366 type = "tableofcontents";
367 } else if (type.contains("floatlist")) {
368 if (type.contains("\"figure"))
370 else if (type.contains("\"table"))
372 else if (type.contains("\"algorithm"))
379 void TocWidget::init(QString const & str)
383 new_index = typeCO->findData(current_type_);
385 new_index = typeCO->findData(decodeType(str));
387 // If everything else fails, settle on the table of contents which is
388 // guaranted to exist.
389 if (new_index == -1) {
390 current_type_ = "tableofcontents";
391 new_index = typeCO->findData(current_type_);
393 current_type_ = typeCO->itemData(new_index).toString();
396 typeCO->blockSignals(true);
397 typeCO->setCurrentIndex(new_index);
398 typeCO->blockSignals(false);
401 } // namespace frontend
404 #include "moc_TocWidget.cpp"