Skip to content

Commit 0170580

Browse files
committed
Save QgsProperty related objects via QVariants
1 parent 1687019 commit 0170580

24 files changed

+483
-296
lines changed

python/core/qgsproperty.sip

+2-4
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,8 @@ class QgsProperty
128128
int valueAsInt( const QgsExpressionContext& context, int defaultValue = 0, bool* ok /Out/ = nullptr ) const;
129129

130130
bool valueAsBool( const QgsExpressionContext& context, bool defaultValue = false, bool* ok /Out/ = nullptr ) const;
131-
132-
virtual bool writeXml( QDomElement& propertyElem, QDomDocument& doc ) const;
133-
134-
virtual bool readXml( const QDomElement& propertyElem, const QDomDocument& doc );
131+
QVariant toVariant() const;
132+
bool loadVariant( const QVariant &property );
135133

136134
void setTransformer( QgsPropertyTransformer* transformer /Transfer/ );
137135

python/core/qgspropertycollection.sip

+8-8
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ class QgsAbstractPropertyCollection
5353

5454
virtual bool hasDynamicProperties() const = 0;
5555

56-
virtual bool writeXml( QDomElement& collectionElem, QDomDocument& doc, const QgsPropertiesDefinition& definitions ) const = 0;
57-
58-
virtual bool readXml( const QDomElement& collectionElem, const QDomDocument& doc, const QgsPropertiesDefinition &definitions ) = 0;
59-
56+
virtual bool writeXml( QDomElement& collectionElem, const QgsPropertiesDefinition& definitions ) const = 0;
57+
virtual bool readXml( const QDomElement& collectionElem, const QgsPropertiesDefinition &definitions ) = 0;
58+
virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
59+
virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
6060
};
6161

6262

@@ -88,9 +88,9 @@ class QgsPropertyCollection : QgsAbstractPropertyCollection
8888
bool isActive( int key ) const;
8989
bool hasActiveProperties() const;
9090
bool hasDynamicProperties() const;
91-
bool writeXml( QDomElement& collectionElem, QDomDocument& doc, const QgsPropertiesDefinition& definitions ) const;
92-
bool readXml( const QDomElement& collectionElem, const QDomDocument& doc, const QgsPropertiesDefinition& definitions );
9391

92+
QVariant toVariant( const QgsPropertiesDefinition &definitions ) const;
93+
bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions );
9494
void setProperty( int key, const QgsProperty& property );
9595

9696
void setProperty( int key, const QVariant& value );
@@ -137,7 +137,7 @@ class QgsPropertyCollectionStack : QgsAbstractPropertyCollection
137137

138138
QSet<int> propertyKeys() const;
139139
bool hasProperty( int key ) const;
140-
bool writeXml( QDomElement& collectionElem, QDomDocument& doc, const QgsPropertiesDefinition& definitions ) const;
141-
bool readXml( const QDomElement& collectionElem, const QDomDocument& doc, const QgsPropertiesDefinition &definitions );
140+
virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const;
141+
virtual bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions );
142142
};
143143

python/core/qgspropertytransformer.sip

+11-9
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ class QgsCurveTransform
2929

3030
bool writeXml( QDomElement& transformElem, QDomDocument& doc ) const;
3131

32+
QVariant toVariant() const;
33+
34+
bool loadVariant( const QVariant &transformer );
3235
};
3336
class QgsPropertyTransformer
3437
{
@@ -67,10 +70,9 @@ class QgsPropertyTransformer
6770
virtual Type transformerType() const = 0;
6871

6972
virtual QgsPropertyTransformer* clone() = 0 /Factory/;
73+
virtual bool loadVariant(const QVariant &transformer );
7074

71-
virtual bool readXml( const QDomElement& transformerElem, const QDomDocument& doc );
72-
73-
virtual bool writeXml( QDomElement& transformerElem, QDomDocument& doc ) const;
75+
virtual QVariant toVariant() const;
7476

7577
double minValue() const;
7678

@@ -111,8 +113,8 @@ class QgsGenericNumericTransformer : QgsPropertyTransformer
111113

112114
virtual Type transformerType() const;
113115
virtual QgsGenericNumericTransformer* clone() /Factory/;
114-
virtual bool writeXml( QDomElement& transformerElem, QDomDocument& doc ) const;
115-
virtual bool readXml( const QDomElement& transformerElem, const QDomDocument& doc );
116+
virtual QVariant toVariant() const;
117+
virtual bool loadVariant( const QVariant &definition );
116118
virtual QVariant transform( const QgsExpressionContext& context, const QVariant& value ) const;
117119
virtual QString toExpression( const QString& baseExpression ) const;
118120
static QgsGenericNumericTransformer* fromExpression( const QString& expression, QString& baseExpression, QString& fieldName ) /Factory/;
@@ -155,8 +157,8 @@ class QgsSizeScaleTransformer : QgsPropertyTransformer
155157

156158
virtual Type transformerType() const;
157159
virtual QgsSizeScaleTransformer* clone() /Factory/;
158-
virtual bool writeXml( QDomElement& transformerElem, QDomDocument& doc ) const;
159-
virtual bool readXml( const QDomElement& transformerElem, const QDomDocument& doc );
160+
virtual QVariant toVariant() const;
161+
virtual bool loadVariant( const QVariant &definition );
160162
virtual QVariant transform( const QgsExpressionContext& context, const QVariant& value ) const;
161163
virtual QString toExpression( const QString& baseExpression ) const;
162164

@@ -206,8 +208,8 @@ class QgsColorRampTransformer : QgsPropertyTransformer
206208

207209
virtual Type transformerType() const;
208210
virtual QgsColorRampTransformer* clone() /Factory/;
209-
virtual bool writeXml( QDomElement& transformerElem, QDomDocument& doc ) const;
210-
virtual bool readXml( const QDomElement& transformerElem, const QDomDocument& doc );
211+
virtual QVariant toVariant() const;
212+
virtual bool loadVariant( const QVariant &definition );
211213
virtual QVariant transform( const QgsExpressionContext& context, const QVariant& value ) const;
212214
virtual QString toExpression( const QString& baseExpression ) const;
213215

python/core/symbology-ng/qgssymbollayerutils.sip

+2
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ class QgsSymbolLayerUtils
284284

285285
static QgsColorRamp* loadColorRamp( QDomElement& element ) /Factory/;
286286
static QDomElement saveColorRamp( const QString& name, QgsColorRamp* ramp, QDomDocument& doc );
287+
static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
288+
static QgsColorRamp *loadColorRamp( const QVariant &value );
287289

288290
/**
289291
* Returns a friendly display name for a color

python/gui/editorwidgets/core/qgseditorconfigwidget.sip

+6-19
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,16 @@ class QgsEditorConfigWidget : QWidget
2121

2222
public:
2323
explicit QgsEditorConfigWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent /TransferThis/ );
24-
25-
/**
26-
* Returns the field for which this configuration widget applies
27-
*
28-
* @return The field index
29-
*/
30-
int field();
31-
32-
/**
33-
* Returns the layer for which this configuration widget applies
34-
*
35-
* @return The layer
36-
*/
37-
QgsVectorLayer* layer();
38-
3924
virtual QVariantMap config() = 0;
4025
virtual void setConfig( const QVariantMap& config ) = 0;
26+
int field();
27+
QgsVectorLayer* layer();
28+
QgsExpressionContext createExpressionContext() const;
4129

4230
signals:
43-
44-
/** Emitted when the configuration of the widget is changed.
45-
* @note added in QGIS 3.0
46-
*/
4731
void changed();
32+
33+
protected:
34+
void initializeDataDefinedButton( QgsPropertyOverrideButton *button, QgsWidgetWrapper::Property key );
4835
};
4936

python/gui/editorwidgets/core/qgswidgetwrapper.sip

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ class QgsWidgetWrapper : QObject
4747
%End
4848

4949
public:
50+
enum Property
51+
{
52+
RootPath = 0,
53+
};
54+
static const QgsPropertiesDefinition &propertyDefinitions();
5055
/**
5156
* Create a new widget wrapper
5257
*
@@ -131,6 +136,8 @@ class QgsWidgetWrapper : QObject
131136
*/
132137
virtual bool valid() const = 0;
133138

139+
const QgsPropertyCollection &dataDefinedProperties() const;
140+
void setDataDefinedProperties( const QgsPropertyCollection &collection );
134141
protected:
135142
/**
136143
* This method should create a new widget with the provided parent. This will only be called

src/core/composer/qgscomposerobject.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ bool QgsComposerObject::writeXml( QDomElement &elem, QDomDocument &doc ) const
117117
}
118118

119119
QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
120-
mDataDefinedProperties.writeXml( ddPropsElement, doc, sPropertyDefinitions );
120+
mDataDefinedProperties.writeXml( ddPropsElement, sPropertyDefinitions );
121121
elem.appendChild( ddPropsElement );
122122

123123
//custom properties
@@ -140,7 +140,7 @@ bool QgsComposerObject::readXml( const QDomElement &itemElem, const QDomDocument
140140
QDomNode propsNode = itemElem.namedItem( QStringLiteral( "dataDefinedProperties" ) );
141141
if ( !propsNode.isNull() )
142142
{
143-
mDataDefinedProperties.readXml( propsNode.toElement(), doc, sPropertyDefinitions );
143+
mDataDefinedProperties.readXml( propsNode.toElement(), sPropertyDefinitions );
144144
}
145145

146146
//custom properties

src/core/composer/qgscomposition.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ bool QgsComposition::writeXml( QDomElement &composerElem, QDomDocument &doc )
903903

904904
//data defined properties
905905
QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
906-
mDataDefinedProperties.writeXml( ddPropsElement, doc, QgsComposerObject::propertyDefinitions() );
906+
mDataDefinedProperties.writeXml( ddPropsElement, QgsComposerObject::propertyDefinitions() );
907907
compositionElem.appendChild( ddPropsElement );
908908

909909
composerElem.appendChild( compositionElem );
@@ -990,7 +990,7 @@ bool QgsComposition::readXml( const QDomElement &compositionElem, const QDomDocu
990990
QDomNode propsNode = compositionElem.namedItem( QStringLiteral( "dataDefinedProperties" ) );
991991
if ( !propsNode.isNull() )
992992
{
993-
mDataDefinedProperties.readXml( propsNode.toElement(), doc, QgsComposerObject::propertyDefinitions() );
993+
mDataDefinedProperties.readXml( propsNode.toElement(), QgsComposerObject::propertyDefinitions() );
994994
}
995995

996996
//custom properties

src/core/qgsdiagramrenderer.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void QgsDiagramLayerSettings::readXml( const QDomElement &elem, const QgsVectorL
111111
QDomNodeList propertyElems = elem.elementsByTagName( "properties" );
112112
if ( !propertyElems.isEmpty() )
113113
{
114-
( void )mDataDefinedProperties.readXml( propertyElems.at( 0 ).toElement(), elem.ownerDocument(), sPropertyDefinitions );
114+
( void )mDataDefinedProperties.readXml( propertyElems.at( 0 ).toElement(), sPropertyDefinitions );
115115
}
116116
else
117117
{
@@ -154,7 +154,7 @@ void QgsDiagramLayerSettings::writeXml( QDomElement &layerElem, QDomDocument &do
154154

155155
QDomElement diagramLayerElem = doc.createElement( QStringLiteral( "DiagramLayerSettings" ) );
156156
QDomElement propertiesElem = doc.createElement( "properties" );
157-
( void )mDataDefinedProperties.writeXml( propertiesElem, doc, sPropertyDefinitions );
157+
( void )mDataDefinedProperties.writeXml( propertiesElem, sPropertyDefinitions );
158158
diagramLayerElem.appendChild( propertiesElem );
159159
diagramLayerElem.setAttribute( QStringLiteral( "placement" ), mPlacement );
160160
diagramLayerElem.setAttribute( QStringLiteral( "linePlacementFlags" ), mPlacementFlags );

src/core/qgspallabeling.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer *layer )
669669
QDomDocument doc( QStringLiteral( "dd" ) );
670670
doc.setContent( layer->customProperty( QStringLiteral( "labeling/ddProperties" ) ).toString() );
671671
QDomElement elem = doc.firstChildElement( QStringLiteral( "properties" ) );
672-
mDataDefinedProperties.readXml( elem, doc, sPropertyDefinitions );
672+
mDataDefinedProperties.readXml( elem, sPropertyDefinitions );
673673
}
674674
else
675675
{
@@ -760,7 +760,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer *layer )
760760

761761
doc = QDomDocument( QStringLiteral( "dd" ) );
762762
QDomElement ddElem = doc.createElement( QStringLiteral( "properties" ) );
763-
mDataDefinedProperties.writeXml( ddElem, doc, sPropertyDefinitions );
763+
mDataDefinedProperties.writeXml( ddElem, sPropertyDefinitions );
764764
QString ddProps;
765765
QTextStream streamProps( &ddProps );
766766
ddElem.save( streamProps, -1 );
@@ -876,7 +876,7 @@ void QgsPalLayerSettings::readXml( QDomElement &elem )
876876
QDomElement ddElem = elem.firstChildElement( QStringLiteral( "dd_properties" ) );
877877
if ( !ddElem.isNull() )
878878
{
879-
mDataDefinedProperties.readXml( ddElem, ddElem.ownerDocument(), sPropertyDefinitions );
879+
mDataDefinedProperties.readXml( ddElem, sPropertyDefinitions );
880880
}
881881
else
882882
{
@@ -965,7 +965,7 @@ QDomElement QgsPalLayerSettings::writeXml( QDomDocument &doc )
965965
renderingElem.setAttribute( QStringLiteral( "zIndex" ), zIndex );
966966

967967
QDomElement ddElem = doc.createElement( QStringLiteral( "dd_properties" ) );
968-
mDataDefinedProperties.writeXml( ddElem, doc, sPropertyDefinitions );
968+
mDataDefinedProperties.writeXml( ddElem, sPropertyDefinitions );
969969

970970
QDomElement elem = doc.createElement( QStringLiteral( "settings" ) );
971971
elem.appendChild( textStyleElem );

src/core/qgsproperty.cpp

+34-24
Original file line numberDiff line numberDiff line change
@@ -636,24 +636,26 @@ bool QgsProperty::valueAsBool( const QgsExpressionContext &context, bool default
636636
return val.toBool();
637637
}
638638

639-
bool QgsProperty::writeXml( QDomElement &propertyElem, QDomDocument &doc ) const
639+
QVariant QgsProperty::toVariant() const
640640
{
641-
propertyElem.setAttribute( "active", d->active ? "1" : "0" );
642-
propertyElem.setAttribute( "type", d->type );
641+
QVariantMap propertyMap;
642+
643+
propertyMap.insert( QStringLiteral( "active" ), d->active );
644+
propertyMap.insert( QStringLiteral( "type" ), d->type );
643645

644646
switch ( d->type )
645647
{
646648
case StaticProperty:
647-
propertyElem.setAttribute( "valType", d->staticValue.typeName() );
648-
propertyElem.setAttribute( "val", d->staticValue.toString() );
649+
// propertyMap.insert( QStringLiteral( "valType" ), d->staticValue.typeName() );
650+
propertyMap.insert( QStringLiteral( "val" ), d->staticValue.toString() );
649651
break;
650652

651653
case FieldBasedProperty:
652-
propertyElem.setAttribute( "field", d->fieldName );
654+
propertyMap.insert( QStringLiteral( "field" ), d->fieldName );
653655
break;
654656

655657
case ExpressionBasedProperty:
656-
propertyElem.setAttribute( "expression", d->expressionString );
658+
propertyMap.insert( QStringLiteral( "expression" ), d->expressionString );
657659
break;
658660

659661
case InvalidProperty:
@@ -662,36 +664,39 @@ bool QgsProperty::writeXml( QDomElement &propertyElem, QDomDocument &doc ) const
662664

663665
if ( d->transformer )
664666
{
665-
QDomElement transformerElem = doc.createElement( "transformer" );
666-
transformerElem.setAttribute( "t", static_cast< int >( d->transformer->transformerType() ) );
667-
if ( d->transformer->writeXml( transformerElem, doc ) )
668-
propertyElem.appendChild( transformerElem );
667+
QVariantMap transformer;
668+
transformer.insert( QStringLiteral( "t" ), d->transformer->transformerType() );
669+
transformer.insert( QStringLiteral( "d" ), d->transformer->toVariant() );
670+
671+
propertyMap.insert( QStringLiteral( "transformer" ), transformer );
669672
}
670673

671-
return true;
674+
return propertyMap;
672675
}
673676

674-
bool QgsProperty::readXml( const QDomElement &propertyElem, const QDomDocument &doc )
677+
bool QgsProperty::loadVariant( const QVariant &property )
675678
{
679+
QVariantMap propertyMap = property.toMap();
680+
676681
d.detach();
677-
d->active = static_cast< bool >( propertyElem.attribute( "active", "1" ).toInt() );
678-
d->type = static_cast< Type >( propertyElem.attribute( "type", "0" ).toInt() );
682+
d->active = propertyMap.value( QStringLiteral( "active" ) ).toBool();
683+
d->type = static_cast< Type >( propertyMap.value( QStringLiteral( "type" ), InvalidProperty ).toInt() );
679684

680685
switch ( d->type )
681686
{
682687
case StaticProperty:
683-
d->staticValue = QVariant( propertyElem.attribute( "val", "" ) );
684-
d->staticValue.convert( QVariant::nameToType( propertyElem.attribute( "valType", "QString" ).toLocal8Bit().constData() ) );
688+
d->staticValue = propertyMap.value( QStringLiteral( "val" ) );
689+
// d->staticValue.convert( QVariant::nameToType( propertyElem.attribute( "valType", "QString" ).toLocal8Bit().constData() ) );
685690
break;
686691

687692
case FieldBasedProperty:
688-
d->fieldName = propertyElem.attribute( "field" );
693+
d->fieldName = propertyMap.value( QStringLiteral( "field" ) ).toString();
689694
if ( d->fieldName.isEmpty() )
690695
d->active = false;
691696
break;
692697

693698
case ExpressionBasedProperty:
694-
d->expressionString = propertyElem.attribute( "expression" );
699+
d->expressionString = propertyMap.value( QStringLiteral( "expression" ) ).toString();
695700
if ( d->expressionString.isEmpty() )
696701
d->active = false;
697702

@@ -709,15 +714,20 @@ bool QgsProperty::readXml( const QDomElement &propertyElem, const QDomDocument &
709714
if ( d->transformer )
710715
delete d->transformer;
711716
d->transformer = nullptr;
712-
QDomNodeList transformerNodeList = propertyElem.elementsByTagName( "transformer" );
713-
if ( !transformerNodeList.isEmpty() )
717+
718+
719+
QVariant transform = propertyMap.value( QStringLiteral( "transformer" ) );
720+
721+
if ( transform.isValid() )
714722
{
715-
QDomElement transformerElem = transformerNodeList.at( 0 ).toElement();
716-
QgsPropertyTransformer::Type type = static_cast< QgsPropertyTransformer::Type >( transformerElem.attribute( "t", "0" ).toInt() );
723+
QVariantMap transformerMap = transform.toMap();
724+
725+
QgsPropertyTransformer::Type type = static_cast< QgsPropertyTransformer::Type >( transformerMap.value( QStringLiteral( "t" ), QgsPropertyTransformer::GenericNumericTransformer ).toInt() );
717726
std::unique_ptr< QgsPropertyTransformer > transformer( QgsPropertyTransformer::create( type ) );
727+
718728
if ( transformer )
719729
{
720-
if ( transformer->readXml( transformerElem, doc ) )
730+
if ( transformer->loadVariant( transformerMap.value( QStringLiteral( "d" ) ) ) )
721731
d->transformer = transformer.release();
722732
}
723733
}

0 commit comments

Comments
 (0)