Skip to content

Commit

Permalink
address review
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec authored and wonder-sk committed May 5, 2021
1 parent 843eebc commit 89c069f
Show file tree
Hide file tree
Showing 10 changed files with 140 additions and 182 deletions.
15 changes: 0 additions & 15 deletions python/core/auto_generated/raster/qgscolorrampshader.sip.in
Expand Up @@ -234,21 +234,6 @@ Ownership of ``settings`` is transferred.
.. versionadded:: 3.18
%End


QVariantMap properties() const;
%Docstring
Returns a string map that contains the configuration information for a symbol layer.

.. versionadded:: 3.20
%End

static QgsColorRampShader createFromSymbolLayerProperties( const QVariantMap &properties );
%Docstring
Creates a color ramp sahder from symbol layer properties

.. versionadded:: 3.20
%End

protected:


Expand Down
Expand Up @@ -89,7 +89,7 @@ This is a hybrid layer, it constructs its own geometry so it does not
care about the geometry of its parents.
%End

void render( QgsSymbolRenderContext &context );
virtual void render( QgsSymbolRenderContext &context );
%Docstring
Will render this symbol layer using the context.
In comparison to other symbols there is no geometry passed in, since
Expand Down
Expand Up @@ -7,6 +7,40 @@
************************************************************************/


class QgsInterpolatedLineSymbolLayerWidget: QgsSymbolLayerWidget
{
%Docstring(signature="appended")
:py:class:`QgsInterpolatedLineSymbolLayerWidget`

.. versionadded:: 3.20
%End

%TypeHeaderCode
#include "qgsinterpolatedlinesymbollayerwidget.h"
%End
public:

QgsInterpolatedLineSymbolLayerWidget( QgsVectorLayer *layer, QWidget *parent /TransferThis/ = 0 );
%Docstring
Constructor

:param layer: the layer where this symbol layer is applied
:param parent: the parent widget
%End

static QgsSymbolLayerWidget *create( QgsVectorLayer *layer ) /Factory/;
%Docstring
Static creation method

:param layer: the layer where this symbol layer is applied
%End

virtual void setSymbolLayer( QgsSymbolLayer *layer );

virtual QgsSymbolLayer *symbolLayer();


};

/************************************************************************
* This file has been generated automatically from *
Expand Down
132 changes: 0 additions & 132 deletions src/core/raster/qgscolorrampshader.cpp
Expand Up @@ -649,135 +649,3 @@ void QgsColorRampShader::setLegendSettings( QgsColorRampLegendNodeSettings *sett
return;
mLegendSettings.reset( settings );
}

QVariantMap QgsColorRampShader::properties() const
{
QVariantMap props;
if ( mSourceColorRamp )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
props.unite( mSourceColorRamp->properties() );
#else
props.insert( mSourceColorRamp->properties() );
#endif
}
props.insert( QStringLiteral( "color_ramp_shader_type" ), mColorRampType );
props.insert( QStringLiteral( "color_ramp_shader_classification_mode" ), mClassificationMode );
QVariantList colorRampItemListVariant;

for ( const QgsColorRampShader::ColorRampItem &item : mColorRampItemList )
{
QVariantMap itemVar;
itemVar[QStringLiteral( "label" )] = item.label;
itemVar[QStringLiteral( "color" )] = QgsSymbolLayerUtils::encodeColor( item.color );
itemVar[QStringLiteral( "value" )] = item.value;
colorRampItemListVariant.append( itemVar );
}
props.insert( QStringLiteral( "color_ramp_shader_items_list" ), colorRampItemListVariant );

props.insert( QStringLiteral( "color_ramp_shader_minimum_value" ), minimumValue() );
props.insert( QStringLiteral( "color_ramp_shader_maximum_value" ), maximumValue() );
props.insert( QStringLiteral( "color_ramp_shader_value_out_of_range" ), mClip ? 1 : 0 );
props.insert( QStringLiteral( "color_ramp_shader_label_precision" ), labelPrecision() );

if ( mLegendSettings )
{
QDomDocument docLegendSettings( QStringLiteral( "legend_settings" ) );
QDomElement elemLegendProperties = docLegendSettings.createElement( "legend_settings_properties" );
docLegendSettings.appendChild( elemLegendProperties );
mLegendSettings->writeXml( docLegendSettings, elemLegendProperties, QgsReadWriteContext() );
props.insert( QStringLiteral( "color_ramp_shader_legend_settings" ), docLegendSettings.toString() );
}

return props;
}

QgsColorRampShader QgsColorRampShader::createFromSymbolLayerProperties( const QVariantMap &properties )
{
QgsColorRampShader colorRampShader;

//attempt to create color ramp from props
if ( properties.contains( QStringLiteral( "rampType" ) ) )
{
if ( properties[QStringLiteral( "rampType" )] == QgsCptCityColorRamp::typeString() )
{
colorRampShader.setSourceColorRamp( QgsCptCityColorRamp::create( properties ) );
}
else if ( properties[QStringLiteral( "rampType" )] == QgsGradientColorRamp::typeString() )
{
colorRampShader.setSourceColorRamp( QgsGradientColorRamp::create( properties ) );
}
else if ( properties[QStringLiteral( "rampType" )] == QgsLimitedRandomColorRamp::typeString() )
{
colorRampShader.setSourceColorRamp( QgsLimitedRandomColorRamp::create( properties ) );
}
else if ( properties[QStringLiteral( "rampType" )] == QgsColorBrewerColorRamp::typeString() )
{
colorRampShader.setSourceColorRamp( QgsColorBrewerColorRamp::create( properties ) );
}
else if ( properties[QStringLiteral( "rampType" )] == QgsPresetSchemeColorRamp::typeString() )
{
colorRampShader.setSourceColorRamp( QgsPresetSchemeColorRamp::create( properties ) );
}
}

if ( properties.contains( QStringLiteral( "color_ramp_shader_type" ) ) )
colorRampShader.mColorRampType = static_cast<QgsColorRampShader::Type>( properties.value( QStringLiteral( "color_ramp_shader_type" ) ).toInt() );
if ( properties.contains( QStringLiteral( "color_ramp_shader_classification_mode" ) ) )
colorRampShader.mClassificationMode = static_cast<QgsColorRampShader::ClassificationMode>( properties.value( QStringLiteral( "color_ramp_shader_classification_mode" ) ).toInt() );


if ( properties.contains( QStringLiteral( "color_ramp_shader_items_list" ) ) )
{
QVariant colorRampItemsVar = properties.value( QStringLiteral( "color_ramp_shader_items_list" ) );
if ( colorRampItemsVar.type() == QVariant::List )
{
QVariantList itemVariantList = colorRampItemsVar.toList();
for ( const QVariant &itemVar : std::as_const( itemVariantList ) )
{
QgsColorRampShader::ColorRampItem item;
if ( itemVar.type() != QVariant::Map )
continue;
QVariantMap itemVarMap = itemVar.toMap();
if ( !itemVarMap.contains( QStringLiteral( "label" ) ) || !itemVarMap.contains( QStringLiteral( "color" ) ) || !itemVarMap.contains( QStringLiteral( "value" ) ) )
continue;

item.label = itemVarMap.value( QStringLiteral( "label" ) ).toString();
item.color = QgsSymbolLayerUtils::decodeColor( itemVarMap.value( QStringLiteral( "color" ) ).toString() );
item.value = itemVarMap.value( QStringLiteral( "value" ) ).toDouble();

colorRampShader.mColorRampItemList.append( item );
}
}
}

if ( properties.contains( QStringLiteral( "color_ramp_shader_minimum_value" ) ) )
colorRampShader.setMinimumValue( properties.value( QStringLiteral( "color_ramp_shader_minimum_value" ) ).toDouble() );
else
colorRampShader.setMinimumValue( std::numeric_limits<double>::quiet_NaN() );

if ( properties.contains( QStringLiteral( "color_ramp_shader_maximum_value" ) ) )
colorRampShader.setMaximumValue( properties.value( QStringLiteral( "color_ramp_shader_maximum_value" ) ).toDouble() );
else
colorRampShader.setMaximumValue( std::numeric_limits<double>::quiet_NaN() );

if ( properties.contains( QStringLiteral( "color_ramp_shader_value_out_of_range" ) ) )
colorRampShader.setClip( properties.value( QStringLiteral( "color_ramp_shader_value_out_of_range" ) ).toInt() == 1 );
if ( properties.contains( QStringLiteral( "color_ramp_shader_label_precision" ) ) )
colorRampShader.setLabelPrecision( properties.value( QStringLiteral( "color_ramp_shader_label_precision" ) ).toInt() );

QDomDocument docLegendSettings;
if ( properties.contains( QStringLiteral( "color_ramp_shader_legend_settings" ) ) &&
docLegendSettings.setContent( properties.value( QStringLiteral( "color_ramp_shader_legend_settings" ) ).toString() ) )
{
QDomElement elemLegendProperties = docLegendSettings.firstChildElement( QStringLiteral( "legend_settings_properties" ) );
if ( !elemLegendProperties.isNull() )
{
std::unique_ptr<QgsColorRampLegendNodeSettings> legendSettings = std::make_unique<QgsColorRampLegendNodeSettings>();
legendSettings->readXml( elemLegendProperties, QgsReadWriteContext() );
colorRampShader.setLegendSettings( legendSettings.release() );
}
}

return colorRampShader;
}
13 changes: 0 additions & 13 deletions src/core/raster/qgscolorrampshader.h
Expand Up @@ -249,19 +249,6 @@ class CORE_EXPORT QgsColorRampShader : public QgsRasterShaderFunction
*/
void setLegendSettings( QgsColorRampLegendNodeSettings *settings SIP_TRANSFER );


/**
* Returns a string map that contains the configuration information for a symbol layer.
* \since QGIS 3.20
*/
QVariantMap properties() const;

/**
* Creates a color ramp sahder from symbol layer properties
* \since QGIS 3.20
*/
static QgsColorRampShader createFromSymbolLayerProperties( const QVariantMap &properties );

protected:

//! Source color ramp
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology/qgsgeometrygeneratorsymbollayer.h
Expand Up @@ -95,7 +95,7 @@ class CORE_EXPORT QgsGeometryGeneratorSymbolLayer : public QgsSymbolLayer
* \param context The rendering context which will be used to render and to
* construct a geometry.
*/
void render( QgsSymbolRenderContext &context );
virtual void render( QgsSymbolRenderContext &context );

void setColor( const QColor &color ) override;

Expand Down
109 changes: 97 additions & 12 deletions src/core/symbology/qgsinterpolatedlinerenderer.cpp
Expand Up @@ -67,7 +67,7 @@ void QgsInterpolatedLineRenderer::renderInDeviceCoordinate( double valueColor1,
QList<QLinearGradient> gradients;

mStrokeColoring.graduatedColors( valueColor1, valueColor2, breakValues, breakColors, gradients );
QColor selectedColor = context.selectionColor();;
QColor selectedColor = context.selectionColor();

if ( gradients.isEmpty() && !breakValues.empty() && !breakColors.isEmpty() ) //exact colors to render
{
Expand Down Expand Up @@ -894,7 +894,8 @@ QgsSymbolLayer *QgsInterpolatedLineSymbolLayer::create( const QVariantMap &prope

if ( properties.contains( QStringLiteral( "single_color" ) ) )
symbolLayer->mLineRender.mStrokeColoring.setColor( QgsSymbolLayerUtils::decodeColor( properties.value( QStringLiteral( "single_color" ) ).toString() ) );
symbolLayer->mLineRender.mStrokeColoring.setColor( QgsColorRampShader::createFromSymbolLayerProperties( properties ) );
if ( properties.contains( QStringLiteral( "color_ramp_shader" ) ) )
symbolLayer->mLineRender.mStrokeColoring.setColor( createColorRampShaderFromProperties( properties.value( QStringLiteral( "color_ramp_shader" ) ) ) );
if ( properties.contains( QStringLiteral( "coloring_method" ) ) )
symbolLayer->mLineRender.mStrokeColoring.setColoringMethod(
static_cast<QgsInterpolatedLineColor::ColoringMethod>( properties.value( QStringLiteral( "coloring_method" ) ).toInt() ) );
Expand Down Expand Up @@ -925,15 +926,7 @@ QVariantMap QgsInterpolatedLineSymbolLayer::properties() const
// Color varying
props.insert( QStringLiteral( "coloring_method" ), mLineRender.mStrokeColoring.coloringMethod() );
props.insert( QStringLiteral( "single_color" ), QgsSymbolLayerUtils::encodeColor( mLineRender.mStrokeColoring.singleColor() ) );
QgsColorRamp *colorRamp = mLineRender.mStrokeColoring.colorRampShader().sourceColorRamp();
if ( colorRamp )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
props.unite( mLineRender.mStrokeColoring.colorRampShader().properties() );
#else
props.insert( mLineRender.mStrokeColoring.colorRampShader().properties() );
#endif
}
props.insert( QStringLiteral( "color_ramp_shader" ), colorRampShaderProperties() );

return props;
}
Expand Down Expand Up @@ -1033,6 +1026,98 @@ QgsInterpolatedLineColor QgsInterpolatedLineSymbolLayer::interpolatedColor() con
return mLineRender.interpolatedColor();
}

QVariant QgsInterpolatedLineSymbolLayer::colorRampShaderProperties() const
{
const QgsColorRampShader &colorRampShader = mLineRender.mStrokeColoring.colorRampShader();

QVariantMap props;
if ( colorRampShader.sourceColorRamp() )
props.insert( QStringLiteral( "color_ramp_source" ), QgsSymbolLayerUtils::colorRampToVariant( QString(), colorRampShader.sourceColorRamp() ) );
props.insert( QStringLiteral( "color_ramp_shader_type" ), colorRampShader.colorRampType() );
props.insert( QStringLiteral( "color_ramp_shader_classification_mode" ), colorRampShader.classificationMode() );
QVariantList colorRampItemListVariant;

const QList<QgsColorRampShader::ColorRampItem> colorRampItemList = colorRampShader.colorRampItemList();
for ( const QgsColorRampShader::ColorRampItem &item : colorRampItemList )
{
QVariantMap itemVar;
itemVar[QStringLiteral( "label" )] = item.label;
itemVar[QStringLiteral( "color" )] = QgsSymbolLayerUtils::encodeColor( item.color );
itemVar[QStringLiteral( "value" )] = item.value;
colorRampItemListVariant.append( itemVar );
}
props.insert( QStringLiteral( "color_ramp_shader_items_list" ), colorRampItemListVariant );

props.insert( QStringLiteral( "color_ramp_shader_minimum_value" ), colorRampShader.minimumValue() );
props.insert( QStringLiteral( "color_ramp_shader_maximum_value" ), colorRampShader.maximumValue() );
props.insert( QStringLiteral( "color_ramp_shader_value_out_of_range" ), colorRampShader.clip() ? 1 : 0 );
props.insert( QStringLiteral( "color_ramp_shader_label_precision" ), colorRampShader.labelPrecision() );

return props;
}

QgsColorRampShader QgsInterpolatedLineSymbolLayer::createColorRampShaderFromProperties( const QVariant &properties )
{
QgsColorRampShader colorRampShader;

if ( properties.type() != QVariant::Map )
return colorRampShader;

QVariantMap shaderVariantMap = properties.toMap();

if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_source" ) ) )
colorRampShader.setSourceColorRamp( QgsSymbolLayerUtils::loadColorRamp( shaderVariantMap.value( QStringLiteral( "color_ramp_source" ) ) ) );

if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_shader_type" ) ) )
colorRampShader.setColorRampType( static_cast<QgsColorRampShader::Type>( shaderVariantMap.value( QStringLiteral( "color_ramp_shader_type" ) ).toInt() ) );
if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_shader_classification_mode" ) ) )
colorRampShader.setClassificationMode( static_cast<QgsColorRampShader::ClassificationMode>(
shaderVariantMap.value( QStringLiteral( "color_ramp_shader_classification_mode" ) ).toInt() ) );

if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_shader_items_list" ) ) )
{
QVariant colorRampItemsVar = shaderVariantMap.value( QStringLiteral( "color_ramp_shader_items_list" ) );
if ( colorRampItemsVar.type() == QVariant::List )
{
QVariantList itemVariantList = colorRampItemsVar.toList();
QList<QgsColorRampShader::ColorRampItem> colorRampItemList;
for ( const QVariant &itemVar : std::as_const( itemVariantList ) )
{
QgsColorRampShader::ColorRampItem item;
if ( itemVar.type() != QVariant::Map )
continue;
QVariantMap itemVarMap = itemVar.toMap();
if ( !itemVarMap.contains( QStringLiteral( "label" ) ) || !itemVarMap.contains( QStringLiteral( "color" ) ) || !itemVarMap.contains( QStringLiteral( "value" ) ) )
continue;

item.label = itemVarMap.value( QStringLiteral( "label" ) ).toString();
item.color = QgsSymbolLayerUtils::decodeColor( itemVarMap.value( QStringLiteral( "color" ) ).toString() );
item.value = itemVarMap.value( QStringLiteral( "value" ) ).toDouble();

colorRampItemList.append( item );
}
colorRampShader.setColorRampItemList( colorRampItemList );
}
}

if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_shader_minimum_value" ) ) )
colorRampShader.setMinimumValue( shaderVariantMap.value( QStringLiteral( "color_ramp_shader_minimum_value" ) ).toDouble() );
else
colorRampShader.setMinimumValue( std::numeric_limits<double>::quiet_NaN() );

if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_shader_maximum_value" ) ) )
colorRampShader.setMaximumValue( shaderVariantMap.value( QStringLiteral( "color_ramp_shader_maximum_value" ) ).toDouble() );
else
colorRampShader.setMaximumValue( std::numeric_limits<double>::quiet_NaN() );

if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_shader_value_out_of_range" ) ) )
colorRampShader.setClip( shaderVariantMap.value( QStringLiteral( "color_ramp_shader_value_out_of_range" ) ).toInt() == 1 );
if ( shaderVariantMap.contains( QStringLiteral( "color_ramp_shader_label_precision" ) ) )
colorRampShader.setLabelPrecision( shaderVariantMap.value( QStringLiteral( "color_ramp_shader_label_precision" ) ).toInt() );

return colorRampShader;
}

QgsInterpolatedLineSymbolLayer::QgsInterpolatedLineSymbolLayer(): QgsLineSymbolLayer( true ) {}


Expand Down Expand Up @@ -1182,7 +1267,7 @@ void QgsInterpolatedLineSymbolLayer::renderPolyline( const QPolygonF &points, Qg
double v1c = startValColor + variationPerMapUnitColor * lengthFromStart;
double v1w = startValWidth + variationPerMapUnitWidth * lengthFromStart;
lengthFromStart += p1.distance( p2 );
double v2c = startValColor + variationPerMapUnitColor * lengthFromStart;;
double v2c = startValColor + variationPerMapUnitColor * lengthFromStart;
double v2w = startValWidth + variationPerMapUnitWidth * lengthFromStart;
mLineRender.render( v1c, v2c, v1w, v2w, p1, p2, renderContext );
}
Expand Down
3 changes: 3 additions & 0 deletions src/core/symbology/qgsinterpolatedlinerenderer.h
Expand Up @@ -354,6 +354,9 @@ class CORE_EXPORT QgsInterpolatedLineSymbolLayer : public QgsLineSymbolLayer
std::unique_ptr<QgsExpression> mStartColorExpression;
std::unique_ptr<QgsExpression> mEndColorExpression;
QgsFeature mFeature;

QVariant colorRampShaderProperties() const;
static QgsColorRampShader createColorRampShaderFromProperties( const QVariant &properties );
};


Expand Down

0 comments on commit 89c069f

Please sign in to comment.