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.
16 #include "support/debug.h"
18 #include <boost/assert.hpp>
26 TocModel::TocModel(Toc const & toc)
32 TocModel const & TocModel::operator=(Toc const & toc)
39 TocIterator const TocModel::tocIterator(QModelIndex const & index) const
41 TocMap::const_iterator map_it = toc_map_.find(index);
42 BOOST_ASSERT(map_it != toc_map_.end());
43 return map_it->second;
47 QModelIndex const TocModel::modelIndex(TocIterator const & it) const
49 ModelMap::const_iterator map_it = model_map_.find(it);
50 //BOOST_ASSERT(it != model_map_.end());
52 if (map_it == model_map_.end())
55 return map_it->second;
59 void TocModel::clear()
61 QStandardItemModel::clear();
64 removeRows(0, rowCount());
65 removeColumns(0, columnCount());
69 void TocModel::populate(Toc const & toc)
76 QModelIndex top_level_item;
78 TocIterator iter = toc.begin();
79 TocIterator end = toc.end();
86 maxdepth_ = max(maxdepth_, iter->depth());
87 mindepth_ = min(mindepth_, iter->depth());
88 current_row = rowCount();
89 insertRows(current_row, 1);
90 top_level_item = QStandardItemModel::index(current_row, 0);
91 //setData(top_level_item, toqstr(iter->str()));
92 setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
94 // This looks like a gcc bug, in principle this should work:
95 //toc_map_[top_level_item] = iter;
96 // but it crashes with gcc-4.1 and 4.0.2
97 toc_map_.insert( TocPair(top_level_item, iter) );
98 model_map_[iter] = top_level_item;
100 LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
101 << ", added item " << to_utf8(iter->str()));
103 populate(iter, end, top_level_item);
111 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
112 // emit headerDataChanged();
116 void TocModel::populate(TocIterator & iter, TocIterator const & end,
117 QModelIndex const & parent)
119 int curdepth = iter->depth() + 1;
122 QModelIndex child_item;
124 insertColumns(0, 1, parent);
125 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_;
160 } // namespace frontend
163 #include "TocModel_moc.cpp"