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.
17 #include "BufferView.h"
19 #include "DocIterator.h"
20 #include "FuncRequest.h"
22 #include "TocBackend.h"
24 #include "support/debug.h"
25 #include "support/lassert.h"
27 #include <QSortFilterProxyModel>
36 /// A QStandardItemModel that gives access to the reset methods.
37 /// This is needed in order to fix http://www.lyx.org/trac/ticket/3740
38 class TocTypeModel : public QStandardItemModel
42 TocTypeModel(QObject * parent) : QStandardItemModel(parent)
47 QStandardItemModel::reset();
50 void beginResetModel()
52 #if QT_VERSION >= 0x040600
53 QStandardItemModel::beginResetModel();
59 #if QT_VERSION >= 0x040600
60 QStandardItemModel::endResetModel();
62 QStandardItemModel::reset();
68 ///////////////////////////////////////////////////////////////////////////////
72 ///////////////////////////////////////////////////////////////////////////////
74 TocModel::TocModel(QObject * parent)
75 : model_(new TocTypeModel(parent)),
76 sorted_model_(new QSortFilterProxyModel(parent)),
77 is_sorted_(false), maxdepth_(0), mindepth_(0)
79 sorted_model_->setSortLocaleAware(true);
80 sorted_model_->setSourceModel(model_);
84 QAbstractItemModel * TocModel::model()
92 QAbstractItemModel const * TocModel::model() const
100 void TocModel::clear()
102 model_->blockSignals(true);
104 model_->blockSignals(false);
108 void TocModel::sort(bool sort_it)
110 is_sorted_ = sort_it;
112 sorted_model_->sort(0);
116 TocItem const & TocModel::tocItem(QModelIndex const & index) const
118 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
122 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
125 return QModelIndex();
127 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
129 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
130 QVariant(toc_index), 1,
131 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
133 LASSERT(!list.isEmpty(), return QModelIndex());
138 void TocModel::reset()
144 void TocModel::updateItem(DocIterator const & dit)
146 QModelIndex index = modelIndex(dit);
147 TocItem const & toc_item = tocItem(index);
148 model_->setData(index, toqstr(toc_item.str()), Qt::DisplayRole);
149 model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
153 void TocModel::reset(Toc const & toc)
163 model_->blockSignals(true);
164 model_->beginResetModel();
165 model_->insertColumns(0, 1);
169 size_t end = toc_->size();
170 for (unsigned int index = 0; index != end; ++index) {
171 TocItem const & item = (*toc_)[index];
172 maxdepth_ = max(maxdepth_, item.depth());
173 mindepth_ = min(mindepth_, item.depth());
174 int current_row = model_->rowCount();
175 model_->insertRows(current_row, 1);
176 QModelIndex top_level_item = model_->index(current_row, 0);
177 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
178 model_->setData(top_level_item, index, Qt::UserRole);
179 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
181 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
182 << ", added item " << item.str());
184 populate(index, top_level_item);
189 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
190 sorted_model_->setSourceModel(model_);
192 sorted_model_->sort(0);
193 model_->blockSignals(false);
194 model_->endResetModel();
198 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
200 int curdepth = (*toc_)[index].depth() + 1;
203 QModelIndex child_item;
204 model_->insertColumns(0, 1, parent);
206 size_t end = toc_->size();
208 for (; index != end; ++index) {
209 TocItem const & item = (*toc_)[index];
210 if (item.depth() < curdepth) {
214 maxdepth_ = max(maxdepth_, item.depth());
215 mindepth_ = min(mindepth_, item.depth());
216 current_row = model_->rowCount(parent);
217 model_->insertRows(current_row, 1, parent);
218 child_item = model_->index(current_row, 0, parent);
219 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
220 model_->setData(child_item, index, Qt::UserRole);
221 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
222 populate(index, child_item);
229 int TocModel::modelDepth() const
231 int const d = maxdepth_ - mindepth_;
232 LASSERT(d >= 0 && d <= 100, return 0);
237 ///////////////////////////////////////////////////////////////////////////////
241 ///////////////////////////////////////////////////////////////////////////////
243 TocModels::TocModels()
246 names_ = new TocTypeModel(this);
247 names_sorted_ = new TocModelSortProxyModel(this);
248 names_sorted_->setSourceModel(names_);
249 names_sorted_->setSortLocaleAware(true);
250 names_sorted_->sort(0);
254 void TocModels::clear()
256 names_->blockSignals(true);
258 names_->blockSignals(false);
259 iterator end = models_.end();
260 for (iterator it = models_.begin(); it != end; ++it)
265 int TocModels::depth(QString const & type)
267 const_iterator it = models_.find(type);
268 if (!bv_ || it == models_.end())
270 return it.value()->modelDepth();
274 QAbstractItemModel * TocModels::model(QString const & type)
278 iterator it = models_.find(type);
279 if (it != models_.end())
280 return it.value()->model();
281 LYXERR0("type not found: " << type);
286 QAbstractItemModel * TocModels::nameModel()
288 return names_sorted_;
292 QModelIndex TocModels::currentIndex(QString const & type) const
294 const_iterator it = models_.find(type);
295 if (!bv_ || it == models_.end())
296 return QModelIndex();
297 return it.value()->modelIndex(bv_->cursor());
301 void TocModels::goTo(QString const & type, QModelIndex const & index) const
303 const_iterator it = models_.find(type);
304 if (it == models_.end() || !index.isValid()) {
305 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
308 LASSERT(index.model() == it.value()->model(), return);
309 TocItem const item = it.value()->tocItem(index);
310 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
311 dispatch(item.action());
315 TocItem const TocModels::currentItem(QString const & type,
316 QModelIndex const & index) const
318 const_iterator it = models_.find(type);
319 if (it == models_.end() || !index.isValid()) {
320 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
323 LASSERT(index.model() == it.value()->model(), return TocItem());
325 return it.value()->tocItem(index);
329 void TocModels::updateBackend() const
331 bv_->buffer().masterBuffer()->tocBackend().update();
332 bv_->buffer().structureChanged();
336 void TocModels::updateItem(QString const & type, DocIterator const & dit)
338 models_[type]->updateItem(dit);
342 void TocModels::reset(BufferView const * bv)
347 iterator end = models_.end();
348 for (iterator it = models_.begin(); it != end; ++it)
354 names_->blockSignals(true);
355 names_->beginResetModel();
356 names_->insertColumns(0, 1);
357 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
358 TocList::const_iterator it = tocs.begin();
359 TocList::const_iterator toc_end = tocs.end();
360 for (; it != toc_end; ++it) {
361 QString const type = toqstr(it->first);
363 // First, fill in the toc models.
364 iterator mod_it = models_.find(type);
365 if (mod_it == models_.end())
366 mod_it = models_.insert(type, new TocModel(this));
367 mod_it.value()->reset(it->second);
369 // Fill in the names_ model.
370 QString const gui_name = guiName(it->first, bv->buffer().params());
371 int const current_row = names_->rowCount();
372 names_->insertRows(current_row, 1);
373 QModelIndex const index = names_->index(current_row, 0);
374 names_->setData(index, gui_name, Qt::DisplayRole);
375 names_->setData(index, type, Qt::UserRole);
377 names_->blockSignals(false);
378 names_->endResetModel();
382 bool TocModels::isSorted(QString const & type) const
384 const_iterator it = models_.find(type);
385 if (it == models_.end()) {
386 LYXERR0("type not found: " << type);
389 return it.value()->isSorted();
393 void TocModels::sort(QString const & type, bool sort_it)
395 iterator it = models_.find(type);
396 if (it == models_.end())
397 LYXERR0("type not found: " << type);
399 it.value()->sort(sort_it);
402 } // namespace frontend
405 #include "moc_TocModel.cpp"