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 TocItem const & TocModel::tocItem(QModelIndex const & index) const
50 return (*toc_)[data(index, Qt::UserRole).toUInt()];
54 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
59 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
61 QModelIndexList list = match(index(0, 0), Qt::UserRole,
62 QVariant(toc_index), 1,
63 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
65 LASSERT(!list.isEmpty(), return QModelIndex());
70 TocModel::TocModel(QObject * parent): QStandardItemModel(parent)
75 void TocModel::reset()
77 QStandardItemModel::reset();
81 void TocModel::reset(Toc const & toc)
91 QModelIndex top_level_item;
96 size_t end = toc_->size();
97 for (unsigned int index = 0; index != end; ++index) {
98 TocItem const & item = (*toc_)[index];
99 maxdepth_ = max(maxdepth_, item.depth());
100 mindepth_ = min(mindepth_, item.depth());
101 current_row = rowCount();
102 insertRows(current_row, 1);
103 top_level_item = QStandardItemModel::index(current_row, 0);
104 setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
105 setData(top_level_item, index, Qt::UserRole);
107 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
108 << ", added item " << item.str());
110 populate(index, top_level_item);
115 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
118 // emit headerDataChanged();
122 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
124 int curdepth = (*toc_)[index].depth() + 1;
127 QModelIndex child_item;
128 insertColumns(0, 1, parent);
130 size_t end = toc_->size();
132 for (; index != end; ++index) {
133 TocItem const & item = (*toc_)[index];
134 if (item.depth() < curdepth) {
138 maxdepth_ = max(maxdepth_, item.depth());
139 mindepth_ = min(mindepth_, item.depth());
140 current_row = rowCount(parent);
141 insertRows(current_row, 1, parent);
142 child_item = QStandardItemModel::index(current_row, 0, parent);
143 setData(child_item, toqstr(item.str()), Qt::DisplayRole);
144 setData(child_item, index, Qt::UserRole);
145 populate(index, child_item);
152 int TocModel::modelDepth() const
154 return maxdepth_ - mindepth_;
158 ///////////////////////////////////////////////////////////////////////////////
159 // TocModels implementation.
160 ///////////////////////////////////////////////////////////////////////////////
162 TocModels::TocModels(): bv_(0)
164 names_ = new TocTypeModel(this);
165 names_sorted_ = new QSortFilterProxyModel(this);
166 names_sorted_->setSourceModel(names_);
167 #if QT_VERSION >= 0x040300
168 names_sorted_->setSortLocaleAware(true);
170 names_sorted_->sort(0);
174 void TocModels::clear()
176 names_->blockSignals(true);
178 names_->blockSignals(false);
179 iterator end = models_.end();
180 for (iterator it = models_.begin(); it != end; ++it) {
181 it.value()->blockSignals(true);
183 it.value()->blockSignals(false);
188 int TocModels::depth(QString const & type)
190 const_iterator it = models_.find(type);
191 if (!bv_ || it == models_.end())
193 return it.value()->modelDepth();
197 QStandardItemModel * TocModels::model(QString const & type)
201 iterator it = models_.find(type);
202 if (it != models_.end())
204 LYXERR0("type not found: " << type);
209 QAbstractItemModel * TocModels::nameModel()
211 return names_sorted_;
215 QModelIndex TocModels::currentIndex(QString const & type) const
217 const_iterator it = models_.find(type);
218 if (!bv_ || it == models_.end())
219 return QModelIndex();
220 return it.value()->modelIndex(bv_->cursor());
224 void TocModels::goTo(QString const & type, QModelIndex const & index) const
226 const_iterator it = models_.find(type);
227 if (it == models_.end() || !index.isValid()) {
228 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
231 LASSERT(index.model() == it.value(), return);
232 TocItem const item = it.value()->tocItem(index);
233 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
234 dispatch(item.action());
238 void TocModels::updateBackend() const
240 bv_->buffer().masterBuffer()->tocBackend().update();
241 bv_->buffer().structureChanged();
245 void TocModels::reset(BufferView const * bv)
250 iterator end = models_.end();
251 for (iterator it = models_.begin(); it != end; ++it)
257 names_->blockSignals(true);
258 names_->insertColumns(0, 1);
259 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
260 TocList::const_iterator it = tocs.begin();
261 TocList::const_iterator toc_end = tocs.end();
262 for (; it != toc_end; ++it) {
263 QString const type = toqstr(it->first);
265 // First, fill in the toc models.
266 iterator mod_it = models_.find(type);
267 if (mod_it == models_.end())
268 mod_it = models_.insert(type, new TocModel(this));
269 mod_it.value()->reset(it->second);
271 // Fill in the names_ model.
272 QString const gui_name = guiName(it->first, bv->buffer().params());
273 int const current_row = names_->rowCount();
274 names_->insertRows(current_row, 1);
275 QModelIndex const index = names_->index(current_row, 0);
276 names_->setData(index, gui_name, Qt::DisplayRole);
277 names_->setData(index, type, Qt::UserRole);
279 names_->blockSignals(false);
284 } // namespace frontend
287 #include "TocModel_moc.cpp"