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