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 "qt_helpers.h"
23 #include "TocBackend.h"
25 #include "support/debug.h"
26 #include "support/lassert.h"
28 #include <QSortFilterProxyModel>
29 #include <QStandardItemModel>
39 /// A QStandardItemModel that gives access to the reset methods.
40 /// This is needed in order to fix http://www.lyx.org/trac/ticket/3740
41 class TocTypeModel : public QStandardItemModel
45 TocTypeModel(QObject * parent) : QStandardItemModel(parent)
50 #if (QT_VERSION < 0x050000)
51 QStandardItemModel::reset();
53 QStandardItemModel::endResetModel();
57 void beginResetModel()
59 #if QT_VERSION >= 0x040600
60 QStandardItemModel::beginResetModel();
66 #if QT_VERSION >= 0x040600
67 QStandardItemModel::endResetModel();
69 QStandardItemModel::reset();
75 ///////////////////////////////////////////////////////////////////////////////
79 ///////////////////////////////////////////////////////////////////////////////
81 TocModel::TocModel(QObject * parent)
82 : model_(new TocTypeModel(parent)),
83 sorted_model_(new QSortFilterProxyModel(parent)),
84 is_sorted_(false), toc_(lyx::make_shared<Toc const>()),
85 maxdepth_(0), mindepth_(0)
87 sorted_model_->setSortLocaleAware(true);
88 sorted_model_->setSourceModel(model_);
92 QAbstractItemModel * TocModel::model()
100 QAbstractItemModel const * TocModel::model() const
103 return sorted_model_;
108 void TocModel::clear()
110 model_->blockSignals(true);
112 toc_ = lyx::make_shared<Toc const>();
113 model_->blockSignals(false);
117 void TocModel::sort(bool sort_it)
119 is_sorted_ = sort_it;
121 sorted_model_->sort(0);
125 TocItem const & TocModel::tocItem(QModelIndex const & index) const
127 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
131 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
134 return QModelIndex();
136 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
138 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
139 QVariant(toc_index), 1,
140 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
142 LASSERT(!list.isEmpty(), return QModelIndex());
147 void TocModel::reset()
153 void TocModel::updateItem(DocIterator const & dit)
155 QModelIndex index = modelIndex(dit);
156 TocItem const & toc_item = tocItem(index);
157 model_->setData(index, toqstr(toc_item.asString()), Qt::DisplayRole);
158 model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
162 void TocModel::reset(shared_ptr<Toc const> toc)
172 model_->blockSignals(true);
173 model_->beginResetModel();
174 model_->insertColumns(0, 1);
178 size_t end = toc_->size();
179 for (unsigned int index = 0; index != end; ++index) {
180 TocItem const & item = (*toc_)[index];
181 maxdepth_ = max(maxdepth_, item.depth());
182 mindepth_ = min(mindepth_, item.depth());
183 int current_row = model_->rowCount();
184 model_->insertRows(current_row, 1);
185 QModelIndex top_level_item = model_->index(current_row, 0);
186 model_->setData(top_level_item, toqstr(item.asString()), Qt::DisplayRole);
187 model_->setData(top_level_item, index, Qt::UserRole);
188 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
190 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
191 << ", added item " << item.asString());
193 populate(index, top_level_item);
198 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
199 sorted_model_->setSourceModel(model_);
201 sorted_model_->sort(0);
202 model_->blockSignals(false);
203 model_->endResetModel();
207 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
209 int curdepth = (*toc_)[index].depth() + 1;
211 QModelIndex child_item;
212 model_->insertColumns(0, 1, parent);
214 size_t end = toc_->size();
216 for (; index != end; ++index) {
217 TocItem const & item = (*toc_)[index];
218 if (item.depth() < curdepth) {
222 maxdepth_ = max(maxdepth_, item.depth());
223 mindepth_ = min(mindepth_, item.depth());
224 int current_row = model_->rowCount(parent);
225 model_->insertRows(current_row, 1, parent);
226 child_item = model_->index(current_row, 0, parent);
227 model_->setData(child_item, toqstr(item.asString()), Qt::DisplayRole);
228 model_->setData(child_item, index, Qt::UserRole);
229 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
230 populate(index, child_item);
237 int TocModel::modelDepth() const
239 int const d = maxdepth_ - mindepth_;
240 LASSERT(d >= 0 && d <= 100, return 0);
245 ///////////////////////////////////////////////////////////////////////////////
249 ///////////////////////////////////////////////////////////////////////////////
251 TocModels::TocModels()
254 names_ = new TocTypeModel(this);
255 names_sorted_ = new TocModelSortProxyModel(this);
256 names_sorted_->setSourceModel(names_);
257 names_sorted_->setSortLocaleAware(true);
258 names_sorted_->sort(0);
262 void TocModels::clear()
264 names_->blockSignals(true);
266 names_->blockSignals(false);
267 iterator end = models_.end();
268 for (iterator it = models_.begin(); it != end; ++it)
273 int TocModels::depth(QString const & type)
275 const_iterator it = models_.find(type);
276 if (!bv_ || it == models_.end())
278 return it.value()->modelDepth();
282 QAbstractItemModel * TocModels::model(QString const & type)
286 iterator it = models_.find(type);
287 if (it != models_.end())
288 return it.value()->model();
289 LYXERR0("type not found: " << type);
294 QAbstractItemModel * TocModels::nameModel()
296 return names_sorted_;
300 QModelIndex TocModels::currentIndex(QString const & type) const
302 const_iterator it = models_.find(type);
303 if (!bv_ || it == models_.end())
304 return QModelIndex();
305 return it.value()->modelIndex(bv_->cursor());
309 void TocModels::goTo(QString const & type, QModelIndex const & index) const
311 const_iterator it = models_.find(type);
312 if (it == models_.end() || !index.isValid()) {
313 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
316 LASSERT(index.model() == it.value()->model(), return);
317 TocItem const item = it.value()->tocItem(index);
318 LYXERR(Debug::GUI, "TocModels::goTo " << item.asString());
319 dispatch(item.action());
323 TocItem const TocModels::currentItem(QString const & type,
324 QModelIndex const & index) const
326 const_iterator it = models_.find(type);
327 if (it == models_.end() || !index.isValid()) {
328 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
331 LASSERT(index.model() == it.value()->model(), return TocItem());
333 return it.value()->tocItem(index);
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"