Skip to content

Commit 1ee0bc5

Browse files
author
Hugo Mercier
committed
Add @geometry_point_count and @geometry_point_num
1 parent 34b7ebc commit 1ee0bc5

File tree

8 files changed

+28
-3
lines changed

8 files changed

+28
-3
lines changed

src/core/qgsexpression.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -4601,6 +4601,8 @@ void QgsExpression::initVariableHelp()
46014601
//symbol variables
46024602
gVariableHelpTexts.insert( "geometry_part_count", QCoreApplication::translate( "variable_help", "Number of parts in rendered feature's geometry." ) );
46034603
gVariableHelpTexts.insert( "geometry_part_num", QCoreApplication::translate( "variable_help", "Current geometry part number for feature being rendered." ) );
4604+
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." ) );
4605+
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." ) );
46044606

46054607
gVariableHelpTexts.insert( "symbol_color", QCoreApplication::translate( "symbol_color", "Color of symbol used to render the feature." ) );
46064608
gVariableHelpTexts.insert( "symbol_angle", QCoreApplication::translate( "symbol_angle", "Angle of symbol used to render the feature (valid for marker symbols only)." ) );

src/core/qgsexpressioncontext.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ const QString QgsExpressionContext::EXPR_SYMBOL_COLOR( "symbol_color" );
3737
const QString QgsExpressionContext::EXPR_SYMBOL_ANGLE( "symbol_angle" );
3838
const QString QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT( "geometry_part_count" );
3939
const QString QgsExpressionContext::EXPR_GEOMETRY_PART_NUM( "geometry_part_num" );
40+
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT( "geometry_point_count" );
41+
const QString QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM( "geometry_point_num" );
4042

4143
//
4244
// QgsExpressionContextScope

src/core/qgsexpressioncontext.h

+4
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,10 @@ class CORE_EXPORT QgsExpressionContext
444444
static const QString EXPR_GEOMETRY_PART_COUNT;
445445
//! Inbuilt variable name for geometry part number variable
446446
static const QString EXPR_GEOMETRY_PART_NUM;
447+
//! Inbuilt variable name for point count variable
448+
static const QString EXPR_GEOMETRY_POINT_COUNT;
449+
//! Inbuilt variable name for point number variable
450+
static const QString EXPR_GEOMETRY_POINT_NUM;
447451

448452
private:
449453

src/core/symbology-ng/qgsarrowsymbollayer.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ QSet<QString> QgsArrowSymbolLayer::usedAttributes() const
150150

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

547+
context.renderContext().expressionContext().appendScope( mExpressionScope.data() );
548+
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, points.size() + 1 );
549+
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, 1 );
546550
if ( isCurved() )
547551
{
548552
for ( int pIdx = 0; pIdx < points.size() - 1; pIdx += 2 )
549553
{
554+
mExpressionScope->setVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, pIdx + 1 );
550555
_resolveDataDefined( context );
551556

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

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

src/core/symbology-ng/qgsarrowsymbollayer.h

+2
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ class CORE_EXPORT QgsArrowSymbolLayer : public QgsLineSymbolLayerV2
151151
double mScaledOffset;
152152
HeadType mComputedHeadType;
153153

154+
QScopedPointer<QgsExpressionContextScope> mExpressionScope;
155+
154156
void _resolveDataDefined( QgsSymbolV2RenderContext& );
155157
};
156158

src/gui/symbology-ng/qgssymbollayerv2widget.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ static QgsExpressionContext _getExpressionContext( const void* context )
8585
expContext << symbolScope;
8686
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT, 1, true ) );
8787
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_PART_NUM, 1, true ) );
88+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT, 1, true ) );
89+
expContext.lastScope()->addVariable( QgsExpressionContextScope::StaticVariable( QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM, 1, true ) );
8890

8991
//TODO - show actual value
9092
expContext.setOriginalValueVariable( QVariant() );
9193
expContext.setHighlightedVariables( QStringList() << QgsExpressionContext::EXPR_ORIGINAL_VALUE << QgsExpressionContext::EXPR_SYMBOL_COLOR
92-
<< QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM );
94+
<< QgsExpressionContext::EXPR_GEOMETRY_PART_COUNT << QgsExpressionContext::EXPR_GEOMETRY_PART_NUM
95+
<< QgsExpressionContext::EXPR_GEOMETRY_POINT_COUNT << QgsExpressionContext::EXPR_GEOMETRY_POINT_NUM );
9396

9497
return expContext;
9598
}

tests/src/python/test_qgsarrowsymbollayer.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
QgsRectangle,
4040
QgsArrowSymbolLayer,
4141
QgsSymbolV2,
42-
QgsMultiRenderChecker
42+
QgsMultiRenderChecker,
43+
QgsDataDefined
4344
)
4445

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

7778
def test_1(self):
7879
sym = self.lines_layer.rendererV2().symbol()
79-
sym_layer = QgsArrowSymbolLayer.create({'arrow_width': '5', 'head_size': '6.5'})
80+
sym_layer = QgsArrowSymbolLayer.create({'head_size': '6.5'})
81+
dd = QgsDataDefined("(@geometry_point_num % 4) * 2")
82+
sym_layer.setDataDefinedProperty("arrow_width", dd)
83+
dd2 = QgsDataDefined("(@geometry_point_num % 4) * 2")
84+
sym_layer.setDataDefinedProperty("head_size", dd2)
8085
fill_sym = QgsFillSymbolV2.createSimple({'color': '#8bcfff', 'outline_color': '#000000', 'outline_style': 'solid', 'outline_width': '1'})
8186
sym_layer.setSubSymbol(fill_sym)
8287
sym.changeSymbolLayer(0, sym_layer)
Loading

0 commit comments

Comments
 (0)