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 "FuncRequest.h"
26 #include "support/debug.h"
27 #include "support/lassert.h"
29 #include <QHeaderView>
40 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
41 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
45 moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
46 moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
47 moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
48 moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
49 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "png")));
52 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
56 // hide the pointless QHeader for now
57 // in the future, new columns may appear
58 // like labels, bookmarks, etc...
59 // tocTV->header()->hide();
60 tocTV->header()->setVisible(false);
62 // Only one item selected at a time.
63 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
65 // The toc types combo won't change its model.
66 typeCO->setModel(gui_view_.tocModels().nameModel());
68 // Make sure the buttons are disabled when first shown without a loaded
70 enableControls(false);
72 // make us responsible for the context menu of the tabbar
73 setContextMenuPolicy(Qt::CustomContextMenu);
74 connect(this, SIGNAL(customContextMenuRequested(const QPoint &)),
75 this, SLOT(showContextMenu(const QPoint &)));
76 connect(tocTV, SIGNAL(customContextMenuRequested(const QPoint &)),
77 this, SLOT(showContextMenu(const QPoint &)));
83 void TocWidget::showContextMenu(const QPoint & pos)
85 std::string name = "context-toc-" + fromqstr(current_type_);
86 QMenu * menu = guiApp->menus().menu(toqstr(name), gui_view_);
89 menu->exec(mapToGlobal(pos));
93 void TocWidget::doDispatch(Cursor const & cur, FuncRequest const & cmd)
102 void TocWidget::on_tocTV_activated(QModelIndex const & index)
108 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
110 Qt::MouseButtons const button = QApplication::mouseButtons();
111 if (button & Qt::LeftButton) {
113 gui_view_.setFocus();
118 void TocWidget::goTo(QModelIndex const & index)
120 LYXERR(Debug::GUI, "goto " << index.row()
121 << ", " << index.column());
123 gui_view_.tocModels().goTo(current_type_, index);
127 void TocWidget::on_updateTB_clicked()
129 // The backend update can take some time so we disable
130 // the controls while waiting.
131 enableControls(false);
132 gui_view_.tocModels().updateBackend();
136 void TocWidget::on_sortCB_stateChanged(int state)
138 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
142 void TocWidget::on_persistentCB_stateChanged(int state)
144 persistent_ = state == Qt::Checked;
148 /* FIXME (Ugras 17/11/06):
149 I have implemented a indexDepth function to get the model indices. In my
150 opinion, somebody should derive a new qvariant class for tocModelItem
151 which saves the string data and depth information. That will save the
152 depth calculation. */
154 static int indexDepth(QModelIndex const & index, int depth = -1)
157 return index.parent() == QModelIndex()
158 ? depth : indexDepth(index.parent(), depth);
162 void TocWidget::on_depthSL_valueChanged(int depth)
167 gui_view_.setFocus();
171 void TocWidget::setTreeDepth(int depth)
177 #if QT_VERSION >= 0x040300
178 // this should be faster than our own code below
180 tocTV->collapseAll();
182 tocTV->expandToDepth(depth - 1);
184 // expanding and then collapsing is probably better,
185 // but my qt 4.1.2 doesn't have expandAll()..
186 //tocTV->expandAll();
187 QModelIndexList indices = tocTV->model()->match(
188 tocTV->model()->index(0, 0),
189 Qt::DisplayRole, "*", -1,
190 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
192 int size = indices.size();
193 for (int i = 0; i < size; i++) {
194 QModelIndex index = indices[i];
195 tocTV->setExpanded(index, indexDepth(index) < depth_);
201 void TocWidget::on_typeCO_currentIndexChanged(int index)
203 current_type_ = typeCO->itemData(index).toString();
205 gui_view_.setFocus();
209 void TocWidget::outline(int func_code)
211 enableControls(false);
212 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
215 enableControls(false);
217 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
218 enableControls(true);
219 gui_view_.setFocus();
223 void TocWidget::on_moveUpTB_clicked()
225 outline(LFUN_OUTLINE_UP);
229 void TocWidget::on_moveDownTB_clicked()
231 outline(LFUN_OUTLINE_DOWN);
235 void TocWidget::on_moveInTB_clicked()
237 outline(LFUN_OUTLINE_IN);
241 void TocWidget::on_moveOutTB_clicked()
243 outline(LFUN_OUTLINE_OUT);
247 void TocWidget::select(QModelIndex const & index)
249 if (!index.isValid()) {
250 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
254 tocTV->scrollTo(index);
255 tocTV->clearSelection();
256 tocTV->setCurrentIndex(index);
260 /// Test if outlining operation is possible
261 static bool canOutline(QString const & type)
263 return type == "tableofcontents";
267 void TocWidget::enableControls(bool enable)
269 updateTB->setEnabled(enable);
271 if (!canOutline(current_type_))
274 moveUpTB->setEnabled(enable);
275 moveDownTB->setEnabled(enable);
276 moveInTB->setEnabled(enable);
277 moveOutTB->setEnabled(enable);
279 depthSL->setMaximum(0);
280 depthSL->setValue(0);
285 /// Test if synchronized navigation is possible
286 static bool canNavigate(QString const & type)
288 // It is not possible to have synchronous navigation in a correctl
289 // and efficient way with the label type because Toc::item() do a linear
290 // seatch. Even if fixed, it might even not be desirable to do so if we
291 // want to support drag&drop of labels and references.
292 return type != "label" && type != "change";
296 void TocWidget::updateView()
298 if (!gui_view_.view()) {
299 enableControls(false);
300 typeCO->setEnabled(false);
302 tocTV->setEnabled(false);
305 typeCO->setEnabled(true);
306 tocTV->setEnabled(false);
307 tocTV->setUpdatesEnabled(false);
309 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
310 if (tocTV->model() != toc_model) {
311 tocTV->setModel(toc_model);
312 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
314 setTreeDepth(depth_);
317 sortCB->blockSignals(true);
318 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
319 sortCB->blockSignals(false);
321 persistentCB->setChecked(persistent_);
323 bool controls_enabled = toc_model && toc_model->rowCount() > 0
324 && !gui_view_.buffer()->isReadonly();
325 enableControls(controls_enabled);
327 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
328 depthSL->setValue(depth_);
330 setTreeDepth(depth_);
331 if (canNavigate(current_type_))
332 select(gui_view_.tocModels().currentIndex(current_type_));
333 tocTV->setEnabled(true);
334 tocTV->setUpdatesEnabled(true);
338 static QString decodeType(QString const & str)
341 if (type.contains("tableofcontents")) {
342 type = "tableofcontents";
343 } else if (type.contains("floatlist")) {
344 if (type.contains("\"figure"))
346 else if (type.contains("\"table"))
348 else if (type.contains("\"algorithm"))
355 void TocWidget::init(QString const & str)
359 new_index = typeCO->findData(current_type_);
361 new_index = typeCO->findData(decodeType(str));
363 // If everything else fails, settle on the table of contents which is
364 // guaranted to exist.
365 if (new_index == -1) {
366 current_type_ = "tableofcontents";
367 new_index = typeCO->findData(current_type_);
369 current_type_ = typeCO->itemData(new_index).toString();
372 typeCO->blockSignals(true);
373 typeCO->setCurrentIndex(new_index);
374 typeCO->blockSignals(false);
377 } // namespace frontend
380 #include "moc_TocWidget.cpp"