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