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 // expanding and then collapsing is probably better,
146 // but my qt 4.1.2 doesn't have expandAll()..
147 //tocTV->expandAll();
148 QModelIndexList indices = tocTV->model()->match(
149 tocTV->model()->index(0, 0),
150 Qt::DisplayRole, "*", -1,
151 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
153 int size = indices.size();
154 for (int i = 0; i < size; i++) {
155 QModelIndex index = indices[i];
156 tocTV->setExpanded(index, indexDepth(index) < depth_);
161 void TocWidget::on_typeCO_currentIndexChanged(int index)
163 current_type_ = typeCO->itemData(index).toString();
165 gui_view_.setFocus();
169 void TocWidget::outline(int func_code)
171 enableControls(false);
172 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
175 enableControls(false);
177 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
178 enableControls(true);
179 gui_view_.setFocus();
183 void TocWidget::on_moveUpTB_clicked()
185 outline(LFUN_OUTLINE_UP);
189 void TocWidget::on_moveDownTB_clicked()
191 outline(LFUN_OUTLINE_DOWN);
195 void TocWidget::on_moveInTB_clicked()
197 outline(LFUN_OUTLINE_IN);
201 void TocWidget::on_moveOutTB_clicked()
203 outline(LFUN_OUTLINE_OUT);
207 void TocWidget::select(QModelIndex const & index)
209 if (!index.isValid()) {
210 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
214 tocTV->scrollTo(index);
215 tocTV->clearSelection();
216 tocTV->setCurrentIndex(index);
220 /// Test if outlining operation is possible
221 static bool canOutline(QString const & type)
223 return type == "tableofcontents";
227 void TocWidget::enableControls(bool enable)
229 updateTB->setEnabled(enable);
230 sortCB->setEnabled(enable);
232 if (!canOutline(current_type_))
235 moveUpTB->setEnabled(enable);
236 moveDownTB->setEnabled(enable);
237 moveInTB->setEnabled(enable);
238 moveOutTB->setEnabled(enable);
239 persistentCB->setEnabled(enable);
241 depthSL->setMaximum(0);
242 depthSL->setValue(0);
247 /// Test if synchronized navigation is possible
248 static bool canNavigate(QString const & type)
250 // It is not possible to have synchronous navigation in a correctl
251 // and efficient way with the label type because Toc::item() do a linear
252 // seatch. Even if fixed, it might even not be desirable to do so if we
253 // want to support drag&drop of labels and references.
254 return type != "label" && type != "change";
258 void TocWidget::updateView()
260 if (!gui_view_.view()) {
261 enableControls(false);
262 typeCO->setEnabled(false);
264 tocTV->setEnabled(false);
267 typeCO->setEnabled(true);
268 tocTV->setEnabled(false);
269 tocTV->setUpdatesEnabled(false);
271 QAbstractItemModel * toc_model = gui_view_.tocModels().model(current_type_);
272 if (tocTV->model() != toc_model) {
273 tocTV->setModel(toc_model);
274 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
275 if (persistent_) setTreeDepth(depth_);
278 sortCB->blockSignals(true);
279 sortCB->setChecked(gui_view_.tocModels().isSorted(current_type_));
280 sortCB->blockSignals(false);
282 persistentCB->setChecked(persistent_);
284 bool controls_enabled = toc_model && toc_model->rowCount() > 0
285 && !gui_view_.buffer()->isReadonly();
286 enableControls(controls_enabled);
288 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
289 depthSL->setValue(depth_);
290 if (!persistent_) setTreeDepth(depth_);
291 if (canNavigate(current_type_))
292 select(gui_view_.tocModels().currentIndex(current_type_));
293 tocTV->setEnabled(true);
294 tocTV->setUpdatesEnabled(true);
298 static QString decodeType(QString const & str)
301 if (type.contains("tableofcontents")) {
302 type = "tableofcontents";
303 } else if (type.contains("floatlist")) {
304 if (type.contains("\"figure"))
306 else if (type.contains("\"table"))
308 else if (type.contains("\"algorithm"))
315 void TocWidget::init(QString const & str)
319 new_index = typeCO->findData(current_type_);
321 new_index = typeCO->findData(decodeType(str));
323 // If everything else fails, settle on the table of contents which is
324 // guaranted to exist.
325 if (new_index == -1) {
326 current_type_ = "tableofcontents";
327 new_index = typeCO->findData(current_type_);
329 current_type_ = typeCO->itemData(new_index).toString();
332 typeCO->blockSignals(true);
333 typeCO->setCurrentIndex(new_index);
334 typeCO->blockSignals(false);
337 } // namespace frontend
340 #include "moc_TocWidget.cpp"