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.
This commit is contained in:
Nyall Dawson 2019-01-22 11:53:35 +10:00
parent 9cf2ff31d8
commit cb7838badc
2 changed files with 87 additions and 1 deletions

View File

@ -889,6 +889,22 @@ static QVariant fcnAggregateArray( const QVariantList &values, const QgsExpressi
return fcnAggregateGeneric( QgsAggregateCalculator::ArrayAggregate, values, QgsAggregateCalculator::AggregateParameters(), context, parent );
}
static QVariant fcnMapScale( const QVariantList &, const QgsExpressionContext *context, QgsExpression *, const QgsExpressionNodeFunction * )
{
if ( !context )
return QVariant();
QVariant scale = context->variable( QStringLiteral( "map_scale" ) );
bool ok = false;
if ( !scale.isValid() || scale.isNull() )
return QVariant();
const double v = scale.toDouble( &ok );
if ( ok )
return v;
return QVariant();
}
static QVariant fcnClamp( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
double minValue = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
@ -4877,7 +4893,10 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
<< new QgsStaticExpressionFunction( QStringLiteral( "color_part" ), 2, fncColorPart, QStringLiteral( "Color" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "darker" ), 2, fncDarker, QStringLiteral( "Color" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "lighter" ), 2, fncLighter, QStringLiteral( "Color" ) )
<< new QgsStaticExpressionFunction( QStringLiteral( "set_color_part" ), 3, fncSetColorPart, QStringLiteral( "Color" ) );
<< new QgsStaticExpressionFunction( QStringLiteral( "set_color_part" ), 3, fncSetColorPart, QStringLiteral( "Color" ) )
// deprecated stuff - hidden from users
<< new QgsStaticExpressionFunction( QStringLiteral( "$scale" ), QgsExpressionFunction::ParameterList(), fcnMapScale, QStringLiteral( "deprecated" ) );
QgsStaticExpressionFunction *geomFunc = new QgsStaticExpressionFunction( QStringLiteral( "$geometry" ), 0, fcnGeometry, QStringLiteral( "GeometryGroup" ), QString(), true );
geomFunc->setIsStatic( false );

View File

@ -46,6 +46,7 @@ class TestQgsMapSettings: public QObject
void testXmlReadWrite();
void testSetLayers();
void testLabelBoundary();
void testExpressionContext();
private:
QString toString( const QPolygonF &p, int decimalPlaces = 2 ) const;
@ -376,5 +377,71 @@ void TestQgsMapSettings::testLabelBoundary()
QCOMPARE( ms.labelBoundaryGeometry().asWkt(), QStringLiteral( "Polygon ((0 0, 1 0, 1 1, 0 1, 0 0))" ) );
}
void TestQgsMapSettings::testExpressionContext()
{
QgsMapSettings ms;
QgsExpressionContext c;
QVariant r;
ms.setOutputSize( QSize( 5000, 5000 ) );
ms.setExtent( QgsRectangle( -1, 0, 2, 2 ) );
ms.setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
ms.setRotation( -32 );
c << QgsExpressionContextUtils::mapSettingsScope( ms );
QgsExpression e( QStringLiteral( "@map_scale" ) );
r = e.evaluate( &c );
QGSCOMPARENEAR( r.toDouble(), 247990, 10 );
// The old $scale function should silently map to @map_scale, so that older projects work without change
e = QgsExpression( QStringLiteral( "$scale" ) );
r = e.evaluate( &c );
QGSCOMPARENEAR( r.toDouble(), 247990, 10 );
// no map settings scope -- $scale is meaningless
e = QgsExpression( QStringLiteral( "$scale" ) );
r = e.evaluate( nullptr );
QVERIFY( !r.isValid() );
e = QgsExpression( QStringLiteral( "@map_id" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "canvas" ) );
e = QgsExpression( QStringLiteral( "@map_rotation" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDouble(), -32.0 );
ms.setRotation( 0 );
c << QgsExpressionContextUtils::mapSettingsScope( ms );
e = QgsExpression( QStringLiteral( "geom_to_wkt( @map_extent )" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "Polygon ((-1 -0.5, 2 -0.5, 2 2.5, -1 2.5, -1 -0.5))" ) );
e = QgsExpression( QStringLiteral( "@map_extent_width" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDouble(), 3 );
e = QgsExpression( QStringLiteral( "@map_extent_height" ) );
r = e.evaluate( &c );
QCOMPARE( r.toDouble(), 3 );
e = QgsExpression( QStringLiteral( "geom_to_wkt( @map_extent_center )" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "Point (0.5 1)" ) );
e = QgsExpression( QStringLiteral( "@map_crs" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "EPSG:4326" ) );
e = QgsExpression( QStringLiteral( "@map_crs_definition" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "+proj=longlat +datum=WGS84 +no_defs" ) );
e = QgsExpression( QStringLiteral( "@map_units" ) );
r = e.evaluate( &c );
QCOMPARE( r.toString(), QStringLiteral( "degrees" ) );
}
QGSTEST_MAIN( TestQgsMapSettings )
#include "testqgsmapsettings.moc"