domUtils.h
1 /****************************************************************************
2 
3  Copyright (C) 2002-2013 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.5.2.
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 #include "config.h"
24 
25 #include <QDomElement>
26 #include <QString>
27 #include <QStringList>
28 #include <QColor>
29 
30 #include <math.h>
31 
32 #ifndef DOXYGEN
33 
34 // QDomElement loading with syntax checking.
35 class DomUtils
36 {
37 private:
38  static void warning(const QString& message)
39  {
40  qWarning("%s", message.toLatin1().constData());
41  }
42 
43 public:
44  static float floatFromDom(const QDomElement& e, const QString& attribute, float defValue)
45  {
46  float value = defValue;
47  if (e.hasAttribute(attribute)) {
48  const QString s = e.attribute(attribute);
49  bool ok;
50  value = s.toFloat(&ok);
51  if (!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)));
54  value = defValue;
55  }
56  } else {
57  warning(QString("\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
58  .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
59  }
60 
61 #if defined(isnan)
62  // The "isnan" method may not be available on all platforms.
63  // Find its equivalent or simply remove these two lines
64  if (isnan(value))
65  warning(QString("Warning, attribute \"%1\" initialized to Not a Number in \"%2\"")
66  .arg(attribute).arg(e.tagName()));
67 #endif
68 
69  return value;
70  }
71 
72  static double doubleFromDom(const QDomElement& e, const QString& attribute, double defValue)
73  {
74  double value = defValue;
75  if (e.hasAttribute(attribute)) {
76  const QString s = e.attribute(attribute);
77  bool ok;
78  value = s.toDouble(&ok);
79  if (!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)));
82  value = defValue;
83  }
84  } else {
85  warning(QString("\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
86  .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
87  }
88 
89 #if defined(isnan)
90  // The "isnan" method may not be available on all platforms.
91  // Find its equivalent or simply remove these two lines
92  if (isnan(value))
93  warning(QString("Warning, attribute \"%1\" initialized to Not a Number in \"%2\"")
94  .arg(attribute).arg(e.tagName()));
95 #endif
96 
97  return value;
98  }
99 
100  static int intFromDom(const QDomElement& e, const QString& attribute, int defValue)
101  {
102  int value = defValue;
103  if (e.hasAttribute(attribute))
104  {
105  const QString s = e.attribute(attribute);
106  bool ok;
107  value = s.toInt(&ok);
108  if (!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)));
111  value = defValue;
112  }
113  } else {
114  warning(QString("\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
115  .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
116  }
117 
118  return value;
119  }
120 
121  static bool boolFromDom(const QDomElement& e, const QString& attribute, bool defValue)
122  {
123  bool value = defValue;
124  if (e.hasAttribute(attribute))
125  {
126  const QString s = e.attribute(attribute);
127  if (s.toLower() == QString("true"))
128  value = true;
129  else if (s.toLower() == QString("false"))
130  value = false;
131  else
132  {
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"));
135  }
136  } else {
137  warning(QString("\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
138  .arg(attribute).arg(e.tagName()).arg(value?"true":"false"));
139  }
140 
141  return value;
142  }
143 
144  static void setBoolAttribute(QDomElement& element, const QString& attribute, bool value) {
145  element.setAttribute(attribute, (value ? "true" : "false"));
146  }
147 
148  static QDomElement QColorDomElement(const QColor& color, const QString& name, QDomDocument& doc)
149  {
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()));
154  return de;
155  }
156 
157  static QColor QColorFromDom(const QDomElement& e)
158  {
159  int color[3];
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]);
165  }
166 };
167 
168 #endif // DOXYGEN