From fe95e4eeeaeaf9fe323061628f34d17541a9430e Mon Sep 17 00:00:00 2001 From: Nyall Dawson Date: Wed, 27 Jan 2016 13:58:32 +1100 Subject: [PATCH] Show variables from symbol scope in symbol layer widget expression builders. (This lets users know they are available). Also swap some variable names to static strings for speed. --- python/core/qgsexpressioncontext.sip | 9 +++++++++ src/core/qgsexpressioncontext.cpp | 2 ++ src/core/qgsexpressioncontext.h | 9 +++++++++ src/core/symbology-ng/qgssymbolv2.cpp | 10 +++++----- src/gui/symbology-ng/qgssizescalewidget.cpp | 6 +++--- src/gui/symbology-ng/qgssymbollayerv2widget.cpp | 15 ++++++++++++--- 6 files changed, 40 insertions(+), 11 deletions(-) diff --git a/python/core/qgsexpressioncontext.sip b/python/core/qgsexpressioncontext.sip index d32969c99ec..7dc4b1125b6 100644 --- a/python/core/qgsexpressioncontext.sip +++ b/python/core/qgsexpressioncontext.sip @@ -388,11 +388,20 @@ class QgsExpressionContext */ void setOriginalValueVariable( const QVariant& value ); + //! Inbuilt variable name for fields storage static const QString EXPR_FIELDS; + //! Inbuilt variable name for feature storage static const QString EXPR_FEATURE; + //! Inbuilt variable name for @value original value variable static const QString EXPR_ORIGINAL_VALUE; + //! Inbuilt variable name for symbol color variable static const QString EXPR_SYMBOL_COLOR; + //! Inbuilt variable name for symbol angle variable static const QString EXPR_SYMBOL_ANGLE; + //! Inbuilt variable name for geometry part count variable + static const QString EXPR_GEOMETRY_PART_COUNT; + //! Inbuilt variable name for geometry part number variable + static const QString EXPR_GEOMETRY_PART_NUM; }; /** \ingroup core diff --git a/src/core/qgsexpressioncontext.cpp b/src/core/qgsexpressioncontext.cpp index ae1ca4b30d2..d1751634d7b 100644 --- a/src/core/qgsexpressioncontext.cpp +++ b/src/core/qgsexpressioncontext.cpp @@ -35,6 +35,8 @@ const QString QgsExpressionContext::EXPR_FEATURE( "_feature_" ); const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( "value" ); const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( "symbol_color" ); const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( "symbol_angle" ); +const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( "geometry_part_count" ); +const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( "geometry_part_num" ); // // QgsExpressionContextScope diff --git a/src/core/qgsexpressioncontext.h b/src/core/qgsexpressioncontext.h index 6d3c5c6d7a8..b856d0b51db 100644 --- a/src/core/qgsexpressioncontext.h +++ b/src/core/qgsexpressioncontext.h @@ -423,11 +423,20 @@ class CORE_EXPORT QgsExpressionContext */ void setOriginalValueVariable( const QVariant& value ); + //! Inbuilt variable name for fields storage static const QString EXPR_FIELDS; + //! Inbuilt variable name for feature storage static const QString EXPR_FEATURE; + //! Inbuilt variable name for @value original value variable static const QString EXPR_ORIGINAL_VALUE; + //! Inbuilt variable name for symbol color variable static const QString EXPR_SYMBOL_COLOR; + //! Inbuilt variable name for symbol angle variable static const QString EXPR_SYMBOL_ANGLE; + //! Inbuilt variable name for geometry part count variable + static const QString EXPR_GEOMETRY_PART_COUNT; + //! Inbuilt variable name for geometry part number variable + static const QString EXPR_GEOMETRY_PART_NUM; private: diff --git a/src/core/symbology-ng/qgssymbolv2.cpp b/src/core/symbology-ng/qgssymbolv2.cpp index 1d5f9cd7f25..cebb0fb60d8 100644 --- a/src/core/symbology-ng/qgssymbolv2.cpp +++ b/src/core/symbology-ng/qgssymbolv2.cpp @@ -717,8 +717,8 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co { context.expressionContext().appendScope( mSymbolRenderContext->expressionContextScope() ); QgsExpressionContextUtils::updateSymbolScope( this, mSymbolRenderContext->expressionContextScope() ); - mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_count", segmentizedGeometry->geometry()->partCount() ); - mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", 1 ); + mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, segmentizedGeometry->geometry()->partCount() ); + mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1 ); } switch ( QgsWKBTypes::flatType( segmentizedGeometry->geometry()->wkbType() ) ) @@ -786,7 +786,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co for ( int i = 0; i < mp->numGeometries(); ++i ) { - mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", i + 1 ); + mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 ); const QgsPointV2* point = static_cast< const QgsPointV2* >( mp->geometryN( i ) ); _getPoint( pt, context, point ); @@ -815,7 +815,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co for ( unsigned int i = 0; i < num; ++i ) { - mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", i + 1 ); + mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 ); if ( geomCollection ) { @@ -848,7 +848,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co for ( unsigned int i = 0; i < num; ++i ) { - mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", i + 1 ); + mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 ); if ( geomCollection ) { diff --git a/src/gui/symbology-ng/qgssizescalewidget.cpp b/src/gui/symbology-ng/qgssizescalewidget.cpp index 360cea96a55..cd6a65e34c4 100644 --- a/src/gui/symbology-ng/qgssizescalewidget.cpp +++ b/src/gui/symbology-ng/qgssizescalewidget.cpp @@ -122,10 +122,10 @@ static QgsExpressionContext _getExpressionContext( const void* context ) if ( widget->layer() ) expContext << QgsExpressionContextUtils::layerScope( widget->layer() ); - expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_count", 1, true ) ); - expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_num", 1, true ) ); + expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, 1, true ) ); + expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) ); - expContext.setHighlightedVariables( QStringList() << "geometry_part_num" ); + expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM ); return expContext; } diff --git a/src/gui/symbology-ng/qgssymbollayerv2widget.cpp b/src/gui/symbology-ng/qgssymbollayerv2widget.cpp index 02f4708cc12..83895ed8974 100644 --- a/src/gui/symbology-ng/qgssymbollayerv2widget.cpp +++ b/src/gui/symbology-ng/qgssymbollayerv2widget.cpp @@ -75,12 +75,21 @@ static QgsExpressionContext _getExpressionContext( const void* context ) if ( layer ) expContext << QgsExpressionContextUtils::layerScope( layer ); - expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_count", 1, true ) ); - expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_num", 1, true ) ); + QgsExpressionContextScope* symbolScope = QgsExpressionContextUtils::updateSymbolScope( nullptr, new QgsExpressionContextScope() ); + if ( const QgsSymbolLayerV2* symbolLayer = const_cast< QgsSymbolLayerV2Widget* >( widget )->symbolLayer() ) + { + //cheat a bit - set the symbol color variable to match the symbol layer's color (when we should really be using the *symbols* + //color, but that's not accessible here). 99% of the time these will be the same anyway + symbolScope->setVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbolLayer->color() ); + } + expContext << symbolScope; + expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, 1, true ) ); + expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) ); //TODO - show actual value expContext.setOriginalValueVariable( QVariant() ); - expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << "geometry_part_count" << "geometry_part_num" ); + expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << QgsExpressionContext::EXPR_SYMBOL_COLOR + << QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM ); return expContext; }