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