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"
18 #include "support/filetools.h"
19 #include "support/lstrings.h"
23 #include <QHeaderView>
24 #include <QPushButton>
25 #include <QTreeWidgetItem>
40 using support::FileName;
41 using support::libFileSearch;
45 TocWidget::TocWidget(GuiToc & form, QWidget * parent)
46 : QWidget(parent), depth_(0), form_(form)
49 setWindowTitle(qt_("Outline"));
51 connect(&form_, SIGNAL(modelReset()), SLOT(updateGui()));
53 FileName icon_path = libFileSearch("images", "promote.png");
54 moveOutTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
55 icon_path = libFileSearch("images", "demote.png");
56 moveInTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
57 icon_path = libFileSearch("images", "up.png");
58 moveUpTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
59 icon_path = libFileSearch("images", "down.png");
60 moveDownTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
61 icon_path = libFileSearch("images", "reload.png");
62 updateTB->setIcon(QIcon(toqstr(icon_path.absFilename())));
65 tocTV->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn);
69 // hide the pointless QHeader for now
70 // in the future, new columns may appear
71 // like labels, bookmarks, etc...
72 // tocTV->header()->hide();
73 tocTV->header()->setVisible(false);
75 // Only one item selected at a time.
76 tocTV->setSelectionMode(QAbstractItemView::SingleSelection);
80 void TocWidget::selectionChanged(const QModelIndex & current,
81 const QModelIndex & /*previous*/)
84 << "selectionChanged index " << current.row()
85 << ", " << current.column()
88 form_.goTo(typeCO->currentIndex(), current);
92 void TocWidget::on_updateTB_clicked()
94 // The backend update can take some time so we disable
95 // the controls while waiting.
96 enableControls(false);
97 form_.updateBackend();
100 /* FIXME (Ugras 17/11/06):
101 I have implemented a getIndexDepth function to get the model indices. In my
102 opinion, somebody should derive a new qvariant class for tocModelItem
103 which saves the string data and depth information. that will save the
106 int TocWidget::getIndexDepth(QModelIndex const & index, int depth)
109 return (index.parent() == QModelIndex())
110 ? depth : getIndexDepth(index.parent(),depth);
114 void TocWidget::on_depthSL_valueChanged(int depth)
122 void TocWidget::setTreeDepth(int depth)
126 // expanding and then collapsing is probably better,
127 // but my qt 4.1.2 doesn't have expandAll()..
128 //tocTV->expandAll();
129 QModelIndexList indices = tocTV->model()->match(
130 tocTV->model()->index(0,0),
131 Qt::DisplayRole, "*", -1,
132 Qt::MatchFlags(Qt::MatchWildcard|Qt::MatchRecursive));
134 int size = indices.size();
135 for (int i = 0; i < size; i++) {
136 QModelIndex index = indices[i];
137 if (getIndexDepth(index) < depth_)
138 tocTV->expand(index);
140 tocTV->collapse(index);
144 void TocWidget::on_typeCO_currentIndexChanged(int value)
150 void TocWidget::on_moveUpTB_clicked()
152 enableControls(false);
153 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
154 if (!list.isEmpty()) {
155 enableControls(false);
156 form_.goTo(typeCO->currentIndex(), list[0]);
158 enableControls(true);
163 void TocWidget::on_moveDownTB_clicked()
165 enableControls(false);
166 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
167 if (!list.isEmpty()) {
168 enableControls(false);
169 form_.goTo(typeCO->currentIndex(), list[0]);
171 enableControls(true);
176 void TocWidget::on_moveInTB_clicked()
178 enableControls(false);
179 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
180 if (!list.isEmpty()) {
181 enableControls(false);
182 form_.goTo(typeCO->currentIndex(), list[0]);
184 enableControls(true);
189 void TocWidget::on_moveOutTB_clicked()
191 QModelIndexList const & list = tocTV->selectionModel()->selectedIndexes();
192 if (!list.isEmpty()) {
193 enableControls(false);
194 form_.goTo(typeCO->currentIndex(), list[0]);
196 enableControls(true);
201 void TocWidget::select(QModelIndex const & index)
203 if (!index.isValid()) {
205 << "TocWidget::select(): QModelIndex is invalid!" << endl;
209 disconnectSelectionModel();
210 tocTV->setCurrentIndex(index);
211 tocTV->scrollTo(index);
212 reconnectSelectionModel();
216 void TocWidget::enableControls(bool enable)
218 updateTB->setEnabled(enable);
220 if (!form_.canOutline(typeCO->currentIndex()))
223 moveUpTB->setEnabled(enable);
224 moveDownTB->setEnabled(enable);
225 moveInTB->setEnabled(enable);
226 moveOutTB->setEnabled(enable);
228 depthSL->setEnabled(enable);
232 void TocWidget::updateView()
234 LYXERR(Debug::GUI) << "In TocWidget::updateView()" << endl;
235 select(form_.currentIndex(typeCO->currentIndex()));
239 void TocWidget::updateGui()
241 vector<docstring> const & type_names = form_.typeNames();
242 if (type_names.empty()) {
243 enableControls(false);
245 tocTV->setModel(new QStandardItemModel);
246 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
250 QString current_text = typeCO->currentText();
251 //lyxerr << "current_text " << fromqstr(current_text) << endl;
252 typeCO->blockSignals(true);
254 int current_type = -1;
255 for (size_t i = 0; i != type_names.size(); ++i) {
256 QString item = toqstr(type_names[i]);
257 typeCO->addItem(item);
258 if (item == current_text)
261 if (current_type != -1)
262 typeCO->setCurrentIndex(current_type);
264 typeCO->setCurrentIndex(form_.selectedType());
265 typeCO->blockSignals(false);
267 setTocModel(typeCO->currentIndex());
271 void TocWidget::setTocModel(size_t type)
273 bool controls_enabled = false;
274 QStandardItemModel * toc_model = form_.tocModel(type);
276 controls_enabled = toc_model->rowCount() > 0;
277 tocTV->setModel(toc_model);
278 tocTV->setEditTriggers(QAbstractItemView::NoEditTriggers);
281 enableControls(controls_enabled);
283 reconnectSelectionModel();
285 if (controls_enabled) {
286 depthSL->setMaximum(form_.getTocDepth(type));
287 depthSL->setValue(depth_);
290 LYXERR(Debug::GUI) << "In TocWidget::updateGui()" << endl;
292 select(form_.currentIndex(typeCO->currentIndex()));
296 << "tocModel()->rowCount "
297 << toc_model->rowCount()
298 << "\nform_->tocModel()->columnCount "
299 << toc_model->columnCount()
305 void TocWidget::reconnectSelectionModel()
307 connect(tocTV->selectionModel(),
308 SIGNAL(currentChanged(const QModelIndex &,
309 const QModelIndex &)),
311 SLOT(selectionChanged(const QModelIndex &,
312 const QModelIndex &)));
315 void TocWidget::disconnectSelectionModel()
317 disconnect(tocTV->selectionModel(),
318 SIGNAL(currentChanged(QModelIndex, QModelIndex)),
319 this, SLOT(selectionChanged(QModelIndex, QModelIndex)));
322 } // namespace frontend
325 #include "TocWidget_moc.cpp"