]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QToc.C
* ControlToc:
[features.git] / src / frontends / qt4 / QToc.C
1 /**
2  * \file QToc.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 "QToc.h"
15
16 #include "TocModel.h"
17 #include "Qt2BC.h"
18 #include "qt_helpers.h"
19
20 #include "debug.h"
21
22 #include "controllers/ControlToc.h"
23
24 #include <algorithm>
25
26 using std::endl;
27
28 using std::pair;
29 using std::vector;
30 using std::string;
31
32 namespace lyx {
33 namespace frontend {
34
35
36 QToc::QToc(Dialog & parent)
37         : ControlToc(parent)
38 {
39 }
40
41
42 bool QToc::canOutline()
43 {
44         vector<string> const & types = getTypes();
45
46         if (types.empty())
47                 return false;
48
49         BOOST_ASSERT(type_ >= 0 && type_ < int(types.size()));
50         return ControlToc::canOutline(types[type_]);
51 }
52
53
54 int QToc::getTocDepth()
55 {
56         return toc_models_[type_]->modelDepth();
57 }
58
59
60 QStandardItemModel * QToc::tocModel()
61 {
62         if (toc_models_.empty()) {
63                 lyxerr[Debug::GUI] << "QToc::tocModel(): no types available " << endl;
64                 return 0;
65         }
66
67         lyxerr[Debug::GUI]
68                 << "QToc: type_ " << type_
69                 << "  toc_models_.size() " << toc_models_.size()
70                 << endl;
71
72         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
73         return toc_models_[type_];
74 }
75
76
77 QStandardItemModel * QToc::setTocModel(int type)
78 {
79         type_ = type;
80
81         lyxerr[Debug::GUI]
82                 << "QToc: type_ " << type_
83                 << "  toc_models_.size() " << toc_models_.size()
84                 << endl;
85
86         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
87         return toc_models_[type_];
88 }
89
90
91 QModelIndex const QToc::getCurrentIndex()
92 {
93         vector<string> const & types = getTypes();
94         TocIterator const it = getCurrentTocItem(types[type_]);
95         if (it == getContents(types[type_]).end() || !it->isValid()) {
96                 lyxerr[Debug::GUI] << "QToc::getCurrentIndex(): TocItem is invalid!" << endl;
97                 return QModelIndex();
98         }
99
100         return toc_models_[type_]->modelIndex(it);
101 }
102
103
104 void QToc::goTo(QModelIndex const & index)
105 {
106         if (!index.isValid() || index.model() != tocModel()) {
107                 lyxerr[Debug::GUI]
108                         << "QToc::goTo(): QModelIndex is invalid!"
109                         << endl;
110                 return;
111         }
112
113         TocIterator const it = toc_models_[type_]->tocIterator(index);
114         
115         lyxerr[Debug::GUI]
116                 << "QToc::goTo " << lyx::to_utf8(it->str())
117                 << endl;
118
119         ControlToc::goTo(*it);
120 }
121
122
123 int QToc::getType()
124 {
125         return type_;
126 }
127
128
129 void QToc::update()
130 {
131         updateType();
132         updateToc();
133         modelReset();
134 }
135
136
137 void QToc::updateType()
138 {
139
140         QStringList type_list;
141
142         vector<string> const & types = getTypes();
143         if (types.empty()) {
144                 type_model_.setStringList(type_list);
145                 toc_models_.clear();
146                 lyxerr[Debug::GUI] << "QToc::updateType(): no types available " << endl;
147                 return;
148         }
149
150         string selected_type ;
151         if(params()["type"].empty()) //Then plain toc...
152                 selected_type = params().getCmdName();
153         else
154                 selected_type = to_ascii(params()["type"]);
155
156         QString gui_names_;
157         for (size_t i = 0; i != types.size(); ++i) {
158                 string const & type_str = types[i];
159                 type_list.append(toqstr(getGuiName(type_str)));
160                 if (type_str == selected_type)
161                         type_ = i;
162                 
163                 lyxerr[Debug::GUI]
164                         << "QToc: new type " << type_str
165                         << "\ttoc_models_.size() " << toc_models_.size()
166                         << endl;
167
168         }
169         type_model_.setStringList(type_list);
170 }
171
172
173 void QToc::updateToc()
174 {
175         toc_models_.clear();
176         vector<string> const & types = getTypes();
177
178         for (size_t i = 0; i != types.size(); ++i) {
179
180                 toc_models_.push_back(new TocModel(getContents(types[i])));
181         }
182         
183 }
184
185
186 } // namespace frontend
187 } // namespace lyx
188
189 #include "QToc_moc.cpp"