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"
23 #include "support/convert.h"
24 #include "support/debug.h"
25 #include "support/lassert.h"
34 typedef std::pair<QModelIndex, TocIterator> TocPair;
37 TocIterator TocModel::tocIterator(QModelIndex const & index) const
39 TocMap::const_iterator map_it = toc_map_.find(index);
40 LASSERT(map_it != toc_map_.end(), /**/);
41 return map_it->second;
45 QModelIndex TocModel::modelIndex(TocIterator const & it) const
47 ModelMap::const_iterator map_it = model_map_.find(it);
48 //LASSERT(it != model_map_.end(), /**/);
50 if (map_it == model_map_.end())
53 return map_it->second;
57 void TocModel::clear()
59 QStandardItemModel::clear();
62 removeRows(0, rowCount());
63 removeColumns(0, columnCount());
67 void TocModel::populate(Toc const & toc)
74 QModelIndex top_level_item;
76 TocIterator iter = toc.begin();
77 TocIterator end = toc.end();
84 maxdepth_ = max(maxdepth_, iter->depth());
85 mindepth_ = min(mindepth_, iter->depth());
86 current_row = rowCount();
87 insertRows(current_row, 1);
88 top_level_item = QStandardItemModel::index(current_row, 0);
89 //setData(top_level_item, toqstr(iter->str()));
90 setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
92 // This looks like a gcc bug, in principle this should work:
93 //toc_map_[top_level_item] = iter;
94 // but it crashes with gcc-4.1 and 4.0.2
95 toc_map_.insert( TocPair(top_level_item, iter) );
96 model_map_[iter] = top_level_item;
98 LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
99 << ", added item " << toqstr(iter->str()));
101 populate(iter, end, top_level_item);
109 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
110 // emit headerDataChanged();
114 void TocModel::populate(TocIterator & iter, TocIterator const & end,
115 QModelIndex const & parent)
117 int curdepth = iter->depth() + 1;
120 QModelIndex child_item;
121 insertColumns(0, 1, parent);
123 while (iter != end) {
129 if (iter->depth() < curdepth) {
134 maxdepth_ = max(maxdepth_, iter->depth());
135 mindepth_ = min(mindepth_, iter->depth());
136 current_row = rowCount(parent);
137 insertRows(current_row, 1, parent);
138 child_item = QStandardItemModel::index(current_row, 0, parent);
139 //setData(child_item, toqstr(iter->str()));
140 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
142 // This looks like a gcc bug, in principle this should work:
143 //toc_map_[child_item] = iter;
144 // but it crashes with gcc-4.1 and 4.0.2
145 toc_map_.insert( TocPair(child_item, iter) );
146 model_map_[iter] = child_item;
147 populate(iter, end, child_item);
152 int TocModel::modelDepth() const
154 return maxdepth_ - mindepth_;
158 ///////////////////////////////////////////////////////////////////////////////
159 // TocModels implementation.
160 ///////////////////////////////////////////////////////////////////////////////
161 void TocModels::clear()
165 const unsigned int size = models_.size();
166 for (unsigned int i = 0; i < size; ++i) {
173 int TocModels::depth(int type)
177 return models_[type]->modelDepth();
181 QStandardItemModel * TocModels::model(int type)
186 if (models_.empty()) {
187 LYXERR(Debug::GUI, "TocModels::tocModel(): no types available ");
191 LYXERR(Debug::GUI, "TocModels: type " << type
192 << " models_.size() " << models_.size());
194 LASSERT(type >= 0 && type < int(models_.size()), /**/);
195 return models_[type];
199 QModelIndex TocModels::currentIndex(int type) const
201 if (type < 0 || !bv_)
202 return QModelIndex();
204 TocIterator const it = bv_->buffer().masterBuffer()->tocBackend().item(
205 fromqstr(types_[type]), bv_->cursor());
206 return models_[type]->modelIndex(it);
210 void TocModels::goTo(int type, QModelIndex const & index) const
212 if (type < 0 || !index.isValid()
213 || index.model() != models_[type]) {
214 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
218 LASSERT(type >= 0 && type < int(models_.size()), /**/);
219 TocIterator const it = models_[type]->tocIterator(index);
220 LYXERR(Debug::GUI, "TocModels::goTo " << it->str());
221 dispatch(it->action());
225 void TocModels::updateBackend() const
227 bv_->buffer().masterBuffer()->tocBackend().update();
228 bv_->buffer().structureChanged();
232 void TocModels::reset(BufferView const * bv)
239 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
240 TocList::const_iterator it = tocs.begin();
241 TocList::const_iterator end = tocs.end();
242 for (; it != end; ++it) {
243 types_.push_back(toqstr(it->first));
244 type_names_.push_back(guiName(it->first, bv->buffer().params()));
245 models_.push_back(new TocModel(it->second));
250 bool TocModels::canOutline(int type) const
252 if (type < 0 || type >= types_.size())
254 return types_[type] == "tableofcontents";
258 int TocModels::decodeType(QString const & str) const
261 if (str.contains("tableofcontents")) {
262 new_type = "tableofcontents";
263 } else if (str.contains("floatlist")) {
264 if (str.contains("\"figure"))
266 else if (str.contains("\"table"))
268 else if (str.contains("\"algorithm"))
269 new_type = "algorithm";
270 } else if (!str.isEmpty()) {
273 // Default to Outliner.
274 new_type = "tableofcontents";
276 int const type = types_.indexOf(new_type);
279 // If everything else fails, settle on the table of contents which is
280 // guaranted to exist.
281 return types_.indexOf("tableofcontents");
284 } // namespace frontend
287 #include "TocModel_moc.cpp"