]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QToc.C
add some safeguards (while investigating bug 3152).
[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         if (type_ < 0)
57                 return 0;
58         return toc_models_[type_]->modelDepth();
59 }
60
61
62 QStandardItemModel * QToc::tocModel()
63 {
64         if (toc_models_.empty()) {
65                 lyxerr[Debug::GUI] << "QToc::tocModel(): no types available " << endl;
66                 return 0;
67         }
68
69         lyxerr[Debug::GUI]
70                 << "QToc: type_ " << type_
71                 << "  toc_models_.size() " << toc_models_.size()
72                 << endl;
73
74         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
75         return toc_models_[type_];
76 }
77
78
79 QStandardItemModel * QToc::setTocModel(int type)
80 {
81         type_ = type;
82
83         lyxerr[Debug::GUI]
84                 << "QToc: type_ " << type_
85                 << "  toc_models_.size() " << toc_models_.size()
86                 << endl;
87
88         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
89         return toc_models_[type_];
90 }
91
92
93 QModelIndex const QToc::getCurrentIndex()
94 {
95         vector<string> const & types = getTypes();
96         if (types.empty() || type_ < 0)
97                 return QModelIndex();
98
99         TocIterator const it = getCurrentTocItem(types[type_]);
100         if (it == getContents(types[type_]).end() || !it->isValid()) {
101                 lyxerr[Debug::GUI] << "QToc::getCurrentIndex(): TocItem is invalid!" << endl;
102                 return QModelIndex();
103         }
104
105         return toc_models_[type_]->modelIndex(it);
106 }
107
108
109 void QToc::goTo(QModelIndex const & index)
110 {
111         if (!index.isValid() || index.model() != tocModel()) {
112                 lyxerr[Debug::GUI]
113                         << "QToc::goTo(): QModelIndex is invalid!"
114                         << endl;
115                 return;
116         }
117
118         BOOST_ASSERT(type_ >= 0 && type_ < int(toc_models_.size()));
119
120         TocIterator const it = toc_models_[type_]->tocIterator(index);
121         
122         lyxerr[Debug::GUI]
123                 << "QToc::goTo " << lyx::to_utf8(it->str())
124                 << endl;
125
126         ControlToc::goTo(*it);
127 }
128
129
130 int QToc::getType()
131 {
132         return type_;
133 }
134
135
136 void QToc::update()
137 {
138         updateType();
139         updateToc();
140         modelReset();
141 }
142
143
144 void QToc::updateType()
145 {
146
147         QStringList type_list;
148
149         vector<string> const & types = getTypes();
150         if (types.empty()) {
151                 type_model_.setStringList(type_list);
152                 toc_models_.clear();
153                 lyxerr[Debug::GUI] << "QToc::updateType(): no types available " << endl;
154                 return;
155         }
156
157         string selected_type ;
158         if(params()["type"].empty()) //Then plain toc...
159                 selected_type = params().getCmdName();
160         else
161                 selected_type = to_ascii(params()["type"]);
162
163         QString gui_names_;
164         type_ = -1;
165         for (size_t i = 0; i != types.size(); ++i) {
166                 string const & type_str = types[i];
167                 type_list.append(toqstr(getGuiName(type_str)));
168                 if (type_str == selected_type)
169                         type_ = i;
170                 
171                 lyxerr[Debug::GUI]
172                         << "QToc: new type " << type_str
173                         << "\ttoc_models_.size() " << toc_models_.size()
174                         << endl;
175
176         }
177         type_model_.setStringList(type_list);
178 }
179
180
181 void QToc::updateToc()
182 {
183         toc_models_.clear();
184         vector<string> const & types = getTypes();
185
186         for (size_t i = 0; i != types.size(); ++i) {
187
188                 toc_models_.push_back(new TocModel(getContents(types[i])));
189         }
190         
191 }
192
193
194 } // namespace frontend
195 } // namespace lyx
196
197 #include "QToc_moc.cpp"