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