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),
71 maxdepth_(0), mindepth_(0)
76 void TocModel::reset()
78 QStandardItemModel::reset();
82 void TocModel::reset(Toc const & toc)
94 QModelIndex top_level_item;
99 size_t end = toc_->size();
100 for (unsigned int index = 0; index != end; ++index) {
101 TocItem const & item = (*toc_)[index];
102 maxdepth_ = max(maxdepth_, item.depth());
103 mindepth_ = min(mindepth_, item.depth());
104 current_row = rowCount();
105 insertRows(current_row, 1);
106 top_level_item = QStandardItemModel::index(current_row, 0);
107 setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
108 setData(top_level_item, index, Qt::UserRole);
110 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
111 << ", added item " << item.str());
113 populate(index, top_level_item);
118 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
121 // emit headerDataChanged();
125 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
127 int curdepth = (*toc_)[index].depth() + 1;
130 QModelIndex child_item;
131 insertColumns(0, 1, parent);
133 size_t end = toc_->size();
135 for (; index != end; ++index) {
136 TocItem const & item = (*toc_)[index];
137 if (item.depth() < curdepth) {
141 maxdepth_ = max(maxdepth_, item.depth());
142 mindepth_ = min(mindepth_, item.depth());
143 current_row = rowCount(parent);
144 insertRows(current_row, 1, parent);
145 child_item = QStandardItemModel::index(current_row, 0, parent);
146 setData(child_item, toqstr(item.str()), Qt::DisplayRole);
147 setData(child_item, index, Qt::UserRole);
148 populate(index, child_item);
155 int TocModel::modelDepth() const
157 int const d = maxdepth_ - mindepth_;
158 LASSERT(d >= 0 && d <= 100, /* */);
163 ///////////////////////////////////////////////////////////////////////////////
164 // TocModels implementation.
165 ///////////////////////////////////////////////////////////////////////////////
167 TocModels::TocModels(): bv_(0)
169 names_ = new TocTypeModel(this);
170 names_sorted_ = new QSortFilterProxyModel(this);
171 names_sorted_->setSourceModel(names_);
172 #if QT_VERSION >= 0x040300
173 names_sorted_->setSortLocaleAware(true);
175 names_sorted_->sort(0);
179 void TocModels::clear()
181 names_->blockSignals(true);
183 names_->blockSignals(false);
184 iterator end = models_.end();
185 for (iterator it = models_.begin(); it != end; ++it) {
186 it.value()->blockSignals(true);
188 it.value()->blockSignals(false);
193 int TocModels::depth(QString const & type)
195 const_iterator it = models_.find(type);
196 if (!bv_ || it == models_.end())
198 return it.value()->modelDepth();
202 QStandardItemModel * TocModels::model(QString const & type)
206 iterator it = models_.find(type);
207 if (it != models_.end())
209 LYXERR0("type not found: " << type);
214 QAbstractItemModel * TocModels::nameModel()
216 return names_sorted_;
220 QModelIndex TocModels::currentIndex(QString const & type) const
222 const_iterator it = models_.find(type);
223 if (!bv_ || it == models_.end())
224 return QModelIndex();
225 return it.value()->modelIndex(bv_->cursor());
229 void TocModels::goTo(QString const & type, QModelIndex const & index) const
231 const_iterator it = models_.find(type);
232 if (it == models_.end() || !index.isValid()) {
233 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
236 LASSERT(index.model() == it.value(), return);
237 TocItem const item = it.value()->tocItem(index);
238 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
239 dispatch(item.action());
243 void TocModels::updateBackend() const
245 bv_->buffer().masterBuffer()->tocBackend().update();
246 bv_->buffer().structureChanged();
250 void TocModels::reset(BufferView const * bv)
255 iterator end = models_.end();
256 for (iterator it = models_.begin(); it != end; ++it)
262 names_->blockSignals(true);
263 names_->insertColumns(0, 1);
264 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
265 TocList::const_iterator it = tocs.begin();
266 TocList::const_iterator toc_end = tocs.end();
267 for (; it != toc_end; ++it) {
268 QString const type = toqstr(it->first);
270 // First, fill in the toc models.
271 iterator mod_it = models_.find(type);
272 if (mod_it == models_.end())
273 mod_it = models_.insert(type, new TocModel(this));
274 mod_it.value()->reset(it->second);
276 // Fill in the names_ model.
277 QString const gui_name = guiName(it->first, bv->buffer().params());
278 int const current_row = names_->rowCount();
279 names_->insertRows(current_row, 1);
280 QModelIndex const index = names_->index(current_row, 0);
281 names_->setData(index, gui_name, Qt::DisplayRole);
282 names_->setData(index, type, Qt::UserRole);
284 names_->blockSignals(false);
289 } // namespace frontend
292 #include "TocModel_moc.cpp"