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*/)
68 << "selectionChanged index " << current.row()
69 << ", " << current.column()
72 form_.goTo(typeCO->currentIndex(), current);
76 void TocWidget::on_updateTB_clicked()
78 // The backend update can take some time so we disable
79 // the controls while waiting.
80 enableControls(false);
81 form_.updateBackend();
84 /* FIXME (Ugras 17/11/06):
85 I have implemented a getIndexDepth function to get the model indices. In my
86 opinion, somebody should derive a new qvariant class for tocModelItem
87 which saves the string data and depth information. that will save the
90 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
93 return (index.parent() == QModelIndex())
94 ? depth : getIndexDepth(index.parent(),depth);
98 void TocWidget::on_depthSL_valueChanged(int depth)
106 void TocWidget::setTreeDepth(int depth)
110 // expanding and then collapsing is probably better,
111 // but my qt 4.1.2 doesn't have expandAll()..
112 //tocTV->expandAll();
113 QModelIndexList indices = tocTV->model()->match(
114 tocTV->model()->index(0,0),
115 Qt::DisplayRole, "*", -1,
116 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
118 int size = indices.size();
119 for (int i = 0; i < size; i++) {
120 QModelIndex index = indices[i];
121 if (getIndexDepth(index) < depth_)
122 tocTV->expand(index);
124 tocTV->collapse(index);
128 void TocWidget::on_typeCO_currentIndexChanged(int value)
134 void TocWidget::on_moveUpTB_clicked()
136 enableControls(false);
137 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
138 if (!list.isEmpty()) {
139 enableControls(false);
140 form_.goTo(typeCO->currentIndex(), list[0]);
142 enableControls(true);
147 void TocWidget::on_moveDownTB_clicked()
149 enableControls(false);
150 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
151 if (!list.isEmpty()) {
152 enableControls(false);
153 form_.goTo(typeCO->currentIndex(), list[0]);
155 enableControls(true);
160 void TocWidget::on_moveInTB_clicked()
162 enableControls(false);
163 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
164 if (!list.isEmpty()) {
165 enableControls(false);
166 form_.goTo(typeCO->currentIndex(), list[0]);
168 enableControls(true);
173 void TocWidget::on_moveOutTB_clicked()
175 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
176 if (!list.isEmpty()) {
177 enableControls(false);
178 form_.goTo(typeCO->currentIndex(), list[0]);
180 enableControls(true);
185 void TocWidget::select(QModelIndex const & index)
187 if (!index.isValid()) {
189 << "TocWidget::select(): QModelIndex is invalid!" << endl;
193 disconnectSelectionModel();
194 tocTV->setCurrentIndex(index);
195 tocTV->scrollTo(index);
196 reconnectSelectionModel();
200 void TocWidget::enableControls(bool enable)
202 updateTB->setEnabled(enable);
204 if (!form_.canOutline(typeCO->currentIndex()))
207 moveUpTB->setEnabled(enable);
208 moveDownTB->setEnabled(enable);
209 moveInTB->setEnabled(enable);
210 moveOutTB->setEnabled(enable);
212 depthSL->setEnabled(enable);
216 void TocWidget::updateView()
218 LYXERR(Debug::GUI) << "In TocWidget::updateView()" << endl;
219 select(form_.currentIndex(typeCO->currentIndex()));
223 void TocWidget::updateGui()
225 vector<docstring> const & type_names = form_.typeNames();
226 if (type_names.empty()) {
227 enableControls(false);
229 tocTV->setModel(new QStandardItemModel);
230 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
234 QString current_text = typeCO->currentText();
235 //lyxerr << "current_text " << fromqstr(current_text) << endl;
236 typeCO->blockSignals(true);
238 int current_type = -1;
239 for (size_t i = 0; i != type_names.size(); ++i) {
240 QString item = toqstr(type_names[i]);
241 typeCO->addItem(item);
242 if (item == current_text)
245 if (current_type != -1)
246 typeCO->setCurrentIndex(current_type);
248 typeCO->setCurrentIndex(form_.selectedType());
249 typeCO->blockSignals(false);
251 setTocModel(typeCO->currentIndex());
255 void TocWidget::setTocModel(size_t type)
257 bool controls_enabled = false;
258 QStandardItemModel * toc_model = form_.tocModel(type);
260 controls_enabled = toc_model->rowCount() > 0;
261 tocTV->setModel(toc_model);
262 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
265 enableControls(controls_enabled);
267 reconnectSelectionModel();
269 if (controls_enabled) {
270 depthSL->setMaximum(form_.getTocDepth(type));
271 depthSL->setValue(depth_);
274 LYXERR(Debug::GUI) << "In TocWidget::updateGui()" << endl;
276 select(form_.currentIndex(typeCO->currentIndex()));
280 << "tocModel()->rowCount "
281 << toc_model->rowCount()
282 << "\nform_->tocModel()->columnCount "
283 << toc_model->columnCount()
289 void TocWidget::reconnectSelectionModel()
291 connect(tocTV->selectionModel(),
292 SIGNAL(currentChanged(const QModelIndex &,
293 const QModelIndex &)),
295 SLOT(selectionChanged(const QModelIndex &,
296 const QModelIndex &)));
299 void TocWidget::disconnectSelectionModel()
301 disconnect(tocTV->selectionModel(),
302 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
303 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
306 } // namespace frontend
309 #include "TocWidget_moc.cpp"