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), maxdepth_(0), mindepth_(0)
86 sorted_model_->setSortLocaleAware(true);
87 sorted_model_->setSourceModel(model_);
91 QAbstractItemModel * TocModel::model()
99 QAbstractItemModel const * TocModel::model() const
102 return sorted_model_;
107 void TocModel::clear()
109 model_->blockSignals(true);
111 model_->blockSignals(false);
115 void TocModel::sort(bool sort_it)
117 is_sorted_ = sort_it;
119 sorted_model_->sort(0);
123 TocItem const & TocModel::tocItem(QModelIndex const & index) const
125 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
129 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
132 return QModelIndex();
134 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
136 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
137 QVariant(toc_index), 1,
138 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
140 LASSERT(!list.isEmpty(), return QModelIndex());
145 void TocModel::reset()
151 void TocModel::updateItem(DocIterator const & dit)
153 QModelIndex index = modelIndex(dit);
154 TocItem const & toc_item = tocItem(index);
155 model_->setData(index, toqstr(toc_item.str()), Qt::DisplayRole);
156 model_->setData(index, toqstr(toc_item.tooltip()), Qt::ToolTipRole);
160 void TocModel::reset(Toc const & toc)
170 model_->blockSignals(true);
171 model_->beginResetModel();
172 model_->insertColumns(0, 1);
176 size_t end = toc_->size();
177 for (unsigned int index = 0; index != end; ++index) {
178 TocItem const & item = (*toc_)[index];
179 maxdepth_ = max(maxdepth_, item.depth());
180 mindepth_ = min(mindepth_, item.depth());
181 int current_row = model_->rowCount();
182 model_->insertRows(current_row, 1);
183 QModelIndex top_level_item = model_->index(current_row, 0);
184 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
185 model_->setData(top_level_item, index, Qt::UserRole);
186 model_->setData(top_level_item, toqstr(item.tooltip()), Qt::ToolTipRole);
188 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
189 << ", added item " << item.str());
191 populate(index, top_level_item);
196 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
197 sorted_model_->setSourceModel(model_);
199 sorted_model_->sort(0);
200 model_->blockSignals(false);
201 model_->endResetModel();
205 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
207 int curdepth = (*toc_)[index].depth() + 1;
210 QModelIndex child_item;
211 model_->insertColumns(0, 1, parent);
213 size_t end = toc_->size();
215 for (; index != end; ++index) {
216 TocItem const & item = (*toc_)[index];
217 if (item.depth() < curdepth) {
221 maxdepth_ = max(maxdepth_, item.depth());
222 mindepth_ = min(mindepth_, item.depth());
223 current_row = model_->rowCount(parent);
224 model_->insertRows(current_row, 1, parent);
225 child_item = model_->index(current_row, 0, parent);
226 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
227 model_->setData(child_item, index, Qt::UserRole);
228 model_->setData(child_item, toqstr(item.tooltip()), Qt::ToolTipRole);
229 populate(index, child_item);
236 int TocModel::modelDepth() const
238 int const d = maxdepth_ - mindepth_;
239 LASSERT(d >= 0 && d <= 100, return 0);
244 ///////////////////////////////////////////////////////////////////////////////
248 ///////////////////////////////////////////////////////////////////////////////
250 TocModels::TocModels()
253 names_ = new TocTypeModel(this);
254 names_sorted_ = new TocModelSortProxyModel(this);
255 names_sorted_->setSourceModel(names_);
256 names_sorted_->setSortLocaleAware(true);
257 names_sorted_->sort(0);
261 void TocModels::clear()
263 names_->blockSignals(true);
265 names_->blockSignals(false);
266 iterator end = models_.end();
267 for (iterator it = models_.begin(); it != end; ++it)
272 int TocModels::depth(QString const & type)
274 const_iterator it = models_.find(type);
275 if (!bv_ || it == models_.end())
277 return it.value()->modelDepth();
281 QAbstractItemModel * TocModels::model(QString const & type)
285 iterator it = models_.find(type);
286 if (it != models_.end())
287 return it.value()->model();
288 LYXERR0("type not found: " << type);
293 QAbstractItemModel * TocModels::nameModel()
295 return names_sorted_;
299 QModelIndex TocModels::currentIndex(QString const & type) const
301 const_iterator it = models_.find(type);
302 if (!bv_ || it == models_.end())
303 return QModelIndex();
304 return it.value()->modelIndex(bv_->cursor());
308 void TocModels::goTo(QString const & type, QModelIndex const & index) const
310 const_iterator it = models_.find(type);
311 if (it == models_.end() || !index.isValid()) {
312 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
315 LASSERT(index.model() == it.value()->model(), return);
316 TocItem const item = it.value()->tocItem(index);
317 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
318 dispatch(item.action());
322 TocItem const TocModels::currentItem(QString const & type,
323 QModelIndex const & index) const
325 const_iterator it = models_.find(type);
326 if (it == models_.end() || !index.isValid()) {
327 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
330 LASSERT(index.model() == it.value()->model(), return TocItem());
332 return it.value()->tocItem(index);
336 void TocModels::updateItem(QString const & type, DocIterator const & dit)
338 models_[type]->updateItem(dit);
342 void TocModels::reset(BufferView const * bv)
347 iterator end = models_.end();
348 for (iterator it = models_.begin(); it != end; ++it)
354 names_->blockSignals(true);
355 names_->beginResetModel();
356 names_->insertColumns(0, 1);
357 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
358 TocList::const_iterator it = tocs.begin();
359 TocList::const_iterator toc_end = tocs.end();
360 for (; it != toc_end; ++it) {
361 QString const type = toqstr(it->first);
363 // First, fill in the toc models.
364 iterator mod_it = models_.find(type);
365 if (mod_it == models_.end())
366 mod_it = models_.insert(type, new TocModel(this));
367 mod_it.value()->reset(it->second);
369 // Fill in the names_ model.
370 QString const gui_name = guiName(it->first, bv->buffer().params());
371 int const current_row = names_->rowCount();
372 names_->insertRows(current_row, 1);
373 QModelIndex const index = names_->index(current_row, 0);
374 names_->setData(index, gui_name, Qt::DisplayRole);
375 names_->setData(index, type, Qt::UserRole);
377 names_->blockSignals(false);
378 names_->endResetModel();
382 bool TocModels::isSorted(QString const & type) const
384 const_iterator it = models_.find(type);
385 if (it == models_.end()) {
386 LYXERR0("type not found: " << type);
389 return it.value()->isSorted();
393 void TocModels::sort(QString const & type, bool sort_it)
395 iterator it = models_.find(type);
396 if (it == models_.end())
397 LYXERR0("type not found: " << type);
399 it.value()->sort(sort_it);
402 } // namespace frontend
405 #include "moc_TocModel.cpp"