]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
adc580bc99f4785aa26fe71221ef8284b5663491
[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 "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, "Toc: at depth " << iter->depth()
104                         << ", added item " << to_utf8(iter->str()));
105
106                 populate(iter, end, top_level_item);
107
108                 if (iter == end)
109                         break;
110
111                 ++iter;
112         }
113
114         setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
115 //      emit headerDataChanged();
116 }
117
118
119 void TocModel::populate(TocIterator & iter, TocIterator const & end,
120         QModelIndex const & parent)
121 {
122         int curdepth = iter->depth() + 1;
123
124         int current_row;
125         QModelIndex child_item;
126
127         insertColumns(0, 1, parent);
128         while (iter != end) {
129
130                 ++iter;
131
132                 if (iter == end)
133                         break;
134
135                 if (iter->depth() < curdepth) {
136                         --iter;
137                         return;
138                 }
139
140                 maxdepth_ = max(maxdepth_, iter->depth());
141                 mindepth_ = min(mindepth_, iter->depth());
142                 current_row = rowCount(parent);
143                 insertRows(current_row, 1, parent);
144                 child_item = QStandardItemModel::index(current_row, 0, parent);
145                 //setData(child_item, toqstr(iter->str()));
146                 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
147
148                 // This looks like a gcc bug, in principle this should work:
149                 //toc_map_[child_item] = iter;
150                 // but it crashes with gcc-4.1 and 4.0.2
151                 toc_map_.insert( TocPair(child_item, iter) );
152                 model_map_[iter] = child_item;
153                 populate(iter, end, child_item);
154         }
155 }
156
157
158 int TocModel::modelDepth() const
159 {
160         return maxdepth_ - mindepth_;
161 }
162
163 } // namespace frontend
164 } // namespace lyx
165
166 #include "TocModel_moc.cpp"