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 TocTypeModel::TocTypeModel(QObject * parent): QStandardItemModel(parent)
42 void TocTypeModel::reset()
44 QStandardItemModel::reset();
48 TocModel::TocModel(QObject * parent)
49 : model_(new TocTypeModel(parent)),
50 sorted_model_(new QSortFilterProxyModel(parent)),
51 is_sorted_(false), maxdepth_(0), mindepth_(0)
53 #if QT_VERSION >= 0x040300
54 sorted_model_->setSortLocaleAware(true);
56 sorted_model_->setSourceModel(model_);
60 QAbstractItemModel * TocModel::model()
68 QAbstractItemModel const * TocModel::model() const
76 void TocModel::clear()
78 model_->blockSignals(true);
80 model_->blockSignals(false);
84 void TocModel::sort(bool sort_it)
88 sorted_model_->sort(0);
91 TocItem const & TocModel::tocItem(QModelIndex const & index) const
93 return (*toc_)[model()->data(index, Qt::UserRole).toUInt()];
97 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
100 return QModelIndex();
102 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
104 QModelIndexList list = model()->match(model()->index(0, 0), Qt::UserRole,
105 QVariant(toc_index), 1,
106 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
108 LASSERT(!list.isEmpty(), return QModelIndex());
113 void TocModel::reset()
119 void TocModel::reset(Toc const & toc)
129 model_->blockSignals(true);
131 QModelIndex top_level_item;
132 model_->insertColumns(0, 1);
136 size_t end = toc_->size();
137 for (unsigned int index = 0; index != end; ++index) {
138 TocItem const & item = (*toc_)[index];
139 maxdepth_ = max(maxdepth_, item.depth());
140 mindepth_ = min(mindepth_, item.depth());
141 current_row = model_->rowCount();
142 model_->insertRows(current_row, 1);
143 top_level_item = model_->index(current_row, 0);
144 model_->setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
145 model_->setData(top_level_item, index, Qt::UserRole);
147 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
148 << ", added item " << item.str());
150 populate(index, top_level_item);
155 model_->setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
157 sorted_model_->sort(0);
158 model_->blockSignals(false);
160 // emit headerDataChanged();
164 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
166 int curdepth = (*toc_)[index].depth() + 1;
169 QModelIndex child_item;
170 model_->insertColumns(0, 1, parent);
172 size_t end = toc_->size();
174 for (; index != end; ++index) {
175 TocItem const & item = (*toc_)[index];
176 if (item.depth() < curdepth) {
180 maxdepth_ = max(maxdepth_, item.depth());
181 mindepth_ = min(mindepth_, item.depth());
182 current_row = model_->rowCount(parent);
183 model_->insertRows(current_row, 1, parent);
184 child_item = model_->index(current_row, 0, parent);
185 model_->setData(child_item, toqstr(item.str()), Qt::DisplayRole);
186 model_->setData(child_item, index, Qt::UserRole);
187 populate(index, child_item);
194 int TocModel::modelDepth() const
196 int const d = maxdepth_ - mindepth_;
197 LASSERT(d >= 0 && d <= 100, /* */);
202 ///////////////////////////////////////////////////////////////////////////////
203 // TocModels implementation.
204 ///////////////////////////////////////////////////////////////////////////////
206 TocModels::TocModels(): bv_(0)
208 names_ = new TocTypeModel(this);
209 names_sorted_ = new QSortFilterProxyModel(this);
210 names_sorted_->setSourceModel(names_);
211 #if QT_VERSION >= 0x040300
212 names_sorted_->setSortLocaleAware(true);
214 names_sorted_->sort(0);
218 void TocModels::clear()
220 names_->blockSignals(true);
222 names_->blockSignals(false);
223 iterator end = models_.end();
224 for (iterator it = models_.begin(); it != end; ++it)
229 int TocModels::depth(QString const & type)
231 const_iterator it = models_.find(type);
232 if (!bv_ || it == models_.end())
234 return it.value()->modelDepth();
238 QAbstractItemModel * TocModels::model(QString const & type)
242 iterator it = models_.find(type);
243 if (it != models_.end())
244 return it.value()->model();
245 LYXERR0("type not found: " << type);
250 QAbstractItemModel * TocModels::nameModel()
252 return names_sorted_;
256 QModelIndex TocModels::currentIndex(QString const & type) const
258 const_iterator it = models_.find(type);
259 if (!bv_ || it == models_.end())
260 return QModelIndex();
261 return it.value()->modelIndex(bv_->cursor());
265 void TocModels::goTo(QString const & type, QModelIndex const & index) const
267 const_iterator it = models_.find(type);
268 if (it == models_.end() || !index.isValid()) {
269 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
272 LASSERT(index.model() == it.value()->model(), return);
273 TocItem const item = it.value()->tocItem(index);
274 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
275 dispatch(item.action());
279 void TocModels::updateBackend() const
281 bv_->buffer().masterBuffer()->tocBackend().update();
282 bv_->buffer().structureChanged();
286 void TocModels::reset(BufferView const * bv)
291 iterator end = models_.end();
292 for (iterator it = models_.begin(); it != end; ++it)
298 names_->blockSignals(true);
299 names_->insertColumns(0, 1);
300 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
301 TocList::const_iterator it = tocs.begin();
302 TocList::const_iterator toc_end = tocs.end();
303 for (; it != toc_end; ++it) {
304 QString const type = toqstr(it->first);
306 // First, fill in the toc models.
307 iterator mod_it = models_.find(type);
308 if (mod_it == models_.end())
309 mod_it = models_.insert(type, new TocModel(this));
310 mod_it.value()->reset(it->second);
312 // Fill in the names_ model.
313 QString const gui_name = guiName(it->first, bv->buffer().params());
314 int const current_row = names_->rowCount();
315 names_->insertRows(current_row, 1);
316 QModelIndex const index = names_->index(current_row, 0);
317 names_->setData(index, gui_name, Qt::DisplayRole);
318 names_->setData(index, type, Qt::UserRole);
320 names_->blockSignals(false);
325 bool TocModels::isSorted(QString const & type) const
327 const_iterator it = models_.find(type);
328 if (it == models_.end()) {
329 LYXERR0("type not found: " << type);
332 return it.value()->isSorted();
336 void TocModels::sort(QString const & type, bool sort_it)
338 iterator it = models_.find(type);
339 if (it == models_.end())
340 LYXERR0("type not found: " << type);
342 it.value()->sort(sort_it);
345 } // namespace frontend
348 #include "TocModel_moc.cpp"