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>
37 TocModel::TocModel(Toc const & toc)
43 TocModel const & TocModel::operator=(Toc const & toc)
50 TocIterator const TocModel::tocIterator(QModelIndex const & index) const
52 TocMap::const_iterator map_it = toc_map_.find(index);
53 BOOST_ASSERT(map_it != toc_map_.end());
54 return map_it->second;
58 QModelIndex const TocModel::modelIndex(TocIterator const & it) const
60 ModelMap::const_iterator map_it = model_map_.find(it);
61 //BOOST_ASSERT(it != model_map_.end());
63 if (map_it == model_map_.end())
66 return map_it->second;
70 void TocModel::clear()
72 QStandardItemModel::clear();
75 removeRows(0, rowCount());
76 removeColumns(0, columnCount());
80 void TocModel::populate(Toc const & toc)
87 QModelIndex top_level_item;
89 TocIterator iter = toc.begin();
90 TocIterator end = toc.end();
97 maxdepth_ = max(maxdepth_, iter->depth());
98 mindepth_ = min(mindepth_, iter->depth());
99 current_row = rowCount();
100 insertRows(current_row, 1);
101 top_level_item = QStandardItemModel::index(current_row, 0);
102 //setData(top_level_item, toqstr(iter->str()));
103 setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
105 // This looks like a gcc bug, in principle this should work:
106 //toc_map_[top_level_item] = iter;
107 // but it crashes with gcc-4.1 and 4.0.2
108 toc_map_.insert( TocPair(top_level_item, iter) );
109 model_map_[iter] = top_level_item;
112 << "Toc: at depth " << iter->depth()
113 << ", added item " << to_utf8(iter->str())
116 populate(iter, end, top_level_item);
124 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
125 // emit headerDataChanged();
129 void TocModel::populate(TocIterator & iter,
130 TocIterator const & end,
131 QModelIndex const & parent)
133 int curdepth = iter->depth() + 1;
136 QModelIndex child_item;
138 insertColumns(0, 1, parent);
139 while (iter != end) {
146 if (iter->depth() < curdepth) {
151 maxdepth_ = max(maxdepth_, iter->depth());
152 mindepth_ = min(mindepth_, iter->depth());
153 current_row = rowCount(parent);
154 insertRows(current_row, 1, parent);
155 child_item = QStandardItemModel::index(current_row, 0, parent);
156 //setData(child_item, toqstr(iter->str()));
157 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
159 // This looks like a gcc bug, in principle this should work:
160 //toc_map_[child_item] = iter;
161 // but it crashes with gcc-4.1 and 4.0.2
162 toc_map_.insert( TocPair(child_item, iter) );
163 model_map_[iter] = child_item;
164 populate(iter, end, child_item);
169 int TocModel::modelDepth()
171 return maxdepth_ - mindepth_;
174 } // namespace frontend
177 #include "TocModel_moc.cpp"