Skip to content

Commit fe95e4e

Browse files
committed
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.
1 parent 9b6fa0d commit fe95e4e

File tree

6 files changed

+40
-11
lines changed

6 files changed

+40
-11
lines changed

python/core/qgsexpressioncontext.sip

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,20 @@ class QgsExpressionContext
388388
*/
389389
void setOriginalValueVariable( const QVariant& value );
390390

391+
//! Inbuilt variable name for fields storage
391392
static const QString EXPR_FIELDS;
393+
//! Inbuilt variable name for feature storage
392394
static const QString EXPR_FEATURE;
395+
//! Inbuilt variable name for @value original value variable
393396
static const QString EXPR_ORIGINAL_VALUE;
397+
//! Inbuilt variable name for symbol color variable
394398
static const QString EXPR_SYMBOL_COLOR;
399+
//! Inbuilt variable name for symbol angle variable
395400
static const QString EXPR_SYMBOL_ANGLE;
401+
//! Inbuilt variable name for geometry part count variable
402+
static const QString EXPR_GEOMETRY_PART_COUNT;
403+
//! Inbuilt variable name for geometry part number variable
404+
static const QString EXPR_GEOMETRY_PART_NUM;
396405
};
397406

398407
/** \ingroup core

src/core/qgsexpressioncontext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const QString QgsExpressionContext::EXPR_FEATURE( "_feature_" );
3535
const QString QgsExpressionContext::EXPR_ORIGINAL_VALUE( "value" );
3636
const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( "symbol_color" );
3737
const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( "symbol_angle" );
38+
const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( "geometry_part_count" );
39+
const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( "geometry_part_num" );
3840

3941
//
4042
// QgsExpressionContextScope

src/core/qgsexpressioncontext.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,11 +423,20 @@ class CORE_EXPORT QgsExpressionContext
423423
*/
424424
void setOriginalValueVariable( const QVariant& value );
425425

426+
//! Inbuilt variable name for fields storage
426427
static const QString EXPR_FIELDS;
428+
//! Inbuilt variable name for feature storage
427429
static const QString EXPR_FEATURE;
430+
//! Inbuilt variable name for @value original value variable
428431
static const QString EXPR_ORIGINAL_VALUE;
432+
//! Inbuilt variable name for symbol color variable
429433
static const QString EXPR_SYMBOL_COLOR;
434+
//! Inbuilt variable name for symbol angle variable
430435
static const QString EXPR_SYMBOL_ANGLE;
436+
//! Inbuilt variable name for geometry part count variable
437+
static const QString EXPR_GEOMETRY_PART_COUNT;
438+
//! Inbuilt variable name for geometry part number variable
439+
static const QString EXPR_GEOMETRY_PART_NUM;
431440

432441
private:
433442

src/core/symbology-ng/qgssymbolv2.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -717,8 +717,8 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
717717
{
718718
context.expressionContext().appendScope( mSymbolRenderContext->expressionContextScope() );
719719
QgsExpressionContextUtils::updateSymbolScope( this, mSymbolRenderContext->expressionContextScope() );
720-
mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_count", segmentizedGeometry->geometry()->partCount() );
721-
mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", 1 );
720+
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, segmentizedGeometry->geometry()->partCount() );
721+
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1 );
722722
}
723723

724724
switch ( QgsWKBTypes::flatType( segmentizedGeometry->geometry()->wkbType() ) )
@@ -786,7 +786,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
786786

787787
for ( int i = 0; i < mp->numGeometries(); ++i )
788788
{
789-
mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", i + 1 );
789+
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 );
790790

791791
const QgsPointV2* point = static_cast< const QgsPointV2* >( mp->geometryN( i ) );
792792
_getPoint( pt, context, point );
@@ -815,7 +815,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
815815

816816
for ( unsigned int i = 0; i < num; ++i )
817817
{
818-
mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", i + 1 );
818+
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 );
819819

820820
if ( geomCollection )
821821
{
@@ -848,7 +848,7 @@ void QgsSymbolV2::renderFeature( const QgsFeature& feature, QgsRenderContext& co
848848

849849
for ( unsigned int i = 0; i < num; ++i )
850850
{
851-
mSymbolRenderContext->expressionContextScope()->setVariable( "geometry_part_num", i + 1 );
851+
mSymbolRenderContext->expressionContextScope()->setVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, i + 1 );
852852

853853
if ( geomCollection )
854854
{

src/gui/symbology-ng/qgssizescalewidget.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,10 @@ static QgsExpressionContext _getExpressionContext( const void* context )
122122
if ( widget->layer() )
123123
expContext << QgsExpressionContextUtils::layerScope( widget->layer() );
124124

125-
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_count", 1, true ) );
126-
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_num", 1, true ) );
125+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, 1, true ) );
126+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) );
127127

128-
expContext.setHighlightedVariables( QStringList() << "geometry_part_num" );
128+
expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM );
129129

130130
return expContext;
131131
}

src/gui/symbology-ng/qgssymbollayerv2widget.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,21 @@ static QgsExpressionContext _getExpressionContext( const void* context )
7575
if ( layer )
7676
expContext << QgsExpressionContextUtils::layerScope( layer );
7777

78-
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_count", 1, true ) );
79-
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( "geometry_part_num", 1, true ) );
78+
QgsExpressionContextScope* symbolScope = QgsExpressionContextUtils::updateSymbolScope( nullptr, new QgsExpressionContextScope() );
79+
if ( const QgsSymbolLayerV2* symbolLayer = const_cast< QgsSymbolLayerV2Widget* >( widget )->symbolLayer() )
80+
{
81+
//cheat a bit - set the symbol color variable to match the symbol layer's color (when we should really be using the *symbols*
82+
//color, but that's not accessible here). 99% of the time these will be the same anyway
83+
symbolScope->setVariable( QgsExpressionContext::EXPR_SYMBOL_COLOR, symbolLayer->color() );
84+
}
85+
expContext << symbolScope;
86+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, 1, true ) );
87+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) );
8088

8189
//TODO - show actual value
8290
expContext.setOriginalValueVariable( QVariant() );
83-
expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << "geometry_part_count" << "geometry_part_num" );
91+
expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << QgsExpressionContext::EXPR_SYMBOL_COLOR
92+
<< QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM );
8493

8594
return expContext;
8695
}

0 commit comments

Comments
 (0)