Skip to content

Commit

Permalink
Fix data defined properties which bind to a field can have incorrect
Browse files Browse the repository at this point in the history
values when the linked field has a null value
  • Loading branch information
nyalldawson committed Jun 7, 2021
1 parent a690609 commit 4e25c6b
Show file tree
Hide file tree
Showing 14 changed files with 141 additions and 92 deletions.
6 changes: 3 additions & 3 deletions src/core/dxf/qgsdxfexport.cpp
Expand Up @@ -1300,7 +1300,7 @@ void QgsDxfExport::writeText( const QString &layer, const QString &text, pal::La
if ( props.isActive( QgsPalLayerSettings::OffsetQuad ) )
{
const QVariant exprVal = props.value( QgsPalLayerSettings::OffsetQuad, expressionContext );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
offsetQuad = static_cast<QgsPalLayerSettings::QuadrantPosition>( exprVal.toInt() );
}
Expand Down Expand Up @@ -1358,7 +1358,7 @@ void QgsDxfExport::writeText( const QString &layer, const QString &text, pal::La

hali = HAlign::HLeft;
QVariant exprVal = props.value( QgsPalLayerSettings::Hali, expressionContext );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
const QString haliString = exprVal.toString();
if ( haliString.compare( QLatin1String( "Center" ), Qt::CaseInsensitive ) == 0 )
Expand All @@ -1377,7 +1377,7 @@ void QgsDxfExport::writeText( const QString &layer, const QString &text, pal::La
{
vali = VAlign::VBottom;
QVariant exprVal = props.value( QgsPalLayerSettings::Vali, expressionContext );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
const QString valiString = exprVal.toString();
if ( valiString.compare( QLatin1String( "Bottom" ), Qt::CaseInsensitive ) != 0 )
Expand Down
2 changes: 1 addition & 1 deletion src/core/labeling/qgslabelobstaclesettings.cpp
Expand Up @@ -34,7 +34,7 @@ void QgsLabelObstacleSettings::updateDataDefinedProperties( const QgsPropertyCol
{
context.setOriginalValueVariable( mObstacleFactor );
QVariant exprVal = properties.value( QgsPalLayerSettings::ObstacleFactor, context );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
bool ok;
double factorD = exprVal.toDouble( &ok );
Expand Down
50 changes: 25 additions & 25 deletions src/core/labeling/qgspallabeling.cpp
Expand Up @@ -1776,7 +1776,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
// data defined font units?
QgsUnitTypes::RenderUnit fontunits = mFormat.sizeUnit();
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::FontSizeUnit, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString units = exprVal.toString();
if ( !units.isEmpty() )
Expand Down Expand Up @@ -1885,7 +1885,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::FontCase ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::FontCase, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString fcase = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal FontCase:%1" ).arg( fcase ), 4 );
Expand Down Expand Up @@ -1990,7 +1990,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::CentroidWhole ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::CentroidWhole, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString str = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal CentroidWhole:%1" ).arg( str ), 4 );
Expand Down Expand Up @@ -2051,7 +2051,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::PolygonLabelOutside ) )
{
const QVariant dataDefinedOutside = mDataDefinedProperties.value( QgsPalLayerSettings::PolygonLabelOutside, context.expressionContext() );
if ( dataDefinedOutside.isValid() )
if ( !dataDefinedOutside.isNull() )
{
if ( dataDefinedOutside.type() == QVariant::String )
{
Expand Down Expand Up @@ -2215,7 +2215,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
{
context.expressionContext().setOriginalValueVariable( static_cast< int >( quadOff ) );
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::OffsetQuad, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
bool ok;
int quadInt = exprVal.toInt( &ok );
Expand Down Expand Up @@ -2287,7 +2287,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::OffsetUnits ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::OffsetUnits, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString units = exprVal.toString().trimmed();
if ( !units.isEmpty() )
Expand Down Expand Up @@ -2322,7 +2322,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
{
context.expressionContext().setOriginalValueVariable( angleOffset );
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::LabelRotation, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
bool ok;
double rotD = exprVal.toDouble( &ok );
Expand All @@ -2340,15 +2340,15 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::PositionX ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::PositionX, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
if ( !exprVal.isNull() )
xPos = exprVal.toDouble( &ddXPos );

if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::PositionY ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::PositionY, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
//data defined position. But field values could be NULL -> positions will be generated by PAL
if ( !exprVal.isNull() )
Expand All @@ -2367,7 +2367,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::Hali ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::Hali, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString haliString = exprVal.toString();
if ( haliString.compare( QLatin1String( "Center" ), Qt::CaseInsensitive ) == 0 )
Expand All @@ -2385,7 +2385,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::Vali ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::Vali, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString valiString = exprVal.toString();
if ( valiString.compare( QLatin1String( "Bottom" ), Qt::CaseInsensitive ) != 0 )
Expand Down Expand Up @@ -2474,7 +2474,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::RepeatDistanceUnit ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::RepeatDistanceUnit, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString units = exprVal.toString().trimmed();
if ( !units.isEmpty() )
Expand Down Expand Up @@ -2606,7 +2606,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::DistanceUnits ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::DistanceUnits, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString units = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal DistanceUnits:%1" ).arg( units ), 4 );
Expand Down Expand Up @@ -2669,7 +2669,7 @@ void QgsPalLayerSettings::registerFeature( const QgsFeature &f, QgsRenderContext
{
context.expressionContext().setOriginalValueVariable( priority );
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::Priority, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
bool ok;
double priorityD = exprVal.toDouble( &ok );
Expand Down Expand Up @@ -2777,7 +2777,7 @@ bool QgsPalLayerSettings::dataDefinedValEval( DataDefinedValueType valType,

context.setOriginalValueVariable( originalValue );
exprVal = mDataDefinedProperties.value( p, context );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
switch ( valType )
{
Expand Down Expand Up @@ -2965,7 +2965,7 @@ void QgsPalLayerSettings::parseTextStyle( QFont &labelFont,
{
context.expressionContext().setOriginalValueVariable( labelFont.family() );
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::Family, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString family = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal Font family:%1" ).arg( family ), 4 );
Expand All @@ -2987,7 +2987,7 @@ void QgsPalLayerSettings::parseTextStyle( QFont &labelFont,
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::FontStyle ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::FontStyle, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString fontstyle = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal Font style:%1" ).arg( fontstyle ), 4 );
Expand Down Expand Up @@ -3258,7 +3258,7 @@ void QgsPalLayerSettings::parseTextFormatting( QgsRenderContext &context )
{
context.expressionContext().setOriginalValueVariable( mFormat.lineHeight() );
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::MultiLineAlignment, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString str = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal MultiLineAlignment:%1" ).arg( str ), 4 );
Expand Down Expand Up @@ -3295,7 +3295,7 @@ void QgsPalLayerSettings::parseTextFormatting( QgsRenderContext &context )
const QString encoded = QgsTextRendererUtils::encodeTextOrientation( mFormat.orientation() );
context.expressionContext().setOriginalValueVariable( encoded );
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::TextOrientation, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString str = exprVal.toString().trimmed();
if ( !str.isEmpty() )
Expand All @@ -3320,7 +3320,7 @@ void QgsPalLayerSettings::parseTextFormatting( QgsRenderContext &context )

// data defined direction symbol placement?
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::DirSymbPlacement, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString str = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal DirSymbPlacement:%1" ).arg( str ), 4 );
Expand Down Expand Up @@ -3388,7 +3388,7 @@ void QgsPalLayerSettings::parseShapeBackground( QgsRenderContext &context )
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::ShapeKind ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::ShapeKind, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString skind = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal ShapeKind:%1" ).arg( skind ), 4 );
Expand All @@ -3407,7 +3407,7 @@ void QgsPalLayerSettings::parseShapeBackground( QgsRenderContext &context )
{
context.expressionContext().setOriginalValueVariable( svgPath );
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::ShapeSVGFile, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString svgfile = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal ShapeSVGFile:%1" ).arg( svgfile ), 4 );
Expand All @@ -3423,7 +3423,7 @@ void QgsPalLayerSettings::parseShapeBackground( QgsRenderContext &context )
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::ShapeSizeType ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::ShapeSizeType, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString stype = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal ShapeSizeType:%1" ).arg( stype ), 4 );
Expand Down Expand Up @@ -3494,7 +3494,7 @@ void QgsPalLayerSettings::parseShapeBackground( QgsRenderContext &context )
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::ShapeRotationType ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::ShapeRotationType, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString rotstr = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal ShapeRotationType:%1" ).arg( rotstr ), 4 );
Expand Down Expand Up @@ -3597,7 +3597,7 @@ void QgsPalLayerSettings::parseDropShadow( QgsRenderContext &context )
if ( mDataDefinedProperties.isActive( QgsPalLayerSettings::ShadowUnder ) )
{
exprVal = mDataDefinedProperties.value( QgsPalLayerSettings::ShadowUnder, context.expressionContext() );
if ( exprVal.isValid() )
if ( !exprVal.isNull() )
{
QString str = exprVal.toString().trimmed();
QgsDebugMsgLevel( QStringLiteral( "exprVal ShadowUnder:%1" ).arg( str ), 4 );
Expand Down
54 changes: 36 additions & 18 deletions src/core/symbology/qgsarrowsymbollayer.cpp
Expand Up @@ -630,40 +630,52 @@ void QgsArrowSymbolLayer::_resolveDataDefined( QgsSymbolRenderContext &context )
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyArrowWidth ) )
{
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowWidth, context.renderContext().expressionContext() );
double w = exprVal.toDouble( &ok );
if ( ok )
if ( !exprVal.isNull() )
{
mScaledArrowWidth = context.renderContext().convertToPainterUnits( w, arrowWidthUnit(), arrowWidthUnitScale() );
double w = exprVal.toDouble( &ok );
if ( ok )
{
mScaledArrowWidth = context.renderContext().convertToPainterUnits( w, arrowWidthUnit(), arrowWidthUnitScale() );
}
}
}
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyArrowStartWidth ) )
{
context.setOriginalValueVariable( arrowStartWidth() );
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowStartWidth, context.renderContext().expressionContext() );
double w = exprVal.toDouble( &ok );
if ( ok )
if ( !exprVal.isNull() )
{
mScaledArrowStartWidth = context.renderContext().convertToPainterUnits( w, arrowStartWidthUnit(), arrowStartWidthUnitScale() );
double w = exprVal.toDouble( &ok );
if ( ok )
{
mScaledArrowStartWidth = context.renderContext().convertToPainterUnits( w, arrowStartWidthUnit(), arrowStartWidthUnitScale() );
}
}
}
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyArrowHeadLength ) )
{
context.setOriginalValueVariable( headLength() );
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowHeadLength, context.renderContext().expressionContext() );
double w = exprVal.toDouble( &ok );
if ( ok )
if ( !exprVal.isNull() )
{
mScaledHeadLength = context.renderContext().convertToPainterUnits( w, headLengthUnit(), headLengthUnitScale() );
double w = exprVal.toDouble( &ok );
if ( ok )
{
mScaledHeadLength = context.renderContext().convertToPainterUnits( w, headLengthUnit(), headLengthUnitScale() );
}
}
}
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyArrowHeadThickness ) )
{
context.setOriginalValueVariable( headThickness() );
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowHeadThickness, context.renderContext().expressionContext() );
double w = exprVal.toDouble( &ok );
if ( ok )
if ( !exprVal.isNull() )
{
mScaledHeadThickness = context.renderContext().convertToPainterUnits( w, headThicknessUnit(), headThicknessUnitScale() );
double w = exprVal.toDouble( &ok );
if ( ok )
{
mScaledHeadThickness = context.renderContext().convertToPainterUnits( w, headThicknessUnit(), headThicknessUnitScale() );
}
}
}
if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyOffset ) )
Expand All @@ -681,21 +693,27 @@ void QgsArrowSymbolLayer::_resolveDataDefined( QgsSymbolRenderContext &context )
{
context.setOriginalValueVariable( headType() );
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowHeadType, context.renderContext().expressionContext() );
HeadType h = QgsSymbolLayerUtils::decodeArrowHeadType( exprVal, &ok );
if ( ok )
if ( !exprVal.isNull() )
{
mComputedHeadType = h;
HeadType h = QgsSymbolLayerUtils::decodeArrowHeadType( exprVal, &ok );
if ( ok )
{
mComputedHeadType = h;
}
}
}

if ( mDataDefinedProperties.isActive( QgsSymbolLayer::PropertyArrowType ) )
{
context.setOriginalValueVariable( arrowType() );
exprVal = mDataDefinedProperties.value( QgsSymbolLayer::PropertyArrowType, context.renderContext().expressionContext() );
ArrowType h = QgsSymbolLayerUtils::decodeArrowType( exprVal, &ok );
if ( ok )
if ( !exprVal.isNull() )
{
mComputedArrowType = h;
ArrowType h = QgsSymbolLayerUtils::decodeArrowType( exprVal, &ok );
if ( ok )
{
mComputedArrowType = h;
}
}
}
}
Expand Down

0 comments on commit 4e25c6b

Please sign in to comment.