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 "support/assert.h"
26 typedef std::pair<QModelIndex, TocIterator> TocPair;
29 TocIterator TocModel::tocIterator(QModelIndex const & index) const
31 TocMap::const_iterator map_it = toc_map_.find(index);
32 LASSERT(map_it != toc_map_.end(), /**/);
33 return map_it->second;
37 QModelIndex TocModel::modelIndex(TocIterator const & it) const
39 ModelMap::const_iterator map_it = model_map_.find(it);
40 //LASSERT(it != model_map_.end(), /**/);
42 if (map_it == model_map_.end())
45 return map_it->second;
49 void TocModel::clear()
51 QStandardItemModel::clear();
54 removeRows(0, rowCount());
55 removeColumns(0, columnCount());
59 void TocModel::populate(Toc const & toc)
66 QModelIndex top_level_item;
68 TocIterator iter = toc.begin();
69 TocIterator end = toc.end();
76 maxdepth_ = max(maxdepth_, iter->depth());
77 mindepth_ = min(mindepth_, iter->depth());
78 current_row = rowCount();
79 insertRows(current_row, 1);
80 top_level_item = QStandardItemModel::index(current_row, 0);
81 //setData(top_level_item, toqstr(iter->str()));
82 setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
84 // This looks like a gcc bug, in principle this should work:
85 //toc_map_[top_level_item] = iter;
86 // but it crashes with gcc-4.1 and 4.0.2
87 toc_map_.insert( TocPair(top_level_item, iter) );
88 model_map_[iter] = top_level_item;
90 LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
91 << ", added item " << toqstr(iter->str()));
93 populate(iter, end, top_level_item);
101 setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
102 // emit headerDataChanged();
106 void TocModel::populate(TocIterator & iter, TocIterator const & end,
107 QModelIndex const & parent)
109 int curdepth = iter->depth() + 1;
112 QModelIndex child_item;
113 insertColumns(0, 1, parent);
115 while (iter != end) {
121 if (iter->depth() < curdepth) {
126 maxdepth_ = max(maxdepth_, iter->depth());
127 mindepth_ = min(mindepth_, iter->depth());
128 current_row = rowCount(parent);
129 insertRows(current_row, 1, parent);
130 child_item = QStandardItemModel::index(current_row, 0, parent);
131 //setData(child_item, toqstr(iter->str()));
132 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
134 // This looks like a gcc bug, in principle this should work:
135 //toc_map_[child_item] = iter;
136 // but it crashes with gcc-4.1 and 4.0.2
137 toc_map_.insert( TocPair(child_item, iter) );
138 model_map_[iter] = child_item;
139 populate(iter, end, child_item);
144 int TocModel::modelDepth() const
146 return maxdepth_ - mindepth_;
149 } // namespace frontend
152 #include "TocModel_moc.cpp"