1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the tools applications of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
31
#include <qsizepolicy.h>
34
#include <qdatetime.h>
39
#include <qbytearray.h>
43
\class DomTool domtool.h
44
\brief The DomTool class provides static functions for Qt Designer
47
A collection of static functions used by Resource (part of the
53
Returns the contents of property \a name of object \a e as
54
a variant or the variant passed as \a defValue if the property does
55
not exist. The \a comment is passed on to the elementToVariant()
60
QVariant DomTool::readProperty(const QDomElement& e, const QString& name, const QVariant& defValue, QString& comment)
63
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
64
if (n.tagName() == QLatin1String("property")) {
65
if (n.attribute(QLatin1String("name")) != name)
67
return elementToVariant(n.firstChild().toElement(), defValue, comment);
77
QVariant DomTool::readProperty(const QDomElement& e, const QString& name, const QVariant& defValue)
80
return readProperty(e, name, defValue, comment);
84
Returns wheter object \a e defines property \a name or not.
88
bool DomTool::hasProperty(const QDomElement& e, const QString& name)
91
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
92
if (n.tagName() == QLatin1String("property")) {
93
if (n.attribute(QLatin1String("name")) != name)
102
Returns a list of the names of the properties of the given \a type
103
found in the element \a e.
105
QStringList DomTool::propertiesOfType(const QDomElement& e, const QString& type)
109
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
110
if (n.tagName() == QLatin1String("property")) {
111
QDomElement n2 = n.firstChild().toElement();
112
if (n2.tagName() == type)
113
result += n.attribute(QLatin1String("name"));
123
QVariant DomTool::elementToVariant(const QDomElement& e, const QVariant& defValue)
126
return elementToVariant(e, defValue, dummy);
130
Interprets element \a e as a variant and returns the result of the
131
interpretation, extracting the data as a text element is the \a
132
comment matches the tag name. If the interpretation fails the \a
133
defValue is returned instead.
135
QVariant DomTool::elementToVariant(const QDomElement& e, const QVariant& defValue, QString &comment)
142
if (e.tagName() == QLatin1String("rect")) {
143
QDomElement n3 = e.firstChild().toElement();
144
int x = 0, y = 0, w = 0, h = 0;
145
while (!n3.isNull()) {
146
if (n3.tagName() == QLatin1String("x"))
147
x = n3.firstChild().toText().data().toInt();
148
else if (n3.tagName() == QLatin1String("y"))
149
y = n3.firstChild().toText().data().toInt();
150
else if (n3.tagName() == QLatin1String("width"))
151
w = n3.firstChild().toText().data().toInt();
152
else if (n3.tagName() == QLatin1String("height"))
153
h = n3.firstChild().toText().data().toInt();
154
n3 = n3.nextSibling().toElement();
156
var.createRect(x, y, w, h);
157
qVariantSetValue(v, var);
158
} else if (e.tagName() == QLatin1String("point")) {
159
QDomElement n3 = e.firstChild().toElement();
161
while (!n3.isNull()) {
162
if (n3.tagName() == QLatin1String("x"))
163
x = n3.firstChild().toText().data().toInt();
164
else if (n3.tagName() == QLatin1String("y"))
165
y = n3.firstChild().toText().data().toInt();
166
n3 = n3.nextSibling().toElement();
168
var.createPoint(x,y);
169
qVariantSetValue(v, var);
170
} else if (e.tagName() == QLatin1String("size")) {
171
QDomElement n3 = e.firstChild().toElement();
173
while (!n3.isNull()) {
174
if (n3.tagName() == QLatin1String("width"))
175
w = n3.firstChild().toText().data().toInt();
176
else if (n3.tagName() == QLatin1String("height"))
177
h = n3.firstChild().toText().data().toInt();
178
n3 = n3.nextSibling().toElement();
180
var.createSize(w, h);
181
qVariantSetValue(v, var);
182
} else if (e.tagName() == QLatin1String("color")) {
183
var.color = readColor(e);
184
qVariantSetValue(v, var);
185
} else if (e.tagName() == QLatin1String("font")) {
186
QDomElement n3 = e.firstChild().toElement();
189
while (!n3.isNull()) {
190
if (n3.tagName() == QLatin1String("family"))
191
f.family = qstrdup(n3.firstChild().toText().data());
192
else if (n3.tagName() == QLatin1String("pointsize"))
193
f.pointsize = n3.firstChild().toText().data().toInt();
194
else if (n3.tagName() == QLatin1String("bold"))
195
f.bold = n3.firstChild().toText().data().toInt();
196
else if (n3.tagName() == QLatin1String("italic"))
197
f.italic = n3.firstChild().toText().data().toInt();
198
else if (n3.tagName() == QLatin1String("underline"))
199
f.underline = n3.firstChild().toText().data().toInt();
200
else if (n3.tagName() == QLatin1String("strikeout"))
201
f.strikeout = n3.firstChild().toText().data().toInt();
202
n3 = n3.nextSibling().toElement();
205
qVariantSetValue(v, var);
206
} else if (e.tagName() == QLatin1String("string")) {
207
v = QVariant(e.firstChild().toText().data());
209
n = n.nextSibling().toElement();
210
if (n.tagName() == QLatin1String("comment"))
211
comment = n.firstChild().toText().data();
212
} else if (e.tagName() == QLatin1String("cstring")) {
213
v = QVariant(e.firstChild().toText().data().toAscii());
214
} else if (e.tagName() == QLatin1String("number")) {
216
v = QVariant(e.firstChild().toText().data().toInt(&ok));
218
v = QVariant(e.firstChild().toText().data().toDouble());
219
} else if (e.tagName() == QLatin1String("bool")) {
220
QString t = e.firstChild().toText().data();
221
v = QVariant(t == QLatin1String("true") || t == QLatin1String("1"));
222
} else if (e.tagName() == QLatin1String("pixmap")) {
223
v = QVariant(e.firstChild().toText().data());
224
} else if (e.tagName() == QLatin1String("iconset")) {
225
v = QVariant(e.firstChild().toText().data());
226
} else if (e.tagName() == QLatin1String("image")) {
227
v = QVariant(e.firstChild().toText().data());
228
} else if (e.tagName() == QLatin1String("enum")) {
229
v = QVariant(e.firstChild().toText().data());
230
} else if (e.tagName() == QLatin1String("set")) {
231
v = QVariant(e.firstChild().toText().data());
232
} else if (e.tagName() == QLatin1String("sizepolicy")) {
233
QDomElement n3 = e.firstChild().toElement();
234
var.createSizePolicy();
235
while (!n3.isNull()) {
236
if (n3.tagName() == QLatin1String("hsizetype"))
237
var.sizePolicy.hsizetype = n3.firstChild().toText().data().toInt();
238
else if (n3.tagName() == QLatin1String("vsizetype"))
239
var.sizePolicy.vsizetype = n3.firstChild().toText().data().toInt();
240
else if (n3.tagName() == QLatin1String("horstretch"))
241
var.sizePolicy.horstretch = n3.firstChild().toText().data().toInt();
242
else if (n3.tagName() == QLatin1String("verstretch"))
243
var.sizePolicy.verstretch = n3.firstChild().toText().data().toInt();
244
n3 = n3.nextSibling().toElement();
246
qVariantSetValue(v, var);
247
} else if (e.tagName() == QLatin1String("cursor")) {
248
var.createCursor(e.firstChild().toText().data().toInt());
249
qVariantSetValue(v, var);
250
} else if (e.tagName() == QLatin1String("stringlist")) {
253
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement())
254
lst << n.firstChild().toText().data();
256
} else if (e.tagName() == QLatin1String("date")) {
257
QDomElement n3 = e.firstChild().toElement();
260
while (!n3.isNull()) {
261
if (n3.tagName() == QLatin1String("year"))
262
y = n3.firstChild().toText().data().toInt();
263
else if (n3.tagName() == QLatin1String("month"))
264
m = n3.firstChild().toText().data().toInt();
265
else if (n3.tagName() == QLatin1String("day"))
266
d = n3.firstChild().toText().data().toInt();
267
n3 = n3.nextSibling().toElement();
269
v = QVariant(QDate(y, m, d));
270
} else if (e.tagName() == QLatin1String("time")) {
271
QDomElement n3 = e.firstChild().toElement();
274
while (!n3.isNull()) {
275
if (n3.tagName() == QLatin1String("hour"))
276
h = n3.firstChild().toText().data().toInt();
277
else if (n3.tagName() == QLatin1String("minute"))
278
m = n3.firstChild().toText().data().toInt();
279
else if (n3.tagName() == QLatin1String("second"))
280
s = n3.firstChild().toText().data().toInt();
281
n3 = n3.nextSibling().toElement();
283
v = QVariant(QTime(h, m, s));
284
} else if (e.tagName() == QLatin1String("datetime")) {
285
QDomElement n3 = e.firstChild().toElement();
286
int h, mi, s, y, mo, d ;
287
h = mi = s = y = mo = d = 0;
288
while (!n3.isNull()) {
289
if (n3.tagName() == QLatin1String("hour"))
290
h = n3.firstChild().toText().data().toInt();
291
else if (n3.tagName() == QLatin1String("minute"))
292
mi = n3.firstChild().toText().data().toInt();
293
else if (n3.tagName() == QLatin1String("second"))
294
s = n3.firstChild().toText().data().toInt();
295
else if (n3.tagName() == QLatin1String("year"))
296
y = n3.firstChild().toText().data().toInt();
297
else if (n3.tagName() == QLatin1String("month"))
298
mo = n3.firstChild().toText().data().toInt();
299
else if (n3.tagName() == QLatin1String("day"))
300
d = n3.firstChild().toText().data().toInt();
301
n3 = n3.nextSibling().toElement();
303
v = QVariant(QDateTime(QDate(y, mo, d), QTime(h, mi, s)));
310
/* Returns the color which is returned in the dom element \a e.
313
Color DomTool::readColor(const QDomElement &e)
315
QDomElement n = e.firstChild().toElement();
316
int r= 0, g = 0, b = 0;
317
while (!n.isNull()) {
318
if (n.tagName() == QLatin1String("red"))
319
r = n.firstChild().toText().data().toInt();
320
else if (n.tagName() == QLatin1String("green"))
321
g = n.firstChild().toText().data().toInt();
322
else if (n.tagName() == QLatin1String("blue"))
323
b = n.firstChild().toText().data().toInt();
324
n = n.nextSibling().toElement();
333
Returns the contents of attribute \a name of object \a e as
334
a variant or the variant passed as \a defValue if the attribute does
335
not exist. The \a comment is passed to the elementToVariant()
340
QVariant DomTool::readAttribute(const QDomElement& e, const QString& name, const QVariant& defValue, QString& comment)
343
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
344
if (n.tagName() == QLatin1String("attribute")) {
345
if (n.attribute(QLatin1String("name")) != name)
347
return elementToVariant(n.firstChild().toElement(), defValue, comment);
356
QVariant DomTool::readAttribute(const QDomElement& e, const QString& name, const QVariant& defValue)
359
return readAttribute(e, name, defValue, comment);
363
Returns wheter object \a e defines attribute \a name or not.
367
bool DomTool::hasAttribute(const QDomElement& e, const QString& name)
370
for (n = e.firstChild().toElement(); !n.isNull(); n = n.nextSibling().toElement()) {
371
if (n.tagName() == QLatin1String("attribute")) {
372
if (n.attribute(QLatin1String("name")) != name)
380
static bool toBool(const QString& s)
382
return s == QLatin1String("true") || s.toInt() != 0;
388
Convert Qt 2.x format to Qt 3.x format if necessary.
390
void DomTool::fixDocument(QDomDocument& doc)
397
e = doc.firstChild().toElement();
398
if (e.tagName() != QLatin1String("UI"))
401
// rename classes and properties
402
double version = e.hasAttribute(QLatin1String("version"))
403
? e.attribute(QLatin1String("version")).toDouble() : 0.0;
406
fixAttributes(nl, version);
408
// 3.x don't do anything more
409
if (e.hasAttribute(QLatin1String("version"))
410
&& e.attribute(QLatin1String("version")).toDouble() >= 3.0)
413
// in versions smaller than 3.0 we need to change more
415
e.setAttribute(QLatin1String("version"), 3.0);
416
e.setAttribute(QLatin1String("stdsetdef"), 1);
417
nl = e.elementsByTagName(QLatin1String("property"));
418
for (i = 0; i < (int) nl.length(); i++) {
419
e = nl.item(i).toElement();
421
QDomElement n2 = e.firstChild().toElement();
422
if (n2.tagName() == QLatin1String("name")) {
423
name = n2.firstChild().toText().data();
424
if (name == QLatin1String("resizeable"))
425
e.setAttribute(QLatin1String("name"), QLatin1String("resizable"));
427
e.setAttribute(QLatin1String("name"), name);
430
bool stdset = toBool(e.attribute(QLatin1String("stdset")));
431
if (stdset || name == QLatin1String("toolTip") || name == QLatin1String("whatsThis") ||
432
name == QLatin1String("buddy") ||
433
e.parentNode().toElement().tagName() == QLatin1String("item") ||
434
e.parentNode().toElement().tagName() == QLatin1String("spacer") ||
435
e.parentNode().toElement().tagName() == QLatin1String("column")
437
e.removeAttribute(QLatin1String("stdset"));
439
e.setAttribute(QLatin1String("stdset"), 0);
442
nl = doc.elementsByTagName(QLatin1String("attribute"));
443
for (i = 0; i < (int) nl.length(); i++) {
444
e = nl.item(i).toElement();
446
QDomElement n2 = e.firstChild().toElement();
447
if (n2.tagName() == QLatin1String("name")) {
448
name = n2.firstChild().toText().data();
449
e.setAttribute(QLatin1String("name"), name);
454
nl = doc.elementsByTagName(QLatin1String("image"));
455
for (i = 0; i < (int) nl.length(); i++) {
456
e = nl.item(i).toElement();
458
QDomElement n2 = e.firstChild().toElement();
459
if (n2.tagName() == QLatin1String("name")) {
460
name = n2.firstChild().toText().data();
461
e.setAttribute(QLatin1String("name"), name);
466
nl = doc.elementsByTagName(QLatin1String("widget"));
467
for (i = 0; i < (int) nl.length(); i++) {
468
e = nl.item(i).toElement();
470
QDomElement n2 = e.firstChild().toElement();
471
if (n2.tagName() == QLatin1String("class")) {
472
name = n2.firstChild().toText().data();
473
e.setAttribute(QLatin1String("class"), name);
481
widgetName(double v, QString b, QString a)
482
: version(v), before(b), after(a) {}
488
struct propertyName : public widgetName {
489
propertyName(double v, QString b, QString a, QString c = QString())
490
: widgetName(v, b, a), clss(c) {}
495
widgetName widgetTable[1] = {
496
widgetName(3.3, QLatin1String("before"), QLatin1String("after")),
500
propertyName propertyTable[1] = {
501
propertyName(3.0, QLatin1String("resizeable"), QLatin1String("resizable")), // we need to fix a spelling error in 3.0
507
void DomTool::fixAttributes(QDomNodeList &nodes, double version)
511
for (int i = 0; i < (int) nodes.count(); ++i) {
513
fixAttribute(n, version);
515
fixAttributes(nl, version);
522
void DomTool::fixAttribute(QDomNode &node, double version)
524
QString tagName = node.toElement().tagName();
525
if (tagName == QLatin1String("widget")) {
526
QString clss = node.toElement().attribute(QLatin1String("class"));
527
for (int i = 0; i < widgs; ++i)
528
if ((version < widgetTable[i].version)
529
&& (clss == widgetTable[i].before)) {
530
node.toElement().setAttribute(QLatin1String("class"), propertyTable[i].after);
535
if (tagName == QLatin1String("property")) {
536
QDomElement e = node.parentNode().toElement();
537
QString clss = e.attribute(QLatin1String("class"));
538
QString name = node.toElement().attribute(QLatin1String("name"), QLatin1String(""));
539
for (int i = 0; i < props; ++i)
540
if ((version < propertyTable[i].version)
541
&& (clss == propertyTable[i].clss)
542
&& (propertyTable[i].before.isNull()
543
|| name == propertyTable[i].before)) {
544
node.toElement().setAttribute(QLatin1String("name"), propertyTable[i].after);