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