domUtils.h
1 /****************************************************************************
2 
3  Copyright (C) 2002-2008 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.3.10.
6 
7  http://www.libqglviewer.com - contact@libqglviewer.com
8 
9  This file may be used under the terms of the GNU General Public License
10  versions 2.0 or 3.0 as published by the Free Software Foundation and
11  appearing in the LICENSE file included in the packaging of this file.
12  In addition, as a special exception, Gilles Debunne gives you certain
13  additional rights, described in the file GPL_EXCEPTION in this package.
14 
15  libQGLViewer uses dual licensing. Commercial/proprietary software must
16  purchase a libQGLViewer Commercial License.
17 
18  This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
19  WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 
21 *****************************************************************************/
22 
23 
24 #if QT_VERSION > 0x040000
25 # include <QGlobal>
26 # include <QDomElement>
27 # include <QString>
28 # include <QStringList>
29 # include <QColor>
30 #else
31 # include <qapplication.h>
32 # include <qdom.h>
33 # include <qstring.h>
34 # include <qstringlist.h>
35 # include <qcolor.h>
36 #endif
37 
38 #include <math.h>
39 
40 #ifndef DOXYGEN
41 
42 // QDomElement loading with syntax checking.
43 class DomUtils
44 {
45 private:
46  static void warning(const QString& message)
47  {
48 #if QT_VERSION >= 0x040000
49  qWarning("%s", message.toLatin1().constData());
50 #else
51  qWarning("%s", message.latin1());
52 #endif
53  }
54 
55 public:
56  static float floatFromDom(const QDomElement& e, const QString& attribute, float defValue)
57  {
58  float value = defValue;
59  if (e.hasAttribute(attribute)) {
60  const QString s = e.attribute(attribute);
61  bool ok;
62  value = s.toFloat(&ok);
63  if (!ok) {
64  warning("Bad float syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
65  value = defValue;
66  }
67  } else
68  warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
69 
70 #if defined(isnan)
71  // The "isnan" method may not be available on all platforms.
72  // Find its equivalent or simply remove these two lines
73  if (isnan(value))
74  warning("Warning, attribute \""+attribute+"\" initialized to Not a Number in \""+e.tagName()+"\"");
75 #endif
76 
77  return value;
78  }
79 
80  static double doubleFromDom(const QDomElement& e, const QString& attribute, double defValue)
81  {
82  double value = defValue;
83  if (e.hasAttribute(attribute)) {
84  const QString s = e.attribute(attribute);
85  bool ok;
86  value = s.toDouble(&ok);
87  if (!ok) {
88  warning("Bad double syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
89  value = defValue;
90  }
91  } else
92  warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
93 
94 #if defined(isnan)
95  // The "isnan" method may not be available on all platforms.
96  // Find its equivalent or simply remove these two lines
97  if (isnan(value))
98  warning("Warning, attribute \""+attribute+"\" initialized to Not a Number in \""+e.tagName()+"\"");
99 #endif
100 
101  return value;
102  }
103 
104  static int intFromDom(const QDomElement& e, const QString& attribute, int defValue)
105  {
106  int value = defValue;
107  if (e.hasAttribute(attribute))
108  {
109  const QString s = e.attribute(attribute);
110  bool ok;
111  s.toInt(&ok);
112  if (ok)
113  value = s.toInt();
114  else
115  warning("Bad integer syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
116  }
117  else
118  warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+QString::number(value)+".");
119  return value;
120  }
121 
122  static bool boolFromDom(const QDomElement& e, const QString& attribute, bool defValue)
123  {
124  bool value = defValue;
125  if (e.hasAttribute(attribute))
126  {
127  const QString s = e.attribute(attribute);
128 #if QT_VERSION >= 0x040000
129  if (s.toLower() == QString("true"))
130 #else
131  if (s.lower() == QString("true"))
132 #endif
133  value = true;
134 #if QT_VERSION >= 0x040000
135  else if (s.toLower() == QString("false"))
136 #else
137  else if (s.lower() == QString("false"))
138 #endif
139  value = false;
140  else
141  {
142  warning("Bad boolean syntax for attribute \""+attribute+"\" in initialization of \""+e.tagName()+"\" (should be \"true\" or \"false\").");
143  warning("Setting value to "+(value?QString("true."):QString("false.")));
144  }
145  }
146  else
147  warning("\""+attribute+"\" attribute missing in initialization of \""+e.tagName()+"\". Setting value to "+(value?QString("true."):QString("false.")));
148  return value;
149  }
150 
151  static QDomElement QColorDomElement(const QColor& color, const QString& name, QDomDocument& doc)
152  {
153  QDomElement de = doc.createElement(name);
154  de.setAttribute("red", QString::number(color.red()));
155  de.setAttribute("green", QString::number(color.green()));
156  de.setAttribute("blue", QString::number(color.blue()));
157  return de;
158  }
159 
160  static QColor QColorFromDom(const QDomElement& e)
161  {
162  int color[3];
163  QStringList attribute;
164  attribute << "red" << "green" << "blue";
165 #if QT_VERSION >= 0x040000
166  for (int i=0; i<attribute.count(); ++i)
167 #else
168  for (unsigned int i=0; i<attribute.count(); ++i)
169 #endif
170  color[i] = DomUtils::intFromDom(e, attribute[i], 0);
171  return QColor(color[0], color[1], color[2]);
172  }
173 };
174 
175 #endif // DOXYGEN