QGIS API Documentation  3.10.0-A Coruña (6c816b4204)
qgsxmlutils.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsxmlutils.h
3  ---------------------
4  begin : December 2013
5  copyright : (C) 2013 by Martin Dobias
6  email : wonder dot sk at gmail dot com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #ifndef QGSXMLUTILS_H
16 #define QGSXMLUTILS_H
17 
18 class QDomDocument;
19 
20 class QgsRectangle;
21 
22 #include <QDomElement>
23 #include <QMetaEnum>
24 
25 #include "qgis_core.h"
26 #include "qgis_sip.h"
27 #include "qgsunittypes.h"
28 
29 
30 
35 class CORE_EXPORT QgsXmlUtils
36 {
37  public:
38 
39  /* reading */
40 
47  static QgsUnitTypes::DistanceUnit readMapUnits( const QDomElement &element );
48 
49  static QgsRectangle readRectangle( const QDomElement &element );
50 
51  /* writing */
52 
60  static QDomElement writeMapUnits( QgsUnitTypes::DistanceUnit units, QDomDocument &doc );
61 
62  static QDomElement writeRectangle( const QgsRectangle &rect, QDomDocument &doc );
63 
76  static QDomElement writeVariant( const QVariant &value, QDomDocument &doc );
77 
81  static QVariant readVariant( const QDomElement &element );
82 
92  template<class T> static T readFlagAttribute( const QDomElement &element, const QString &attributeName, T defaultValue ) SIP_SKIP
93  {
94  T value = defaultValue;
95  // Get source categories
96  QMetaEnum metaEnum = QMetaEnum::fromType<T>();
97  QString sourceCategoriesStr( element.attribute( attributeName, metaEnum.valueToKeys( static_cast<int>( defaultValue ) ) ) );
98  if ( metaEnum.isValid() )
99  {
100  bool ok = false;
101  int newValue = metaEnum.keysToValue( sourceCategoriesStr.toUtf8().constData(), &ok );
102  if ( ok )
103  value = static_cast<T>( newValue );
104  }
105  return value;
106  }
107 };
108 
109 
110 #endif // QGSXMLUTILS_H
A rectangle specified with double values.
Definition: qgsrectangle.h:41
Assorted helper methods for reading and writing chunks of XML.
Definition: qgsxmlutils.h:35
static T readFlagAttribute(const QDomElement &element, const QString &attributeName, T defaultValue)
Read a flag value from an attribute of the element.
Definition: qgsxmlutils.h:92
#define SIP_SKIP
Definition: qgis_sip.h:126
DistanceUnit
Units of distance.
Definition: qgsunittypes.h:66