Skip to content
Permalink
Browse files

Add @geometry_point_count and @geometry_point_num

  • Loading branch information
Hugo Mercier
Hugo Mercier committed Apr 18, 2016
1 parent 34b7ebc commit 1ee0bc57042cabb0bb683d1dd4053766819ed9ea
@@ -4601,6 +4601,8 @@ void QgsExpression::initVariableHelp()
//symbol variables
gVariableHelpTexts.insert( "geometry_part_count", QCoreApplication::translate( "variable_help", "Number of parts in rendered feature's geometry." ) );
gVariableHelpTexts.insert( "geometry_part_num", QCoreApplication::translate( "variable_help", "Current geometry part number for feature being rendered." ) );
gVariableHelpTexts.insert( "geometry_point_count", QCoreApplication::translate( "variable_help", "Number of points in the rendered geometry's part. It is only meaningful for line geometries and for symbol layers that set this variable." ) );
gVariableHelpTexts.insert( "geometry_point_num", QCoreApplication::translate( "variable_help", "Current point number in the rendered geometry's part. It is only meaningful for line geometries and for symbol layers that set this variable." ) );

gVariableHelpTexts.insert( "symbol_color", QCoreApplication::translate( "symbol_color", "Color of symbol used to render the feature." ) );
gVariableHelpTexts.insert( "symbol_angle", QCoreApplication::translate( "symbol_angle", "Angle of symbol used to render the feature (valid for marker symbols only)." ) );
@@ -37,6 +37,8 @@ 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" );
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( "geometry_point_count" );
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( "geometry_point_num" );

//
// QgsExpressionContextScope
@@ -444,6 +444,10 @@ class CORE_EXPORT QgsExpressionContext
static const QString EXPR_GEOMETRY_PART_COUNT;
//! Inbuilt variable name for geometry part number variable
static const QString EXPR_GEOMETRY_PART_NUM;
//! Inbuilt variable name for point count variable
static const QString EXPR_GEOMETRY_POINT_COUNT;
//! Inbuilt variable name for point number variable
static const QString EXPR_GEOMETRY_POINT_NUM;

private:

@@ -150,6 +150,7 @@ QSet<QString> QgsArrowSymbolLayer::usedAttributes() const

void QgsArrowSymbolLayer::startRender( QgsSymbolV2RenderContext& context )
{
mExpressionScope.reset( new QgsExpressionContextScope() );
mScaledArrowWidth = QgsSymbolLayerV2Utils::convertToPainterUnits( context.renderContext(), arrowWidth(), arrowWidthUnit(), arrowWidthUnitScale() );
mScaledArrowStartWidth = QgsSymbolLayerV2Utils::convertToPainterUnits( context.renderContext(), arrowStartWidth(), arrowStartWidthUnit(), arrowStartWidthUnitScale() );
mScaledHeadSize = QgsSymbolLayerV2Utils::convertToPainterUnits( context.renderContext(), headSize(), headSizeUnit(), headSizeUnitScale() );
@@ -543,10 +544,14 @@ void QgsArrowSymbolLayer::renderPolyline( const QPolygonF& points, QgsSymbolV2Re
return;
}

context.renderContext().expressionContext().appendScope( mExpressionScope.data() );
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, points.size() + 1 );
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, 1 );
if ( isCurved() )
{
for ( int pIdx = 0; pIdx < points.size() - 1; pIdx += 2 )
{
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, pIdx + 1 );
_resolveDataDefined( context );

if ( points.size() - pIdx >= 3 )
@@ -579,6 +584,7 @@ void QgsArrowSymbolLayer::renderPolyline( const QPolygonF& points, QgsSymbolV2Re
// only straight arrows
for ( int pIdx = 0; pIdx < points.size() - 1; pIdx++ )
{
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, pIdx + 1 );
_resolveDataDefined( context );

// origin point
@@ -590,5 +596,6 @@ void QgsArrowSymbolLayer::renderPolyline( const QPolygonF& points, QgsSymbolV2Re
mSymbol->renderPolygon( poly, /* rings */ nullptr, context.feature(), context.renderContext() );
}
}
context.renderContext().expressionContext().popScope();
}

@@ -151,6 +151,8 @@ class CORE_EXPORT QgsArrowSymbolLayer : public QgsLineSymbolLayerV2
double mScaledOffset;
HeadType mComputedHeadType;

QScopedPointer<QgsExpressionContextScope> mExpressionScope;

void _resolveDataDefined( QgsSymbolV2RenderContext& );
};

@@ -85,11 +85,14 @@ static QgsExpressionContext _getExpressionContext( const void* context )
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 ) );
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, 1, true ) );
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, 1, true ) );

//TODO - show actual value
expContext.setOriginalValueVariable( QVariant() );
expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << QgsExpressionContext::EXPR_SYMBOL_COLOR
<< QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM );
<< QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM
<< QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT << QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM );

return expContext;
}
@@ -39,7 +39,8 @@
QgsRectangle,
QgsArrowSymbolLayer,
QgsSymbolV2,
QgsMultiRenderChecker
QgsMultiRenderChecker,
QgsDataDefined
)

from qgis.testing import start_app, unittest
@@ -76,7 +77,11 @@ def tearDown(self):

def test_1(self):
sym = self.lines_layer.rendererV2().symbol()
sym_layer = QgsArrowSymbolLayer.create({'arrow_width': '5', 'head_size': '6.5'})
sym_layer = QgsArrowSymbolLayer.create({'head_size': '6.5'})
dd = QgsDataDefined("(@geometry_point_num % 4) * 2")
sym_layer.setDataDefinedProperty("arrow_width", dd)
dd2 = QgsDataDefined("(@geometry_point_num % 4) * 2")
sym_layer.setDataDefinedProperty("head_size", dd2)
fill_sym = QgsFillSymbolV2.createSimple({'color': '#8bcfff', 'outline_color': '#000000', 'outline_style': 'solid', 'outline_width': '1'})
sym_layer.setSubSymbol(fill_sym)
sym.changeSymbolLayer(0, sym_layer)
Binary file not shown.

0 comments on commit 1ee0bc5

Please sign in to comment.
You can’t perform that action at this time.