|
| 1 | +/*************************************************************************** |
| 2 | + qgsconfigurationmap.cpp - QgsConfigurationMap |
| 3 | +
|
| 4 | + --------------------- |
| 5 | + begin : 11.11.2016 |
| 6 | + copyright : (C) 2016 by Matthias Kuhn |
| 7 | + email : matthias@opengis.ch |
| 8 | + *************************************************************************** |
| 9 | + * * |
| 10 | + * This program is free software; you can redistribute it and/or modify * |
| 11 | + * it under the terms of the GNU General Public License as published by * |
| 12 | + * the Free Software Foundation; either version 2 of the License, or * |
| 13 | + * (at your option) any later version. * |
| 14 | + * * |
| 15 | + ***************************************************************************/ |
| 16 | +#include "qgsconfigurationmap.h" |
| 17 | +#include <QMetaEnum> |
| 18 | +#include "qgis.h" |
| 19 | + |
| 20 | +QgsConfigurationMap::QgsConfigurationMap() |
| 21 | +{ |
| 22 | + |
| 23 | +} |
| 24 | + |
| 25 | +QgsConfigurationMap::QgsConfigurationMap( const QVariantMap& value ) |
| 26 | + : mValue( value ) |
| 27 | +{ |
| 28 | +} |
| 29 | + |
| 30 | +QVariantMap QgsConfigurationMap::get() const |
| 31 | +{ |
| 32 | + return mValue; |
| 33 | +} |
| 34 | + |
| 35 | +void QgsConfigurationMap::set( const QVariantMap& value ) |
| 36 | +{ |
| 37 | + mValue = value; |
| 38 | +} |
| 39 | + |
| 40 | +void QgsConfigurationMap::toXml( QDomElement& parentElement ) const |
| 41 | +{ |
| 42 | + return toXml( parentElement, mValue ); |
| 43 | +} |
| 44 | + |
| 45 | +void QgsConfigurationMap::fromXml( const QDomElement& element ) |
| 46 | +{ |
| 47 | + mValue = fromXmlHelper( element ).toMap(); |
| 48 | +} |
| 49 | + |
| 50 | +void QgsConfigurationMap::toXml( QDomElement& parentElement, const QVariant& value ) const |
| 51 | +{ |
| 52 | + switch ( value.type() ) |
| 53 | + { |
| 54 | + case QVariant::Map: |
| 55 | + { |
| 56 | + QVariantMap map = value.toMap(); |
| 57 | + |
| 58 | + for ( auto option = map.constBegin(); option != map.constEnd(); ++option ) |
| 59 | + { |
| 60 | + QDomElement optionElement = parentElement.ownerDocument().createElement( "Option" ); |
| 61 | + optionElement.setAttribute( "name", option.key() ); |
| 62 | + toXml( optionElement, option.value() ); |
| 63 | + parentElement.appendChild( optionElement ); |
| 64 | + parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Map" ) ); |
| 65 | + } |
| 66 | + break; |
| 67 | + } |
| 68 | + |
| 69 | + case QVariant::Int: |
| 70 | + parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Int" ) ); |
| 71 | + parentElement.setAttribute( QStringLiteral( "value" ), value.toString() ); |
| 72 | + break; |
| 73 | + |
| 74 | + case QVariant::Bool: |
| 75 | + parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Bool" ) ); |
| 76 | + parentElement.setAttribute( QStringLiteral( "value" ), value.toString() ); |
| 77 | + break; |
| 78 | + |
| 79 | + case QVariant::Double: |
| 80 | + parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Double" ) ); |
| 81 | + parentElement.setAttribute( QStringLiteral( "value" ), value.toString() ); |
| 82 | + break; |
| 83 | + |
| 84 | + case QVariant::String: |
| 85 | + parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "String" ) ); |
| 86 | + parentElement.setAttribute( QStringLiteral( "value" ), value.toString() ); |
| 87 | + break; |
| 88 | + |
| 89 | + default: |
| 90 | + parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Unknown" ) ); |
| 91 | + parentElement.setAttribute( QStringLiteral( "value" ), value.toString() ); |
| 92 | + break; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +QVariant QgsConfigurationMap::fromXmlHelper( const QDomElement& element ) const |
| 97 | +{ |
| 98 | + QString type = element.attribute( QStringLiteral( "type" ) ); |
| 99 | + |
| 100 | + if ( type == QLatin1String( "Int" ) ) |
| 101 | + { |
| 102 | + return element.attribute( QStringLiteral( "value" ) ).toInt(); |
| 103 | + } |
| 104 | + else if ( type == QLatin1String( "Double" ) ) |
| 105 | + { |
| 106 | + return element.attribute( QStringLiteral( "value" ) ).toDouble(); |
| 107 | + } |
| 108 | + else if ( type == QLatin1String( "String" ) ) |
| 109 | + { |
| 110 | + return element.attribute( QStringLiteral( "value" ) ); |
| 111 | + } |
| 112 | + else if ( type == QLatin1String( "Bool" ) ) |
| 113 | + { |
| 114 | + return element.attribute( QStringLiteral( "value" ) ) == QLatin1String( "true" ); |
| 115 | + } |
| 116 | + else if ( type == QLatin1String( "Map" ) ) |
| 117 | + { |
| 118 | + QVariantMap map; |
| 119 | + QDomNodeList options = element.elementsByTagName( QStringLiteral( "Option" ) ); |
| 120 | + |
| 121 | + for ( int i = 0; i < options.count(); ++i ) |
| 122 | + { |
| 123 | + QDomElement elem = options.at( i ).toElement(); |
| 124 | + map.insert( elem.attribute( QStringLiteral( "name" ) ), fromXmlHelper( elem ) ); |
| 125 | + } |
| 126 | + return map; |
| 127 | + } |
| 128 | + else |
| 129 | + { |
| 130 | + return QVariant(); |
| 131 | + } |
| 132 | +} |
0 commit comments