25 #include <QDomElement> 27 #include <QStringList> 38 static void warning(
const QString& message)
40 qWarning(
"%s", message.toLatin1().constData());
44 static qreal
qrealFromDom(
const QDomElement& e,
const QString& attribute, qreal defValue)
46 qreal value = defValue;
47 if (e.hasAttribute(attribute)) {
48 const QString s = e.attribute(attribute);
50 value = s.toDouble(&ok);
52 warning(QString(
"'%1' is not a valid qreal 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 int intFromDom(
const QDomElement& e,
const QString& attribute,
int defValue)
75 if (e.hasAttribute(attribute))
77 const QString s = e.attribute(attribute);
81 warning(QString(
"'%1' is not a valid integer syntax for attribute \"%2\" in initialization of \"%3\". Setting value to %4.")
82 .arg(s).arg(attribute).arg(e.tagName()).arg(QString::number(defValue)));
86 warning(QString(
"\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
87 .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
93 static unsigned int uintFromDom(
const QDomElement& e,
const QString& attribute,
unsigned int defValue)
95 unsigned int value = defValue;
96 if (e.hasAttribute(attribute))
98 const QString s = e.attribute(attribute);
100 value = s.toUInt(&ok);
102 warning(QString(
"'%1' is not a valid unsigned integer syntax for attribute \"%2\" in initialization of \"%3\". Setting value to %4.")
103 .arg(s).arg(attribute).arg(e.tagName()).arg(QString::number(defValue)));
107 warning(QString(
"\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
108 .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
114 static bool boolFromDom(
const QDomElement& e,
const QString& attribute,
bool defValue)
116 bool value = defValue;
117 if (e.hasAttribute(attribute))
119 const QString s = e.attribute(attribute);
120 if (s.toLower() == QString(
"true"))
122 else if (s.toLower() == QString(
"false"))
126 warning(QString(
"'%1' is not a valid boolean syntax for attribute \"%2\" in initialization of \"%3\". Setting value to %4.")
127 .arg(s).arg(attribute).arg(e.tagName()).arg(defValue?
"true":
"false"));
130 warning(QString(
"\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
131 .arg(attribute).arg(e.tagName()).arg(value?
"true":
"false"));
138 element.setAttribute(attribute, (value ?
"true" :
"false"));
141 static QDomElement
QColorDomElement(
const QColor& color,
const QString& name, QDomDocument& doc)
143 QDomElement de = doc.createElement(name);
144 de.setAttribute(
"red", QString::number(color.red()));
145 de.setAttribute(
"green", QString::number(color.green()));
146 de.setAttribute(
"blue", QString::number(color.blue()));
153 QStringList attribute;
154 attribute <<
"red" <<
"green" <<
"blue";
155 for (
int i=0; i<attribute.count(); ++i)
157 return QColor(color[0], color[1], color[2]);
static QDomElement QColorDomElement(const QColor &color, const QString &name, QDomDocument &doc)
static void warning(const QString &message)
static void setBoolAttribute(QDomElement &element, const QString &attribute, bool value)
static unsigned int uintFromDom(const QDomElement &e, const QString &attribute, unsigned int defValue)
static qreal qrealFromDom(const QDomElement &e, const QString &attribute, qreal defValue)
static QColor QColorFromDom(const QDomElement &e)
static bool boolFromDom(const QDomElement &e, const QString &attribute, bool defValue)
static int intFromDom(const QDomElement &e, const QString &attribute, int defValue)