Skip to content

Commit 80b3bfd

Browse files
committed
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.
1 parent e6f84de commit 80b3bfd

8 files changed

+407
-0
lines changed

python/core/core.sip

+1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
%Include qgscolorschemeregistry.sip
3232
%Include qgsconditionalstyle.sip
3333
%Include qgscontexthelp.sip
34+
%Include qgsconfigurationmap.sip
3435
%Include qgscoordinatereferencesystem.sip
3536
%Include qgscoordinatetransform.sip
3637
%Include qgscredentials.sip

python/core/qgsconfigurationmap.sip

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/***************************************************************************
2+
qgsconfigurationmap.sip - 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+
class QgsConfigurationMap
17+
{
18+
%TypeHeaderCode
19+
#include "qgsconfigurationmap.h"
20+
%End
21+
22+
public:
23+
QgsConfigurationMap();
24+
25+
QgsConfigurationMap( QVariantMap value );
26+
27+
QVariantMap get() const;
28+
29+
void set( const QVariantMap& value );
30+
31+
void toXml( QDomElement& parentElement ) const;
32+
33+
void fromXml( const QDomElement& element );
34+
};

src/core/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ SET(QGIS_CORE_SRCS
9797
qgscolorscheme.cpp
9898
qgscolorschemeregistry.cpp
9999
qgsconditionalstyle.cpp
100+
qgsconfigurationmap.cpp
100101
qgscontexthelp.cpp
101102
qgscoordinatereferencesystem.cpp
102103
qgscoordinatetransform.cpp
@@ -626,6 +627,7 @@ SET(QGIS_CORE_HDRS
626627
qgscolorramp.h
627628
qgscolorscheme.h
628629
qgscolorschemeregistry.h
630+
qgsconfigurationmap.h
629631
qgsconnectionpool.h
630632
qgscontexthelp.h
631633
qgsconditionalstyle.h

src/core/qgsconfigurationmap.cpp

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
}

src/core/qgsconfigurationmap.h

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/***************************************************************************
2+
qgsconfigurationmap.h - 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+
#ifndef QGSCONFIGURATIONMAP_H
17+
#define QGSCONFIGURATIONMAP_H
18+
19+
#include <QVariant>
20+
#include <QDomElement>
21+
22+
/**
23+
* A configuration map holds a QVariantMap as a
24+
* key-value store where each value can be a string,
25+
* boolean, integer, double or yet another map.
26+
*
27+
* \note Added in QGIS 3.0
28+
* \ingroup core
29+
*/
30+
31+
class CORE_EXPORT QgsConfigurationMap
32+
{
33+
public:
34+
35+
/**
36+
* Create a new empty configuration map
37+
*/
38+
QgsConfigurationMap();
39+
40+
/**
41+
* Create a new configuration map from an existing
42+
* QVariantMap.
43+
*/
44+
QgsConfigurationMap( const QVariantMap& value );
45+
46+
/**
47+
* Get the content of this configuration map
48+
* as QVariantMap.
49+
*/
50+
QVariantMap get() const;
51+
52+
/**
53+
* Set the content of this configuration map
54+
* to a QVariantMap.
55+
*/
56+
void set( const QVariantMap& value );
57+
58+
/**
59+
* Write this map into the provided parentElement.
60+
* The attribute type of the parentElement will be
61+
* used internally.
62+
*/
63+
void toXml( QDomElement& parentElement ) const;
64+
65+
/**
66+
* Read this map from the provided element.
67+
*/
68+
void fromXml( const QDomElement& element );
69+
70+
private:
71+
QVariantMap mValue;
72+
73+
void toXml( QDomElement& parentElement, const QVariant& value ) const;
74+
75+
QVariant fromXmlHelper( const QDomElement& element ) const;
76+
};
77+
78+
#endif // QGSCONFIGURATIONMAP_H

src/core/qgsvectorlayer.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -1956,12 +1956,40 @@ bool QgsVectorLayer::writeSymbology( QDomNode& node, QDomDocument& doc, QString&
19561956
{
19571957
( void )writeStyle( node, doc, errorMessage );
19581958

1959+
<<<<<<< HEAD
19591960
// FIXME
19601961
// edittypes are written to the layerNode
19611962
// by slot QgsEditorWidgetRegistry::writeMapLayer()
19621963
// triggered by signal QgsProject::writeMapLayer()
19631964
// still other editing settings are written here,
19641965
// although they are not part of symbology either
1966+
=======
1967+
QDomElement fieldConfigurationElement = doc.createElement( QStringLiteral( "fieldConfiguration" ) );
1968+
1969+
int index = 0;
1970+
Q_FOREACH ( const QgsField& field, mFields )
1971+
{
1972+
QDomElement fieldElement = doc.createElement( QStringLiteral( "field" ) );
1973+
fieldElement.setAttribute( QStringLiteral( "name" ), field.name() );
1974+
1975+
fieldConfigurationElement.appendChild( fieldElement );
1976+
1977+
QgsEditorWidgetSetup widgetSetup = field.editorWidgetSetup();
1978+
1979+
// TODO : wrap this part in an if to only save if it was user-modified
1980+
QDomElement editWidgetElement = doc.createElement( QStringLiteral( "editWidget" ) );
1981+
fieldElement.appendChild( editWidgetElement );
1982+
editWidgetElement.setAttribute( "type", field.editorWidgetSetup().type() );
1983+
1984+
QDomElement editWidgetConfigElement = doc.createElement( QStringLiteral( "config" ) );
1985+
1986+
QgsConfigurationMap( widgetSetup.config() ).toXml( editWidgetConfigElement );
1987+
editWidgetElement.appendChild( editWidgetConfigElement );
1988+
// END TODO : wrap this part in an if to only save if it was user-modified
1989+
1990+
++index;
1991+
}
1992+
>>>>>>> dc0c72f... Fixup
19651993

19661994
QDomElement afField = doc.createElement( QStringLiteral( "annotationform" ) );
19671995
QDomText afText = doc.createTextNode( QgsProject::instance()->writePath( mAnnotationForm ) );

tests/src/python/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ ADD_PYTHON_TEST(PyQgsComposerPolyline test_qgscomposerpolyline.py)
3636
ADD_PYTHON_TEST(PyQgsComposerView test_qgscomposerview.py)
3737
ADD_PYTHON_TEST(PyQgsComposition test_qgscomposition.py)
3838
ADD_PYTHON_TEST(PyQgsConditionalStyle test_qgsconditionalstyle.py)
39+
ADD_PYTHON_TEST(PyQgsConfigurationMap test_qgsconfigurationmap.py)
3940
ADD_PYTHON_TEST(PyQgsCoordinateTransform test_qgscoordinatetransform.py)
4041
ADD_PYTHON_TEST(PyQgsDateTimeStatisticalSummary test_qgsdatetimestatisticalsummary.py)
4142
ADD_PYTHON_TEST(PyQgsDelimitedTextProvider test_qgsdelimitedtextprovider.py)

0 commit comments

Comments
 (0)