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"
36 TocItem const & TocModel::tocItem(QModelIndex const & index) const
38 return toc_[data(index, Qt::UserRole).toUInt()];
42 QModelIndex TocModel::modelIndex(DocIterator const & dit) const
47 unsigned int const toc_index = toc_.item(dit) - toc_.begin();
49 QModelIndexList list = match(index(0, 0), Qt::UserRole,
50 QVariant(toc_index), 1,
51 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
53 LASSERT(!list.isEmpty(), return QModelIndex());
58 TocModel::TocModel(Toc const & toc): toc_(toc)
63 QModelIndex top_level_item;
68 size_t end = toc.size();
69 for (unsigned int index = 0; index != end; ++index) {
70 TocItem const & item = toc_[index];
71 maxdepth_ = max(maxdepth_, item.depth());
72 mindepth_ = min(mindepth_, item.depth());
73 current_row = rowCount();
74 insertRows(current_row, 1);
75 top_level_item = QStandardItemModel::index(current_row, 0);
76 setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
77 setData(top_level_item, index, Qt::UserRole);
79 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
80 << ", added item " << item.str());
82 populate(index, top_level_item);
87 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
88 // emit headerDataChanged();
92 void TocModel::populate(unsigned int & index, QModelIndex const & parent)
94 int curdepth = toc_[index].depth() + 1;
97 QModelIndex child_item;
98 insertColumns(0, 1, parent);
100 size_t end = toc_.size();
102 for (; index != end; ++index) {
103 TocItem const & item = toc_[index];
104 if (item.depth() < curdepth) {
108 maxdepth_ = max(maxdepth_, item.depth());
109 mindepth_ = min(mindepth_, item.depth());
110 current_row = rowCount(parent);
111 insertRows(current_row, 1, parent);
112 child_item = QStandardItemModel::index(current_row, 0, parent);
113 setData(child_item, toqstr(item.str()), Qt::DisplayRole);
114 setData(child_item, index, Qt::UserRole);
115 populate(index, child_item);
122 int TocModel::modelDepth() const
124 return maxdepth_ - mindepth_;
128 ///////////////////////////////////////////////////////////////////////////////
129 // TocModels implementation.
130 ///////////////////////////////////////////////////////////////////////////////
131 void TocModels::clear()
135 const unsigned int size = models_.size();
136 for (unsigned int i = 0; i < size; ++i) {
143 int TocModels::depth(int type)
147 return models_[type]->modelDepth();
151 QStandardItemModel * TocModels::model(int type)
156 if (models_.empty()) {
157 LYXERR(Debug::GUI, "TocModels::tocModel(): no types available ");
161 LYXERR(Debug::GUI, "TocModels: type " << type
162 << " models_.size() " << models_.size());
164 LASSERT(type >= 0 && type < int(models_.size()), /**/);
165 return models_[type];
169 QModelIndex TocModels::currentIndex(int type) const
171 if (type < 0 || !bv_)
172 return QModelIndex();
173 return models_[type]->modelIndex(bv_->cursor());
177 void TocModels::goTo(int type, QModelIndex const & index) const
179 if (type < 0 || !index.isValid()
180 || index.model() != models_[type]) {
181 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
185 LASSERT(type >= 0 && type < int(models_.size()), /**/);
186 TocItem const item = models_[type]->tocItem(index);
187 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
188 dispatch(item.action());
192 void TocModels::updateBackend() const
194 bv_->buffer().masterBuffer()->tocBackend().update();
195 bv_->buffer().structureChanged();
199 void TocModels::reset(BufferView const * bv)
206 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
207 TocList::const_iterator it = tocs.begin();
208 TocList::const_iterator end = tocs.end();
209 for (; it != end; ++it) {
210 types_.push_back(toqstr(it->first));
211 type_names_.push_back(guiName(it->first, bv->buffer().params()));
212 models_.push_back(new TocModel(it->second));
217 bool TocModels::canOutline(int type) const
219 if (type < 0 || type >= types_.size())
221 return types_[type] == "tableofcontents";
225 int TocModels::decodeType(QString const & str) const
228 if (str.contains("tableofcontents")) {
229 new_type = "tableofcontents";
230 } else if (str.contains("floatlist")) {
231 if (str.contains("\"figure"))
233 else if (str.contains("\"table"))
235 else if (str.contains("\"algorithm"))
236 new_type = "algorithm";
237 } else if (!str.isEmpty()) {
240 // Default to Outliner.
241 new_type = "tableofcontents";
243 int const type = types_.indexOf(new_type);
246 // If everything else fails, settle on the table of contents which is
247 // guaranted to exist.
248 return types_.indexOf("tableofcontents");
251 } // namespace frontend
254 #include "TocModel_moc.cpp"