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"
35 TocTypeModel::TocTypeModel(QObject * parent): QStandardItemModel(parent)
40 void TocTypeModel::reset()
42 QStandardItemModel::reset();
46 TocItem const & TocModel::tocItem(QModelIndex const & index) const
48 return (*toc_)[data(index, Qt::UserRole).toUInt()];
52 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
57 unsigned int const toc_index = toc_->item(dit) - toc_->begin();
59 QModelIndexList list = match(index(0, 0), Qt::UserRole,
60 QVariant(toc_index), 1,
61 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
63 LASSERT(!list.isEmpty(), return QModelIndex());
68 TocModel::TocModel(QObject * parent): QStandardItemModel(parent)
73 void TocModel::reset()
75 QStandardItemModel::reset();
79 void TocModel::reset(Toc const & toc)
89 QModelIndex top_level_item;
94 size_t end = toc_->size();
95 for (unsigned int index = 0; index != end; ++index) {
96 TocItem const & item = (*toc_)[index];
97 maxdepth_ = max(maxdepth_, item.depth());
98 mindepth_ = min(mindepth_, item.depth());
99 current_row = rowCount();
100 insertRows(current_row, 1);
101 top_level_item = QStandardItemModel::index(current_row, 0);
102 setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
103 setData(top_level_item, index, Qt::UserRole);
105 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
106 << ", added item " << item.str());
108 populate(index, top_level_item);
113 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
116 // emit headerDataChanged();
120 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
122 int curdepth = (*toc_)[index].depth() + 1;
125 QModelIndex child_item;
126 insertColumns(0, 1, parent);
128 size_t end = toc_->size();
130 for (; index != end; ++index) {
131 TocItem const & item = (*toc_)[index];
132 if (item.depth() < curdepth) {
136 maxdepth_ = max(maxdepth_, item.depth());
137 mindepth_ = min(mindepth_, item.depth());
138 current_row = rowCount(parent);
139 insertRows(current_row, 1, parent);
140 child_item = QStandardItemModel::index(current_row, 0, parent);
141 setData(child_item, toqstr(item.str()), Qt::DisplayRole);
142 setData(child_item, index, Qt::UserRole);
143 populate(index, child_item);
150 int TocModel::modelDepth() const
152 return maxdepth_ - mindepth_;
156 ///////////////////////////////////////////////////////////////////////////////
157 // TocModels implementation.
158 ///////////////////////////////////////////////////////////////////////////////
160 TocModels::TocModels(): bv_(0)
162 names_ = new TocTypeModel(this);
166 void TocModels::clear()
168 names_->blockSignals(true);
170 names_->blockSignals(false);
171 iterator end = models_.end();
172 for (iterator it = models_.begin(); it != end; ++it) {
173 it.value()->blockSignals(true);
175 it.value()->blockSignals(false);
180 int TocModels::depth(QString const & type)
182 const_iterator it = models_.find(type);
183 if (!bv_ || it == models_.end())
185 return it.value()->modelDepth();
189 QStandardItemModel * TocModels::model(QString const & type)
193 iterator it = models_.find(type);
194 if (it != models_.end())
196 LYXERR0("type not found: " << type);
201 QModelIndex TocModels::currentIndex(QString const & type) const
203 const_iterator it = models_.find(type);
204 if (!bv_ || it == models_.end())
205 return QModelIndex();
206 return it.value()->modelIndex(bv_->cursor());
210 void TocModels::goTo(QString const & type, QModelIndex const & index) const
212 const_iterator it = models_.find(type);
213 if (it == models_.end() || !index.isValid()) {
214 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
217 LASSERT(index.model() == it.value(), return);
218 TocItem const item = it.value()->tocItem(index);
219 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
220 dispatch(item.action());
224 void TocModels::updateBackend() const
226 bv_->buffer().masterBuffer()->tocBackend().update();
227 bv_->buffer().structureChanged();
231 void TocModels::reset(BufferView const * bv)
236 iterator end = models_.end();
237 for (iterator it = models_.begin(); it != end; ++it)
243 names_->blockSignals(true);
244 names_->insertColumns(0, 1);
245 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
246 TocList::const_iterator it = tocs.begin();
247 TocList::const_iterator toc_end = tocs.end();
248 for (; it != toc_end; ++it) {
249 QString const type = toqstr(it->first);
251 // First, fill in the toc models.
252 iterator mod_it = models_.find(type);
253 if (mod_it == models_.end())
254 mod_it = models_.insert(type, new TocModel(this));
255 mod_it.value()->reset(it->second);
257 // Fill in the names_ model.
258 QString const gui_name = guiName(it->first, bv->buffer().params());
259 int const current_row = names_->rowCount();
260 names_->insertRows(current_row, 1);
261 QModelIndex const index = names_->index(current_row, 0);
262 names_->setData(index, gui_name, Qt::DisplayRole);
263 names_->setData(index, type, Qt::UserRole);
265 names_->blockSignals(false);
270 } // namespace frontend
273 #include "TocModel_moc.cpp"