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"
25 #include "support/debug.h"
26 #include "support/lassert.h"
28 #include <QHeaderView>
38 TocWidget::TocWidget(GuiView & gui_view, QWidget * parent)
39 : QWidget(parent), depth_(0), persistent_(false), gui_view_(gui_view)
43 moveOutTB->setIcon(QIcon(getPixmap("images/", "promote", "png")));
44 moveInTB->setIcon(QIcon(getPixmap("images/", "demote", "png")));
45 moveUpTB->setIcon(QIcon(getPixmap("images/", "up", "png")));
46 moveDownTB->setIcon(QIcon(getPixmap("images/", "down", "png")));
47 updateTB->setIcon(QIcon(getPixmap("images/", "reload", "png")));
50 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
54 // hide the pointless QHeader for now
55 // in the future, new columns may appear
56 // like labels, bookmarks, etc...
57 // tocTV->header()->hide();
58 tocTV->header()->setVisible(false);
60 // Only one item selected at a time.
61 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
63 // The toc types combo won't change its model.
64 typeCO->setModel(gui_view_.tocModels().nameModel());
66 // Make sure the buttons are disabled when first shown without a loaded
68 enableControls(false);
74 void TocWidget::on_tocTV_activated(QModelIndex const & index)
80 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
87 void TocWidget::goTo(QModelIndex const & index)
89 LYXERR(Debug::GUI, "goto " << index.row()
90 << ", " << index.column());
92 gui_view_.tocModels().goTo(current_type_, index);
96 void TocWidget::on_updateTB_clicked()
98 // The backend update can take some time so we disable
99 // the controls while waiting.
100 enableControls(false);
101 gui_view_.tocModels().updateBackend();
105 void TocWidget::on_sortCB_stateChanged(int state)
107 gui_view_.tocModels().sort(current_type_, state == Qt::Checked);
111 void TocWidget::on_persistentCB_stateChanged(int state)
113 persistent_ = state == Qt::Checked;
117 /* FIXME (Ugras 17/11/06):
118 I have implemented a indexDepth function to get the model indices. In my
119 opinion, somebody should derive a new qvariant class for tocModelItem
120 which saves the string data and depth information. That will save the
121 depth calculation. */
123 static int indexDepth(QModelIndex const & index, int depth = -1)
126 return index.parent() == QModelIndex()
127 ? depth : indexDepth(index.parent(), depth);
131 void TocWidget::on_depthSL_valueChanged(int depth)
136 gui_view_.setFocus();
140 void TocWidget::setTreeDepth(int depth)
146 #if QT_VERSION >= 0x040300
147 // this should be faster than our own code below
149 tocTV->collapseAll();
151 tocTV->expandToDepth(depth - 1);
153 // expanding and then collapsing is probably better,
154 // but my qt 4.1.2 doesn't have expandAll()..
155 //tocTV->expandAll();
156 QModelIndexList indices = tocTV->model()->match(
157 tocTV->model()->index(0, 0),
158 Qt::DisplayRole, "*", -1,
159 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
161 int size = indices.size();
162 for (int i = 0; i < size; i++) {
163 QModelIndex index = indices[i];
164 tocTV->setExpanded(index, indexDepth(index) < depth_);
170 void TocWidget::on_typeCO_currentIndexChanged(int index)
172 current_type_ = typeCO->itemData(index).toString();
174 gui_view_.setFocus();
178 void TocWidget::outline(int func_code)
180 enableControls(false);
181 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
184 enableControls(false);
186 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
187 enableControls(true);
188 gui_view_.setFocus();
192 void TocWidget::on_moveUpTB_clicked()
194 outline(LFUN_OUTLINE_UP);
198 void TocWidget::on_moveDownTB_clicked()
200 outline(LFUN_OUTLINE_DOWN);
204 void TocWidget::on_moveInTB_clicked()
206 outline(LFUN_OUTLINE_IN);
210 void TocWidget::on_moveOutTB_clicked()
212 outline(LFUN_OUTLINE_OUT);
216 void TocWidget::select(QModelIndex const & index)
218 if (!index.isValid()) {
219 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
223 tocTV->scrollTo(index);
224 tocTV->clearSelection();
225 tocTV->setCurrentIndex(index);
229 /// Test if outlining operation is possible
230 static bool canOutline(QString const & type)
232 return type == "tableofcontents";
236 void TocWidget::enableControls(bool enable)
238 updateTB->setEnabled(enable);
240 if (!canOutline(current_type_))
243 moveUpTB->setEnabled(enable);
244 moveDownTB->setEnabled(enable);
245 moveInTB->setEnabled(enable);
246 moveOutTB->setEnabled(enable);
248 depthSL->setMaximum(0);
249 depthSL->setValue(0);
254 /// Test if synchronized navigation is possible
255 static bool canNavigate(QString const & type)
257 // It is not possible to have synchronous navigation in a correctl
258 // and efficient way with the label type because Toc::item() do a linear
259 // seatch. Even if fixed, it might even not be desirable to do so if we
260 // want to support drag&drop of labels and references.
261 return type != "label" && type != "change";
265 void TocWidget::updateView()
267 if (!gui_view_.view()) {
268 enableControls(false);
269 typeCO->setEnabled(false);
271 tocTV->setEnabled(false);
274 typeCO->setEnabled(true);
275 tocTV->setEnabled(false);
276 tocTV->setUpdatesEnabled(false);
278 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
279 if (tocTV->model() != toc_model) {
280 tocTV->setModel(toc_model);
281 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
283 setTreeDepth(depth_);
286 sortCB->blockSignals(true);
287 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
288 sortCB->blockSignals(false);
290 persistentCB->setChecked(persistent_);
292 bool controls_enabled = toc_model && toc_model->rowCount() > 0
293 && !gui_view_.buffer()->isReadonly();
294 enableControls(controls_enabled);
296 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
297 depthSL->setValue(depth_);
299 setTreeDepth(depth_);
300 if (canNavigate(current_type_))
301 select(gui_view_.tocModels().currentIndex(current_type_));
302 tocTV->setEnabled(true);
303 tocTV->setUpdatesEnabled(true);
307 static QString decodeType(QString const & str)
310 if (type.contains("tableofcontents")) {
311 type = "tableofcontents";
312 } else if (type.contains("floatlist")) {
313 if (type.contains("\"figure"))
315 else if (type.contains("\"table"))
317 else if (type.contains("\"algorithm"))
324 void TocWidget::init(QString const & str)
328 new_index = typeCO->findData(current_type_);
330 new_index = typeCO->findData(decodeType(str));
332 // If everything else fails, settle on the table of contents which is
333 // guaranted to exist.
334 if (new_index == -1) {
335 current_type_ = "tableofcontents";
336 new_index = typeCO->findData(current_type_);
338 current_type_ = typeCO->itemData(new_index).toString();
341 typeCO->blockSignals(true);
342 typeCO->setCurrentIndex(new_index);
343 typeCO->blockSignals(false);
346 } // namespace frontend
349 #include "moc_TocWidget.cpp"