]> git.lyx.org Git - lyx.git/blob - src/frontends/qt4/TocModel.C
rename LFUN enum values according to their command (as used in th minibuffer/bind...
[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(TocBackend::Toc const & toc)
31 {
32         populate(toc);
33 }
34
35
36 TocModel const & TocModel::operator=(TocBackend::Toc const & toc)
37 {
38         populate(toc);
39         return *this;
40 }
41
42
43 TocIterator const TocModel::tocIterator(QModelIndex const & index) const
44 {
45         TocMap::const_iterator map_it = toc_map_.find(index);
46         BOOST_ASSERT(map_it != toc_map_.end()); 
47         return map_it->second;
48 }
49
50
51 QModelIndex const TocModel::modelIndex(TocIterator const & it) const
52 {
53         ModelMap::const_iterator map_it = model_map_.find(it);
54         //BOOST_ASSERT(it != model_map_.end());
55
56         if (map_it == model_map_.end())
57                 return QModelIndex();
58         
59         return map_it->second;
60 }
61
62
63 void TocModel::clear()
64 {
65         QStandardItemModel::clear();
66         toc_map_.clear();
67         model_map_.clear();
68         removeRows(0, rowCount());
69         removeColumns(0, columnCount());
70 }
71
72
73 void TocModel::populate(TocBackend::Toc const & toc)
74 {
75         clear();
76
77         if (toc.empty())
78                 return;
79
80         int current_row;
81         QModelIndex top_level_item;
82
83         TocIterator iter = toc.begin();
84         TocIterator end = toc.end();
85
86         insertColumns(0, 1);
87
88         while (iter != end) {
89
90                 if (iter->isValid()) {
91
92                         current_row = rowCount();
93                         insertRows(current_row, 1);
94                         top_level_item = QStandardItemModel::index(current_row, 0);
95                         //setData(top_level_item, toqstr(iter->str()));
96                         setData(top_level_item, toqstr(iter->str()), Qt::DisplayRole);
97                         toc_map_[top_level_item] = iter;
98                         model_map_[iter] = top_level_item;
99
100                         lyxerr[Debug::GUI]
101                                 << "Toc: at depth " << iter->depth()
102                                 << ", added item " << iter->str()
103                                 << endl;
104
105                         populate(iter, end, top_level_item);
106                 }
107
108                 if (iter == end)
109                         break;
110
111                 ++iter;
112         }
113         
114         setHeaderData(0, Qt::Horizontal, QVariant("title"), Qt::DisplayRole);
115 //      emit headerDataChanged();
116 }
117
118
119 void TocModel::populate(TocIterator & iter,
120                                                 TocIterator const & end,
121                                                 QModelIndex const & parent)
122 {
123         int curdepth = iter->depth() + 1;
124         int current_row;
125         QModelIndex child_item;
126
127         insertColumns(0, 1, parent);
128         while (iter != end) {
129
130                 ++iter;
131
132                 if (iter == end)
133                         break;
134
135                 if (iter->depth() < curdepth) {
136                         --iter;
137                         return;
138                 }
139                 
140                 current_row = rowCount(parent);
141                 insertRows(current_row, 1, parent);
142                 child_item = QStandardItemModel::index(current_row, 0, parent);
143                 //setData(child_item, toqstr(iter->str()));
144                 setData(child_item, toqstr(iter->str()), Qt::DisplayRole);
145                 toc_map_[child_item] = iter;
146                 model_map_[iter] = child_item;
147                 populate(iter, end, child_item);
148         }
149 }
150
151
152 } // namespace frontend
153 } // namespace lyx