]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.C
* ControlToc.[Ch]
[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 "TocModel.h"
13
14 #include "debug.h"
15
16 #include <vector>
17 #include <string>
18
19 using std::endl;
20 using std::pair;
21 using std::map;
22 using std::vector;
23 using std::string;
24 using std::make_pair;
25
26 namespace lyx {
27 namespace frontend {
28
29
30 TocModel::TocModel(toc::Toc const & toc_list)
31 {
32         populate(toc_list);
33 }
34
35
36 TocModel const & TocModel::operator=(toc::Toc const & toc_list)
37 {
38         populate(toc_list);
39         return *this;
40 }
41
42 toc::TocItem const TocModel::item(QModelIndex const & index) const
43 {
44         ItemMap::const_iterator it = item_map_.find(index);
45         BOOST_ASSERT(it != item_map_.end());
46         
47         return it->second;
48 }
49
50 QModelIndex const TocModel::index(string const & toc_str) const
51 {
52         IndexMap::const_iterator it = index_map_.find(toc_str);
53         //BOOST_ASSERT(it != index_map_.end());
54
55         if (it == index_map_.end())
56                 return QModelIndex();
57         
58         return it->second;
59 }
60
61 void TocModel::clear()
62 {
63         QStandardItemModel::clear();
64         item_map_.clear();
65         index_map_.clear();
66         removeRows(0, rowCount());
67         removeColumns(0, columnCount());
68 }
69
70
71 void TocModel::populate(toc::Toc const & toc_list)
72 {
73         clear();
74
75         if (toc_list.empty())
76                 return;
77
78         int current_row;
79         QModelIndex top_level_item;
80
81         toc::Toc::const_iterator iter = toc_list.begin();
82         toc::Toc::const_iterator end = toc_list.end();
83
84     insertColumns(0, 1);
85
86         while (iter != end) {
87
88                 if (iter->depth == 1) {
89
90                         current_row = rowCount();
91                         insertRows(current_row, 1);
92                         top_level_item = QStandardItemModel::index(current_row, 0);
93                         //setData(top_level_item, toqstr(iter->str));
94                         setData(top_level_item, toqstr(iter->str), Qt::DisplayRole);
95                         item_map_.insert(make_pair(top_level_item, *iter));
96                         index_map_.insert(make_pair(
97                                 iter->str.substr(iter->str.find(' ') + 1), top_level_item));
98
99                         lyxerr[Debug::GUI]
100                                 << "Toc: at depth " << iter->depth
101                                 << ", added item " << iter->str
102                                 << endl;
103
104                         populate(iter, end, top_level_item);
105                 }
106
107                 if (iter == end)
108                         break;
109
110                 ++iter;
111         }
112         
113         setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
114 //      emit headerDataChanged();
115 }
116
117
118 void TocModel::populate(toc::Toc::const_iterator & iter,
119                                                 toc::Toc::const_iterator const & end,
120                                                 QModelIndex const & parent)
121 {
122         int curdepth = iter->depth + 1;
123         int current_row;
124         QModelIndex child_item;
125
126     insertColumns(0, 1, parent);
127         while (iter != end) {
128
129                 ++iter;
130
131                 if (iter == end)
132                         break;
133
134                 if (iter->depth < curdepth) {
135                         --iter;
136                         return;
137                 }
138                 if (iter->depth > curdepth) {
139                         return;
140                 }
141                 
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                 item_map_.insert(make_pair(child_item, *iter));
148                 index_map_.insert(make_pair(
149                         iter->str.substr(iter->str.find(' ') + 1), child_item));
150
151 //              lyxerr[Debug::GUI]
152 //                      << "Toc: at depth " << iter->depth
153 //                      << ", added item " << iter->str 
154 //                      << endl;
155
156                 populate(iter, end, child_item);
157         }
158 }
159
160 } // namespace frontend
161 } // namespace lyx