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), 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());
67 void TocWidget::on_tocTV_activated(QModelIndex const & index)
73 void TocWidget::on_tocTV_clicked(QModelIndex const & index)
80 void TocWidget::goTo(QModelIndex const & index)
82 LYXERR(Debug::GUI, "goto " << index.row()
83 << ", " << index.column());
85 gui_view_.tocModels().goTo(current_type_, index);
89 void TocWidget::on_updateTB_clicked()
91 // The backend update can take some time so we disable
92 // the controls while waiting.
93 enableControls(false);
94 gui_view_.tocModels().updateBackend();
98 /* FIXME (Ugras 17/11/06):
99 I have implemented a indexDepth function to get the model indices. In my
100 opinion, somebody should derive a new qvariant class for tocModelItem
101 which saves the string data and depth information. That will save the
102 depth calculation. */
104 static int indexDepth(QModelIndex const & index, int depth = -1)
107 return index.parent() == QModelIndex()
108 ? depth : indexDepth(index.parent(), depth);
112 void TocWidget::on_depthSL_valueChanged(int depth)
117 gui_view_.setFocus();
121 void TocWidget::setTreeDepth(int depth)
127 // expanding and then collapsing is probably better,
128 // but my qt 4.1.2 doesn't have expandAll()..
129 //tocTV->expandAll();
130 QModelIndexList indices = tocTV->model()->match(
131 tocTV->model()->index(0,0),
132 Qt::DisplayRole, "*", -1,
133 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
135 int size = indices.size();
136 for (int i = 0; i < size; i++) {
137 QModelIndex index = indices[i];
138 tocTV->setExpanded(index, indexDepth(index) < depth_);
143 void TocWidget::on_typeCO_currentIndexChanged(int index)
145 current_type_ = typeCO->itemData(index).toString();
147 gui_view_.setFocus();
151 void TocWidget::outline(int func_code)
153 enableControls(false);
154 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
157 enableControls(false);
159 dispatch(FuncRequest(static_cast<FuncCode>(func_code)));
160 enableControls(true);
161 gui_view_.setFocus();
165 void TocWidget::on_moveUpTB_clicked()
167 outline(LFUN_OUTLINE_UP);
171 void TocWidget::on_moveDownTB_clicked()
173 outline(LFUN_OUTLINE_DOWN);
177 void TocWidget::on_moveInTB_clicked()
179 outline(LFUN_OUTLINE_IN);
183 void TocWidget::on_moveOutTB_clicked()
185 outline(LFUN_OUTLINE_OUT);
189 void TocWidget::select(QModelIndex const & index)
191 if (!index.isValid()) {
192 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
196 tocTV->scrollTo(index);
197 tocTV->clearSelection();
198 tocTV->setCurrentIndex(index);
202 /// Test if outlining operation is possible
203 static bool canOutline(QString const & type)
205 return type == "tableofcontents";
209 void TocWidget::enableControls(bool enable)
211 updateTB->setEnabled(enable);
213 if (!canOutline(current_type_))
216 moveUpTB->setEnabled(enable);
217 moveDownTB->setEnabled(enable);
218 moveInTB->setEnabled(enable);
219 moveOutTB->setEnabled(enable);
223 void TocWidget::updateView()
225 if (!gui_view_.view()) {
226 enableControls(false);
227 typeCO->setEnabled(false);
229 tocTV->setEnabled(false);
232 typeCO->setEnabled(true);
233 tocTV->setEnabled(true);
235 QStandardItemModel * toc_model = gui_view_.tocModels().model(current_type_);
236 if (tocTV->model() != toc_model) {
237 tocTV->setModel(toc_model);
238 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
240 bool controls_enabled = toc_model && toc_model->rowCount() > 0
241 && !gui_view_.buffer()->isReadonly();
242 enableControls(controls_enabled);
244 depthSL->setMaximum(gui_view_.tocModels().depth(current_type_));
245 depthSL->setValue(depth_);
246 setTreeDepth(depth_);
247 select(gui_view_.tocModels().currentIndex(current_type_));
251 static QString decodeType(QString const & str)
254 if (type.contains("tableofcontents")) {
255 type = "tableofcontents";
256 } else if (type.contains("floatlist")) {
257 if (type.contains("\"figure"))
259 else if (type.contains("\"table"))
261 else if (type.contains("\"algorithm"))
268 void TocWidget::init(QString const & str)
270 typeCO->blockSignals(true);
274 new_index = typeCO->findData(current_type_);
276 new_index = typeCO->findData(decodeType(str));
278 // If everything else fails, settle on the table of contents which is
279 // guaranted to exist.
280 if (new_index == -1) {
281 current_type_ = "tableofcontents";
282 new_index = typeCO->findData(current_type_);
285 typeCO->setCurrentIndex(new_index);
286 typeCO->blockSignals(false);
289 } // namespace frontend
292 #include "TocWidget_moc.cpp"