]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.C
enable Font cache only for MacOSX and inline width() for other platform.
[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(TocBackend::Toc const & toc)
33 {
34         populate(toc);
35 }
36
37
38 TocModel const & TocModel::operator=(TocBackend::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(TocBackend::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                         toc_map_[top_level_item] = iter;
100                         model_map_[iter] = top_level_item;
101
102                         lyxerr[Debug::GUI]
103                                 << "Toc: at depth " << iter->depth()
104                                 << ", added item " << iter->str()
105                                 << endl;
106
107                         populate(iter, end, top_level_item);
108                 }
109
110                 if (iter == end)
111                         break;
112
113                 ++iter;
114         }
115         
116         setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
117 //      emit headerDataChanged();
118 }
119
120
121 void TocModel::populate(TocIterator & iter,
122                                                 TocIterator const & end,
123                                                 QModelIndex const & parent)
124 {
125         int curdepth = iter->depth() + 1;
126         int current_row;
127         QModelIndex child_item;
128
129         insertColumns(0, 1, parent);
130         while (iter != end) {
131
132                 ++iter;
133
134                 if (iter == end)
135                         break;
136
137                 if (iter->depth() < curdepth) {
138                         --iter;
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                 toc_map_[child_item] = iter;
148                 model_map_[iter] = child_item;
149                 populate(iter, end, child_item);
150         }
151 }
152
153
154 } // namespace frontend
155 } // namespace lyx
156
157 #include "TocModel_moc.cpp"