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 const & cur, FuncRequest const & cmd)
101 case LFUN_COPY_LABEL_AS_REF: {
102 QModelIndex index = tocTV->currentIndex();
103 TocItem const & item =
104 gui_view_.tocModels().currentItem(current_type_, index);
105 if (!item.str().empty()) {
106 InsetCommandParams p(REF_CODE, "ref");
107 p["reference"] = item.str();
108 cap::clearSelection();
109 cap::copyInset(cur, new InsetRef(*cur.buffer(), p), item.str());
120 void TocWidget::on_tocTV_activated(QModelIndex const & index)
126 void TocWidget::on_tocTV_pressed(QModelIndex const & index)
128 Qt::MouseButtons const button = QApplication::mouseButtons();
129 if (button & Qt::LeftButton) {
131 gui_view_.setFocus();
136 void TocWidget::goTo(QModelIndex const & index)
138 LYXERR(Debug::GUI, "goto " << index.row()
139 << ", " << index.column());
141 gui_view_.tocModels().goTo(current_type_, index);
145 void TocWidget::on_updateTB_clicked()
147 // The backend update can take some time so we disable
148 // the controls while waiting.
149 enableControls(false);
150 gui_view_.tocModels().updateBackend();
154 void TocWidget::on_sortCB_stateChanged(int state)
156 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
160 void TocWidget::on_persistentCB_stateChanged(int state)
162 persistent_ = state == Qt::Checked;
166 /* FIXME (Ugras 17/11/06):
167 I have implemented a indexDepth function to get the model indices. In my
168 opinion, somebody should derive a new qvariant class for tocModelItem
169 which saves the string data and depth information. That will save the
170 depth calculation. */
172 static int indexDepth(QModelIndex const & index, int depth = -1)
175 return index.parent() == QModelIndex()
176 ? depth : indexDepth(index.parent(), depth);
180 void TocWidget::on_depthSL_valueChanged(int depth)
185 gui_view_.setFocus();
189 void TocWidget::setTreeDepth(int depth)
195 #if QT_VERSION >= 0x040300
196 // this should be faster than our own code below
198 tocTV->collapseAll();
200 tocTV->expandToDepth(depth - 1);
202 // expanding and then collapsing is probably better,
203 // but my qt 4.1.2 doesn't have expandAll()..
204 //tocTV->expandAll();
205 QModelIndexList indices = tocTV->model()->match(
206 tocTV->model()->index(0, 0),
207 Qt::DisplayRole, "*", -1,
208 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
210 int size = indices.size();
211 for (int i = 0; i < size; i++) {
212 QModelIndex index = indices[i];
213 tocTV->setExpanded(index, indexDepth(index) < depth_);
219 void TocWidget::on_typeCO_currentIndexChanged(int index)
221 current_type_ = typeCO->itemData(index).toString();
223 gui_view_.setFocus();
227 void TocWidget::outline(int func_code)
229 enableControls(false);
230 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
233 enableControls(false);
235 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
236 enableControls(true);
237 gui_view_.setFocus();
241 void TocWidget::on_moveUpTB_clicked()
243 outline(LFUN_OUTLINE_UP);
247 void TocWidget::on_moveDownTB_clicked()
249 outline(LFUN_OUTLINE_DOWN);
253 void TocWidget::on_moveInTB_clicked()
255 outline(LFUN_OUTLINE_IN);
259 void TocWidget::on_moveOutTB_clicked()
261 outline(LFUN_OUTLINE_OUT);
265 void TocWidget::select(QModelIndex const & index)
267 if (!index.isValid()) {
268 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
272 tocTV->scrollTo(index);
273 tocTV->clearSelection();
274 tocTV->setCurrentIndex(index);
278 /// Test if outlining operation is possible
279 static bool canOutline(QString const & type)
281 return type == "tableofcontents";
285 void TocWidget::enableControls(bool enable)
287 updateTB->setEnabled(enable);
289 if (!canOutline(current_type_))
292 moveUpTB->setEnabled(enable);
293 moveDownTB->setEnabled(enable);
294 moveInTB->setEnabled(enable);
295 moveOutTB->setEnabled(enable);
297 depthSL->setMaximum(0);
298 depthSL->setValue(0);
303 /// Test if synchronized navigation is possible
304 static bool canNavigate(QString const & type)
306 // It is not possible to have synchronous navigation in a correctl
307 // and efficient way with the label type because Toc::item() do a linear
308 // seatch. Even if fixed, it might even not be desirable to do so if we
309 // want to support drag&drop of labels and references.
310 return type != "label" && type != "change";
314 void TocWidget::updateView()
316 if (!gui_view_.view()) {
317 enableControls(false);
318 typeCO->setEnabled(false);
320 tocTV->setEnabled(false);
323 typeCO->setEnabled(true);
324 tocTV->setEnabled(false);
325 tocTV->setUpdatesEnabled(false);
327 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
328 if (tocTV->model() != toc_model) {
329 tocTV->setModel(toc_model);
330 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
332 setTreeDepth(depth_);
335 sortCB->blockSignals(true);
336 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
337 sortCB->blockSignals(false);
339 persistentCB->setChecked(persistent_);
341 bool controls_enabled = toc_model && toc_model->rowCount() > 0
342 && !gui_view_.buffer()->isReadonly();
343 enableControls(controls_enabled);
345 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
346 depthSL->setValue(depth_);
348 setTreeDepth(depth_);
349 if (canNavigate(current_type_))
350 select(gui_view_.tocModels().currentIndex(current_type_));
351 tocTV->setEnabled(true);
352 tocTV->setUpdatesEnabled(true);
356 static QString decodeType(QString const & str)
359 if (type.contains("tableofcontents")) {
360 type = "tableofcontents";
361 } else if (type.contains("floatlist")) {
362 if (type.contains("\"figure"))
364 else if (type.contains("\"table"))
366 else if (type.contains("\"algorithm"))
373 void TocWidget::init(QString const & str)
377 new_index = typeCO->findData(current_type_);
379 new_index = typeCO->findData(decodeType(str));
381 // If everything else fails, settle on the table of contents which is
382 // guaranted to exist.
383 if (new_index == -1) {
384 current_type_ = "tableofcontents";
385 new_index = typeCO->findData(current_type_);
387 current_type_ = typeCO->itemData(new_index).toString();
390 typeCO->blockSignals(true);
391 typeCO->setCurrentIndex(new_index);
392 typeCO->blockSignals(false);
395 } // namespace frontend
398 #include "moc_TocWidget.cpp"