]> git.lyx.org Git - features.git/blob - src/frontends/qt4/QToc.C
coding style: fix crash on buffer-switching (2 tabs) + toc-clicking
[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 #include "TocModel.h"
16 #include "Qt2BC.h"
17 #include "qt_helpers.h"
18
19 #include "debug.h"
20
21 #include "controllers/ControlToc.h"
22
23 #include <algorithm>
24
25 using std::endl;
26
27 using std::pair;
28 using std::vector;
29 using std::string;
30
31 namespace lyx {
32 namespace frontend {
33
34
35 QToc::QToc(Dialog & parent)
36         : ControlToc(parent)
37 {
38         update();
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->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 }
134
135
136 void QToc::updateType()
137 {
138
139         QStringList type_list;
140
141         vector<string> const & types = getTypes();
142         if (types.empty()) {
143                 type_model_.setStringList(type_list);
144                 toc_models_.clear();
145                 lyxerr[Debug::GUI] << "QToc::updateType(): no types available " << endl;
146                 return;
147         }
148
149         string selected_type ;
150         if(params()["type"].empty()) //Then plain toc...
151                 selected_type = params().getCmdName();
152         else
153                 selected_type = to_ascii(params()["type"]);
154
155         QString gui_names_;
156         for (size_t i = 0; i != types.size(); ++i) {
157                 string const & type_str = types[i];
158                 type_list.append(toqstr(getGuiName(type_str)));
159                 if (type_str == selected_type)
160                         type_ = i;
161                 
162                 lyxerr[Debug::GUI]
163                         << "QToc: new type " << type_str
164                         << "\ttoc_models_.size() " << toc_models_.size()
165                         << endl;
166
167         }
168         type_model_.setStringList(type_list);
169 }
170
171
172 void QToc::updateToc()
173 {
174         toc_models_.clear();
175         vector<string> const & types = getTypes();
176
177         for (size_t i = 0; i != types.size(); ++i) {
178
179                 toc_models_.push_back(new TocModel(getContents(types[i])));
180         }
181         
182 }
183
184
185 } // namespace frontend
186 } // namespace lyx