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 <QHeaderView>
22 #include <QPushButton>
23 #include <QTreeWidgetItem>
34 TocWidget::TocWidget(GuiToc & form, QWidget * parent)
35 : QWidget(parent), depth_(0), form_(form)
38 setWindowTitle(qt_("Outline"));
40 connect(&form_, SIGNAL(modelReset()), SLOT(updateGui()));
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);
64 void TocWidget::selectionChanged(const QModelIndex & current,
65 const QModelIndex & /*previous*/)
67 LYXERR(Debug::GUI, "selectionChanged index " << current.row()
68 << ", " << current.column());
70 form_.goTo(typeCO->currentIndex(), current);
74 void TocWidget::on_updateTB_clicked()
76 // The backend update can take some time so we disable
77 // the controls while waiting.
78 enableControls(false);
79 form_.updateBackend();
82 /* FIXME (Ugras 17/11/06):
83 I have implemented a getIndexDepth function to get the model indices. In my
84 opinion, somebody should derive a new qvariant class for tocModelItem
85 which saves the string data and depth information. that will save the
88 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
91 return (index.parent() == QModelIndex())
92 ? depth : getIndexDepth(index.parent(),depth);
96 void TocWidget::on_depthSL_valueChanged(int depth)
104 void TocWidget::setTreeDepth(int depth)
108 // expanding and then collapsing is probably better,
109 // but my qt 4.1.2 doesn't have expandAll()..
110 //tocTV->expandAll();
111 QModelIndexList indices = tocTV->model()->match(
112 tocTV->model()->index(0,0),
113 Qt::DisplayRole, "*", -1,
114 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
116 int size = indices.size();
117 for (int i = 0; i < size; i++) {
118 QModelIndex index = indices[i];
119 if (getIndexDepth(index) < depth_)
120 tocTV->expand(index);
122 tocTV->collapse(index);
126 void TocWidget::on_typeCO_currentIndexChanged(int value)
132 void TocWidget::on_moveUpTB_clicked()
134 enableControls(false);
135 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
136 if (!list.isEmpty()) {
137 enableControls(false);
138 form_.goTo(typeCO->currentIndex(), list[0]);
140 enableControls(true);
145 void TocWidget::on_moveDownTB_clicked()
147 enableControls(false);
148 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
149 if (!list.isEmpty()) {
150 enableControls(false);
151 form_.goTo(typeCO->currentIndex(), list[0]);
153 enableControls(true);
158 void TocWidget::on_moveInTB_clicked()
160 enableControls(false);
161 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
162 if (!list.isEmpty()) {
163 enableControls(false);
164 form_.goTo(typeCO->currentIndex(), list[0]);
166 enableControls(true);
171 void TocWidget::on_moveOutTB_clicked()
173 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
174 if (!list.isEmpty()) {
175 enableControls(false);
176 form_.goTo(typeCO->currentIndex(), list[0]);
178 enableControls(true);
183 void TocWidget::select(QModelIndex const & index)
185 if (!index.isValid()) {
186 LYXERR(Debug::GUI, "TocWidget::select(): QModelIndex is invalid!");
190 disconnectSelectionModel();
191 tocTV->setCurrentIndex(index);
192 tocTV->scrollTo(index);
193 reconnectSelectionModel();
197 void TocWidget::enableControls(bool enable)
199 updateTB->setEnabled(enable);
201 if (!form_.canOutline(typeCO->currentIndex()))
204 moveUpTB->setEnabled(enable);
205 moveDownTB->setEnabled(enable);
206 moveInTB->setEnabled(enable);
207 moveOutTB->setEnabled(enable);
209 depthSL->setEnabled(enable);
213 void TocWidget::updateView()
215 LYXERR(Debug::GUI, "In TocWidget::updateView()");
216 select(form_.currentIndex(typeCO->currentIndex()));
220 void TocWidget::updateGui()
222 vector<docstring> const & type_names = form_.typeNames();
223 if (type_names.empty()) {
224 enableControls(false);
226 tocTV->setModel(new QStandardItemModel);
227 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
231 QString current_text = typeCO->currentText();
232 //lyxerr << "current_text " << fromqstr(current_text) << endl;
233 typeCO->blockSignals(true);
235 int current_type = -1;
236 for (size_t i = 0; i != type_names.size(); ++i) {
237 QString item = toqstr(type_names[i]);
238 typeCO->addItem(item);
239 if (item == current_text)
242 if (current_type != -1)
243 typeCO->setCurrentIndex(current_type);
245 typeCO->setCurrentIndex(form_.selectedType());
246 typeCO->blockSignals(false);
248 setTocModel(typeCO->currentIndex());
252 void TocWidget::setTocModel(size_t type)
254 bool controls_enabled = false;
255 QStandardItemModel * toc_model = form_.tocModel(type);
257 controls_enabled = toc_model->rowCount() > 0;
258 tocTV->setModel(toc_model);
259 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
262 enableControls(controls_enabled);
264 reconnectSelectionModel();
266 if (controls_enabled) {
267 depthSL->setMaximum(form_.getTocDepth(type));
268 depthSL->setValue(depth_);
271 LYXERR(Debug::GUI, "In TocWidget::updateGui()");
273 select(form_.currentIndex(typeCO->currentIndex()));
276 LYXERR(Debug::GUI, "tocModel()->rowCount "
277 << toc_model->rowCount()
278 << "\nform_->tocModel()->columnCount "
279 << toc_model->columnCount());
284 void TocWidget::reconnectSelectionModel()
286 connect(tocTV->selectionModel(),
287 SIGNAL(currentChanged(const QModelIndex &,
288 const QModelIndex &)),
290 SLOT(selectionChanged(const QModelIndex &,
291 const QModelIndex &)));
294 void TocWidget::disconnectSelectionModel()
296 disconnect(tocTV->selectionModel(),
297 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
298 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
301 } // namespace frontend
304 #include "TocWidget_moc.cpp"