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