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.
18 #include <boost/assert.hpp>
29 TocModel::TocModel(Toc const & toc)
35 TocModel const & TocModel::operator=(Toc const & toc)
42 TocIterator const TocModel::tocIterator(QModelIndex const & index) const
44 TocMap::const_iterator map_it = toc_map_.find(index);
45 BOOST_ASSERT(map_it != toc_map_.end());
46 return map_it->second;
50 QModelIndex const TocModel::modelIndex(TocIterator const & it) const
52 ModelMap::const_iterator map_it = model_map_.find(it);
53 //BOOST_ASSERT(it != model_map_.end());
55 if (map_it == model_map_.end())
58 return map_it->second;
62 void TocModel::clear()
64 QStandardItemModel::clear();
67 removeRows(0, rowCount());
68 removeColumns(0, columnCount());
72 void TocModel::populate(Toc const & toc)
79 QModelIndex top_level_item;
81 TocIterator iter = toc.begin();
82 TocIterator end = toc.end();
89 maxdepth_ = max(maxdepth_, iter->depth());
90 mindepth_ = min(mindepth_, iter->depth());
91 current_row = rowCount();
92 insertRows(current_row, 1);
93 top_level_item = QStandardItemModel::index(current_row, 0);
94 //setData(top_level_item, toqstr(iter->str()));
95 setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
97 // This looks like a gcc bug, in principle this should work:
98 //toc_map_[top_level_item] = iter;
99 // but it crashes with gcc-4.1 and 4.0.2
100 toc_map_.insert( TocPair(top_level_item, iter) );
101 model_map_[iter] = top_level_item;
104 << "Toc: at depth " << iter->depth()
105 << ", added item " << to_utf8(iter->str())
108 populate(iter, end, top_level_item);
116 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
117 // emit headerDataChanged();
121 void TocModel::populate(TocIterator & iter, TocIterator const & end,
122 QModelIndex const & parent)
124 int curdepth = iter->depth() + 1;
127 QModelIndex child_item;
129 insertColumns(0, 1, parent);
130 while (iter != end) {
137 if (iter->depth() < curdepth) {
142 maxdepth_ = max(maxdepth_, iter->depth());
143 mindepth_ = min(mindepth_, iter->depth());
144 current_row = rowCount(parent);
145 insertRows(current_row, 1, parent);
146 child_item = QStandardItemModel::index(current_row, 0, parent);
147 //setData(child_item, toqstr(iter->str()));
148 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
150 // This looks like a gcc bug, in principle this should work:
151 //toc_map_[child_item] = iter;
152 // but it crashes with gcc-4.1 and 4.0.2
153 toc_map_.insert( TocPair(child_item, iter) );
154 model_map_[iter] = child_item;
155 populate(iter, end, child_item);
160 int TocModel::modelDepth() const
162 return maxdepth_ - mindepth_;
165 } // namespace frontend
168 #include "TocModel_moc.cpp"