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/debug.h"
25 #include "support/lassert.h"
27 #include <QSortFilterProxyModel>
36 ///////////////////////////////////////////////////////////////////////////////
40 ///////////////////////////////////////////////////////////////////////////////
42 TocTypeModel::TocTypeModel(QObject * parent)
43 : QStandardItemModel(parent)
48 void TocTypeModel::reset()
50 QStandardItemModel::reset();
54 ///////////////////////////////////////////////////////////////////////////////
58 ///////////////////////////////////////////////////////////////////////////////
60 TocModel::TocModel(QObject * parent)
61 : model_(new TocTypeModel(parent)),
62 sorted_model_(new QSortFilterProxyModel(parent)),
63 is_sorted_(false), maxdepth_(0), mindepth_(0)
65 #if QT_VERSION >= 0x040300
66 sorted_model_->setSortLocaleAware(true);
68 sorted_model_->setSourceModel(model_);
72 QAbstractItemModel * TocModel::model()
80 QAbstractItemModel const * TocModel::model() const
88 void TocModel::clear()
90 model_->blockSignals(true);
92 model_->blockSignals(false);
96 void TocModel::sort(bool sort_it)
100 sorted_model_->sort(0);
104 TocItem const & TocModel::tocItem(QModelIndex const & index) const
106 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
110 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
113 return QModelIndex();
115 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
117 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
118 QVariant(toc_index), 1,
119 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
121 LASSERT(!list.isEmpty(), return QModelIndex());
126 void TocModel::reset()
132 void TocModel::updateItem(DocIterator const & dit)
134 QModelIndex index = modelIndex(dit);
135 TocItem const & toc_item = tocItem(index);
136 model_->setData(index, toqstr(toc_item.str()), Qt::DisplayRole);
140 void TocModel::reset(Toc const & toc)
150 model_->blockSignals(true);
151 model_->insertColumns(0, 1);
155 size_t end = toc_->size();
156 for (unsigned int index = 0; index != end; ++index) {
157 TocItem const & item = (*toc_)[index];
158 maxdepth_ = max(maxdepth_, item.depth());
159 mindepth_ = min(mindepth_, item.depth());
160 int current_row = model_->rowCount();
161 model_->insertRows(current_row, 1);
162 QModelIndex top_level_item = model_->index(current_row, 0);
163 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
164 model_->setData(top_level_item, index, Qt::UserRole);
166 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
167 << ", added item " << item.str());
169 populate(index, top_level_item);
174 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
176 sorted_model_->sort(0);
177 model_->blockSignals(false);
179 // emit headerDataChanged();
183 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
185 int curdepth = (*toc_)[index].depth() + 1;
188 QModelIndex child_item;
189 model_->insertColumns(0, 1, parent);
191 size_t end = toc_->size();
193 for (; index != end; ++index) {
194 TocItem const & item = (*toc_)[index];
195 if (item.depth() < curdepth) {
199 maxdepth_ = max(maxdepth_, item.depth());
200 mindepth_ = min(mindepth_, item.depth());
201 current_row = model_->rowCount(parent);
202 model_->insertRows(current_row, 1, parent);
203 child_item = model_->index(current_row, 0, parent);
204 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
205 model_->setData(child_item, index, Qt::UserRole);
206 populate(index, child_item);
213 int TocModel::modelDepth() const
215 int const d = maxdepth_ - mindepth_;
216 LASSERT(d >= 0 && d <= 100, return 0);
221 ///////////////////////////////////////////////////////////////////////////////
225 ///////////////////////////////////////////////////////////////////////////////
227 TocModels::TocModels()
230 names_ = new TocTypeModel(this);
231 names_sorted_ = new QSortFilterProxyModel(this);
232 names_sorted_->setSourceModel(names_);
233 #if QT_VERSION >= 0x040300
234 names_sorted_->setSortLocaleAware(true);
236 names_sorted_->sort(0);
240 void TocModels::clear()
242 names_->blockSignals(true);
244 names_->blockSignals(false);
245 iterator end = models_.end();
246 for (iterator it = models_.begin(); it != end; ++it)
251 int TocModels::depth(QString const & type)
253 const_iterator it = models_.find(type);
254 if (!bv_ || it == models_.end())
256 return it.value()->modelDepth();
260 QAbstractItemModel * TocModels::model(QString const & type)
264 iterator it = models_.find(type);
265 if (it != models_.end())
266 return it.value()->model();
267 LYXERR0("type not found: " << type);
272 QAbstractItemModel * TocModels::nameModel()
274 return names_sorted_;
278 QModelIndex TocModels::currentIndex(QString const & type) const
280 const_iterator it = models_.find(type);
281 if (!bv_ || it == models_.end())
282 return QModelIndex();
283 return it.value()->modelIndex(bv_->cursor());
287 void TocModels::goTo(QString const & type, QModelIndex const & index) const
289 const_iterator it = models_.find(type);
290 if (it == models_.end() || !index.isValid()) {
291 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
294 LASSERT(index.model() == it.value()->model(), return);
295 TocItem const item = it.value()->tocItem(index);
296 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
297 dispatch(item.action());
301 TocItem const TocModels::currentItem(QString const & type,
302 QModelIndex const & index) const
304 const_iterator it = models_.find(type);
305 if (it == models_.end() || !index.isValid()) {
306 LYXERR(Debug::GUI, "TocModels::currentItem(): QModelIndex is invalid!");
309 LASSERT(index.model() == it.value()->model(), return TocItem());
311 return it.value()->tocItem(index);
315 void TocModels::updateBackend() const
317 bv_->buffer().masterBuffer()->tocBackend().update();
318 bv_->buffer().structureChanged();
322 void TocModels::updateItem(QString const & type, DocIterator const & dit)
324 models_[type]->updateItem(dit);
328 void TocModels::reset(BufferView const * bv)
333 iterator end = models_.end();
334 for (iterator it = models_.begin(); it != end; ++it)
340 names_->blockSignals(true);
341 names_->insertColumns(0, 1);
342 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
343 TocList::const_iterator it = tocs.begin();
344 TocList::const_iterator toc_end = tocs.end();
345 for (; it != toc_end; ++it) {
346 QString const type = toqstr(it->first);
348 // First, fill in the toc models.
349 iterator mod_it = models_.find(type);
350 if (mod_it == models_.end())
351 mod_it = models_.insert(type, new TocModel(this));
352 mod_it.value()->reset(it->second);
354 // Fill in the names_ model.
355 QString const gui_name = guiName(it->first, bv->buffer().params());
356 int const current_row = names_->rowCount();
357 names_->insertRows(current_row, 1);
358 QModelIndex const index = names_->index(current_row, 0);
359 names_->setData(index, gui_name, Qt::DisplayRole);
360 names_->setData(index, type, Qt::UserRole);
362 names_->blockSignals(false);
367 bool TocModels::isSorted(QString const & type) const
369 const_iterator it = models_.find(type);
370 if (it == models_.end()) {
371 LYXERR0("type not found: " << type);
374 return it.value()->isSorted();
378 void TocModels::sort(QString const & type, bool sort_it)
380 iterator it = models_.find(type);
381 if (it == models_.end())
382 LYXERR0("type not found: " << type);
384 it.value()->sort(sort_it);
387 } // namespace frontend
390 #include "moc_TocModel.cpp"