Skip to content

Commit 7bc0844

Browse files
committedJan 24, 2019
Silently alias old "$scale" expression function to "@map_scale" (when available)
Allows older projects to open without change. We still hide $scale from the builder UI, as we eventually want to clamp out its use. (cherry picked from commit cb7838b)
1 parent bfef851 commit 7bc0844

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed
 

‎src/core/expression/qgsexpressionfunction.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -884,6 +884,22 @@ static QVariant fcnAggregateArray( const QVariantList &values, const QgsExpressi
884884
return fcnAggregateGeneric( QgsAggregateCalculator::ArrayAggregate, values, QgsAggregateCalculator::AggregateParameters(), context, parent );
885885
}
886886

887+
static QVariant fcnMapScale( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * )
888+
{
889+
if ( !context )
890+
return QVariant();
891+
892+
QVariant scale = context->variable( QStringLiteral( "map_scale" ) );
893+
bool ok = false;
894+
if ( !scale.isValid() || scale.isNull() )
895+
return QVariant();
896+
897+
const double v = scale.toDouble( &ok );
898+
if ( ok )
899+
return v;
900+
return QVariant();
901+
}
902+
887903
static QVariant fcnClamp( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
888904
{
889905
double minValue = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
@@ -4593,7 +4609,10 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
45934609
<< new QgsStaticExpressionFunction( QStringLiteral( "color_part" ), 2, fncColorPart, QStringLiteral( "Color" ) )
45944610
<< new QgsStaticExpressionFunction( QStringLiteral( "darker" ), 2, fncDarker, QStringLiteral( "Color" ) )
45954611
<< new QgsStaticExpressionFunction( QStringLiteral( "lighter" ), 2, fncLighter, QStringLiteral( "Color" ) )
4596-
<< new QgsStaticExpressionFunction( QStringLiteral( "set_color_part" ), 3, fncSetColorPart, QStringLiteral( "Color" ) );
4612+
<< new QgsStaticExpressionFunction( QStringLiteral( "set_color_part" ), 3, fncSetColorPart, QStringLiteral( "Color" ) )
4613+
4614+
// deprecated stuff - hidden from users
4615+
<< new QgsStaticExpressionFunction( QStringLiteral( "$scale" ), QgsExpressionFunction::ParameterList(), fcnMapScale, QStringLiteral( "deprecated" ) );
45974616

45984617
QgsStaticExpressionFunction *geomFunc = new QgsStaticExpressionFunction( QStringLiteral( "$geometry" ), 0, fcnGeometry, QStringLiteral( "GeometryGroup" ), QString(), true );
45994618
geomFunc->setIsStatic( false );

‎tests/src/core/testqgsmapsettings.cpp

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ class TestQgsMapSettings: public QObject
4545
void testMapLayerListUtils();
4646
void testXmlReadWrite();
4747
void testSetLayers();
48+
void testExpressionContext();
4849

4950
private:
5051
QString toString( const QPolygonF &p, int decimalPlaces = 2 ) const;
@@ -367,5 +368,71 @@ void TestQgsMapSettings::testSetLayers()
367368
QCOMPARE( ms.layers(), QList< QgsMapLayer * >() << vlA.get() << vlB.get() );
368369
}
369370

371+
void TestQgsMapSettings::testExpressionContext()
372+
{
373+
QgsMapSettings ms;
374+
QgsExpressionContext c;
375+
QVariant r;
376+
377+
ms.setOutputSize( QSize( 5000, 5000 ) );
378+
ms.setExtent( QgsRectangle( -1, 0, 2, 2 ) );
379+
ms.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
380+
ms.setRotation( -32 );
381+
c << QgsExpressionContextUtils::mapSettingsScope( ms );
382+
383+
QgsExpression e( QStringLiteral( "@map_scale" ) );
384+
r = e.evaluate( &c );
385+
QGSCOMPARENEAR( r.toDouble(), 247990, 10 );
386+
387+
// The old $scale function should silently map to @map_scale, so that older projects work without change
388+
e = QgsExpression( QStringLiteral( "$scale" ) );
389+
r = e.evaluate( &c );
390+
QGSCOMPARENEAR( r.toDouble(), 247990, 10 );
391+
392+
// no map settings scope -- $scale is meaningless
393+
e = QgsExpression( QStringLiteral( "$scale" ) );
394+
r = e.evaluate( nullptr );
395+
QVERIFY( !r.isValid() );
396+
397+
e = QgsExpression( QStringLiteral( "@map_id" ) );
398+
r = e.evaluate( &c );
399+
QCOMPARE( r.toString(), QStringLiteral( "canvas" ) );
400+
401+
e = QgsExpression( QStringLiteral( "@map_rotation" ) );
402+
r = e.evaluate( &c );
403+
QCOMPARE( r.toDouble(), -32.0 );
404+
405+
ms.setRotation( 0 );
406+
c << QgsExpressionContextUtils::mapSettingsScope( ms );
407+
408+
e = QgsExpression( QStringLiteral( "geom_to_wkt( @map_extent )" ) );
409+
r = e.evaluate( &c );
410+
QCOMPARE( r.toString(), QStringLiteral( "Polygon ((-1 -0.5, 2 -0.5, 2 2.5, -1 2.5, -1 -0.5))" ) );
411+
412+
e = QgsExpression( QStringLiteral( "@map_extent_width" ) );
413+
r = e.evaluate( &c );
414+
QCOMPARE( r.toDouble(), 3 );
415+
416+
e = QgsExpression( QStringLiteral( "@map_extent_height" ) );
417+
r = e.evaluate( &c );
418+
QCOMPARE( r.toDouble(), 3 );
419+
420+
e = QgsExpression( QStringLiteral( "geom_to_wkt( @map_extent_center )" ) );
421+
r = e.evaluate( &c );
422+
QCOMPARE( r.toString(), QStringLiteral( "Point (0.5 1)" ) );
423+
424+
e = QgsExpression( QStringLiteral( "@map_crs" ) );
425+
r = e.evaluate( &c );
426+
QCOMPARE( r.toString(), QStringLiteral( "EPSG:4326" ) );
427+
428+
e = QgsExpression( QStringLiteral( "@map_crs_definition" ) );
429+
r = e.evaluate( &c );
430+
QCOMPARE( r.toString(), QStringLiteral( "+proj=longlat +datum=WGS84 +no_defs" ) );
431+
432+
e = QgsExpression( QStringLiteral( "@map_units" ) );
433+
r = e.evaluate( &c );
434+
QCOMPARE( r.toString(), QStringLiteral( "degrees" ) );
435+
}
436+
370437
QGSTEST_MAIN( TestQgsMapSettings )
371438
#include "testqgsmapsettings.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.