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 "BufferParams.h"
18 #include "BufferView.h"
20 #include "FloatList.h"
21 #include "FuncRequest.h"
23 #include "ParIterator.h"
24 #include "TextClass.h"
26 #include "support/convert.h"
27 #include "support/debug.h"
28 #include "support/lassert.h"
37 typedef std::pair<QModelIndex, TocIterator> TocPair;
40 TocIterator TocModel::tocIterator(QModelIndex const & index) const
42 TocMap::const_iterator map_it = toc_map_.find(index);
43 LASSERT(map_it != toc_map_.end(), /**/);
44 return map_it->second;
48 QModelIndex TocModel::modelIndex(TocIterator const & it) const
50 ModelMap::const_iterator map_it = model_map_.find(it);
51 //LASSERT(it != model_map_.end(), /**/);
53 if (map_it == model_map_.end())
56 return map_it->second;
60 void TocModel::clear()
62 QStandardItemModel::clear();
65 removeRows(0, rowCount());
66 removeColumns(0, columnCount());
70 void TocModel::populate(Toc const & toc)
77 QModelIndex top_level_item;
79 TocIterator iter = toc.begin();
80 TocIterator end = toc.end();
87 maxdepth_ = max(maxdepth_, iter->depth());
88 mindepth_ = min(mindepth_, iter->depth());
89 current_row = rowCount();
90 insertRows(current_row, 1);
91 top_level_item = QStandardItemModel::index(current_row, 0);
92 //setData(top_level_item, toqstr(iter->str()));
93 setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
95 // This looks like a gcc bug, in principle this should work:
96 //toc_map_[top_level_item] = iter;
97 // but it crashes with gcc-4.1 and 4.0.2
98 toc_map_.insert( TocPair(top_level_item, iter) );
99 model_map_[iter] = top_level_item;
101 LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
102 << ", added item " << toqstr(iter->str()));
104 populate(iter, end, top_level_item);
112 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
113 // emit headerDataChanged();
117 void TocModel::populate(TocIterator & iter, TocIterator const & end,
118 QModelIndex const & parent)
120 int curdepth = iter->depth() + 1;
123 QModelIndex child_item;
124 insertColumns(0, 1, parent);
126 while (iter != end) {
132 if (iter->depth() < curdepth) {
137 maxdepth_ = max(maxdepth_, iter->depth());
138 mindepth_ = min(mindepth_, iter->depth());
139 current_row = rowCount(parent);
140 insertRows(current_row, 1, parent);
141 child_item = QStandardItemModel::index(current_row, 0, parent);
142 //setData(child_item, toqstr(iter->str()));
143 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
145 // This looks like a gcc bug, in principle this should work:
146 //toc_map_[child_item] = iter;
147 // but it crashes with gcc-4.1 and 4.0.2
148 toc_map_.insert( TocPair(child_item, iter) );
149 model_map_[iter] = child_item;
150 populate(iter, end, child_item);
155 int TocModel::modelDepth() const
157 return maxdepth_ - mindepth_;
161 ///////////////////////////////////////////////////////////////////////////////
162 // TocModels implementation.
163 ///////////////////////////////////////////////////////////////////////////////
164 void TocModels::clear()
168 const unsigned int size = models_.size();
169 for (unsigned int i = 0; i < size; ++i) {
176 int TocModels::depth(int type)
180 return models_[type]->modelDepth();
184 QStandardItemModel * TocModels::model(int type)
189 if (models_.empty()) {
190 LYXERR(Debug::GUI, "TocModels::tocModel(): no types available ");
194 LYXERR(Debug::GUI, "TocModels: type " << type
195 << " models_.size() " << models_.size());
197 LASSERT(type >= 0 && type < int(models_.size()), /**/);
198 return models_[type];
202 QModelIndex TocModels::currentIndex(int type) const
204 if (type < 0 || !bv_)
205 return QModelIndex();
207 ParConstIterator it(bv_->cursor());
208 return models_[type]->modelIndex(bv_->buffer().masterBuffer()->
209 tocBackend().item(fromqstr(types_[type]), it));
213 void TocModels::goTo(int type, QModelIndex const & index) const
215 if (type < 0 || !index.isValid()
216 || index.model() != models_[type]) {
217 LYXERR(Debug::GUI, "TocModels::goTo(): QModelIndex is invalid!");
221 LASSERT(type >= 0 && type < int(models_.size()), /**/);
223 TocIterator const it = models_[type]->tocIterator(index);
225 LYXERR(Debug::GUI, "TocModels::goTo " << it->str());
227 string const tmp = convert<string>(it->id());
228 dispatch(FuncRequest(LFUN_PARAGRAPH_GOTO, tmp));
232 void TocModels::updateBackend() const
234 bv_->buffer().masterBuffer()->tocBackend().update();
235 bv_->buffer().structureChanged();
239 QString TocModels::guiName(string const & type) const
241 if (type == "tableofcontents")
242 return qt_("Table of Contents");
244 return qt_("Child Documents");
245 if (type == "graphics")
246 return qt_("List of Graphics");
247 if (type == "equation")
248 return qt_("List of Equations");
249 if (type == "footnote")
250 return qt_("List of Footnotes");
251 if (type == "listing")
252 return qt_("List of Listings");
254 return qt_("List of Indexes");
255 if (type == "marginalnote")
256 return qt_("List of Marginal notes");
258 return qt_("List of Notes");
259 if (type == "citation")
260 return qt_("List of Citations");
262 return qt_("Labels and References");
264 FloatList const & floats = bv_->buffer().params().documentClass().floats();
265 if (floats.typeExist(type))
266 return qt_(floats.getType(type).listName());
272 void TocModels::reset(BufferView const * bv)
279 TocList const & tocs = bv_->buffer().masterBuffer()->tocBackend().tocs();
280 TocList::const_iterator it = tocs.begin();
281 TocList::const_iterator end = tocs.end();
282 for (; it != end; ++it) {
283 types_.push_back(toqstr(it->first));
284 type_names_.push_back(guiName(it->first));
285 models_.push_back(new TocModel(it->second));
290 bool TocModels::canOutline(int type) const
292 if (type < 0 || type >= types_.size())
294 return types_[type] == "tableofcontents";
298 int TocModels::decodeType(QString const & str) const
301 if (str.contains("tableofcontents")) {
302 new_type = "tableofcontents";
303 } else if (str.contains("floatlist")) {
304 if (str.contains("\"figure"))
306 else if (str.contains("\"table"))
308 else if (str.contains("\"algorithm"))
309 new_type = "algorithm";
310 } else if (!str.isEmpty()) {
313 // Default to Outliner.
314 new_type = "tableofcontents";
316 return types_.indexOf(new_type);
319 } // namespace frontend
322 #include "TocModel_moc.cpp"