]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.cpp
Complete the removal of the embedding stuff. Maybe. It's hard to be sure we got every...
[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 " << to_utf8(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
114         insertColumns(0, 1, parent);
115         while (iter != end) {
116
117                 ++iter;
118
119                 if (iter == end)
120                         break;
121
122                 if (iter->depth() < curdepth) {
123                         --iter;
124                         return;
125                 }
126
127                 maxdepth_ = max(maxdepth_, iter->depth());
128                 mindepth_ = min(mindepth_, iter->depth());
129                 current_row = rowCount(parent);
130                 insertRows(current_row, 1, parent);
131                 child_item = QStandardItemModel::index(current_row, 0, parent);
132                 //setData(child_item, toqstr(iter->str()));
133                 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
134
135                 // This looks like a gcc bug, in principle this should work:
136                 //toc_map_[child_item] = iter;
137                 // but it crashes with gcc-4.1 and 4.0.2
138                 toc_map_.insert( TocPair(child_item, iter) );
139                 model_map_[iter] = child_item;
140                 populate(iter, end, child_item);
141         }
142 }
143
144
145 int TocModel::modelDepth() const
146 {
147         return maxdepth_ - mindepth_;
148 }
149
150 } // namespace frontend
151 } // namespace lyx
152
153 #include "TocModel_moc.cpp"