Skip to content
Permalink
Browse files

Save QgsProperty related objects via QVariants

  • Loading branch information
m-kuhn committed Mar 9, 2017
1 parent 1687019 commit 01705800198833a574e036ecb9890336c58c6dfc
@@ -128,10 +128,8 @@ class QgsProperty
int valueAsInt( const QgsExpressionContext& context, int defaultValue = 0, bool* ok /Out/ = nullptr ) const;

bool valueAsBool( const QgsExpressionContext& context, bool defaultValue = false, bool* ok /Out/ = nullptr ) const;

virtual bool writeXml( QDomElement& propertyElem, QDomDocument& doc ) const;

virtual bool readXml( const QDomElement& propertyElem, const QDomDocument& doc );
QVariant toVariant() const;
bool loadVariant( const QVariant &property );

void setTransformer( QgsPropertyTransformer* transformer /Transfer/ );

@@ -53,10 +53,10 @@ class QgsAbstractPropertyCollection

virtual bool hasDynamicProperties() const = 0;

virtual bool writeXml( QDomElement& collectionElem, QDomDocument& doc, const QgsPropertiesDefinition& definitions ) const = 0;

virtual bool readXml( const QDomElement& collectionElem, const QDomDocument& doc, const QgsPropertiesDefinition &definitions ) = 0;

virtual bool writeXml( QDomElement& collectionElem, const QgsPropertiesDefinition& definitions ) const = 0;
virtual bool readXml( const QDomElement& collectionElem, const QgsPropertiesDefinition &definitions ) = 0;
virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const = 0;
virtual bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions ) = 0;
};


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

QVariant toVariant( const QgsPropertiesDefinition &definitions ) const;
bool loadVariant( const QVariant &configuration, const QgsPropertiesDefinition &definitions );
void setProperty( int key, const QgsProperty& property );

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

QSet<int> propertyKeys() const;
bool hasProperty( int key ) const;
bool writeXml( QDomElement& collectionElem, QDomDocument& doc, const QgsPropertiesDefinition& definitions ) const;
bool readXml( const QDomElement& collectionElem, const QDomDocument& doc, const QgsPropertiesDefinition &definitions );
virtual QVariant toVariant( const QgsPropertiesDefinition &definitions ) const;
virtual bool loadVariant( const QVariant &collection, const QgsPropertiesDefinition &definitions );
};

@@ -29,6 +29,9 @@ class QgsCurveTransform

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

QVariant toVariant() const;

bool loadVariant( const QVariant &transformer );
};
class QgsPropertyTransformer
{
@@ -67,10 +70,9 @@ class QgsPropertyTransformer
virtual Type transformerType() const = 0;

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

virtual bool readXml( const QDomElement& transformerElem, const QDomDocument& doc );

virtual bool writeXml( QDomElement& transformerElem, QDomDocument& doc ) const;
virtual QVariant toVariant() const;

double minValue() const;

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

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

virtual Type transformerType() const;
virtual QgsSizeScaleTransformer* clone() /Factory/;
virtual bool writeXml( QDomElement& transformerElem, QDomDocument& doc ) const;
virtual bool readXml( const QDomElement& transformerElem, const QDomDocument& doc );
virtual QVariant toVariant() const;
virtual bool loadVariant( const QVariant &definition );
virtual QVariant transform( const QgsExpressionContext& context, const QVariant& value ) const;
virtual QString toExpression( const QString& baseExpression ) const;

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

virtual Type transformerType() const;
virtual QgsColorRampTransformer* clone() /Factory/;
virtual bool writeXml( QDomElement& transformerElem, QDomDocument& doc ) const;
virtual bool readXml( const QDomElement& transformerElem, const QDomDocument& doc );
virtual QVariant toVariant() const;
virtual bool loadVariant( const QVariant &definition );
virtual QVariant transform( const QgsExpressionContext& context, const QVariant& value ) const;
virtual QString toExpression( const QString& baseExpression ) const;

@@ -284,6 +284,8 @@ class QgsSymbolLayerUtils

static QgsColorRamp* loadColorRamp( QDomElement& element ) /Factory/;
static QDomElement saveColorRamp( const QString& name, QgsColorRamp* ramp, QDomDocument& doc );
static QVariant colorRampToVariant( const QString &name, QgsColorRamp *ramp );
static QgsColorRamp *loadColorRamp( const QVariant &value );

/**
* Returns a friendly display name for a color
@@ -21,29 +21,16 @@ class QgsEditorConfigWidget : QWidget

public:
explicit QgsEditorConfigWidget( QgsVectorLayer* vl, int fieldIdx, QWidget* parent /TransferThis/ );

/**
* Returns the field for which this configuration widget applies
*
* @return The field index
*/
int field();

/**
* Returns the layer for which this configuration widget applies
*
* @return The layer
*/
QgsVectorLayer* layer();

virtual QVariantMap config() = 0;
virtual void setConfig( const QVariantMap& config ) = 0;
int field();
QgsVectorLayer* layer();
QgsExpressionContext createExpressionContext() const;

signals:

/** Emitted when the configuration of the widget is changed.
* @note added in QGIS 3.0
*/
void changed();

protected:
void initializeDataDefinedButton( QgsPropertyOverrideButton *button, QgsWidgetWrapper::Property key );
};

@@ -47,6 +47,11 @@ class QgsWidgetWrapper : QObject
%End

public:
enum Property
{
RootPath = 0,
};
static const QgsPropertiesDefinition &propertyDefinitions();
/**
* Create a new widget wrapper
*
@@ -131,6 +136,8 @@ class QgsWidgetWrapper : QObject
*/
virtual bool valid() const = 0;

const QgsPropertyCollection &dataDefinedProperties() const;
void setDataDefinedProperties( const QgsPropertyCollection &collection );
protected:
/**
* This method should create a new widget with the provided parent. This will only be called
@@ -117,7 +117,7 @@ bool QgsComposerObject::writeXml( QDomElement &elem, QDomDocument &doc ) const
}

QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
mDataDefinedProperties.writeXml( ddPropsElement, doc, sPropertyDefinitions );
mDataDefinedProperties.writeXml( ddPropsElement, sPropertyDefinitions );
elem.appendChild( ddPropsElement );

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

//custom properties
@@ -903,7 +903,7 @@ bool QgsComposition::writeXml( QDomElement &composerElem, QDomDocument &doc )

//data defined properties
QDomElement ddPropsElement = doc.createElement( QStringLiteral( "dataDefinedProperties" ) );
mDataDefinedProperties.writeXml( ddPropsElement, doc, QgsComposerObject::propertyDefinitions() );
mDataDefinedProperties.writeXml( ddPropsElement, QgsComposerObject::propertyDefinitions() );
compositionElem.appendChild( ddPropsElement );

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

//custom properties
@@ -111,7 +111,7 @@ void QgsDiagramLayerSettings::readXml( const QDomElement &elem, const QgsVectorL
QDomNodeList propertyElems = elem.elementsByTagName( "properties" );
if ( !propertyElems.isEmpty() )
{
( void )mDataDefinedProperties.readXml( propertyElems.at( 0 ).toElement(), elem.ownerDocument(), sPropertyDefinitions );
( void )mDataDefinedProperties.readXml( propertyElems.at( 0 ).toElement(), sPropertyDefinitions );
}
else
{
@@ -154,7 +154,7 @@ void QgsDiagramLayerSettings::writeXml( QDomElement &layerElem, QDomDocument &do

QDomElement diagramLayerElem = doc.createElement( QStringLiteral( "DiagramLayerSettings" ) );
QDomElement propertiesElem = doc.createElement( "properties" );
( void )mDataDefinedProperties.writeXml( propertiesElem, doc, sPropertyDefinitions );
( void )mDataDefinedProperties.writeXml( propertiesElem, sPropertyDefinitions );
diagramLayerElem.appendChild( propertiesElem );
diagramLayerElem.setAttribute( QStringLiteral( "placement" ), mPlacement );
diagramLayerElem.setAttribute( QStringLiteral( "linePlacementFlags" ), mPlacementFlags );
@@ -669,7 +669,7 @@ void QgsPalLayerSettings::readFromLayer( QgsVectorLayer *layer )
QDomDocument doc( QStringLiteral( "dd" ) );
doc.setContent( layer->customProperty( QStringLiteral( "labeling/ddProperties" ) ).toString() );
QDomElement elem = doc.firstChildElement( QStringLiteral( "properties" ) );
mDataDefinedProperties.readXml( elem, doc, sPropertyDefinitions );
mDataDefinedProperties.readXml( elem, sPropertyDefinitions );
}
else
{
@@ -760,7 +760,7 @@ void QgsPalLayerSettings::writeToLayer( QgsVectorLayer *layer )

doc = QDomDocument( QStringLiteral( "dd" ) );
QDomElement ddElem = doc.createElement( QStringLiteral( "properties" ) );
mDataDefinedProperties.writeXml( ddElem, doc, sPropertyDefinitions );
mDataDefinedProperties.writeXml( ddElem, sPropertyDefinitions );
QString ddProps;
QTextStream streamProps( &ddProps );
ddElem.save( streamProps, -1 );
@@ -876,7 +876,7 @@ void QgsPalLayerSettings::readXml( QDomElement &elem )
QDomElement ddElem = elem.firstChildElement( QStringLiteral( "dd_properties" ) );
if ( !ddElem.isNull() )
{
mDataDefinedProperties.readXml( ddElem, ddElem.ownerDocument(), sPropertyDefinitions );
mDataDefinedProperties.readXml( ddElem, sPropertyDefinitions );
}
else
{
@@ -965,7 +965,7 @@ QDomElement QgsPalLayerSettings::writeXml( QDomDocument &doc )
renderingElem.setAttribute( QStringLiteral( "zIndex" ), zIndex );

QDomElement ddElem = doc.createElement( QStringLiteral( "dd_properties" ) );
mDataDefinedProperties.writeXml( ddElem, doc, sPropertyDefinitions );
mDataDefinedProperties.writeXml( ddElem, sPropertyDefinitions );

QDomElement elem = doc.createElement( QStringLiteral( "settings" ) );
elem.appendChild( textStyleElem );
@@ -636,24 +636,26 @@ bool QgsProperty::valueAsBool( const QgsExpressionContext &context, bool default
return val.toBool();
}

bool QgsProperty::writeXml( QDomElement &propertyElem, QDomDocument &doc ) const
QVariant QgsProperty::toVariant() const
{
propertyElem.setAttribute( "active", d->active ? "1" : "0" );
propertyElem.setAttribute( "type", d->type );
QVariantMap propertyMap;

propertyMap.insert( QStringLiteral( "active" ), d->active );
propertyMap.insert( QStringLiteral( "type" ), d->type );

switch ( d->type )
{
case StaticProperty:
propertyElem.setAttribute( "valType", d->staticValue.typeName() );
propertyElem.setAttribute( "val", d->staticValue.toString() );
// propertyMap.insert( QStringLiteral( "valType" ), d->staticValue.typeName() );
propertyMap.insert( QStringLiteral( "val" ), d->staticValue.toString() );
break;

case FieldBasedProperty:
propertyElem.setAttribute( "field", d->fieldName );
propertyMap.insert( QStringLiteral( "field" ), d->fieldName );
break;

case ExpressionBasedProperty:
propertyElem.setAttribute( "expression", d->expressionString );
propertyMap.insert( QStringLiteral( "expression" ), d->expressionString );
break;

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

if ( d->transformer )
{
QDomElement transformerElem = doc.createElement( "transformer" );
transformerElem.setAttribute( "t", static_cast< int >( d->transformer->transformerType() ) );
if ( d->transformer->writeXml( transformerElem, doc ) )
propertyElem.appendChild( transformerElem );
QVariantMap transformer;
transformer.insert( QStringLiteral( "t" ), d->transformer->transformerType() );
transformer.insert( QStringLiteral( "d" ), d->transformer->toVariant() );

propertyMap.insert( QStringLiteral( "transformer" ), transformer );
}

return true;
return propertyMap;
}

bool QgsProperty::readXml( const QDomElement &propertyElem, const QDomDocument &doc )
bool QgsProperty::loadVariant( const QVariant &property )
{
QVariantMap propertyMap = property.toMap();

d.detach();
d->active = static_cast< bool >( propertyElem.attribute( "active", "1" ).toInt() );
d->type = static_cast< Type >( propertyElem.attribute( "type", "0" ).toInt() );
d->active = propertyMap.value( QStringLiteral( "active" ) ).toBool();
d->type = static_cast< Type >( propertyMap.value( QStringLiteral( "type" ), InvalidProperty ).toInt() );

switch ( d->type )
{
case StaticProperty:
d->staticValue = QVariant( propertyElem.attribute( "val", "" ) );
d->staticValue.convert( QVariant::nameToType( propertyElem.attribute( "valType", "QString" ).toLocal8Bit().constData() ) );
d->staticValue = propertyMap.value( QStringLiteral( "val" ) );
// d->staticValue.convert( QVariant::nameToType( propertyElem.attribute( "valType", "QString" ).toLocal8Bit().constData() ) );
break;

case FieldBasedProperty:
d->fieldName = propertyElem.attribute( "field" );
d->fieldName = propertyMap.value( QStringLiteral( "field" ) ).toString();
if ( d->fieldName.isEmpty() )
d->active = false;
break;

case ExpressionBasedProperty:
d->expressionString = propertyElem.attribute( "expression" );
d->expressionString = propertyMap.value( QStringLiteral( "expression" ) ).toString();
if ( d->expressionString.isEmpty() )
d->active = false;

@@ -709,15 +714,20 @@ bool QgsProperty::readXml( const QDomElement &propertyElem, const QDomDocument &
if ( d->transformer )
delete d->transformer;
d->transformer = nullptr;
QDomNodeList transformerNodeList = propertyElem.elementsByTagName( "transformer" );
if ( !transformerNodeList.isEmpty() )


QVariant transform = propertyMap.value( QStringLiteral( "transformer" ) );

if ( transform.isValid() )
{
QDomElement transformerElem = transformerNodeList.at( 0 ).toElement();
QgsPropertyTransformer::Type type = static_cast< QgsPropertyTransformer::Type >( transformerElem.attribute( "t", "0" ).toInt() );
QVariantMap transformerMap = transform.toMap();

QgsPropertyTransformer::Type type = static_cast< QgsPropertyTransformer::Type >( transformerMap.value( QStringLiteral( "t" ), QgsPropertyTransformer::GenericNumericTransformer ).toInt() );
std::unique_ptr< QgsPropertyTransformer > transformer( QgsPropertyTransformer::create( type ) );

if ( transformer )
{
if ( transformer->readXml( transformerElem, doc ) )
if ( transformer->loadVariant( transformerMap.value( QStringLiteral( "d" ) ) ) )
d->transformer = transformer.release();
}
}

0 comments on commit 0170580

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