1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
#include <QtDebug>
#include <QStringList>
#include <QScriptValue>
#include "skin/skincontext.h"
#include "skin/svgparser.h"
SkinContext::SkinContext() {
}
SkinContext::SkinContext(const SkinContext& parent)
: m_variables(parent.variables()),
m_skinBasePath(parent.m_skinBasePath) {
QScriptValue context = m_scriptEngine.currentContext()->activationObject();
for (QHash<QString, QString>::const_iterator it = m_variables.begin();
it != m_variables.end(); ++it) {
context.setProperty(it.key(), it.value());
}
}
SkinContext::~SkinContext() {
}
SkinContext& SkinContext::operator=(const SkinContext& other) {
m_variables = other.variables();
QScriptValue context = m_scriptEngine.currentContext()->activationObject();
for (QHash<QString, QString>::const_iterator it = m_variables.begin();
it != m_variables.end(); ++it) {
context.setProperty(it.key(), it.value());
}
return *this;
}
QString SkinContext::variable(const QString& name) const {
return m_variables.value(name, QString());
}
void SkinContext::setVariable(const QString& name, const QString& value) {
m_variables[name] = value;
QScriptValue context = m_scriptEngine.currentContext()->activationObject();
context.setProperty(name, value);
}
void SkinContext::updateVariables(const QDomNode& node) {
QDomNode child = node.firstChild();
while (!child.isNull()) {
if (child.isElement() && child.nodeName() == "SetVariable") {
updateVariable(child.toElement());
}
child = child.nextSibling();
}
}
void SkinContext::updateVariable(const QDomElement& element) {
if (!element.hasAttribute("name")) {
qDebug() << "Can't update variable without name:" << element.text();
return;
}
QString name = element.attribute("name");
QString value = variableNodeToText(element);
setVariable(name, value);
}
bool SkinContext::hasNode(const QDomNode& node, const QString& nodeName) const {
return !selectNode(node, nodeName).isNull();
}
QDomNode SkinContext::selectNode(const QDomNode& node,
const QString& nodeName) const {
QDomNode child = node.firstChild();
while (!child.isNull()) {
if (child.nodeName() == nodeName) {
return child;
}
child = child.nextSibling();
}
return child;
}
QDomElement SkinContext::selectElement(const QDomNode& node,
const QString& nodeName) const {
QDomNode child = selectNode(node, nodeName);
return child.toElement();
}
QString SkinContext::selectString(const QDomNode& node,
const QString& nodeName) const {
QDomElement child = selectElement(node, nodeName);
return nodeToString(child);
}
float SkinContext::selectFloat(const QDomNode& node,
const QString& nodeName) const {
bool ok = false;
float conv = nodeToString(selectElement(node, nodeName)).toFloat(&ok);
return ok ? conv : 0.0f;
}
double SkinContext::selectDouble(const QDomNode& node,
const QString& nodeName) const {
bool ok = false;
double conv = nodeToString(selectElement(node, nodeName)).toDouble(&ok);
return ok ? conv : 0.0;
}
int SkinContext::selectInt(const QDomNode& node,
const QString& nodeName,
bool* pOk) const {
bool ok = false;
int conv = nodeToString(selectElement(node, nodeName)).toInt(&ok);
if (pOk != NULL) {
*pOk = ok;
}
return ok ? conv : 0;
}
bool SkinContext::selectBool(const QDomNode& node,
const QString& nodeName,
bool defaultValue) const {
QDomNode child = selectNode(node, nodeName);
if (!child.isNull()) {
QString stringValue = nodeToString(child);
return stringValue.contains("true", Qt::CaseInsensitive);
}
return defaultValue;
}
bool SkinContext::hasNodeSelectString(const QDomNode& node,
const QString& nodeName, QString *value) const {
QDomNode child = selectNode(node, nodeName);
if (!child.isNull()) {
*value = nodeToString(child);
return true;
}
return false;
}
bool SkinContext::hasNodeSelectBool(const QDomNode& node,
const QString& nodeName, bool *value) const {
QDomNode child = selectNode(node, nodeName);
if (!child.isNull()) {
QString stringValue = nodeToString(child);
*value = stringValue.contains("true", Qt::CaseInsensitive);
return true;
}
return false;
}
bool SkinContext::selectAttributeBool(const QDomElement& element,
const QString& attributeName,
bool defaultValue) const {
if (element.hasAttribute(attributeName)) {
QString stringValue = element.attribute(attributeName);
return stringValue.contains("true", Qt::CaseInsensitive);
}
return defaultValue;
}
QString SkinContext::selectAttributeString(const QDomElement& element,
const QString& attributeName,
QString defaultValue) const {
if (element.hasAttribute(attributeName)) {
QString stringValue = element.attribute(attributeName);
return stringValue == "" ? defaultValue :stringValue;
}
return defaultValue;
}
QString SkinContext::variableNodeToText(const QDomElement& variableNode) const {
if (variableNode.hasAttribute("expression")) {
QScriptValue result = m_scriptEngine.evaluate(
variableNode.attribute("expression"));
return result.toString();
} else if (variableNode.hasAttribute("name")) {
QString variableName = variableNode.attribute("name");
if (variableNode.hasAttribute("format")) {
QString formatString = variableNode.attribute("format");
return formatString.arg(variable(variableName));
} else if (variableNode.nodeName() == "SetVariable") {
// If we are setting the variable name and we didn't get a format
// string then return the node text. Use nodeToString to translate
// embedded variable references.
return nodeToString(variableNode);
} else {
return variable(variableName);
}
}
return nodeToString(variableNode);
}
QString SkinContext::nodeToString(const QDomNode& node) const {
QStringList result;
QDomNode child = node.firstChild();
while (!child.isNull()) {
if (child.isElement()) {
if (child.nodeName() == "Variable") {
result.append(variableNodeToText(child.toElement()));
} else {
qDebug() << "Unhandled tag in node:" << child.nodeName();
}
} else if (child.isText()) {
result.append(child.nodeValue());
}
// Ignore all other node types.
child = child.nextSibling();
}
return result.join("");
}
QString SkinContext::getPixmapPath(const QDomNode& pixmapNode) cons
|