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/convert.h"
25 #include "support/debug.h"
26 #include "support/lassert.h"
28 #include <QSortFilterProxyModel>
37 ///////////////////////////////////////////////////////////////////////////////
41 ///////////////////////////////////////////////////////////////////////////////
43 TocTypeModel::TocTypeModel(QObject * parent)
44 : QStandardItemModel(parent)
49 void TocTypeModel::reset()
51 QStandardItemModel::reset();
55 ///////////////////////////////////////////////////////////////////////////////
59 ///////////////////////////////////////////////////////////////////////////////
61 TocModel::TocModel(QObject * parent)
62 : model_(new TocTypeModel(parent)),
63 sorted_model_(new QSortFilterProxyModel(parent)),
64 is_sorted_(false), maxdepth_(0), mindepth_(0)
66 #if QT_VERSION >= 0x040300
67 sorted_model_->setSortLocaleAware(true);
69 sorted_model_->setSourceModel(model_);
73 QAbstractItemModel * TocModel::model()
81 QAbstractItemModel const * TocModel::model() const
89 void TocModel::clear()
91 model_->blockSignals(true);
93 model_->blockSignals(false);
97 void TocModel::sort(bool sort_it)
101 sorted_model_->sort(0);
105 TocItem const & TocModel::tocItem(QModelIndex const & index) const
107 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
111 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
114 return QModelIndex();
116 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
118 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
119 QVariant(toc_index), 1,
120 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
122 LASSERT(!list.isEmpty(), return QModelIndex());
127 void TocModel::reset()
133 void TocModel::updateItem(DocIterator const & dit)
135 QModelIndex index = modelIndex(dit);
136 TocItem const & toc_item = tocItem(index);
137 model_->setData(index, toqstr(toc_item.str()), Qt::DisplayRole);
141 void TocModel::reset(Toc const & toc)
151 model_->blockSignals(true);
152 model_->insertColumns(0, 1);
156 size_t end = toc_->size();
157 for (unsigned int index = 0; index != end; ++index) {
158 TocItem const & item = (*toc_)[index];
159 maxdepth_ = max(maxdepth_, item.depth());
160 mindepth_ = min(mindepth_, item.depth());
161 int current_row = model_->rowCount();
162 model_->insertRows(current_row, 1);
163 QModelIndex top_level_item = model_->index(current_row, 0);
164 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
165 model_->setData(top_level_item, index, Qt::UserRole);
167 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
168 << ", added item " << item.str());
170 populate(index, top_level_item);
175 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
177 sorted_model_->sort(0);
178 model_->blockSignals(false);
180 // emit headerDataChanged();
184 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
186 int curdepth = (*toc_)[index].depth() + 1;
189 QModelIndex child_item;
190 model_->insertColumns(0, 1, parent);
192 size_t end = toc_->size();
194 for (; index != end; ++index) {
195 TocItem const & item = (*toc_)[index];
196 if (item.depth() < curdepth) {
200 maxdepth_ = max(maxdepth_, item.depth());
201 mindepth_ = min(mindepth_, item.depth());
202 current_row = model_->rowCount(parent);
203 model_->insertRows(current_row, 1, parent);
204 child_item = model_->index(current_row, 0, parent);
205 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
206 model_->setData(child_item, index, Qt::UserRole);
207 populate(index, child_item);
214 int TocModel::modelDepth() const
216 int const d = maxdepth_ - mindepth_;
217 LASSERT(d >= 0 && d <= 100, return 0);
222 ///////////////////////////////////////////////////////////////////////////////
226 ///////////////////////////////////////////////////////////////////////////////
228 TocModels::TocModels()
231 names_ = new TocTypeModel(this);
232 names_sorted_ = new QSortFilterProxyModel(this);
233 names_sorted_->setSourceModel(names_);
234 #if QT_VERSION >= 0x040300
235 names_sorted_->setSortLocaleAware(true);
237 names_sorted_->sort(0);
241 void TocModels::clear()
243 names_->blockSignals(true);
245 names_->blockSignals(false);
246 iterator end = models_.end();
247 for (iterator it = models_.begin(); it != end; ++it)
252 int TocModels::depth(QString const & type)
254 const_iterator it = models_.find(type);
255 if (!bv_ || it == models_.end())
257 return it.value()->modelDepth();
261 QAbstractItemModel * TocModels::model(QString const & type)
265 iterator it = models_.find(type);
266 if (it != models_.end())
267 return it.value()->model();
268 LYXERR0("type not found: " << type);
273 QAbstractItemModel * TocModels::nameModel()
275 return names_sorted_;
279 QModelIndex TocModels::currentIndex(QString const & type) const
281 const_iterator it = models_.find(type);
282 if (!bv_ || it == models_.end())
283 return QModelIndex();
284 return it.value()->modelIndex(bv_->cursor());
288 void TocModels::goTo(QString const & type, QModelIndex const & index) const
290 const_iterator it = models_.find(type);
291 if (it == models_.end() || !index.isValid()) {
292 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
295 LASSERT(index.model() == it.value()->model(), return);
296 TocItem const item = it.value()->tocItem(index);
297 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
298 dispatch(item.action());
302 TocItem const TocModels::currentItem(QString const & type,
303 QModelIndex const & index) const
305 const_iterator it = models_.find(type);
306 if (it == models_.end() || !index.isValid()) {
307 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
310 LASSERT(index.model() == it.value()->model(), return TocItem());
312 return it.value()->tocItem(index);
316 void TocModels::updateBackend() const
318 bv_->buffer().masterBuffer()->tocBackend().update();
319 bv_->buffer().structureChanged();
323 void TocModels::updateItem(QString const & type, DocIterator const & dit)
325 models_[type]->updateItem(dit);
329 void TocModels::reset(BufferView const * bv)
334 iterator end = models_.end();
335 for (iterator it = models_.begin(); it != end; ++it)
341 names_->blockSignals(true);
342 names_->insertColumns(0, 1);
343 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
344 TocList::const_iterator it = tocs.begin();
345 TocList::const_iterator toc_end = tocs.end();
346 for (; it != toc_end; ++it) {
347 QString const type = toqstr(it->first);
349 // First, fill in the toc models.
350 iterator mod_it = models_.find(type);
351 if (mod_it == models_.end())
352 mod_it = models_.insert(type, new TocModel(this));
353 mod_it.value()->reset(it->second);
355 // Fill in the names_ model.
356 QString const gui_name = guiName(it->first, bv->buffer().params());
357 int const current_row = names_->rowCount();
358 names_->insertRows(current_row, 1);
359 QModelIndex const index = names_->index(current_row, 0);
360 names_->setData(index, gui_name, Qt::DisplayRole);
361 names_->setData(index, type, Qt::UserRole);
363 names_->blockSignals(false);
368 bool TocModels::isSorted(QString const & type) const
370 const_iterator it = models_.find(type);
371 if (it == models_.end()) {
372 LYXERR0("type not found: " << type);
375 return it.value()->isSorted();
379 void TocModels::sort(QString const & type, bool sort_it)
381 iterator it = models_.find(type);
382 if (it == models_.end())
383 LYXERR0("type not found: " << type);
385 it.value()->sort(sort_it);
388 } // namespace frontend
391 #include "moc_TocModel.cpp"