VAT  3.0
Video Analysis Tool
xmlutils.hpp
1 #ifndef XMLUTILS_HPP
2 #define XMLUTILS_HPP
3 
4 #include <QDomNode>
5 #include <QDomElement>
6 
7 // XML helper functions
8 QString nodeAttribute(QDomNode node, QString route, QString defaultval);
9 bool nodeAttributeBoolean(QDomNode node, QString route, bool defaultval, bool* formatok = NULL);
10 
21 template <typename Type>
22 Type nodeAttributeNumber(QDomNode node, QString route, Type defaultval, bool negativesallowed, bool* formatok)
23 {
24  double retval;
25  bool formatok_internal;
26 
27  QString attvalue = nodeAttribute(node, route, QString::number(defaultval));
28 
29  if(attvalue.endsWith('%')) // To make sure it accepts a "%" symbol at the end (we are talking about percentages after all...)
30  attvalue.chop(1);
31 
32  retval = attvalue.toDouble(&formatok_internal);
33 
34  if(!formatok_internal)
35  retval = defaultval;
36 
37  if(formatok != NULL)
38  *formatok = formatok_internal;
39 
40  if(!negativesallowed && retval < 0.0)
41  {
42  retval = defaultval;
43  if(formatok != NULL)
44  *formatok = false;
45  }
46 
47  return static_cast<Type>(retval);
48 }
49 
50 #endif // XMLUTILS_HPP