Skip to content

Commit

Permalink
Handle data defined properties upgrade when reading pre-3.20 project …
Browse files Browse the repository at this point in the history
…files
  • Loading branch information
nirvn committed May 4, 2021
1 parent 4fad4ba commit 672d53e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 2 deletions.
Expand Up @@ -668,7 +668,20 @@ Write settings into a DOM element.
%Docstring
Updates the format by evaluating current values of data defined properties.

.. note::

Since QGIS 3.20, data defined fill color, stroke color, stroke width, and
pen join style will only modify SVG backgrounds. For other background types, the
data defined properties within symbols are to be used.

.. versionadded:: 3.10
%End

void upgradeDataDefinedProperties( QgsPropertyCollection &properties );
%Docstring
Upgrade data defined properties when reading a project file saved in QGIS prior to version 3.20.

.. versionadded:: 3.20
%End

QSet<QString> referencedFields( const QgsRenderContext &context ) const;
Expand Down
44 changes: 42 additions & 2 deletions src/core/textrenderer/qgstextbackgroundsettings.cpp
Expand Up @@ -302,9 +302,9 @@ QColor QgsTextBackgroundSettings::fillColor() const
void QgsTextBackgroundSettings::setFillColor( const QColor &color )
{
d->fillColor = color;
if ( d->fillSymbol )
if ( d->fillSymbol && d->fillSymbol->symbolLayers().at( 0 )->layerType() == QStringLiteral( "SimpleFill" ) )
{
d->fillSymbol->setColor( color );
static_cast< QgsSimpleFillSymbolLayer * >( d->fillSymbol->symbolLayers().at( 0 ) )->setColor( color );
}
}

Expand Down Expand Up @@ -718,6 +718,44 @@ QDomElement QgsTextBackgroundSettings::writeXml( QDomDocument &doc, const QgsRea
return backgroundElem;
}

void QgsTextBackgroundSettings::upgradeDataDefinedProperties( QgsPropertyCollection &properties )
{
if ( !d->fillSymbol || d->fillSymbol->symbolLayers().at( 0 )->layerType() != QStringLiteral( "SimpleFill" ) )
return;
QgsSimpleFillSymbolLayer *fill = static_cast< QgsSimpleFillSymbolLayer * >( d->fillSymbol->symbolLayers().at( 0 ) );

if ( d->type != QgsTextBackgroundSettings::ShapeSVG )
{
if ( properties.hasProperty( QgsPalLayerSettings::ShapeFillColor ) &&
!fill->dataDefinedProperties().hasProperty( QgsSymbolLayer::PropertyFillColor ) )
{
fill->dataDefinedProperties().setProperty( QgsSymbolLayer::PropertyFillColor, properties.property( QgsPalLayerSettings::ShapeFillColor ) );
properties.setProperty( QgsPalLayerSettings::ShapeFillColor, QgsProperty() );
}

if ( properties.hasProperty( QgsPalLayerSettings::ShapeStrokeColor ) &&
!fill->dataDefinedProperties().hasProperty( QgsSymbolLayer::PropertyStrokeColor ) )
{
fill->dataDefinedProperties().setProperty( QgsSymbolLayer::PropertyStrokeColor, properties.property( QgsPalLayerSettings::ShapeStrokeColor ) );
properties.setProperty( QgsPalLayerSettings::ShapeStrokeColor, QgsProperty() );
}

if ( properties.hasProperty( QgsPalLayerSettings::ShapeStrokeWidth ) &&
!fill->dataDefinedProperties().hasProperty( QgsSymbolLayer::PropertyStrokeWidth ) )
{
fill->dataDefinedProperties().setProperty( QgsSymbolLayer::PropertyStrokeWidth, properties.property( QgsPalLayerSettings::ShapeStrokeWidth ) );
properties.setProperty( QgsPalLayerSettings::ShapeStrokeWidth, QgsProperty() );
}

if ( properties.hasProperty( QgsPalLayerSettings::ShapeJoinStyle ) &&
!fill->dataDefinedProperties().hasProperty( QgsSymbolLayer::PropertyJoinStyle ) )
{
fill->dataDefinedProperties().setProperty( QgsSymbolLayer::PropertyJoinStyle, properties.property( QgsPalLayerSettings::ShapeJoinStyle ) );
properties.setProperty( QgsPalLayerSettings::ShapeJoinStyle, QgsProperty() );
}
}
}

void QgsTextBackgroundSettings::updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties )
{
if ( properties.isActive( QgsPalLayerSettings::ShapeDraw ) )
Expand Down Expand Up @@ -847,6 +885,8 @@ void QgsTextBackgroundSettings::updateDataDefinedProperties( QgsRenderContext &c
d->opacity = properties.value( QgsPalLayerSettings::ShapeOpacity, context.expressionContext(), d->opacity * 100 ).toDouble() / 100.0;
}

// for non-SVG background types, those data defined properties will not having an impact,
// instead use data defined properties within symbols
if ( properties.isActive( QgsPalLayerSettings::ShapeFillColor ) )
{
context.expressionContext().setOriginalValueVariable( QgsSymbolLayerUtils::encodeColor( d->fillColor ) );
Expand Down
9 changes: 9 additions & 0 deletions src/core/textrenderer/qgstextbackgroundsettings.h
Expand Up @@ -544,10 +544,19 @@ class CORE_EXPORT QgsTextBackgroundSettings

/**
* Updates the format by evaluating current values of data defined properties.
* \note Since QGIS 3.20, data defined fill color, stroke color, stroke width, and
* pen join style will only modify SVG backgrounds. For other background types, the
* data defined properties within symbols are to be used.
* \since QGIS 3.10
*/
void updateDataDefinedProperties( QgsRenderContext &context, const QgsPropertyCollection &properties );

/**
* Upgrade data defined properties when reading a project file saved in QGIS prior to version 3.20.
* \since QGIS 3.20
*/
void upgradeDataDefinedProperties( QgsPropertyCollection &properties );

/**
* Returns all field names referenced by the configuration (e.g. from data defined properties).
* \since QGIS 3.14
Expand Down
1 change: 1 addition & 0 deletions src/core/textrenderer/qgstextformat.cpp
Expand Up @@ -582,6 +582,7 @@ void QgsTextFormat::readXml( const QDomElement &elem, const QgsReadWriteContext
if ( !ddElem.isNull() )
{
d->mDataDefinedProperties.readXml( ddElem, QgsPalLayerSettings::propertyDefinitions() );
mBackgroundSettings.upgradeDataDefinedProperties( d->mDataDefinedProperties );
}
else
{
Expand Down

0 comments on commit 672d53e

Please sign in to comment.