Skip to content
Permalink
Browse files

use QgsProperty to handle SVG parameters

  • Loading branch information
3nids committed Jan 6, 2021
1 parent 9116a1a commit 3201fa4ecb7e465a5c9ae3135bded767b1458eae
@@ -656,14 +656,14 @@ if the value set is lower or equal to 0 the aspect ratio will be preserved in re
double strokeWidth() const;
void setStrokeWidth( double w );

QgsSvgParameters parameters() const;
QMap<QString, QgsProperty> parameters() const;
%Docstring
Returns the dynamic SVG parameters

.. versionadded:: 3.18
%End

void setParameters( const QgsSvgParameters &parameters );
void setParameters( const QMap<QString, QgsProperty> &parameters );
%Docstring
Sets the dynamic SVG parameters

@@ -49,7 +49,7 @@ Constructor for QgsSvgCache.

QImage svgAsImage( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
double widthScaleFactor, bool &fitsInCache, double fixedAspectRatio = 0, bool blocking = false,
const QgsSvgParameters &parameters = QgsSvgParameters() );
const QMap<QString, QString> &parameters = QMap<QString, QString>() );
%Docstring
Returns an SVG drawing as a QImage.

@@ -72,7 +72,7 @@ Returns an SVG drawing as a QImage.

QPicture svgAsPicture( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
double widthScaleFactor, bool forceVectorOutput = false, double fixedAspectRatio = 0, bool blocking = false,
const QgsSvgParameters &parameters = QgsSvgParameters() );
const QMap<QString, QString> &parameters = QMap<QString, QString>() );
%Docstring
Returns an SVG drawing as a QPicture.

@@ -102,7 +102,7 @@ Returns an SVG drawing as a QPicture.
%End

QSizeF svgViewboxSize( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false, const QgsSvgParameters &parameters = QgsSvgParameters() );
double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false, const QMap<QString, QString> &parameters = QMap<QString, QString>() );
%Docstring
Calculates the viewbox size of a (possibly cached) SVG file.

@@ -195,7 +195,7 @@ in the same thread to ensure provided the remote content.
%End

QByteArray svgContent( const QString &path, double size, const QColor &fill, const QColor &stroke, double strokeWidth,
double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false, const QgsSvgParameters &parameters = QgsSvgParameters() );
double widthScaleFactor, double fixedAspectRatio = 0, bool blocking = false, const QMap<QString, QString> &parameters = QMap<QString, QString>() );
%Docstring
Gets the SVG content corresponding to the given ``path``.

This file was deleted.

@@ -869,6 +869,44 @@ Creates a new symbol with size restricted to min/max size if original size is ou
:param height: expected height, can be changed by this function

:return: 0 if size is within minSize/maxSize range. New symbol if size was out of min/max range. Caller takes ownership
%End

static QgsStringMap evaluatePropertiesMap( const QMap<QString, QgsProperty> &propertiesMap, const QgsExpressionContext &context );
%Docstring
Evaluates a map of properties using the given ``context`` and returns a variant map with evaluated expressions from the properties.

.. versionadded:: 3.18
%End

static QMap<QString, QString> serializeProperties( const QMap<QString, QgsProperty> &propertiesMap );
%Docstring
Serialize a map of properties
{"key1": "value1", "key2": "value2"}
becomes
{
"properties_0_name": "key1", "properties_0_value": "value1",
"properties_1_name": "key2", "properties_1_value": "value2",
}

.. seealso:: :py:func:`readSerializedProperties`

.. versionadded:: 3.18
%End

static QMap<QString, QgsProperty> readSerializedProperties( const QMap<QString, QString> &serializedProperties );
%Docstring
Reads a serialized map of properties
It expects
{
"properties_0_name": "key1", "properties_0_value": "value1",
"properties_1_name": "key2", "properties_1_value": "value2",
}
which will return
{"key1": "value1", "key2": "value2"}

.. seealso:: :py:func:`serializeProperties`

.. versionadded:: 3.18
%End
};

@@ -570,7 +570,6 @@
%Include auto_generated/symbology/qgsstyleentityvisitor.sip
%Include auto_generated/symbology/qgsstylemodel.sip
%Include auto_generated/symbology/qgssvgcache.sip
%Include auto_generated/symbology/qgssvgparameter.sip
%Include auto_generated/symbology/qgssymbol.sip
%Include auto_generated/symbology/qgssymbollayer.sip
%Include auto_generated/symbology/qgssymbollayerreference.sip
@@ -1502,7 +1502,6 @@ set(QGIS_CORE_HDRS
symbology/qgsstyleentityvisitor.h
symbology/qgsstylemodel.h
symbology/qgssvgcache.h
symbology/qgssvgparameter.h
symbology/qgssymbol.h
symbology/qgssymbollayer.h
symbology/qgssymbollayerreference.h
@@ -473,7 +473,7 @@ void QgsLayoutItemPicture::loadPictureUsingCache( const QString &path )
QColor strokeColor = mDataDefinedProperties.valueAsColor( QgsLayoutObject::PictureSvgStrokeColor, context, mSvgStrokeColor );
double strokeWidth = mDataDefinedProperties.valueAsDouble( QgsLayoutObject::PictureSvgStrokeWidth, context, mSvgStrokeWidth );
// TODO parameters (handle this in the gui part)
QgsSvgParameters parameters;
QMap<QString, QString> parameters;
bool isMissingImage = false;
const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( path, rect().width(), fillColor, strokeColor, strokeWidth,
1.0, 0, false, parameters, &isMissingImage );
@@ -1881,6 +1881,8 @@ QgsSymbolLayer *QgsSvgMarkerSymbolLayer::create( const QVariantMap &props )

m->updateDefaultAspectRatio();

m->setParameters( QgsSymbolLayerUtils::readSerializedProperties( props ) );

return m;
}

@@ -2016,6 +2018,8 @@ void QgsSvgMarkerSymbolLayer::renderPoint( QPointF point, QgsSymbolRenderContext
double aspectRatio = calculateAspectRatio( context, scaledWidth, hasDataDefinedAspectRatio );
double scaledHeight = scaledWidth * ( !qgsDoubleNear( aspectRatio, 0.0 ) ? aspectRatio : mDefaultAspectRatio );

QgsStringMap evaluatedParameters = QgsSymbolLayerUtils::evaluatePropertiesMap( mParameters, context.renderContext().expressionContext() );

double strokeWidth = mStrokeWidth;
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyStrokeWidth ) )
{
@@ -2053,7 +2057,7 @@ void QgsSvgMarkerSymbolLayer::renderPoint( QPointF point, QgsSymbolRenderContext
// adjust height of data defined path
QSizeF svgViewbox = QgsApplication::svgCache()->svgViewboxSize( path, scaledWidth, fillColor, strokeColor, strokeWidth,
context.renderContext().scaleFactor(), aspectRatio,
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ) );
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ), evaluatedParameters );
scaledHeight = svgViewbox.isValid() ? scaledWidth * svgViewbox.height() / svgViewbox.width() : scaledWidth;
}
}
@@ -2075,7 +2079,7 @@ void QgsSvgMarkerSymbolLayer::renderPoint( QPointF point, QgsSymbolRenderContext
{
QImage img = QgsApplication::svgCache()->svgAsImage( path, width, fillColor, strokeColor, strokeWidth,
context.renderContext().scaleFactor(), fitsInCache, aspectRatio,
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ) );
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ), evaluatedParameters );
if ( fitsInCache && img.width() > 1 )
{
usePict = false;
@@ -2102,7 +2106,7 @@ void QgsSvgMarkerSymbolLayer::renderPoint( QPointF point, QgsSymbolRenderContext
p->setOpacity( context.opacity() );
QPicture pct = QgsApplication::svgCache()->svgAsPicture( path, width, fillColor, strokeColor, strokeWidth,
context.renderContext().scaleFactor(), context.renderContext().forceVectorOutput(), aspectRatio,
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ) );
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ), evaluatedParameters );
if ( pct.width() > 1 )
{
QgsScopedQPainterState painterPictureState( p );
@@ -2251,6 +2255,16 @@ QVariantMap QgsSvgMarkerSymbolLayer::properties() const
map[QStringLiteral( "outline_width_map_unit_scale" )] = QgsSymbolLayerUtils::encodeMapUnitScale( mStrokeWidthMapUnitScale );
map[QStringLiteral( "horizontal_anchor_point" )] = QString::number( mHorizontalAnchorPoint );
map[QStringLiteral( "vertical_anchor_point" )] = QString::number( mVerticalAnchorPoint );
const QgsStringMap serializedParameters = QgsSymbolLayerUtils::serializeProperties( mParameters );
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
for ( QgsStringMap::const_iterator parameterIt = serializedParameters.constBegin(); parameterIt != serializedParameters.constEnd(); ++parameterIt )
{
map.insert( parameterIt.key(), parameterIt.value() );
}
#else
map.insert( serializedParameters );
#endif

return map;
}

@@ -2277,6 +2291,7 @@ QgsSvgMarkerSymbolLayer *QgsSvgMarkerSymbolLayer::clone() const
m->setSizeMapUnitScale( mSizeMapUnitScale );
m->setHorizontalAnchorPoint( mHorizontalAnchorPoint );
m->setVerticalAnchorPoint( mVerticalAnchorPoint );
m->setParameters( mParameters );
copyDataDefinedProperties( m );
copyPaintEffect( m );
return m;
@@ -2477,9 +2492,11 @@ bool QgsSvgMarkerSymbolLayer::writeDxf( QgsDxfExport &e, double mmMapUnitScaleFa
strokeColor = mDataDefinedProperties.valueAsColor( QgsSymbolLayer::PropertyStrokeColor, context.renderContext().expressionContext(), mStrokeColor );
}

QgsStringMap evaluatedParameters = QgsSymbolLayerUtils::evaluatePropertiesMap( mParameters, context.renderContext().expressionContext() );

const QByteArray &svgContent = QgsApplication::svgCache()->svgContent( path, size, fillColor, strokeColor, strokeWidth,
context.renderContext().scaleFactor(), mFixedAspectRatio,
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ) );
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ), evaluatedParameters );

QSvgRenderer r( svgContent );
if ( !r.isValid() )
@@ -2560,10 +2577,12 @@ QRectF QgsSvgMarkerSymbolLayer::bounds( QPointF point, QgsSymbolRenderContext &c
fillColor = mDataDefinedProperties.valueAsColor( QgsSymbolLayer::PropertyStrokeColor, context.renderContext().expressionContext(), mStrokeColor );
}

QgsStringMap evaluatedParameters = QgsSymbolLayerUtils::evaluatePropertiesMap( mParameters, context.renderContext().expressionContext() );

// adjust height of data defined path
QSizeF svgViewbox = QgsApplication::svgCache()->svgViewboxSize( path, scaledWidth, fillColor, strokeColor, strokeWidth,
context.renderContext().scaleFactor(), aspectRatio,
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ) );
( context.renderContext().flags() & QgsRenderContext::RenderBlocking ), evaluatedParameters );
scaledHeight = svgViewbox.isValid() ? scaledWidth * svgViewbox.height() / svgViewbox.width() : scaledWidth;
}
}
@@ -19,7 +19,6 @@
#include "qgis_core.h"
#include "qgis_sip.h"
#include "qgssymbollayer.h"
#include "qgssvgparameter.h"

#define DEFAULT_SIMPLEMARKER_NAME "circle"
#define DEFAULT_SIMPLEMARKER_COLOR QColor(255,0,0)
@@ -594,13 +593,13 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayer : public QgsMarkerSymbolLayer
* Returns the dynamic SVG parameters
* \since QGIS 3.18
*/
QgsSvgParameters parameters() const { return mParameters; }
QMap<QString, QgsProperty> parameters() const { return mParameters; }

/**
* Sets the dynamic SVG parameters
* \since QGIS 3.18
*/
void setParameters( const QgsSvgParameters &parameters ) { mParameters = parameters; }
void setParameters( const QMap<QString, QgsProperty> &parameters ) { mParameters = parameters; }

/**
* Sets the units for the stroke width.
@@ -649,7 +648,7 @@ class CORE_EXPORT QgsSvgMarkerSymbolLayer : public QgsMarkerSymbolLayer
bool mHasFillParam = false;
QColor mStrokeColor;
double mStrokeWidth;
QList<QgsSvgParameter> mParameters;
QMap<QString, QgsProperty> mParameters;

QgsUnitTypes::RenderUnit mStrokeWidthUnit;
QgsMapUnitScale mStrokeWidthMapUnitScale;

0 comments on commit 3201fa4

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