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 = TocBackend::findItem(*toc_, dit) -
139 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
140 QVariant(toc_index), 1,
141 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
143 LASSERT(!list.isEmpty(), return QModelIndex());
148 void TocModel::reset()
154 void TocModel::updateItem(DocIterator const & dit)
156 QModelIndex index = modelIndex(dit);
157 TocItem const & toc_item = tocItem(index);
158 model_->setData(index, toqstr(toc_item.asString()), Qt::DisplayRole);
159 model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
163 void TocModel::reset(shared_ptr<Toc const> toc)
173 model_->blockSignals(true);
174 model_->beginResetModel();
175 model_->insertColumns(0, 1);
179 size_t end = toc_->size();
180 for (unsigned int index = 0; index != end; ++index) {
181 TocItem const & item = (*toc_)[index];
182 maxdepth_ = max(maxdepth_, item.depth());
183 mindepth_ = min(mindepth_, item.depth());
184 int current_row = model_->rowCount();
185 model_->insertRows(current_row, 1);
186 QModelIndex top_level_item = model_->index(current_row, 0);
187 model_->setData(top_level_item, toqstr(item.asString()), Qt::DisplayRole);
188 model_->setData(top_level_item, index, Qt::UserRole);
189 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
191 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
192 << ", added item " << item.asString());
194 populate(index, top_level_item);
199 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
200 sorted_model_->setSourceModel(model_);
202 sorted_model_->sort(0);
203 model_->blockSignals(false);
204 model_->endResetModel();
208 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
210 int curdepth = (*toc_)[index].depth() + 1;
212 QModelIndex child_item;
213 model_->insertColumns(0, 1, parent);
215 size_t end = toc_->size();
217 for (; index != end; ++index) {
218 TocItem const & item = (*toc_)[index];
219 if (item.depth() < curdepth) {
223 maxdepth_ = max(maxdepth_, item.depth());
224 mindepth_ = min(mindepth_, item.depth());
225 int current_row = model_->rowCount(parent);
226 model_->insertRows(current_row, 1, parent);
227 child_item = model_->index(current_row, 0, parent);
228 model_->setData(child_item, toqstr(item.asString()), Qt::DisplayRole);
229 model_->setData(child_item, index, Qt::UserRole);
230 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
231 populate(index, child_item);
238 int TocModel::modelDepth() const
240 int const d = maxdepth_ - mindepth_;
241 LASSERT(d >= 0 && d <= 100, return 0);
246 ///////////////////////////////////////////////////////////////////////////////
250 ///////////////////////////////////////////////////////////////////////////////
252 TocModels::TocModels()
255 names_ = new TocTypeModel(this);
256 names_sorted_ = new TocModelSortProxyModel(this);
257 names_sorted_->setSourceModel(names_);
258 names_sorted_->setSortLocaleAware(true);
259 names_sorted_->sort(0);
263 void TocModels::clear()
265 names_->blockSignals(true);
267 names_->blockSignals(false);
268 iterator end = models_.end();
269 for (iterator it = models_.begin(); it != end; ++it)
274 int TocModels::depth(QString const & type)
276 const_iterator it = models_.find(type);
277 if (!bv_ || it == models_.end())
279 return it.value()->modelDepth();
283 QAbstractItemModel * TocModels::model(QString const & type)
287 iterator it = models_.find(type);
288 if (it != models_.end())
289 return it.value()->model();
290 LYXERR0("type not found: " << type);
295 QAbstractItemModel * TocModels::nameModel()
297 return names_sorted_;
301 QModelIndex TocModels::currentIndex(QString const & type) const
303 const_iterator it = models_.find(type);
304 if (!bv_ || it == models_.end())
305 return QModelIndex();
306 return it.value()->modelIndex(bv_->cursor());
310 void TocModels::goTo(QString const & type, QModelIndex const & index) const
312 const_iterator it = models_.find(type);
313 if (it == models_.end() || !index.isValid()) {
314 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
317 LASSERT(index.model() == it.value()->model(), return);
318 TocItem const item = it.value()->tocItem(index);
319 LYXERR(Debug::GUI, "TocModels::goTo " << item.asString());
320 dispatch(item.action());
324 TocItem const TocModels::currentItem(QString const & type,
325 QModelIndex const & index) const
327 const_iterator it = models_.find(type);
328 if (it == models_.end() || !index.isValid()) {
329 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
332 LASSERT(index.model() == it.value()->model(), return TocItem());
334 return it.value()->tocItem(index);
338 void TocModels::updateItem(QString const & type, DocIterator const & dit)
340 models_[type]->updateItem(dit);
344 void TocModels::reset(BufferView const * bv)
349 iterator end = models_.end();
350 for (iterator it = models_.begin(); it != end; ++it)
356 names_->blockSignals(true);
357 names_->beginResetModel();
358 names_->insertColumns(0, 1);
359 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
360 TocList::const_iterator it = tocs.begin();
361 TocList::const_iterator toc_end = tocs.end();
362 for (; it != toc_end; ++it) {
363 QString const type = toqstr(it->first);
365 // First, fill in the toc models.
366 iterator mod_it = models_.find(type);
367 if (mod_it == models_.end())
368 mod_it = models_.insert(type, new TocModel(this));
369 mod_it.value()->reset(it->second);
371 // Fill in the names_ model.
372 QString const gui_name = guiName(it->first, bv->buffer().params());
373 int const current_row = names_->rowCount();
374 names_->insertRows(current_row, 1);
375 QModelIndex const index = names_->index(current_row, 0);
376 names_->setData(index, gui_name, Qt::DisplayRole);
377 names_->setData(index, type, Qt::UserRole);
379 names_->blockSignals(false);
380 names_->endResetModel();
384 bool TocModels::isSorted(QString const & type) const
386 const_iterator it = models_.find(type);
387 if (it == models_.end()) {
388 LYXERR0("type not found: " << type);
391 return it.value()->isSorted();
395 void TocModels::sort(QString const & type, bool sort_it)
397 iterator it = models_.find(type);
398 if (it == models_.end())
399 LYXERR0("type not found: " << type);
401 it.value()->sort(sort_it);
404 } // namespace frontend
407 #include "moc_TocModel.cpp"