]> git.lyx.org Git - features.git/blob - src/frontends/qt4/TocModel.cpp
Remove warnings reported with gcc 4.3:
[features.git] / src / frontends / qt4 / TocModel.cpp
1 /**
2  * \file GuiTocDialog.C
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 "debug.h"
17
18 #include <climits>
19
20 #include <vector>
21 #include <string>
22
23 using std::endl;
24 using std::pair;
25 using std::map;
26 using std::vector;
27 using std::string;
28 using std::make_pair;
29 using std::max;
30 using std::min;
31
32 namespace lyx {
33 namespace frontend {
34
35
36 TocModel::TocModel(Toc const & toc)
37 {
38         populate(toc);
39 }
40
41
42 TocModel const & TocModel::operator=(Toc const & toc)
43 {
44         populate(toc);
45         return *this;
46 }
47
48
49 TocIterator const TocModel::tocIterator(QModelIndex const & index) const
50 {
51         TocMap::const_iterator map_it = toc_map_.find(index);
52         BOOST_ASSERT(map_it != toc_map_.end());
53         return map_it->second;
54 }
55
56
57 QModelIndex const TocModel::modelIndex(TocIterator const & it) const
58 {
59         ModelMap::const_iterator map_it = model_map_.find(it);
60         //BOOST_ASSERT(it != model_map_.end());
61
62         if (map_it == model_map_.end())
63                 return QModelIndex();
64
65         return map_it->second;
66 }
67
68
69 void TocModel::clear()
70 {
71         QStandardItemModel::clear();
72         toc_map_.clear();
73         model_map_.clear();
74         removeRows(0, rowCount());
75         removeColumns(0, columnCount());
76 }
77
78
79 void TocModel::populate(Toc const & toc)
80 {
81         clear();
82
83         if (toc.empty())
84                 return;
85         int current_row;
86         QModelIndex top_level_item;
87
88         TocIterator iter = toc.begin();
89         TocIterator end = toc.end();
90
91         insertColumns(0, 1);
92         maxdepth_ = 0;
93         mindepth_ = INT_MAX;
94
95         while (iter != end) {
96                 maxdepth_ = max(maxdepth_, iter->depth());
97                 mindepth_ = min(mindepth_, iter->depth());
98                 current_row = rowCount();
99                 insertRows(current_row, 1);
100                 top_level_item = QStandardItemModel::index(current_row, 0);
101                 //setData(top_level_item, toqstr(iter->str()));
102                 setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
103
104                 // This looks like a gcc bug, in principle this should work:
105                 //toc_map_[top_level_item] = iter;
106                 // but it crashes with gcc-4.1 and 4.0.2
107                 toc_map_.insert( TocPair(top_level_item, iter) );
108                 model_map_[iter] = top_level_item;
109
110                 LYXERR(Debug::GUI)
111                         << "Toc: at depth " << iter->depth()
112                         << ", added item " << to_utf8(iter->str())
113                         << endl;
114
115                 populate(iter, end, top_level_item);
116
117                 if (iter == end)
118                         break;
119
120                 ++iter;
121         }
122
123         setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
124 //      emit headerDataChanged();
125 }
126
127
128 void TocModel::populate(TocIterator & iter,
129                                                 TocIterator const & end,
130                                                 QModelIndex const & parent)
131 {
132         int curdepth = iter->depth() + 1;
133
134         int current_row;
135         QModelIndex child_item;
136
137         insertColumns(0, 1, parent);
138         while (iter != end) {
139
140                 ++iter;
141
142                 if (iter == end)
143                         break;
144
145                 if (iter->depth() < curdepth) {
146                         --iter;
147                         return;
148                 }
149
150                 maxdepth_ = max(maxdepth_, iter->depth());
151                 mindepth_ = min(mindepth_, iter->depth());
152                 current_row = rowCount(parent);
153                 insertRows(current_row, 1, parent);
154                 child_item = QStandardItemModel::index(current_row, 0, parent);
155                 //setData(child_item, toqstr(iter->str()));
156                 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
157
158                 // This looks like a gcc bug, in principle this should work:
159                 //toc_map_[child_item] = iter;
160                 // but it crashes with gcc-4.1 and 4.0.2
161                 toc_map_.insert( TocPair(child_item, iter) );
162                 model_map_[iter] = child_item;
163                 populate(iter, end, child_item);
164         }
165 }
166
167
168 int TocModel::modelDepth()
169 {
170         return maxdepth_ - mindepth_;
171 }
172
173 } // namespace frontend
174 } // namespace lyx
175
176 #include "TocModel_moc.cpp"