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>
27 TocModel::TocModel(Toc const & toc)
33 TocModel const & TocModel::operator=(Toc const & toc)
40 TocIterator const TocModel::tocIterator(QModelIndex const & index) const
42 TocMap::const_iterator map_it = toc_map_.find(index);
43 BOOST_ASSERT(map_it != toc_map_.end());
44 return map_it->second;
48 QModelIndex const TocModel::modelIndex(TocIterator const & it) const
50 ModelMap::const_iterator map_it = model_map_.find(it);
51 //BOOST_ASSERT(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 " << to_utf8(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;
125 insertColumns(0, 1, parent);
126 while (iter != end) {
133 if (iter->depth() < curdepth) {
138 maxdepth_ = max(maxdepth_, iter->depth());
139 mindepth_ = min(mindepth_, iter->depth());
140 current_row = rowCount(parent);
141 insertRows(current_row, 1, parent);
142 child_item = QStandardItemModel::index(current_row, 0, parent);
143 //setData(child_item, toqstr(iter->str()));
144 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
146 // This looks like a gcc bug, in principle this should work:
147 //toc_map_[child_item] = iter;
148 // but it crashes with gcc-4.1 and 4.0.2
149 toc_map_.insert( TocPair(child_item, iter) );
150 model_map_[iter] = child_item;
151 populate(iter, end, child_item);
156 int TocModel::modelDepth() const
158 return maxdepth_ - mindepth_;
161 } // namespace frontend
164 #include "TocModel_moc.cpp"