1
//=============================================================================
3
// Audio Utility Library
6
// Copyright (C) 2002-2006 by Werner Schweer and others
8
// This program is free software; you can redistribute it and/or modify
9
// it under the terms of the GNU General Public License version 2.
11
// This program is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
// GNU General Public License for more details.
16
// You should have received a copy of the GNU General Public License
17
// along with this program; if not, write to the Free Software
18
// Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
//=============================================================================
24
#include <QDomElement>
25
#include <QMetaProperty>
30
//---------------------------------------------------------
32
//---------------------------------------------------------
39
Xml::Xml(QIODevice* device)
46
//---------------------------------------------------------
48
//---------------------------------------------------------
52
for (int i = 0; i < level*2; ++i)
56
//---------------------------------------------------------
58
//---------------------------------------------------------
62
*this << "<?xml version=\"1.0\" encoding=\"utf8\"?>" << endl;
65
//---------------------------------------------------------
67
//---------------------------------------------------------
69
void Xml::put(const QString& s)
72
*this << xmlString(s) << endl;
75
//---------------------------------------------------------
77
// <mops attribute="value">
78
//---------------------------------------------------------
80
void Xml::stag(const QString& s)
83
*this << '<' << s << '>' << endl;
87
//---------------------------------------------------------
89
//---------------------------------------------------------
91
void Xml::etag(const char* s)
94
*this << "</" << s << '>' << endl;
98
//---------------------------------------------------------
100
// <mops attribute="value"/>
101
//---------------------------------------------------------
103
void Xml::tagE(const QString& s)
106
*this << '<' << s << "/>" << endl;
109
void Xml::tag(const char* name, int val)
112
*this << '<' << name << '>' << val << "</" << name << '>' << endl;
115
void Xml::tag(const char* name, unsigned val)
118
*this << '<' << name << '>' << val << "</" << name << '>' << endl;
121
void Xml::tag(const char* name, float val)
124
*this << '<' << name << '>' << val << "</" << name << '>' << endl;
127
void Xml::tag(const char* name, const double& val)
130
*this << '<' << name << '>' << val << "</" << name << '>' << endl;
133
void Xml::tag(const char* name, const QString& val)
136
*this << "<" << name << ">" << xmlString(val) << "</" << name << '>' << endl;
139
void Xml::tag(const char* name, const QColor& color)
142
*this << QString("<%1 r=\"%2\" g=\"%3\" b=\"%4\"/>")
143
.arg(name).arg(color.red()).arg(color.green()).arg(color.blue()) << endl;
146
void Xml::tag(const char* name, const QWidget* g)
148
tag(name, QRect(g->pos(), g->size()));
151
void Xml::tag(const char* name, const QRect& r)
154
*this << "<" << name;
155
*this << QString(" x=\"%1\" y=\"%2\" w=\"%3\" h=\"%4\"/>")
156
.arg(r.x()).arg(r.y()).arg(r.width()).arg(r.height()) << endl;
159
//---------------------------------------------------------
161
//---------------------------------------------------------
163
QString Xml::xmlString(const QString& ss)
166
s.replace('&', "&");
167
s.replace('<', "<");
168
s.replace('>', ">");
169
s.replace('\'', "'");
170
s.replace('"', """);
174
//---------------------------------------------------------
176
//---------------------------------------------------------
178
QRect readGeometry(QDomNode node)
180
QDomElement e = node.toElement();
181
int x = e.attribute("x","0").toInt();
182
int y = e.attribute("y","0").toInt();
183
int w = e.attribute("w","50").toInt();
184
int h = e.attribute("h","50").toInt();
185
return QRect(x, y, w, h);
188
//---------------------------------------------------------
190
//---------------------------------------------------------
192
void Xml::writeProperties(const QObject* o)
194
const QMetaObject* meta = o->metaObject();
197
// start from dummy "muse" property, assuming this is the
198
// first muse propertie in widget hierarchy
200
int from = meta->indexOfProperty("muse") + 1;
201
int n = meta->propertyCount();
202
for (int i = from; i < n; ++i) {
203
QMetaProperty p = meta->property(i);
204
if (!p.isScriptable())
206
const char* name = p.name();
207
QVariant v = p.read(o);
211
tag(name, v.toInt());
213
case QVariant::Double:
214
tag(name, v.toDouble());
216
case QVariant::String:
217
tag(name, v.toString());
220
tag(name, v.toRect());
222
case QVariant::Point:
224
QPoint p = v.toPoint();
226
*this << "<" << name << QString(" x=\"%1\" y=\"%2\" />")
227
.arg(p.x()).arg(p.y()) << endl;
232
printf("MusE:%s type %d not implemented\n",
233
meta->className(), v.type());
239
//---------------------------------------------------------
241
//---------------------------------------------------------
243
void readProperties(QObject* o, QDomNode node)
245
const QMetaObject* meta = o->metaObject();
247
QDomElement e = node.toElement();
248
QString tag(e.tagName());
249
int idx = meta->indexOfProperty(tag.toLatin1().constData());
251
printf("MusE:%s: unknown tag %s\n",
252
meta->className(), tag.toLatin1().constData());
255
QMetaProperty p = meta->property(idx);
260
v.setValue(e.text().toInt());
262
case QVariant::Double:
263
v.setValue(e.text().toDouble());
265
case QVariant::String:
266
v.setValue(e.text());
269
v.setValue(AL::readGeometry(node));
271
case QVariant::Point:
273
int x = e.attribute("x","0").toInt();
274
int y = e.attribute("y","0").toInt();
275
v.setValue(QPoint(x, y));
279
printf("MusE:%s type %d not implemented\n",
280
meta->className(), p.type());
287
//---------------------------------------------------------
289
//---------------------------------------------------------
291
void Xml::dump(int len, const unsigned char* p)
296
setNumberFlags(numberFlags() | QTextStream::ShowBase);
298
for (int i = 0; i < len; ++i, ++col) {
306
*this << (p[i] & 0xff);
309
*this << endl << dec;
314
//---------------------------------------------------------
316
//---------------------------------------------------------
318
void domError(QDomNode node)
320
QDomElement e = node.toElement();
321
QString tag(e.tagName());
324
while (!dn.parentNode().isNull()) {
325
dn = dn.parentNode();
326
const QDomElement e = dn.toElement();
327
const QString k(e.tagName());
332
fprintf(stderr, "%s: Unknown Node <%s>, type %d\n",
333
s.toLatin1().constData(), tag.toLatin1().constData(), node.nodeType());
335
fprintf(stderr, " text node <%s>\n", node.toText().data().toLatin1().constData());
339
//---------------------------------------------------------
341
//---------------------------------------------------------
343
void domNotImplemented(QDomNode node)
347
QDomElement e = node.toElement();
348
QString tag(e.tagName());
351
while (!dn.parentNode().isNull()) {
352
dn = dn.parentNode();
353
const QDomElement e = dn.toElement();
354
const QString k(e.tagName());
359
fprintf(stderr, "%s: Node not implemented: <%s>, type %d\n",
360
s.toLatin1().constData(), tag.toLatin1().constData(), node.nodeType());
362
fprintf(stderr, " text node <%s>\n", node.toText().data().toLatin1().constData());