Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 #include "config.h"
00024 
00025 #if QT_VERSION >= 0x040000
00026 # include <QDomElement>
00027 # include <QString>
00028 # include <QStringList>
00029 # include <QColor>
00030 #else
00031 # include <qapplication.h>
00032 # include <qdom.h>
00033 # include <qstring.h>
00034 # include <qstringlist.h>
00035 # include <qcolor.h>
00036 #endif
00037 
00038 #include <math.h>
00039 
00040 #ifndef DOXYGEN
00041 
00042 
00043 class DomUtils
00044 {
00045 private:
00046   static void warning(const QString& message)
00047   {
00048 #if QT_VERSION >= 0x040000
00049     qWarning("%s", message.toLatin1().constData());
00050 #else
00051     qWarning("%s", message.latin1());
00052 #endif
00053   }
00054 
00055 public:
00056   static float floatFromDom(const QDomElement& e, const QString& attribute, float defValue)
00057   {
00058     float value = defValue;
00059     if (e.hasAttribute(attribute)) {
00060         const QString s = e.attribute(attribute);
00061         bool ok;
00062         value = s.toFloat(&ok);
00063         if (!ok) {
00064           warning("Bad float syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
00065           value = defValue;
00066         }
00067     } else
00068       warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
00069 
00070 #if defined(isnan)
00071     
00072     
00073     if (isnan(value))
00074       warning("Warning, attribute \""+attribute+"\" initialized to Not a Number in \""+e.tagName()+"\"");
00075 #endif
00076 
00077     return value;
00078   }
00079 
00080   static double doubleFromDom(const QDomElement& e, const QString& attribute, double defValue)
00081   {
00082     double value = defValue;
00083     if (e.hasAttribute(attribute)) {
00084       const QString s = e.attribute(attribute);
00085       bool ok;
00086       value = s.toDouble(&ok);
00087       if (!ok) {
00088         warning("Bad double syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
00089         value = defValue;
00090       }
00091     } else
00092       warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
00093 
00094 #if defined(isnan)
00095     
00096     
00097     if (isnan(value))
00098       warning("Warning, attribute \""+attribute+"\" initialized to Not a Number in \""+e.tagName()+"\"");
00099 #endif
00100 
00101     return value;
00102   }
00103 
00104   static int intFromDom(const QDomElement& e, const QString& attribute, int defValue)
00105   {
00106     int value = defValue;
00107     if (e.hasAttribute(attribute))
00108       {
00109     const QString s = e.attribute(attribute);
00110     bool ok;
00111     s.toInt(&ok);
00112     if (ok)
00113       value = s.toInt();
00114     else
00115       warning("Bad integer syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
00116       }
00117     else
00118       warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
00119     return value;
00120   }
00121 
00122   static bool boolFromDom(const QDomElement& e, const QString& attribute, bool defValue)
00123   {
00124     bool value = defValue;
00125     if (e.hasAttribute(attribute))
00126       {
00127     const QString s = e.attribute(attribute);
00128 #if QT_VERSION >= 0x040000
00129     if (s.toLower() == QString("true"))
00130 #else
00131     if (s.lower() == QString("true"))
00132 #endif
00133       value = true;
00134 #if QT_VERSION >= 0x040000
00135     else if (s.toLower() == QString("false"))
00136 #else
00137     else if (s.lower() == QString("false"))
00138 #endif
00139       value = false;
00140     else
00141       {
00142         warning("Bad boolean syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\" (should be \"true\" or \"false\").");
00143         warning("Setting value to "+(value?QString("true."):QString("false.")));
00144       }
00145       }
00146     else
00147       warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+(value?QString("true."):QString("false.")));
00148     return value;
00149   }
00150 
00151   static QDomElement QColorDomElement(const QColor& color, const QString& name, QDomDocument& doc)
00152   {
00153     QDomElement de = doc.createElement(name);
00154     de.setAttribute("red", QString::number(color.red()));
00155     de.setAttribute("green", QString::number(color.green()));
00156     de.setAttribute("blue", QString::number(color.blue()));
00157     return de;
00158   }
00159 
00160   static QColor QColorFromDom(const QDomElement& e)
00161   {
00162     int color[3];
00163     QStringList attribute;
00164     attribute << "red" << "green" << "blue";
00165 #if QT_VERSION >= 0x040000
00166     for (int i=0; i<attribute.count(); ++i)
00167 #else
00168     for (unsigned int i=0; i<attribute.count(); ++i)
00169 #endif
00170       color[i] = DomUtils::intFromDom(e, attribute[i], 0);
00171     return QColor(color[0], color[1], color[2]);
00172   }
00173 };
00174 
00175 #endif // DOXYGEN