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 =
130 static_cast<unsigned int>(TocBackend::findItem(*toc_, dit) - toc_->begin());
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::setString(TocItem const & item, QModelIndex index)
149 // Use implicit sharing of QStrings
150 QString str = toqstr(item.asString());
151 model_->setData(index, str, Qt::DisplayRole);
152 model_->setData(index, str, Qt::ToolTipRole);
156 void TocModel::updateItem(DocIterator const & dit)
158 QModelIndex const index = modelIndex(dit);
159 setString(tocItem(index), index);
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 setString(item, top_level_item);
188 model_->setData(top_level_item, index, Qt::UserRole);
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 setString(item, child_item);
228 model_->setData(child_item, index, Qt::UserRole);
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()
252 names_ = new TocTypeModel(this);
253 names_sorted_ = new TocModelSortProxyModel(this);
254 names_sorted_->setSourceModel(names_);
255 names_sorted_->setSortLocaleAware(true);
256 names_sorted_->sort(0);
260 void TocModels::clear()
262 names_->blockSignals(true);
264 names_->blockSignals(false);
265 iterator end = models_.end();
266 for (iterator it = models_.begin(); it != end; ++it)
271 int TocModels::depth(QString const & type)
273 const_iterator it = models_.find(type);
274 if (it == models_.end())
276 return it.value()->modelDepth();
280 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,
297 DocIterator const & dit) const
299 const_iterator it = models_.find(type);
300 if (it == models_.end())
301 return QModelIndex();
302 return it.value()->modelIndex(dit);
306 FuncRequest TocModels::goTo(QString const & type, QModelIndex const & index) const
308 const_iterator it = models_.find(type);
309 if (it == models_.end() || !index.isValid()) {
310 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
311 return FuncRequest(LFUN_NOACTION);
313 LASSERT(index.model() == it.value()->model(), return FuncRequest(LFUN_NOACTION));
314 TocItem const item = it.value()->tocItem(index);
315 LYXERR(Debug::GUI, "TocModels::goTo " << item.asString());
316 return item.action();
320 TocItem const TocModels::currentItem(QString const & type,
321 QModelIndex const & index) const
323 const_iterator it = models_.find(type);
324 if (it == models_.end() || !index.isValid()) {
325 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
328 LASSERT(index.model() == it.value()->model(), return TocItem());
330 return it.value()->tocItem(index);
334 void TocModels::updateItem(QString const & type, DocIterator const & dit)
336 models_[type]->updateItem(dit);
339 TocModels::~TocModels(){
340 QHashIterator<QString, TocModel *> iter(models_);
341 while(iter.hasNext()) {
347 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 // In the outliner, add Tocs from the master document
362 TocBackend const & backend = bv->buffer().masterBuffer()->tocBackend();
363 for (auto const & toc : backend.tocs()) {
364 QString const type = toqstr(toc.first);
366 // First, fill in the toc models.
367 iterator mod_it = models_.find(type);
368 if (mod_it == models_.end())
369 mod_it = models_.insert(type, new TocModel(this));
370 mod_it.value()->reset(toc.second);
372 // Fill in the names_ model.
373 QString const gui_name = toqstr(backend.outlinerName(toc.first));
374 int const current_row = names_->rowCount();
375 names_->insertRows(current_row, 1);
376 QModelIndex const index = names_->index(current_row, 0);
377 names_->setData(index, gui_name, Qt::DisplayRole);
378 names_->setData(index, type, Qt::UserRole);
380 names_->blockSignals(false);
381 names_->endResetModel();
385 bool TocModels::isSorted(QString const & type) const
387 const_iterator it = models_.find(type);
388 if (it == models_.end()) {
389 LYXERR0("type not found: " << type);
392 return it.value()->isSorted();
396 void TocModels::sort(QString const & type, bool sort_it)
398 iterator it = models_.find(type);
399 if (it == models_.end())
400 LYXERR0("type not found: " << type);
402 it.value()->sort(sort_it);
405 } // namespace frontend
408 #include "moc_TocModel.cpp"