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);
151 model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
155 void TocModel::reset(Toc const & toc)
165 model_->blockSignals(true);
166 model_->beginResetModel();
167 model_->insertColumns(0, 1);
171 size_t end = toc_->size();
172 for (unsigned int index = 0; index != end; ++index) {
173 TocItem const & item = (*toc_)[index];
174 maxdepth_ = max(maxdepth_, item.depth());
175 mindepth_ = min(mindepth_, item.depth());
176 int current_row = model_->rowCount();
177 model_->insertRows(current_row, 1);
178 QModelIndex top_level_item = model_->index(current_row, 0);
179 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
180 model_->setData(top_level_item, index, Qt::UserRole);
181 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
183 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
184 << ", added item " << item.str());
186 populate(index, top_level_item);
191 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
192 sorted_model_->setSourceModel(model_);
194 sorted_model_->sort(0);
195 model_->blockSignals(false);
196 model_->endResetModel();
200 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
202 int curdepth = (*toc_)[index].depth() + 1;
205 QModelIndex child_item;
206 model_->insertColumns(0, 1, parent);
208 size_t end = toc_->size();
210 for (; index != end; ++index) {
211 TocItem const & item = (*toc_)[index];
212 if (item.depth() < curdepth) {
216 maxdepth_ = max(maxdepth_, item.depth());
217 mindepth_ = min(mindepth_, item.depth());
218 current_row = model_->rowCount(parent);
219 model_->insertRows(current_row, 1, parent);
220 child_item = model_->index(current_row, 0, parent);
221 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
222 model_->setData(child_item, index, Qt::UserRole);
223 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
224 populate(index, child_item);
231 int TocModel::modelDepth() const
233 int const d = maxdepth_ - mindepth_;
234 LASSERT(d >= 0 && d <= 100, return 0);
239 ///////////////////////////////////////////////////////////////////////////////
243 ///////////////////////////////////////////////////////////////////////////////
245 TocModels::TocModels()
248 names_ = new TocTypeModel(this);
249 names_sorted_ = new TocModelSortProxyModel(this);
250 names_sorted_->setSourceModel(names_);
251 #if QT_VERSION >= 0x040300
252 names_sorted_->setSortLocaleAware(true);
254 names_sorted_->sort(0);
258 void TocModels::clear()
260 names_->blockSignals(true);
262 names_->blockSignals(false);
263 iterator end = models_.end();
264 for (iterator it = models_.begin(); it != end; ++it)
269 int TocModels::depth(QString const & type)
271 const_iterator it = models_.find(type);
272 if (!bv_ || it == models_.end())
274 return it.value()->modelDepth();
278 QAbstractItemModel * TocModels::model(QString const & type)
282 iterator it = models_.find(type);
283 if (it != models_.end())
284 return it.value()->model();
285 LYXERR0("type not found: " << type);
290 QAbstractItemModel * TocModels::nameModel()
292 return names_sorted_;
296 QModelIndex TocModels::currentIndex(QString const & type) const
298 const_iterator it = models_.find(type);
299 if (!bv_ || it == models_.end())
300 return QModelIndex();
301 return it.value()->modelIndex(bv_->cursor());
305 void TocModels::goTo(QString const & type, QModelIndex const & index) const
307 const_iterator it = models_.find(type);
308 if (it == models_.end() || !index.isValid()) {
309 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
312 LASSERT(index.model() == it.value()->model(), return);
313 TocItem const item = it.value()->tocItem(index);
314 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
315 dispatch(item.action());
319 TocItem const TocModels::currentItem(QString const & type,
320 QModelIndex const & index) const
322 const_iterator it = models_.find(type);
323 if (it == models_.end() || !index.isValid()) {
324 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
327 LASSERT(index.model() == it.value()->model(), return TocItem());
329 return it.value()->tocItem(index);
333 void TocModels::updateBackend() const
335 bv_->buffer().masterBuffer()->tocBackend().update();
336 bv_->buffer().structureChanged();
340 void TocModels::updateItem(QString const & type, DocIterator const & dit)
342 models_[type]->updateItem(dit);
346 void TocModels::reset(BufferView const * bv)
351 iterator end = models_.end();
352 for (iterator it = models_.begin(); it != end; ++it)
358 names_->blockSignals(true);
359 names_->beginResetModel();
360 names_->insertColumns(0, 1);
361 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
362 TocList::const_iterator it = tocs.begin();
363 TocList::const_iterator toc_end = tocs.end();
364 for (; it != toc_end; ++it) {
365 QString const type = toqstr(it->first);
367 // First, fill in the toc models.
368 iterator mod_it = models_.find(type);
369 if (mod_it == models_.end())
370 mod_it = models_.insert(type, new TocModel(this));
371 mod_it.value()->reset(it->second);
373 // Fill in the names_ model.
374 QString const gui_name = guiName(it->first, bv->buffer().params());
375 int const current_row = names_->rowCount();
376 names_->insertRows(current_row, 1);
377 QModelIndex const index = names_->index(current_row, 0);
378 names_->setData(index, gui_name, Qt::DisplayRole);
379 names_->setData(index, type, Qt::UserRole);
381 names_->blockSignals(false);
382 names_->endResetModel();
386 bool TocModels::isSorted(QString const & type) const
388 const_iterator it = models_.find(type);
389 if (it == models_.end()) {
390 LYXERR0("type not found: " << type);
393 return it.value()->isSorted();
397 void TocModels::sort(QString const & type, bool sort_it)
399 iterator it = models_.find(type);
400 if (it == models_.end())
401 LYXERR0("type not found: " << type);
403 it.value()->sort(sort_it);
406 } // namespace frontend
409 #include "moc_TocModel.cpp"