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