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 TocModel::TocModel(QObject * parent)
56 : model_(new TocTypeModel(parent)),
57 sorted_model_(new QSortFilterProxyModel(parent)),
58 is_sorted_(false), maxdepth_(0), mindepth_(0)
60 #if QT_VERSION >= 0x040300
61 sorted_model_->setSortLocaleAware(true);
63 sorted_model_->setSourceModel(model_);
67 QAbstractItemModel * TocModel::model()
75 QAbstractItemModel const * TocModel::model() const
83 void TocModel::clear()
85 model_->blockSignals(true);
87 model_->blockSignals(false);
91 void TocModel::sort(bool sort_it)
95 sorted_model_->sort(0);
98 TocItem const & TocModel::tocItem(QModelIndex const & index) const
100 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
104 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
107 return QModelIndex();
109 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
111 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
112 QVariant(toc_index), 1,
113 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
115 LASSERT(!list.isEmpty(), return QModelIndex());
120 void TocModel::reset()
126 void TocModel::reset(Toc const & toc)
136 model_->blockSignals(true);
137 model_->insertColumns(0, 1);
141 size_t end = toc_->size();
142 for (unsigned int index = 0; index != end; ++index) {
143 TocItem const & item = (*toc_)[index];
144 maxdepth_ = max(maxdepth_, item.depth());
145 mindepth_ = min(mindepth_, item.depth());
146 int current_row = model_->rowCount();
147 model_->insertRows(current_row, 1);
148 QModelIndex top_level_item = model_->index(current_row, 0);
149 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
150 model_->setData(top_level_item, index, Qt::UserRole);
152 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
153 << ", added item " << item.str());
155 populate(index, top_level_item);
160 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
162 sorted_model_->sort(0);
163 model_->blockSignals(false);
165 // emit headerDataChanged();
169 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
171 int curdepth = (*toc_)[index].depth() + 1;
174 QModelIndex child_item;
175 model_->insertColumns(0, 1, parent);
177 size_t end = toc_->size();
179 for (; index != end; ++index) {
180 TocItem const & item = (*toc_)[index];
181 if (item.depth() < curdepth) {
185 maxdepth_ = max(maxdepth_, item.depth());
186 mindepth_ = min(mindepth_, item.depth());
187 current_row = model_->rowCount(parent);
188 model_->insertRows(current_row, 1, parent);
189 child_item = model_->index(current_row, 0, parent);
190 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
191 model_->setData(child_item, index, Qt::UserRole);
192 populate(index, child_item);
199 int TocModel::modelDepth() const
201 int const d = maxdepth_ - mindepth_;
202 LASSERT(d >= 0 && d <= 100, /* */);
207 ///////////////////////////////////////////////////////////////////////////////
211 ///////////////////////////////////////////////////////////////////////////////
213 TocModels::TocModels()
216 names_ = new TocTypeModel(this);
217 names_sorted_ = new QSortFilterProxyModel(this);
218 names_sorted_->setSourceModel(names_);
219 #if QT_VERSION >= 0x040300
220 names_sorted_->setSortLocaleAware(true);
222 names_sorted_->sort(0);
226 void TocModels::clear()
228 names_->blockSignals(true);
230 names_->blockSignals(false);
231 iterator end = models_.end();
232 for (iterator it = models_.begin(); it != end; ++it)
237 int TocModels::depth(QString const & type)
239 const_iterator it = models_.find(type);
240 if (!bv_ || it == models_.end())
242 return it.value()->modelDepth();
246 QAbstractItemModel * TocModels::model(QString const & type)
250 iterator it = models_.find(type);
251 if (it != models_.end())
252 return it.value()->model();
253 LYXERR0("type not found: " << type);
258 QAbstractItemModel * TocModels::nameModel()
260 return names_sorted_;
264 QModelIndex TocModels::currentIndex(QString const & type) const
266 const_iterator it = models_.find(type);
267 if (!bv_ || it == models_.end())
268 return QModelIndex();
269 return it.value()->modelIndex(bv_->cursor());
273 void TocModels::goTo(QString const & type, QModelIndex const & index) const
275 const_iterator it = models_.find(type);
276 if (it == models_.end() || !index.isValid()) {
277 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
280 LASSERT(index.model() == it.value()->model(), return);
281 TocItem const item = it.value()->tocItem(index);
282 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
283 dispatch(item.action());
287 void TocModels::updateBackend() const
289 bv_->buffer().masterBuffer()->tocBackend().update();
290 bv_->buffer().structureChanged();
294 void TocModels::reset(BufferView const * bv)
299 iterator end = models_.end();
300 for (iterator it = models_.begin(); it != end; ++it)
306 names_->blockSignals(true);
307 names_->insertColumns(0, 1);
308 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
309 TocList::const_iterator it = tocs.begin();
310 TocList::const_iterator toc_end = tocs.end();
311 for (; it != toc_end; ++it) {
312 QString const type = toqstr(it->first);
314 // First, fill in the toc models.
315 iterator mod_it = models_.find(type);
316 if (mod_it == models_.end())
317 mod_it = models_.insert(type, new TocModel(this));
318 mod_it.value()->reset(it->second);
320 // Fill in the names_ model.
321 QString const gui_name = guiName(it->first, bv->buffer().params());
322 int const current_row = names_->rowCount();
323 names_->insertRows(current_row, 1);
324 QModelIndex const index = names_->index(current_row, 0);
325 names_->setData(index, gui_name, Qt::DisplayRole);
326 names_->setData(index, type, Qt::UserRole);
328 names_->blockSignals(false);
333 bool TocModels::isSorted(QString const & type) const
335 const_iterator it = models_.find(type);
336 if (it == models_.end()) {
337 LYXERR0("type not found: " << type);
340 return it.value()->isSorted();
344 void TocModels::sort(QString const & type, bool sort_it)
346 iterator it = models_.find(type);
347 if (it == models_.end())
348 LYXERR0("type not found: " << type);
350 it.value()->sort(sort_it);
353 } // namespace frontend
356 #include "TocModel_moc.cpp"