17 #include <QDomElement> 26 if (
"unknown" == element.text() )
41 QDomNode xminNode = element.namedItem( QStringLiteral(
"xmin" ) );
42 QDomNode yminNode = element.namedItem( QStringLiteral(
"ymin" ) );
43 QDomNode xmaxNode = element.namedItem( QStringLiteral(
"xmax" ) );
44 QDomNode ymaxNode = element.namedItem( QStringLiteral(
"ymax" ) );
46 QDomElement exElement = xminNode.toElement();
47 double xmin = exElement.text().toDouble();
50 exElement = yminNode.toElement();
51 double ymin = exElement.text().toDouble();
54 exElement = xmaxNode.toElement();
55 double xmax = exElement.text().toDouble();
58 exElement = ymaxNode.toElement();
59 double ymax = exElement.text().toDouble();
72 unitsString = QStringLiteral(
"unknown" );
74 QDomElement unitsNode = doc.createElement( QStringLiteral(
"units" ) );
75 unitsNode.appendChild( doc.createTextNode( unitsString ) );
81 QDomElement xMin = doc.createElement( QStringLiteral(
"xmin" ) );
82 QDomElement yMin = doc.createElement( QStringLiteral(
"ymin" ) );
83 QDomElement xMax = doc.createElement( QStringLiteral(
"xmax" ) );
84 QDomElement yMax = doc.createElement( QStringLiteral(
"ymax" ) );
91 xMin.appendChild( xMinText );
92 yMin.appendChild( yMinText );
93 xMax.appendChild( xMaxText );
94 yMax.appendChild( yMaxText );
96 QDomElement extentNode = doc.createElement( QStringLiteral(
"extent" ) );
97 extentNode.appendChild( xMin );
98 extentNode.appendChild( yMin );
99 extentNode.appendChild( xMax );
100 extentNode.appendChild( yMax );
106 QDomElement element = doc.createElement( QStringLiteral(
"Option" ) );
107 switch ( value.type() )
109 case QVariant::Invalid:
111 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"invalid" ) );
117 QVariantMap map = value.toMap();
119 for (
auto option = map.constBegin(); option != map.constEnd(); ++option )
121 QDomElement optionElement =
writeVariant( option.value(), doc );
122 optionElement.setAttribute( QStringLiteral(
"name" ), option.key() );
123 element.appendChild( optionElement );
124 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"Map" ) );
131 QVariantList list = value.toList();
133 const auto constList = list;
134 for (
const QVariant &value : constList )
137 element.appendChild( valueElement );
138 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"List" ) );
143 case QVariant::StringList:
145 QStringList list = value.toStringList();
147 const auto constList = list;
148 for (
const QString &value : constList )
151 element.appendChild( valueElement );
152 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"StringList" ) );
160 case QVariant::Double:
161 case QVariant::LongLong:
162 case QVariant::ULongLong:
163 case QVariant::String:
164 element.setAttribute( QStringLiteral(
"type" ), QVariant::typeToName( value.type() ) );
165 element.setAttribute( QStringLiteral(
"value" ), value.toString() );
168 case QVariant::Color:
169 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"color" ) );
170 element.setAttribute( QStringLiteral(
"value" ), value.value< QColor >().isValid() ?
QgsSymbolLayerUtils::encodeColor( value.value< QColor >() ) : QString() );
173 case QVariant::UserType:
177 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"QgsProperty" ) );
179 element.appendChild( propertyElem );
184 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"QgsCoordinateReferenceSystem" ) );
191 element.setAttribute( QStringLiteral(
"type" ), QStringLiteral(
"QgsGeometry" ) );
193 element.setAttribute( QStringLiteral(
"value" ), geom.
asWkt() );
200 Q_ASSERT_X(
false,
"QgsXmlUtils::writeVariant", QStringLiteral(
"unsupported variant type %1" ).arg( QVariant::typeToName( value.type() ) ).toLocal8Bit() );
209 QString type = element.attribute( QStringLiteral(
"type" ) );
211 if ( type == QLatin1String(
"invalid" ) )
215 else if ( type == QLatin1String(
"int" ) )
217 return element.attribute( QStringLiteral(
"value" ) ).toInt();
219 else if ( type == QLatin1String(
"uint" ) )
221 return element.attribute( QStringLiteral(
"value" ) ).toUInt();
223 else if ( type == QLatin1String(
"qlonglong" ) )
225 return element.attribute( QStringLiteral(
"value" ) ).toLongLong();
227 else if ( type == QLatin1String(
"qulonglong" ) )
229 return element.attribute( QStringLiteral(
"value" ) ).toULongLong();
231 else if ( type == QLatin1String(
"double" ) )
233 return element.attribute( QStringLiteral(
"value" ) ).toDouble();
235 else if ( type == QLatin1String(
"QString" ) )
237 return element.attribute( QStringLiteral(
"value" ) );
239 else if ( type == QLatin1String(
"bool" ) )
241 return element.attribute( QStringLiteral(
"value" ) ) == QLatin1String(
"true" );
243 else if ( type == QLatin1String(
"color" ) )
245 return element.attribute( QStringLiteral(
"value" ) ).isEmpty() ? QColor() :
QgsSymbolLayerUtils::decodeColor( element.attribute( QStringLiteral(
"value" ) ) );
247 else if ( type == QLatin1String(
"Map" ) )
250 QDomNodeList options = element.childNodes();
252 for (
int i = 0; i < options.count(); ++i )
254 QDomElement elem = options.at( i ).toElement();
255 if ( elem.tagName() == QLatin1String(
"Option" ) )
256 map.insert( elem.attribute( QStringLiteral(
"name" ) ),
readVariant( elem ) );
260 else if ( type == QLatin1String(
"List" ) )
263 QDomNodeList values = element.childNodes();
264 for (
int i = 0; i < values.count(); ++i )
266 QDomElement elem = values.at( i ).toElement();
271 else if ( type == QLatin1String(
"StringList" ) )
274 QDomNodeList values = element.childNodes();
275 for (
int i = 0; i < values.count(); ++i )
277 QDomElement elem = values.at( i ).toElement();
282 else if ( type == QLatin1String(
"QgsProperty" ) )
284 const QDomNodeList values = element.childNodes();
285 if ( values.isEmpty() )
294 else if ( type == QLatin1String(
"QgsCoordinateReferenceSystem" ) )
300 else if ( type == QLatin1String(
"QgsGeometry" ) )
A rectangle specified with double values.
void setXMaximum(double x)
Set the maximum x value.
static QDomElement writeRectangle(const QgsRectangle &rect, QDomDocument &doc)
A geometry is the spatial representation of a feature.
static Q_INVOKABLE QgsUnitTypes::DistanceUnit decodeDistanceUnit(const QString &string, bool *ok=nullptr)
Decodes a distance unit from a string.
const QgsCoordinateReferenceSystem & crs
static QString encodeColor(const QColor &color)
QVariant toVariant() const
Saves this property to a QVariantMap, wrapped in a QVariant.
static QVariant readVariant(const QDomElement &element)
Read a QVariant from a QDomElement.
bool loadVariant(const QVariant &property)
Loads this property from a QVariantMap, wrapped in a QVariant.
static QgsUnitTypes::DistanceUnit readMapUnits(const QDomElement &element)
Decodes a distance unit from a DOM element.
QString qgsDoubleToString(double a, int precision=17)
Returns a string representation of a double.
void setYMinimum(double y)
Set the minimum y value.
static QDomElement writeMapUnits(QgsUnitTypes::DistanceUnit units, QDomDocument &doc)
Encodes a distance unit to a DOM element.
Degrees, for planar geographic CRS distance measurements.
A store for object properties.
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
double yMinimum() const
Returns the y minimum value (bottom side of rectangle).
QString asWkt(int precision=17) const
Exports the geometry to WKT.
DistanceUnit
Units of distance.
double xMaximum() const
Returns the x maximum value (right side of rectangle).
static QgsGeometry fromWkt(const QString &wkt)
Creates a new geometry from a WKT string.
void setYMaximum(double y)
Set the maximum y value.
This class represents a coordinate reference system (CRS).
double xMinimum() const
Returns the x minimum value (left side of rectangle).
bool readXml(const QDomNode &node)
Restores state from the given DOM node.
double yMaximum() const
Returns the y maximum value (top side of rectangle).
bool writeXml(QDomNode &node, QDomDocument &doc) const
Stores state to the given Dom node in the given document.
static QgsRectangle readRectangle(const QDomElement &element)
static QDomElement writeVariant(const QVariant &value, QDomDocument &doc)
Write a QVariant to a QDomElement.
void setXMinimum(double x)
Set the minimum x value.
static QColor decodeColor(const QString &str)