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"
17 #include "qt_helpers.h"
21 #include "FuncRequest.h"
24 #include "support/debug.h"
25 #include "support/lassert.h"
27 #include <QHeaderView>
37 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
38 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
42 moveOutTB->setIcon(QIcon(":/images/promote.png"));
43 moveInTB->setIcon(QIcon(":/images/demote.png"));
44 moveUpTB->setIcon(QIcon(":/images/up.png"));
45 moveDownTB->setIcon(QIcon(":/images/down.png"));
46 updateTB->setIcon(QIcon(":/images/reload.png"));
49 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
53 // hide the pointless QHeader for now
54 // in the future, new columns may appear
55 // like labels, bookmarks, etc...
56 // tocTV->header()->hide();
57 tocTV->header()->setVisible(false);
59 // Only one item selected at a time.
60 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
62 // The toc types combo won't change its model.
63 typeCO->setModel(gui_view_.tocModels().nameModel());
65 // Make sure the buttons are disabled when first shown without a loaded
67 enableControls(false);
73 void TocWidget::on_tocTV_activated(QModelIndex const & index)
79 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
86 void TocWidget::goTo(QModelIndex const & index)
88 LYXERR(Debug::GUI, "goto " << index.row()
89 << ", " << index.column());
91 gui_view_.tocModels().goTo(current_type_, index);
95 void TocWidget::on_updateTB_clicked()
97 // The backend update can take some time so we disable
98 // the controls while waiting.
99 enableControls(false);
100 gui_view_.tocModels().updateBackend();
104 void TocWidget::on_sortCB_stateChanged(int state)
106 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
110 void TocWidget::on_persistentCB_stateChanged(int state)
112 persistent_ = state == Qt::Checked;
116 /* FIXME (Ugras 17/11/06):
117 I have implemented a indexDepth function to get the model indices. In my
118 opinion, somebody should derive a new qvariant class for tocModelItem
119 which saves the string data and depth information. That will save the
120 depth calculation. */
122 static int indexDepth(QModelIndex const & index, int depth = -1)
125 return index.parent() == QModelIndex()
126 ? depth : indexDepth(index.parent(), depth);
130 void TocWidget::on_depthSL_valueChanged(int depth)
135 gui_view_.setFocus();
139 void TocWidget::setTreeDepth(int depth)
145 #if QT_VERSION >= 0x040300
146 // this should be faster than our own code below
148 tocTV->collapseAll();
150 tocTV->expandToDepth(depth - 1);
152 // expanding and then collapsing is probably better,
153 // but my qt 4.1.2 doesn't have expandAll()..
154 //tocTV->expandAll();
155 QModelIndexList indices = tocTV->model()->match(
156 tocTV->model()->index(0, 0),
157 Qt::DisplayRole, "*", -1,
158 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
160 int size = indices.size();
161 for (int i = 0; i < size; i++) {
162 QModelIndex index = indices[i];
163 tocTV->setExpanded(index, indexDepth(index) < depth_);
169 void TocWidget::on_typeCO_currentIndexChanged(int index)
171 current_type_ = typeCO->itemData(index).toString();
173 gui_view_.setFocus();
177 void TocWidget::outline(int func_code)
179 enableControls(false);
180 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
183 enableControls(false);
185 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
186 enableControls(true);
187 gui_view_.setFocus();
191 void TocWidget::on_moveUpTB_clicked()
193 outline(LFUN_OUTLINE_UP);
197 void TocWidget::on_moveDownTB_clicked()
199 outline(LFUN_OUTLINE_DOWN);
203 void TocWidget::on_moveInTB_clicked()
205 outline(LFUN_OUTLINE_IN);
209 void TocWidget::on_moveOutTB_clicked()
211 outline(LFUN_OUTLINE_OUT);
215 void TocWidget::select(QModelIndex const & index)
217 if (!index.isValid()) {
218 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
222 tocTV->scrollTo(index);
223 tocTV->clearSelection();
224 tocTV->setCurrentIndex(index);
228 /// Test if outlining operation is possible
229 static bool canOutline(QString const & type)
231 return type == "tableofcontents";
235 void TocWidget::enableControls(bool enable)
237 updateTB->setEnabled(enable);
239 if (!canOutline(current_type_))
242 moveUpTB->setEnabled(enable);
243 moveDownTB->setEnabled(enable);
244 moveInTB->setEnabled(enable);
245 moveOutTB->setEnabled(enable);
247 depthSL->setMaximum(0);
248 depthSL->setValue(0);
253 /// Test if synchronized navigation is possible
254 static bool canNavigate(QString const & type)
256 // It is not possible to have synchronous navigation in a correctl
257 // and efficient way with the label type because Toc::item() do a linear
258 // seatch. Even if fixed, it might even not be desirable to do so if we
259 // want to support drag&drop of labels and references.
260 return type != "label" && type != "change";
264 void TocWidget::updateView()
266 if (!gui_view_.view()) {
267 enableControls(false);
268 typeCO->setEnabled(false);
270 tocTV->setEnabled(false);
273 typeCO->setEnabled(true);
274 tocTV->setEnabled(false);
275 tocTV->setUpdatesEnabled(false);
277 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
278 if (tocTV->model() != toc_model) {
279 tocTV->setModel(toc_model);
280 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
282 setTreeDepth(depth_);
285 sortCB->blockSignals(true);
286 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
287 sortCB->blockSignals(false);
289 persistentCB->setChecked(persistent_);
291 bool controls_enabled = toc_model && toc_model->rowCount() > 0
292 && !gui_view_.buffer()->isReadonly();
293 enableControls(controls_enabled);
295 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
296 depthSL->setValue(depth_);
298 setTreeDepth(depth_);
299 if (canNavigate(current_type_))
300 select(gui_view_.tocModels().currentIndex(current_type_));
301 tocTV->setEnabled(true);
302 tocTV->setUpdatesEnabled(true);
306 static QString decodeType(QString const & str)
309 if (type.contains("tableofcontents")) {
310 type = "tableofcontents";
311 } else if (type.contains("floatlist")) {
312 if (type.contains("\"figure"))
314 else if (type.contains("\"table"))
316 else if (type.contains("\"algorithm"))
323 void TocWidget::init(QString const & str)
327 new_index = typeCO->findData(current_type_);
329 new_index = typeCO->findData(decodeType(str));
331 // If everything else fails, settle on the table of contents which is
332 // guaranted to exist.
333 if (new_index == -1) {
334 current_type_ = "tableofcontents";
335 new_index = typeCO->findData(current_type_);
337 current_type_ = typeCO->itemData(new_index).toString();
340 typeCO->blockSignals(true);
341 typeCO->setCurrentIndex(new_index);
342 typeCO->blockSignals(false);
345 } // namespace frontend
348 #include "moc_TocWidget.cpp"