Skip to content
Permalink
Browse files

Add QgsConfigurationProperty class

This class holds a QVariantMap to manage key-value-pairs where each
value can be a string, bool, int, double or itself a map and can be
stored to and restored from XML.
  • Loading branch information
m-kuhn committed Dec 20, 2016
1 parent e6f84de commit 80b3bfdf18ab08a0a03e7aca961911ab278d83be
@@ -31,6 +31,7 @@
%Include qgscolorschemeregistry.sip
%Include qgsconditionalstyle.sip
%Include qgscontexthelp.sip
%Include qgsconfigurationmap.sip
%Include qgscoordinatereferencesystem.sip
%Include qgscoordinatetransform.sip
%Include qgscredentials.sip
@@ -0,0 +1,34 @@
/***************************************************************************
qgsconfigurationmap.sip - QgsConfigurationMap

---------------------
begin : 11.11.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
class QgsConfigurationMap
{
%TypeHeaderCode
#include "qgsconfigurationmap.h"
%End

public:
QgsConfigurationMap();

QgsConfigurationMap( QVariantMap value );

QVariantMap get() const;

void set( const QVariantMap& value );

void toXml( QDomElement& parentElement ) const;

void fromXml( const QDomElement& element );
};
@@ -97,6 +97,7 @@ SET(QGIS_CORE_SRCS
qgscolorscheme.cpp
qgscolorschemeregistry.cpp
qgsconditionalstyle.cpp
qgsconfigurationmap.cpp
qgscontexthelp.cpp
qgscoordinatereferencesystem.cpp
qgscoordinatetransform.cpp
@@ -626,6 +627,7 @@ SET(QGIS_CORE_HDRS
qgscolorramp.h
qgscolorscheme.h
qgscolorschemeregistry.h
qgsconfigurationmap.h
qgsconnectionpool.h
qgscontexthelp.h
qgsconditionalstyle.h
@@ -0,0 +1,132 @@
/***************************************************************************
qgsconfigurationmap.cpp - QgsConfigurationMap
---------------------
begin : 11.11.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsconfigurationmap.h"
#include <QMetaEnum>
#include "qgis.h"

QgsConfigurationMap::QgsConfigurationMap()
{

}

QgsConfigurationMap::QgsConfigurationMap( const QVariantMap& value )
: mValue( value )
{
}

QVariantMap QgsConfigurationMap::get() const
{
return mValue;
}

void QgsConfigurationMap::set( const QVariantMap& value )
{
mValue = value;
}

void QgsConfigurationMap::toXml( QDomElement& parentElement ) const
{
return toXml( parentElement, mValue );
}

void QgsConfigurationMap::fromXml( const QDomElement& element )
{
mValue = fromXmlHelper( element ).toMap();
}

void QgsConfigurationMap::toXml( QDomElement& parentElement, const QVariant& value ) const
{
switch ( value.type() )
{
case QVariant::Map:
{
QVariantMap map = value.toMap();

for ( auto option = map.constBegin(); option != map.constEnd(); ++option )
{
QDomElement optionElement = parentElement.ownerDocument().createElement( "Option" );
optionElement.setAttribute( "name", option.key() );
toXml( optionElement, option.value() );
parentElement.appendChild( optionElement );
parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Map" ) );
}
break;
}

case QVariant::Int:
parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Int" ) );
parentElement.setAttribute( QStringLiteral( "value" ), value.toString() );
break;

case QVariant::Bool:
parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Bool" ) );
parentElement.setAttribute( QStringLiteral( "value" ), value.toString() );
break;

case QVariant::Double:
parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Double" ) );
parentElement.setAttribute( QStringLiteral( "value" ), value.toString() );
break;

case QVariant::String:
parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "String" ) );
parentElement.setAttribute( QStringLiteral( "value" ), value.toString() );
break;

default:
parentElement.setAttribute( QStringLiteral( "type" ), QStringLiteral( "Unknown" ) );
parentElement.setAttribute( QStringLiteral( "value" ), value.toString() );
break;
}
}

QVariant QgsConfigurationMap::fromXmlHelper( const QDomElement& element ) const
{
QString type = element.attribute( QStringLiteral( "type" ) );

if ( type == QLatin1String( "Int" ) )
{
return element.attribute( QStringLiteral( "value" ) ).toInt();
}
else if ( type == QLatin1String( "Double" ) )
{
return element.attribute( QStringLiteral( "value" ) ).toDouble();
}
else if ( type == QLatin1String( "String" ) )
{
return element.attribute( QStringLiteral( "value" ) );
}
else if ( type == QLatin1String( "Bool" ) )
{
return element.attribute( QStringLiteral( "value" ) ) == QLatin1String( "true" );
}
else if ( type == QLatin1String( "Map" ) )
{
QVariantMap map;
QDomNodeList options = element.elementsByTagName( QStringLiteral( "Option" ) );

for ( int i = 0; i < options.count(); ++i )
{
QDomElement elem = options.at( i ).toElement();
map.insert( elem.attribute( QStringLiteral( "name" ) ), fromXmlHelper( elem ) );
}
return map;
}
else
{
return QVariant();
}
}
@@ -0,0 +1,78 @@
/***************************************************************************
qgsconfigurationmap.h - QgsConfigurationMap
---------------------
begin : 11.11.2016
copyright : (C) 2016 by Matthias Kuhn
email : matthias@opengis.ch
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef QGSCONFIGURATIONMAP_H
#define QGSCONFIGURATIONMAP_H

#include <QVariant>
#include <QDomElement>

/**
* A configuration map holds a QVariantMap as a
* key-value store where each value can be a string,
* boolean, integer, double or yet another map.
*
* \note Added in QGIS 3.0
* \ingroup core
*/

class CORE_EXPORT QgsConfigurationMap
{
public:

/**
* Create a new empty configuration map
*/
QgsConfigurationMap();

/**
* Create a new configuration map from an existing
* QVariantMap.
*/
QgsConfigurationMap( const QVariantMap& value );

/**
* Get the content of this configuration map
* as QVariantMap.
*/
QVariantMap get() const;

/**
* Set the content of this configuration map
* to a QVariantMap.
*/
void set( const QVariantMap& value );

/**
* Write this map into the provided parentElement.
* The attribute type of the parentElement will be
* used internally.
*/
void toXml( QDomElement& parentElement ) const;

/**
* Read this map from the provided element.
*/
void fromXml( const QDomElement& element );

private:
QVariantMap mValue;

void toXml( QDomElement& parentElement, const QVariant& value ) const;

QVariant fromXmlHelper( const QDomElement& element ) const;
};

#endif // QGSCONFIGURATIONMAP_H
@@ -1956,12 +1956,40 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
{
( void )writeStyle( node, doc, errorMessage );

<<<<<<< HEAD
// FIXME
// edittypes are written to the layerNode
// by slot QgsEditorWidgetRegistry::writeMapLayer()
// triggered by signal QgsProject::writeMapLayer()
// still other editing settings are written here,
// although they are not part of symbology either
=======
QDomElement fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );

int index = 0;
Q_FOREACH ( const QgsField& field, mFields )
{
QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );

fieldConfigurationElement.appendChild( fieldElement );

QgsEditorWidgetSetup widgetSetup = field.editorWidgetSetup();

// TODO : wrap this part in an if to only save if it was user-modified
QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
fieldElement.appendChild( editWidgetElement );
editWidgetElement.setAttribute( "type", field.editorWidgetSetup().type() );

QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );

QgsConfigurationMap( widgetSetup.config() ).toXml( editWidgetConfigElement );
editWidgetElement.appendChild( editWidgetConfigElement );
// END TODO : wrap this part in an if to only save if it was user-modified

++index;
}
>>>>>>> dc0c72f... Fixup

QDomElement afField = doc.createElement( QStringLiteral( "annotationform" ) );
QDomText afText = doc.createTextNode( QgsProject::instance()->writePath( mAnnotationForm ) );
@@ -36,6 +36,7 @@ ADD_PYTHON_TEST(PyQgsComposerPolyline test_qgscomposerpolyline.py)
ADD_PYTHON_TEST(PyQgsComposerView test_qgscomposerview.py)
ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
ADD_PYTHON_TEST(PyQgsConditionalStyle test_qgsconditionalstyle.py)
ADD_PYTHON_TEST(PyQgsConfigurationMap test_qgsconfigurationmap.py)
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
ADD_PYTHON_TEST(PyQgsDateTimeStatisticalSummary test_qgsdatetimestatisticalsummary.py)
ADD_PYTHON_TEST(PyQgsDelimitedTextProvider test_qgsdelimitedtextprovider.py)

0 comments on commit 80b3bfd

Please sign in to comment.
You can’t perform that action at this time.