domUtils.h
Go to the documentation of this file.
1 /****************************************************************************
2 
3  Copyright (C) 2002-2014 Gilles Debunne. All rights reserved.
4 
5  This file is part of the QGLViewer library version 2.6.3.
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 qreal qrealFromDom(const QDomElement& e, const QString& attribute, qreal defValue)
45  {
46  qreal value = defValue;
47  if (e.hasAttribute(attribute)) {
48  const QString s = e.attribute(attribute);
49  bool ok;
50  value = s.toDouble(&ok);
51  if (!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)));
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 int intFromDom(const QDomElement& e, const QString& attribute, int defValue)
73  {
74  int value = defValue;
75  if (e.hasAttribute(attribute))
76  {
77  const QString s = e.attribute(attribute);
78  bool ok;
79  value = s.toInt(&ok);
80  if (!ok) {
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)));
83  value = defValue;
84  }
85  } else {
86  warning(QString("\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
87  .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
88  }
89 
90  return value;
91  }
92 
93  static unsigned int uintFromDom(const QDomElement& e, const QString& attribute, unsigned int defValue)
94  {
95  unsigned int value = defValue;
96  if (e.hasAttribute(attribute))
97  {
98  const QString s = e.attribute(attribute);
99  bool ok;
100  value = s.toUInt(&ok);
101  if (!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)));
104  value = defValue;
105  }
106  } else {
107  warning(QString("\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
108  .arg(attribute).arg(e.tagName()).arg(QString::number(value)));
109  }
110 
111  return value;
112  }
113 
114  static bool boolFromDom(const QDomElement& e, const QString& attribute, bool defValue)
115  {
116  bool value = defValue;
117  if (e.hasAttribute(attribute))
118  {
119  const QString s = e.attribute(attribute);
120  if (s.toLower() == QString("true"))
121  value = true;
122  else if (s.toLower() == QString("false"))
123  value = false;
124  else
125  {
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"));
128  }
129  } else {
130  warning(QString("\"%1\" attribute missing in initialization of \"%2\". Setting value to %3.")
131  .arg(attribute).arg(e.tagName()).arg(value?"true":"false"));
132  }
133 
134  return value;
135  }
136 
137  static void setBoolAttribute(QDomElement& element, const QString& attribute, bool value) {
138  element.setAttribute(attribute, (value ? "true" : "false"));
139  }
140 
141  static QDomElement QColorDomElement(const QColor& color, const QString& name, QDomDocument& doc)
142  {
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()));
147  return de;
148  }
149 
150  static QColor QColorFromDom(const QDomElement& e)
151  {
152  int color[3];
153  QStringList attribute;
154  attribute << "red" << "green" << "blue";
155  for (int i=0; i<attribute.count(); ++i)
156  color[i] = DomUtils::intFromDom(e, attribute[i], 0);
157  return QColor(color[0], color[1], color[2]);
158  }
159 };
160 
161 #endif // DOXYGEN
static QDomElement QColorDomElement(const QColor &color, const QString &name, QDomDocument &doc)
Definition: domUtils.h:141
static void warning(const QString &message)
Definition: domUtils.h:38
static void setBoolAttribute(QDomElement &element, const QString &attribute, bool value)
Definition: domUtils.h:137
static unsigned int uintFromDom(const QDomElement &e, const QString &attribute, unsigned int defValue)
Definition: domUtils.h:93
static qreal qrealFromDom(const QDomElement &e, const QString &attribute, qreal defValue)
Definition: domUtils.h:44
static QColor QColorFromDom(const QDomElement &e)
Definition: domUtils.h:150
static bool boolFromDom(const QDomElement &e, const QString &attribute, bool defValue)
Definition: domUtils.h:114
static int intFromDom(const QDomElement &e, const QString &attribute, int defValue)
Definition: domUtils.h:72


octovis
Author(s): Kai M. Wurm , Armin Hornung
autogenerated on Wed Jun 5 2019 19:26:39