From 31a30006cba050d8342ac7b4c6d4c0e6cb33faa3 Mon Sep 17 00:00:00 2001 From: Marco Hugentobler Date: Thu, 28 Mar 2013 08:18:18 +0100 Subject: [PATCH] Moved code for data defined simple line symbolisation into its own functions --- .../symbology-ng/qgslinesymbollayerv2.cpp | 155 ++++++++++-------- src/core/symbology-ng/qgslinesymbollayerv2.h | 5 + 2 files changed, 89 insertions(+), 71 deletions(-) diff --git a/src/core/symbology-ng/qgslinesymbollayerv2.cpp b/src/core/symbology-ng/qgslinesymbollayerv2.cpp index 9412bde4e771..afb08c0036ab 100644 --- a/src/core/symbology-ng/qgslinesymbollayerv2.cpp +++ b/src/core/symbology-ng/qgslinesymbollayerv2.cpp @@ -288,23 +288,7 @@ void QgsSimpleLineSymbolLayerV2::startRender( QgsSymbolV2RenderContext& context mSelPen.setColor( selColor ); //prepare expressions for data defined properties - const QgsVectorLayer* vLayer = context.layer(); - if ( vLayer ) - { - const QgsFields& fields = vLayer->pendingFields(); - if ( mStrokeColorExpression ) - mStrokeColorExpression->prepare( fields ); - if ( mStrokeWidthExpression ) - mStrokeWidthExpression->prepare( fields ); - if ( mLineOffsetExpression ) - mLineOffsetExpression->prepare( fields ); - if ( mDashPatternExpression ) - mDashPatternExpression->prepare( fields ); - if ( mJoinStyleExpression ) - mJoinStyleExpression->prepare( fields ); - if ( mCapStyleExpression ) - mCapStyleExpression->prepare( fields ); - } + prepareExpressions( context.layer() ); } void QgsSimpleLineSymbolLayerV2::stopRender( QgsSymbolV2RenderContext& context ) @@ -320,61 +304,9 @@ void QgsSimpleLineSymbolLayerV2::renderPolyline( const QPolygonF& points, QgsSym return; } - //data defined properties - double scaledWidth = 0; - if ( mStrokeWidthExpression ) - { - scaledWidth = mStrokeWidthExpression->evaluate( const_cast( context.feature() ) ).toDouble() - * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mWidthUnit ); - mPen.setWidthF( scaledWidth ); - mSelPen.setWidthF( scaledWidth ); - } - else if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale ) - { - scaledWidth = mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mWidthUnit ); - mPen.setWidthF( scaledWidth ); - mSelPen.setWidthF( scaledWidth ); - } - - //color - if ( mStrokeColorExpression ) - { - mPen.setColor( QColor( mStrokeColorExpression->evaluate( const_cast( context.feature() ) ).toString() ) ); - } - - //offset - double offset = mOffset; - if ( mLineOffsetExpression ) - { - offset = mLineOffsetExpression->evaluate( const_cast( context.feature() ) ).toDouble(); - } - - //dash dot vector - if ( mDashPatternExpression ) - { - QVector dashVector; - QStringList dashList = mDashPatternExpression->evaluate( const_cast( context.feature() ) ).toString().split( ";" ); - QStringList::const_iterator dashIt = dashList.constBegin(); - for ( ; dashIt != dashList.constEnd(); ++dashIt ) - { - dashVector.push_back( dashIt->toDouble() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mCustomDashPatternUnit ) / mPen.widthF() ); - } - mPen.setDashPattern( dashVector ); - } - - //join style - if ( mJoinStyleExpression ) - { - QString joinStyleString = mJoinStyleExpression->evaluate( const_cast( context.feature() ) ).toString(); - mPen.setJoinStyle( QgsSymbolLayerV2Utils::decodePenJoinStyle( joinStyleString ) ); - } + double offset = 0.0; + applyDataDefinedSymbology( context, mPen, mSelPen, offset ); - //cap style - if ( mCapStyleExpression ) - { - QString capStyleString = mCapStyleExpression->evaluate( const_cast( context.feature() ) ).toString(); - mPen.setCapStyle( QgsSymbolLayerV2Utils::decodePenCapStyle( capStyleString ) ); - } p->setPen( context.selected() ? mSelPen : mPen ); if ( offset == 0 ) @@ -534,6 +466,87 @@ QgsSymbolLayerV2* QgsSimpleLineSymbolLayerV2::createFromSld( QDomElement &elemen return l; } +void QgsSimpleLineSymbolLayerV2::prepareExpressions( const QgsVectorLayer* vl ) +{ + if ( !vl ) + { + return; + } + + const QgsFields& fields = vl->pendingFields(); + if ( mStrokeColorExpression ) + mStrokeColorExpression->prepare( fields ); + if ( mStrokeWidthExpression ) + mStrokeWidthExpression->prepare( fields ); + if ( mLineOffsetExpression ) + mLineOffsetExpression->prepare( fields ); + if ( mDashPatternExpression ) + mDashPatternExpression->prepare( fields ); + if ( mJoinStyleExpression ) + mJoinStyleExpression->prepare( fields ); + if ( mCapStyleExpression ) + mCapStyleExpression->prepare( fields ); +} + +void QgsSimpleLineSymbolLayerV2::applyDataDefinedSymbology( QgsSymbolV2RenderContext& context, QPen& pen, QPen& selPen, double& offset ) +{ + //data defined properties + double scaledWidth = 0; + if ( mStrokeWidthExpression ) + { + scaledWidth = mStrokeWidthExpression->evaluate( const_cast( context.feature() ) ).toDouble() + * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mWidthUnit ); + pen.setWidthF( scaledWidth ); + selPen.setWidthF( scaledWidth ); + } + else if ( context.renderHints() & QgsSymbolV2::DataDefinedSizeScale ) + { + scaledWidth = mWidth * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mWidthUnit ); + pen.setWidthF( scaledWidth ); + selPen.setWidthF( scaledWidth ); + } + + //color + if ( mStrokeColorExpression ) + { + pen.setColor( QColor( mStrokeColorExpression->evaluate( const_cast( context.feature() ) ).toString() ) ); + } + + //offset + offset = mOffset; + if ( mLineOffsetExpression ) + { + offset = mLineOffsetExpression->evaluate( const_cast( context.feature() ) ).toDouble(); + } + + //dash dot vector + if ( mDashPatternExpression ) + { + QVector dashVector; + QStringList dashList = mDashPatternExpression->evaluate( const_cast( context.feature() ) ).toString().split( ";" ); + QStringList::const_iterator dashIt = dashList.constBegin(); + for ( ; dashIt != dashList.constEnd(); ++dashIt ) + { + dashVector.push_back( dashIt->toDouble() * QgsSymbolLayerV2Utils::lineWidthScaleFactor( context.renderContext(), mCustomDashPatternUnit ) / mPen.widthF() ); + } + pen.setDashPattern( dashVector ); + } + + //join style + if ( mJoinStyleExpression ) + { + QString joinStyleString = mJoinStyleExpression->evaluate( const_cast( context.feature() ) ).toString(); + pen.setJoinStyle( QgsSymbolLayerV2Utils::decodePenJoinStyle( joinStyleString ) ); + } + + //cap style + if ( mCapStyleExpression ) + { + QString capStyleString = mCapStyleExpression->evaluate( const_cast( context.feature() ) ).toString(); + pen.setCapStyle( QgsSymbolLayerV2Utils::decodePenCapStyle( capStyleString ) ); + } +} + ///////// diff --git a/src/core/symbology-ng/qgslinesymbollayerv2.h b/src/core/symbology-ng/qgslinesymbollayerv2.h index 6043ed16a395..089bf5ff188d 100644 --- a/src/core/symbology-ng/qgslinesymbollayerv2.h +++ b/src/core/symbology-ng/qgslinesymbollayerv2.h @@ -122,6 +122,11 @@ class CORE_EXPORT QgsSimpleLineSymbolLayerV2 : public QgsLineSymbolLayerV2 QgsExpression* mDashPatternExpression; QgsExpression* mJoinStyleExpression; QgsExpression* mCapStyleExpression; + + private: + //helper functions for data defined symbology + void prepareExpressions( const QgsVectorLayer* vl ); + void applyDataDefinedSymbology( QgsSymbolV2RenderContext& context, QPen& pen, QPen& selPen, double& offset ); }; /////////