25 #include <QDomElement>
27 #include <QStringList>
38 static void warning(
const QString& message)
40 qWarning(
"%s", message.toLatin1().constData());
44 static float floatFromDom(
const QDomElement& e,
const QString& attribute,
float defValue)
46 float value = defValue;
47 if (e.hasAttribute(attribute)) {
48 const QString s = e.attribute(attribute);
50 value = s.toFloat(&ok);
52 warning(QString(
"'%1' is not a valid float syntax for attribute \"%2\" in initialization of \"%3\". Setting value to %4.")
53 .arg(s).arg(attribute).arg(e.tagName()).arg(QString::number(defValue)));
57 warning(QString(
"\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
58 .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
65 warning(QString(
"Warning, attribute \"%1\" initialized to Not a Number in \"%2\"")
66 .arg(attribute).arg(e.tagName()));
72 static double doubleFromDom(
const QDomElement& e,
const QString& attribute,
double defValue)
74 double value = defValue;
75 if (e.hasAttribute(attribute)) {
76 const QString s = e.attribute(attribute);
78 value = s.toDouble(&ok);
80 warning(QString(
"'%1' is not a valid double syntax for attribute \"%2\" in initialization of \"%3\". Setting value to %4.")
81 .arg(s).arg(attribute).arg(e.tagName()).arg(QString::number(defValue)));
85 warning(QString(
"\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
86 .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
93 warning(QString(
"Warning, attribute \"%1\" initialized to Not a Number in \"%2\"")
94 .arg(attribute).arg(e.tagName()));
100 static int intFromDom(
const QDomElement& e,
const QString& attribute,
int defValue)
102 int value = defValue;
103 if (e.hasAttribute(attribute))
105 const QString s = e.attribute(attribute);
107 value = s.toInt(&ok);
109 warning(QString(
"'%1' is not a valid integer syntax for attribute \"%2\" in initialization of \"%3\". Setting value to %4.")
110 .arg(s).arg(attribute).arg(e.tagName()).arg(QString::number(defValue)));
114 warning(QString(
"\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
115 .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
121 static bool boolFromDom(
const QDomElement& e,
const QString& attribute,
bool defValue)
123 bool value = defValue;
124 if (e.hasAttribute(attribute))
126 const QString s = e.attribute(attribute);
127 if (s.toLower() == QString(
"true"))
129 else if (s.toLower() == QString(
"false"))
133 warning(QString(
"'%1' is not a valid boolean syntax for attribute \"%2\" in initialization of \"%3\". Setting value to %4.")
134 .arg(s).arg(attribute).arg(e.tagName()).arg(defValue?
"true":
"false"));
137 warning(QString(
"\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
138 .arg(attribute).arg(e.tagName()).arg(value?
"true":
"false"));
144 static void setBoolAttribute(QDomElement& element,
const QString& attribute,
bool value) {
145 element.setAttribute(attribute, (value ?
"true" :
"false"));
148 static QDomElement QColorDomElement(
const QColor& color,
const QString& name, QDomDocument& doc)
150 QDomElement de = doc.createElement(name);
151 de.setAttribute(
"red", QString::number(color.red()));
152 de.setAttribute(
"green", QString::number(color.green()));
153 de.setAttribute(
"blue", QString::number(color.blue()));
157 static QColor QColorFromDom(
const QDomElement& e)
160 QStringList attribute;
161 attribute <<
"red" <<
"green" <<
"blue";
162 for (
int i=0; i<attribute.count(); ++i)
163 color[i] = DomUtils::intFromDom(e, attribute[i], 0);
164 return QColor(color[0], color[1], color[2]);