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