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 // FIXME: Better appropriately subclass QStandardItemModel and implement
42 // the toc-specific reset methods there.
43 class TocTypeModel : public QStandardItemModel
47 TocTypeModel(QObject * parent) : QStandardItemModel(parent)
52 QStandardItemModel::beginResetModel();
53 QStandardItemModel::endResetModel();
56 void beginResetModel()
58 QStandardItemModel::beginResetModel();
63 QStandardItemModel::endResetModel();
68 ///////////////////////////////////////////////////////////////////////////////
72 ///////////////////////////////////////////////////////////////////////////////
74 TocModel::TocModel(QObject * parent)
75 : model_(new TocTypeModel(parent)),
76 sorted_model_(new QSortFilterProxyModel(parent)),
77 is_sorted_(false), toc_(new Toc()),
78 maxdepth_(0), mindepth_(0)
80 sorted_model_->setSortLocaleAware(true);
81 sorted_model_->setSourceModel(model_);
85 QAbstractItemModel * TocModel::model()
93 QAbstractItemModel const * TocModel::model() const
101 void TocModel::clear()
103 model_->blockSignals(true);
105 toc_ = make_shared<Toc>();
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 = TocBackend::findItem(*toc_, dit) -
132 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
133 QVariant(toc_index), 1,
134 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
136 LASSERT(!list.isEmpty(), return QModelIndex());
141 void TocModel::reset()
147 void TocModel::updateItem(DocIterator const & dit)
149 QModelIndex index = modelIndex(dit);
150 TocItem const & toc_item = tocItem(index);
151 model_->setData(index, toqstr(toc_item.asString()), Qt::DisplayRole);
152 model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
156 void TocModel::reset(shared_ptr<Toc const> toc)
166 model_->blockSignals(true);
167 model_->beginResetModel();
168 model_->insertColumns(0, 1);
172 size_t end = toc_->size();
173 for (unsigned int index = 0; index != end; ++index) {
174 TocItem const & item = (*toc_)[index];
175 maxdepth_ = max(maxdepth_, item.depth());
176 mindepth_ = min(mindepth_, item.depth());
177 int current_row = model_->rowCount();
178 model_->insertRows(current_row, 1);
179 QModelIndex top_level_item = model_->index(current_row, 0);
180 model_->setData(top_level_item, toqstr(item.asString()), Qt::DisplayRole);
181 model_->setData(top_level_item, index, Qt::UserRole);
182 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
184 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
185 << ", added item " << item.asString());
187 populate(index, top_level_item);
192 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
193 sorted_model_->setSourceModel(model_);
195 sorted_model_->sort(0);
196 model_->blockSignals(false);
197 model_->endResetModel();
201 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
203 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 int 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.asString()), 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 names_sorted_->setSortLocaleAware(true);
252 names_sorted_->sort(0);
256 void TocModels::clear()
258 names_->blockSignals(true);
260 names_->blockSignals(false);
261 iterator end = models_.end();
262 for (iterator it = models_.begin(); it != end; ++it)
267 int TocModels::depth(QString const & type)
269 const_iterator it = models_.find(type);
270 if (!bv_ || it == models_.end())
272 return it.value()->modelDepth();
276 QAbstractItemModel * TocModels::model(QString const & type)
280 iterator it = models_.find(type);
281 if (it != models_.end())
282 return it.value()->model();
283 LYXERR0("type not found: " << type);
288 QAbstractItemModel * TocModels::nameModel()
290 return names_sorted_;
294 QModelIndex TocModels::currentIndex(QString const & type) const
296 const_iterator it = models_.find(type);
297 if (!bv_ || it == models_.end())
298 return QModelIndex();
299 return it.value()->modelIndex(bv_->cursor());
303 void TocModels::goTo(QString const & type, QModelIndex const & index) const
305 const_iterator it = models_.find(type);
306 if (it == models_.end() || !index.isValid()) {
307 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
310 LASSERT(index.model() == it.value()->model(), return);
311 TocItem const item = it.value()->tocItem(index);
312 LYXERR(Debug::GUI, "TocModels::goTo " << item.asString());
313 dispatch(item.action());
317 TocItem const TocModels::currentItem(QString const & type,
318 QModelIndex const & index) const
320 const_iterator it = models_.find(type);
321 if (it == models_.end() || !index.isValid()) {
322 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
325 LASSERT(index.model() == it.value()->model(), return TocItem());
327 return it.value()->tocItem(index);
331 void TocModels::updateItem(QString const & type, DocIterator const & dit)
333 models_[type]->updateItem(dit);
337 void TocModels::reset(BufferView const * bv)
342 iterator end = models_.end();
343 for (iterator it = models_.begin(); it != end; ++it)
349 names_->blockSignals(true);
350 names_->beginResetModel();
351 names_->insertColumns(0, 1);
352 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
353 TocList::const_iterator it = tocs.begin();
354 TocList::const_iterator toc_end = tocs.end();
355 for (; it != toc_end; ++it) {
356 QString const type = toqstr(it->first);
358 // First, fill in the toc models.
359 iterator mod_it = models_.find(type);
360 if (mod_it == models_.end())
361 mod_it = models_.insert(type, new TocModel(this));
362 mod_it.value()->reset(it->second);
364 // Fill in the names_ model.
365 QString const gui_name = guiName(it->first, bv->buffer().params());
366 int const current_row = names_->rowCount();
367 names_->insertRows(current_row, 1);
368 QModelIndex const index = names_->index(current_row, 0);
369 names_->setData(index, gui_name, Qt::DisplayRole);
370 names_->setData(index, type, Qt::UserRole);
372 names_->blockSignals(false);
373 names_->endResetModel();
377 bool TocModels::isSorted(QString const & type) const
379 const_iterator it = models_.find(type);
380 if (it == models_.end()) {
381 LYXERR0("type not found: " << type);
384 return it.value()->isSorted();
388 void TocModels::sort(QString const & type, bool sort_it)
390 iterator it = models_.find(type);
391 if (it == models_.end())
392 LYXERR0("type not found: " << type);
394 it.value()->sort(sort_it);
397 } // namespace frontend
400 #include "moc_TocModel.cpp"