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 "FuncRequest.h"
22 #include "support/debug.h"
24 #include <QHeaderView>
34 TocWidget::TocWidget(TocModels & models, QWidget * parent)
35 : QWidget(parent), depth_(0), models_(models)
39 moveOutTB->setIcon(QIcon(":/images/promote.png"));
40 moveInTB->setIcon(QIcon(":/images/demote.png"));
41 moveUpTB->setIcon(QIcon(":/images/up.png"));
42 moveDownTB->setIcon(QIcon(":/images/down.png"));
43 updateTB->setIcon(QIcon(":/images/reload.png"));
46 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
50 // hide the pointless QHeader for now
51 // in the future, new columns may appear
52 // like labels, bookmarks, etc...
53 // tocTV->header()->hide();
54 tocTV->header()->setVisible(false);
56 // Only one item selected at a time.
57 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
61 void TocWidget::selectionChanged(const QModelIndex & current,
62 const QModelIndex & /*previous*/)
64 LYXERR(Debug::GUI, "selectionChanged index " << current.row()
65 << ", " << current.column());
67 models_.goTo(typeCO->currentIndex(), current);
71 void TocWidget::on_updateTB_clicked()
73 // The backend update can take some time so we disable
74 // the controls while waiting.
75 enableControls(false);
76 models_.updateBackend();
79 /* FIXME (Ugras 17/11/06):
80 I have implemented a getIndexDepth function to get the model indices. In my
81 opinion, somebody should derive a new qvariant class for tocModelItem
82 which saves the string data and depth information. that will save the
85 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
88 return (index.parent() == QModelIndex())
89 ? depth : getIndexDepth(index.parent(),depth);
93 void TocWidget::on_depthSL_valueChanged(int depth)
101 void TocWidget::setTreeDepth(int depth)
105 // expanding and then collapsing is probably better,
106 // but my qt 4.1.2 doesn't have expandAll()..
107 //tocTV->expandAll();
108 QModelIndexList indices = tocTV->model()->match(
109 tocTV->model()->index(0,0),
110 Qt::DisplayRole, "*", -1,
111 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
113 int size = indices.size();
114 for (int i = 0; i < size; i++) {
115 QModelIndex index = indices[i];
116 if (getIndexDepth(index) < depth_)
117 tocTV->expand(index);
119 tocTV->collapse(index);
123 void TocWidget::on_typeCO_currentIndexChanged(int value)
129 void TocWidget::on_moveUpTB_clicked()
131 enableControls(false);
132 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
133 if (!list.isEmpty()) {
134 enableControls(false);
135 models_.goTo(typeCO->currentIndex(), list[0]);
136 dispatch(FuncRequest(LFUN_OUTLINE_UP));
137 enableControls(true);
142 void TocWidget::on_moveDownTB_clicked()
144 enableControls(false);
145 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
146 if (!list.isEmpty()) {
147 enableControls(false);
148 models_.goTo(typeCO->currentIndex(), list[0]);
149 dispatch(FuncRequest(LFUN_OUTLINE_DOWN));
150 enableControls(true);
155 void TocWidget::on_moveInTB_clicked()
157 enableControls(false);
158 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
159 if (!list.isEmpty()) {
160 enableControls(false);
161 models_.goTo(typeCO->currentIndex(), list[0]);
162 dispatch(FuncRequest(LFUN_OUTLINE_IN));
163 enableControls(true);
168 void TocWidget::on_moveOutTB_clicked()
170 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
171 if (!list.isEmpty()) {
172 enableControls(false);
173 models_.goTo(typeCO->currentIndex(), list[0]);
174 dispatch(FuncRequest(LFUN_OUTLINE_OUT));
175 enableControls(true);
180 void TocWidget::select(QModelIndex const & index)
182 if (!index.isValid()) {
183 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
187 disconnectSelectionModel();
188 tocTV->setCurrentIndex(index);
189 tocTV->scrollTo(index);
190 reconnectSelectionModel();
194 void TocWidget::enableControls(bool enable)
196 updateTB->setEnabled(enable);
198 if (!models_.canOutline(typeCO->currentIndex()))
201 moveUpTB->setEnabled(enable);
202 moveDownTB->setEnabled(enable);
203 moveInTB->setEnabled(enable);
204 moveOutTB->setEnabled(enable);
206 depthSL->setEnabled(enable);
210 void TocWidget::updateView()
212 LYXERR(Debug::GUI, "In TocWidget::updateView()");
214 select(models_.currentIndex(typeCO->currentIndex()));
218 void TocWidget::init(QString const & str)
220 QStringList const & type_names = models_.typeNames();
221 if (type_names.isEmpty()) {
222 enableControls(false);
224 tocTV->setModel(new QStandardItemModel);
225 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
229 int selected_type = models_.decodeType(str);
231 QString const current_text = typeCO->currentText();
232 typeCO->blockSignals(true);
234 for (int i = 0; i != type_names.size(); ++i)
235 typeCO->addItem(type_names[i]);
237 typeCO->setCurrentIndex(selected_type);
239 int const new_index = typeCO->findText(current_text);
241 typeCO->setCurrentIndex(new_index);
243 typeCO->setCurrentIndex(selected_type);
246 typeCO->blockSignals(false);
248 setTocModel(typeCO->currentIndex());
250 // setTocModel produce QTreeView reset and setting depth again
251 // is needed. That must be done after all Qt updates are processed.
252 QTimer::singleShot(0, this, SLOT(updateView()));
256 void TocWidget::setTocModel(size_t type)
258 bool controls_enabled = false;
259 QStandardItemModel * toc_model = models_.model(type);
261 controls_enabled = toc_model->rowCount() > 0;
262 tocTV->setModel(toc_model);
263 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
266 enableControls(controls_enabled);
268 reconnectSelectionModel();
270 if (controls_enabled) {
271 depthSL->setMaximum(models_.depth(type));
272 depthSL->setValue(depth_);
275 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
277 select(models_.currentIndex(typeCO->currentIndex()));
280 LYXERR(Debug::GUI, "tocModel()->rowCount "
281 << toc_model->rowCount()
282 << "\nform_->tocModel()->columnCount "
283 << toc_model->columnCount());
288 void TocWidget::reconnectSelectionModel()
290 connect(tocTV->selectionModel(), SIGNAL(
291 currentChanged(const QModelIndex &, const QModelIndex &)),
292 this, SLOT(selectionChanged(const QModelIndex &, const QModelIndex &)));
296 void TocWidget::disconnectSelectionModel()
298 disconnect(tocTV->selectionModel(),
299 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
300 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
303 } // namespace frontend
306 #include "TocWidget_moc.cpp"