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"
19 #include "support/debug.h"
21 #include <QHeaderView>
30 TocWidget::TocWidget(GuiToc & form, QWidget * parent)
31 : QWidget(parent), depth_(0), form_(form)
34 setWindowTitle(qt_("Outline"));
36 connect(&form_, SIGNAL(modelReset()), SLOT(updateGui()));
38 moveOutTB->setIcon(QIcon(":/images/promote.png"));
39 moveInTB->setIcon(QIcon(":/images/demote.png"));
40 moveUpTB->setIcon(QIcon(":/images/up.png"));
41 moveDownTB->setIcon(QIcon(":/images/down.png"));
42 updateTB->setIcon(QIcon(":/images/reload.png"));
45 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
49 // hide the pointless QHeader for now
50 // in the future, new columns may appear
51 // like labels, bookmarks, etc...
52 // tocTV->header()->hide();
53 tocTV->header()->setVisible(false);
55 // Only one item selected at a time.
56 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
60 void TocWidget::selectionChanged(const QModelIndex & current,
61 const QModelIndex & /*previous*/)
63 LYXERR(Debug::GUI, "selectionChanged index " << current.row()
64 << ", " << current.column());
66 form_.goTo(typeCO->currentIndex(), current);
70 void TocWidget::on_updateTB_clicked()
72 // The backend update can take some time so we disable
73 // the controls while waiting.
74 enableControls(false);
75 form_.updateBackend();
78 /* FIXME (Ugras 17/11/06):
79 I have implemented a getIndexDepth function to get the model indices. In my
80 opinion, somebody should derive a new qvariant class for tocModelItem
81 which saves the string data and depth information. that will save the
84 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
87 return (index.parent() == QModelIndex())
88 ? depth : getIndexDepth(index.parent(),depth);
92 void TocWidget::on_depthSL_valueChanged(int depth)
100 void TocWidget::setTreeDepth(int depth)
104 // expanding and then collapsing is probably better,
105 // but my qt 4.1.2 doesn't have expandAll()..
106 //tocTV->expandAll();
107 QModelIndexList indices = tocTV->model()->match(
108 tocTV->model()->index(0,0),
109 Qt::DisplayRole, "*", -1,
110 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
112 int size = indices.size();
113 for (int i = 0; i < size; i++) {
114 QModelIndex index = indices[i];
115 if (getIndexDepth(index) < depth_)
116 tocTV->expand(index);
118 tocTV->collapse(index);
122 void TocWidget::on_typeCO_currentIndexChanged(int value)
128 void TocWidget::on_moveUpTB_clicked()
130 enableControls(false);
131 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
132 if (!list.isEmpty()) {
133 enableControls(false);
134 form_.goTo(typeCO->currentIndex(), list[0]);
136 enableControls(true);
141 void TocWidget::on_moveDownTB_clicked()
143 enableControls(false);
144 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
145 if (!list.isEmpty()) {
146 enableControls(false);
147 form_.goTo(typeCO->currentIndex(), list[0]);
149 enableControls(true);
154 void TocWidget::on_moveInTB_clicked()
156 enableControls(false);
157 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
158 if (!list.isEmpty()) {
159 enableControls(false);
160 form_.goTo(typeCO->currentIndex(), list[0]);
162 enableControls(true);
167 void TocWidget::on_moveOutTB_clicked()
169 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
170 if (!list.isEmpty()) {
171 enableControls(false);
172 form_.goTo(typeCO->currentIndex(), list[0]);
174 enableControls(true);
179 void TocWidget::select(QModelIndex const & index)
181 if (!index.isValid()) {
182 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
186 disconnectSelectionModel();
187 tocTV->setCurrentIndex(index);
188 tocTV->scrollTo(index);
189 reconnectSelectionModel();
193 void TocWidget::enableControls(bool enable)
195 updateTB->setEnabled(enable);
197 if (!form_.canOutline(typeCO->currentIndex()))
200 moveUpTB->setEnabled(enable);
201 moveDownTB->setEnabled(enable);
202 moveInTB->setEnabled(enable);
203 moveOutTB->setEnabled(enable);
205 depthSL->setEnabled(enable);
209 void TocWidget::updateView()
211 LYXERR(Debug::GUI, "In TocWidget::updateView()");
212 select(form_.currentIndex(typeCO->currentIndex()));
216 void TocWidget::updateGui()
218 vector<docstring> const & type_names = form_.typeNames();
219 if (type_names.empty()) {
220 enableControls(false);
222 tocTV->setModel(new QStandardItemModel);
223 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
227 QString current_text = typeCO->currentText();
228 //lyxerr << "current_text " << fromqstr(current_text) << endl;
229 typeCO->blockSignals(true);
231 int current_type = -1;
232 for (size_t i = 0; i != type_names.size(); ++i) {
233 QString item = toqstr(type_names[i]);
234 typeCO->addItem(item);
235 if (item == current_text)
238 if (current_type != -1)
239 typeCO->setCurrentIndex(current_type);
241 typeCO->setCurrentIndex(form_.selectedType());
242 typeCO->blockSignals(false);
244 setTocModel(typeCO->currentIndex());
248 void TocWidget::setTocModel(size_t type)
250 bool controls_enabled = false;
251 QStandardItemModel * toc_model = form_.tocModel(type);
253 controls_enabled = toc_model->rowCount() > 0;
254 tocTV->setModel(toc_model);
255 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
258 enableControls(controls_enabled);
260 reconnectSelectionModel();
262 if (controls_enabled) {
263 depthSL->setMaximum(form_.getTocDepth(type));
264 depthSL->setValue(depth_);
267 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
269 select(form_.currentIndex(typeCO->currentIndex()));
272 LYXERR(Debug::GUI, "tocModel()->rowCount "
273 << toc_model->rowCount()
274 << "\nform_->tocModel()->columnCount "
275 << toc_model->columnCount());
280 void TocWidget::reconnectSelectionModel()
282 connect(tocTV->selectionModel(),
283 SIGNAL(currentChanged(const QModelIndex &,
284 const QModelIndex &)),
286 SLOT(selectionChanged(const QModelIndex &,
287 const QModelIndex &)));
290 void TocWidget::disconnectSelectionModel()
292 disconnect(tocTV->selectionModel(),
293 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
294 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
297 } // namespace frontend
300 #include "TocWidget_moc.cpp"