Navigation Menu

Skip to content

Commit

Permalink
Show variables from symbol scope in symbol layer widget expression
Browse files Browse the repository at this point in the history
builders. (This lets users know they are available).

Also swap some variable names to static strings for speed.
  • Loading branch information
nyalldawson committed Jan 27, 2016
1 parent 9b6fa0d commit fe95e4e
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 11 deletions.
9 changes: 9 additions & 0 deletions python/core/qgsexpressioncontext.sip
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/core/qgsexpressioncontext.cpp
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions src/core/qgsexpressioncontext.h
Expand Up @@ -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:

Expand Down
10 changes: 5 additions & 5 deletions src/core/symbology-ng/qgssymbolv2.cpp
Expand Up @@ -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() ) )
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 )
{
Expand Down Expand Up @@ -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 )
{
Expand Down
6 changes: 3 additions & 3 deletions src/gui/symbology-ng/qgssizescalewidget.cpp
Expand Up @@ -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;
}
Expand Down
15 changes: 12 additions & 3 deletions src/gui/symbology-ng/qgssymbollayerv2widget.cpp
Expand Up @@ -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;
}
Expand Down

0 comments on commit fe95e4e

Please sign in to comment.