]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
getting rid of superfluous lyx::support:: statements.
[lyx.git] / src / frontends / qt4 / TocModel.cpp
1 /**
2  * \file TocModel.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author John Levon
7  * \author Abdelrazak Younes
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #include <config.h>
13
14 #include "TocModel.h"
15
16 #include "support/debug.h"
17
18 #include <boost/assert.hpp>
19
20 using namespace std;
21
22 namespace lyx {
23 namespace frontend {
24
25
26 TocModel::TocModel(Toc const & toc)
27 {
28         populate(toc);
29 }
30
31
32 TocModel const & TocModel::operator=(Toc const & toc)
33 {
34         populate(toc);
35         return *this;
36 }
37
38
39 TocIterator const TocModel::tocIterator(QModelIndex const & index) const
40 {
41         TocMap::const_iterator map_it = toc_map_.find(index);
42         BOOST_ASSERT(map_it != toc_map_.end());
43         return map_it->second;
44 }
45
46
47 QModelIndex const TocModel::modelIndex(TocIterator const & it) const
48 {
49         ModelMap::const_iterator map_it = model_map_.find(it);
50         //BOOST_ASSERT(it != model_map_.end());
51
52         if (map_it == model_map_.end())
53                 return QModelIndex();
54
55         return map_it->second;
56 }
57
58
59 void TocModel::clear()
60 {
61         QStandardItemModel::clear();
62         toc_map_.clear();
63         model_map_.clear();
64         removeRows(0, rowCount());
65         removeColumns(0, columnCount());
66 }
67
68
69 void TocModel::populate(Toc const & toc)
70 {
71         clear();
72
73         if (toc.empty())
74                 return;
75         int current_row;
76         QModelIndex top_level_item;
77
78         TocIterator iter = toc.begin();
79         TocIterator end = toc.end();
80
81         insertColumns(0, 1);
82         maxdepth_ = 0;
83         mindepth_ = INT_MAX;
84
85         while (iter != 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);
93
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;
99
100                 LYXERR(Debug::GUI, "Toc: at depth " << iter->depth()
101                         << ", added item " << to_utf8(iter->str()));
102
103                 populate(iter, end, top_level_item);
104
105                 if (iter == end)
106                         break;
107
108                 ++iter;
109         }
110
111         setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
112 //      emit headerDataChanged();
113 }
114
115
116 void TocModel::populate(TocIterator & iter, TocIterator const & end,
117         QModelIndex const & parent)
118 {
119         int curdepth = iter->depth() + 1;
120
121         int current_row;
122         QModelIndex child_item;
123
124         insertColumns(0, 1, parent);
125         while (iter != end) {
126
127                 ++iter;
128
129                 if (iter == end)
130                         break;
131
132                 if (iter->depth() < curdepth) {
133                         --iter;
134                         return;
135                 }
136
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);
144
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);
151         }
152 }
153
154
155 int TocModel::modelDepth() const
156 {
157         return maxdepth_ - mindepth_;
158 }
159
160 } // namespace frontend
161 } // namespace lyx
162
163 #include "TocModel_moc.cpp"