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 #if QT_VERSION >= 0x040300
80 sorted_model_->setSortLocaleAware(true);
82 sorted_model_->setSourceModel(model_);
86 QAbstractItemModel * TocModel::model()
94 QAbstractItemModel const * TocModel::model() const
102 void TocModel::clear()
104 model_->blockSignals(true);
106 model_->blockSignals(false);
110 void TocModel::sort(bool sort_it)
112 is_sorted_ = sort_it;
114 sorted_model_->sort(0);
118 TocItem const & TocModel::tocItem(QModelIndex const & index) const
120 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
124 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
127 return QModelIndex();
129 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
131 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
132 QVariant(toc_index), 1,
133 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
135 LASSERT(!list.isEmpty(), return QModelIndex());
140 void TocModel::reset()
146 void TocModel::updateItem(DocIterator const & dit)
148 QModelIndex index = modelIndex(dit);
149 TocItem const & toc_item = tocItem(index);
150 model_->setData(index, toqstr(toc_item.str()), Qt::DisplayRole);
154 void TocModel::reset(Toc const & toc)
164 model_->blockSignals(true);
165 model_->beginResetModel();
166 model_->insertColumns(0, 1);
170 size_t end = toc_->size();
171 for (unsigned int index = 0; index != end; ++index) {
172 TocItem const & item = (*toc_)[index];
173 maxdepth_ = max(maxdepth_, item.depth());
174 mindepth_ = min(mindepth_, item.depth());
175 int current_row = model_->rowCount();
176 model_->insertRows(current_row, 1);
177 QModelIndex top_level_item = model_->index(current_row, 0);
178 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
179 model_->setData(top_level_item, index, Qt::UserRole);
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);
191 sorted_model_->sort(0);
192 model_->blockSignals(false);
193 model_->endResetModel();
194 // emit headerDataChanged();
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 populate(index, child_item);
228 int TocModel::modelDepth() const
230 int const d = maxdepth_ - mindepth_;
231 LASSERT(d >= 0 && d <= 100, return 0);
236 ///////////////////////////////////////////////////////////////////////////////
240 ///////////////////////////////////////////////////////////////////////////////
242 TocModels::TocModels()
245 names_ = new TocTypeModel(this);
246 names_sorted_ = new QSortFilterProxyModel(this);
247 names_sorted_->setSourceModel(names_);
248 #if QT_VERSION >= 0x040300
249 names_sorted_->setSortLocaleAware(true);
251 names_sorted_->sort(0);
255 void TocModels::clear()
257 names_->blockSignals(true);
259 names_->blockSignals(false);
260 iterator end = models_.end();
261 for (iterator it = models_.begin(); it != end; ++it)
266 int TocModels::depth(QString const & type)
268 const_iterator it = models_.find(type);
269 if (!bv_ || it == models_.end())
271 return it.value()->modelDepth();
275 QAbstractItemModel * TocModels::model(QString const & type)
279 iterator it = models_.find(type);
280 if (it != models_.end())
281 return it.value()->model();
282 LYXERR0("type not found: " << type);
287 QAbstractItemModel * TocModels::nameModel()
289 return names_sorted_;
293 QModelIndex TocModels::currentIndex(QString const & type) const
295 const_iterator it = models_.find(type);
296 if (!bv_ || it == models_.end())
297 return QModelIndex();
298 return it.value()->modelIndex(bv_->cursor());
302 void TocModels::goTo(QString const & type, QModelIndex const & index) const
304 const_iterator it = models_.find(type);
305 if (it == models_.end() || !index.isValid()) {
306 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
309 LASSERT(index.model() == it.value()->model(), return);
310 TocItem const item = it.value()->tocItem(index);
311 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
312 dispatch(item.action());
316 TocItem const TocModels::currentItem(QString const & type,
317 QModelIndex const & index) const
319 const_iterator it = models_.find(type);
320 if (it == models_.end() || !index.isValid()) {
321 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
324 LASSERT(index.model() == it.value()->model(), return TocItem());
326 return it.value()->tocItem(index);
330 void TocModels::updateBackend() const
332 bv_->buffer().masterBuffer()->tocBackend().update();
333 bv_->buffer().structureChanged();
337 void TocModels::updateItem(QString const & type, DocIterator const & dit)
339 models_[type]->updateItem(dit);
343 void TocModels::reset(BufferView const * bv)
348 iterator end = models_.end();
349 for (iterator it = models_.begin(); it != end; ++it)
355 names_->blockSignals(true);
356 names_->beginResetModel();
357 names_->insertColumns(0, 1);
358 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
359 TocList::const_iterator it = tocs.begin();
360 TocList::const_iterator toc_end = tocs.end();
361 for (; it != toc_end; ++it) {
362 QString const type = toqstr(it->first);
364 // First, fill in the toc models.
365 iterator mod_it = models_.find(type);
366 if (mod_it == models_.end())
367 mod_it = models_.insert(type, new TocModel(this));
368 mod_it.value()->reset(it->second);
370 // Fill in the names_ model.
371 QString const gui_name = guiName(it->first, bv->buffer().params());
372 int const current_row = names_->rowCount();
373 names_->insertRows(current_row, 1);
374 QModelIndex const index = names_->index(current_row, 0);
375 names_->setData(index, gui_name, Qt::DisplayRole);
376 names_->setData(index, type, Qt::UserRole);
378 names_->blockSignals(false);
379 names_->endResetModel();
383 bool TocModels::isSorted(QString const & type) const
385 const_iterator it = models_.find(type);
386 if (it == models_.end()) {
387 LYXERR0("type not found: " << type);
390 return it.value()->isSorted();
394 void TocModels::sort(QString const & type, bool sort_it)
396 iterator it = models_.find(type);
397 if (it == models_.end())
398 LYXERR0("type not found: " << type);
400 it.value()->sort(sort_it);
403 } // namespace frontend
406 #include "moc_TocModel.cpp"