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::reset(Toc const & toc)
143 model_->blockSignals(true);
144 model_->insertColumns(0, 1);
148 size_t end = toc_->size();
149 for (unsigned int index = 0; index != end; ++index) {
150 TocItem const & item = (*toc_)[index];
151 maxdepth_ = max(maxdepth_, item.depth());
152 mindepth_ = min(mindepth_, item.depth());
153 int current_row = model_->rowCount();
154 model_->insertRows(current_row, 1);
155 QModelIndex top_level_item = model_->index(current_row, 0);
156 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
157 model_->setData(top_level_item, index, Qt::UserRole);
159 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
160 << ", added item " << item.str());
162 populate(index, top_level_item);
167 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
169 sorted_model_->sort(0);
170 model_->blockSignals(false);
172 // emit headerDataChanged();
176 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
178 int curdepth = (*toc_)[index].depth() + 1;
181 QModelIndex child_item;
182 model_->insertColumns(0, 1, parent);
184 size_t end = toc_->size();
186 for (; index != end; ++index) {
187 TocItem const & item = (*toc_)[index];
188 if (item.depth() < curdepth) {
192 maxdepth_ = max(maxdepth_, item.depth());
193 mindepth_ = min(mindepth_, item.depth());
194 current_row = model_->rowCount(parent);
195 model_->insertRows(current_row, 1, parent);
196 child_item = model_->index(current_row, 0, parent);
197 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
198 model_->setData(child_item, index, Qt::UserRole);
199 populate(index, child_item);
206 int TocModel::modelDepth() const
208 int const d = maxdepth_ - mindepth_;
209 LASSERT(d >= 0 && d <= 100, return 0);
214 ///////////////////////////////////////////////////////////////////////////////
218 ///////////////////////////////////////////////////////////////////////////////
220 TocModels::TocModels()
223 names_ = new TocTypeModel(this);
224 names_sorted_ = new QSortFilterProxyModel(this);
225 names_sorted_->setSourceModel(names_);
226 #if QT_VERSION >= 0x040300
227 names_sorted_->setSortLocaleAware(true);
229 names_sorted_->sort(0);
233 void TocModels::clear()
235 names_->blockSignals(true);
237 names_->blockSignals(false);
238 iterator end = models_.end();
239 for (iterator it = models_.begin(); it != end; ++it)
244 int TocModels::depth(QString const & type)
246 const_iterator it = models_.find(type);
247 if (!bv_ || it == models_.end())
249 return it.value()->modelDepth();
253 QAbstractItemModel * TocModels::model(QString const & type)
257 iterator it = models_.find(type);
258 if (it != models_.end())
259 return it.value()->model();
260 LYXERR0("type not found: " << type);
265 QAbstractItemModel * TocModels::nameModel()
267 return names_sorted_;
271 QModelIndex TocModels::currentIndex(QString const & type) const
273 const_iterator it = models_.find(type);
274 if (!bv_ || it == models_.end())
275 return QModelIndex();
276 return it.value()->modelIndex(bv_->cursor());
280 void TocModels::goTo(QString const & type, QModelIndex const & index) const
282 const_iterator it = models_.find(type);
283 if (it == models_.end() || !index.isValid()) {
284 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
287 LASSERT(index.model() == it.value()->model(), return);
288 TocItem const item = it.value()->tocItem(index);
289 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
290 dispatch(item.action());
294 void TocModels::updateBackend() const
296 bv_->buffer().masterBuffer()->tocBackend().update();
297 bv_->buffer().structureChanged();
301 void TocModels::reset(BufferView const * bv)
306 iterator end = models_.end();
307 for (iterator it = models_.begin(); it != end; ++it)
313 names_->blockSignals(true);
314 names_->insertColumns(0, 1);
315 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
316 TocList::const_iterator it = tocs.begin();
317 TocList::const_iterator toc_end = tocs.end();
318 for (; it != toc_end; ++it) {
319 QString const type = toqstr(it->first);
321 // First, fill in the toc models.
322 iterator mod_it = models_.find(type);
323 if (mod_it == models_.end())
324 mod_it = models_.insert(type, new TocModel(this));
325 mod_it.value()->reset(it->second);
327 // Fill in the names_ model.
328 QString const gui_name = guiName(it->first, bv->buffer().params());
329 int const current_row = names_->rowCount();
330 names_->insertRows(current_row, 1);
331 QModelIndex const index = names_->index(current_row, 0);
332 names_->setData(index, gui_name, Qt::DisplayRole);
333 names_->setData(index, type, Qt::UserRole);
335 names_->blockSignals(false);
340 bool TocModels::isSorted(QString const & type) const
342 const_iterator it = models_.find(type);
343 if (it == models_.end()) {
344 LYXERR0("type not found: " << type);
347 return it.value()->isSorted();
351 void TocModels::sort(QString const & type, bool sort_it)
353 iterator it = models_.find(type);
354 if (it == models_.end())
355 LYXERR0("type not found: " << type);
357 it.value()->sort(sort_it);
360 } // namespace frontend
363 #include "TocModel_moc.cpp"