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
44 size_t const toc_index = toc_.item(dit) - toc_.begin();
46 QModelIndexList list = match(index(0, 0), Qt::UserRole,
47 QVariant(toc_index), 1,
48 Qt::MatchFlags(Qt::MatchExactly | Qt::MatchRecursive));
50 LASSERT(!list.isEmpty(), return QModelIndex());
55 void TocModel::clear()
57 QStandardItemModel::clear();
58 removeRows(0, rowCount());
59 removeColumns(0, columnCount());
63 TocModel::TocModel(Toc const & toc): toc_(toc)
68 QModelIndex top_level_item;
73 size_t end = toc.size();
74 for (size_t index = 0; index != end; ++index) {
75 TocItem const & item = toc_[index];
76 maxdepth_ = max(maxdepth_, item.depth());
77 mindepth_ = min(mindepth_, item.depth());
78 current_row = rowCount();
79 insertRows(current_row, 1);
80 top_level_item = QStandardItemModel::index(current_row, 0);
81 setData(top_level_item, toqstr(item.str()), Qt::DisplayRole);
82 setData(top_level_item, index, Qt::UserRole);
84 LYXERR(Debug::GUI, "Toc: at depth " << item.depth()
85 << ", added item " << item.str());
87 populate(index, top_level_item);
92 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
93 // emit headerDataChanged();
97 void TocModel::populate(size_t & index, QModelIndex const & parent)
99 int curdepth = toc_[index].depth() + 1;
102 QModelIndex child_item;
103 insertColumns(0, 1, parent);
105 size_t end = toc_.size();
107 for (; index != end; ++index) {
108 TocItem const & item = toc_[index];
109 if (item.depth() < curdepth) {
113 maxdepth_ = max(maxdepth_, item.depth());
114 mindepth_ = min(mindepth_, item.depth());
115 current_row = rowCount(parent);
116 insertRows(current_row, 1, parent);
117 child_item = QStandardItemModel::index(current_row, 0, parent);
118 setData(child_item, toqstr(item.str()), Qt::DisplayRole);
119 setData(child_item, index, Qt::UserRole);
120 populate(index, child_item);
127 int TocModel::modelDepth() const
129 return maxdepth_ - mindepth_;
133 ///////////////////////////////////////////////////////////////////////////////
134 // TocModels implementation.
135 ///////////////////////////////////////////////////////////////////////////////
136 void TocModels::clear()
140 const unsigned int size = models_.size();
141 for (unsigned int i = 0; i < size; ++i) {
148 int TocModels::depth(int type)
152 return models_[type]->modelDepth();
156 QStandardItemModel * TocModels::model(int type)
161 if (models_.empty()) {
162 LYXERR(Debug::GUI, "TocModels::tocModel(): no types available ");
166 LYXERR(Debug::GUI, "TocModels: type " << type
167 << " models_.size() " << models_.size());
169 LASSERT(type >= 0 && type < int(models_.size()), /**/);
170 return models_[type];
174 QModelIndex TocModels::currentIndex(int type) const
176 if (type < 0 || !bv_)
177 return QModelIndex();
178 return models_[type]->modelIndex(bv_->cursor());
182 void TocModels::goTo(int type, QModelIndex const & index) const
184 if (type < 0 || !index.isValid()
185 || index.model() != models_[type]) {
186 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
190 LASSERT(type >= 0 && type < int(models_.size()), /**/);
191 TocItem const item = models_[type]->tocItem(index);
192 LYXERR(Debug::GUI, "TocModels::goTo " << item.str());
193 dispatch(item.action());
197 void TocModels::updateBackend() const
199 bv_->buffer().masterBuffer()->tocBackend().update();
200 bv_->buffer().structureChanged();
204 void TocModels::reset(BufferView const * bv)
211 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
212 TocList::const_iterator it = tocs.begin();
213 TocList::const_iterator end = tocs.end();
214 for (; it != end; ++it) {
215 types_.push_back(toqstr(it->first));
216 type_names_.push_back(guiName(it->first, bv->buffer().params()));
217 models_.push_back(new TocModel(it->second));
222 bool TocModels::canOutline(int type) const
224 if (type < 0 || type >= types_.size())
226 return types_[type] == "tableofcontents";
230 int TocModels::decodeType(QString const & str) const
233 if (str.contains("tableofcontents")) {
234 new_type = "tableofcontents";
235 } else if (str.contains("floatlist")) {
236 if (str.contains("\"figure"))
238 else if (str.contains("\"table"))
240 else if (str.contains("\"algorithm"))
241 new_type = "algorithm";
242 } else if (!str.isEmpty()) {
245 // Default to Outliner.
246 new_type = "tableofcontents";
248 int const type = types_.indexOf(new_type);
251 // If everything else fails, settle on the table of contents which is
252 // guaranted to exist.
253 return types_.indexOf("tableofcontents");
256 } // namespace frontend
259 #include "TocModel_moc.cpp"