Skip to content

Commit 3d70f1b

Browse files
committedSep 25, 2018
[layouts] Fix is_layer_visible does not work within layout map items
1 parent 0f62685 commit 3d70f1b

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
 

‎src/core/layout/qgslayoutitemmap.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "qgsmaplayerlistutils.h"
3030
#include "qgsmaplayerstylemanager.h"
3131
#include "qgsvectorlayer.h"
32+
#include "qgsexpressioncontext.h"
3233

3334
#include <QPainter>
3435
#include <QStyleOptionGraphicsItem>
@@ -1179,6 +1180,8 @@ QgsExpressionContext QgsLayoutItemMap::createExpressionContext() const
11791180
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layer_ids" ), layersIds, true ) );
11801181
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
11811182

1183+
scope->addFunction( QStringLiteral( "is_layer_visible" ), new QgsExpressionContextUtils::GetLayerVisibility( layersInMap ) );
1184+
11821185
return context;
11831186
}
11841187

‎src/core/qgsexpressioncontext.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,23 +955,39 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
955955
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
956956
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
957957

958+
// and because people don't read that ^^, I'm going to blast it all over this function
959+
958960
QgsExpressionContextScope *scope = new QgsExpressionContextScope( QObject::tr( "Map Settings" ) );
959961

960962
//add known map settings context variables
961963
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_id" ), "canvas", true ) );
962964
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_rotation" ), mapSettings.rotation(), true ) );
963965
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_scale" ), mapSettings.scale(), true ) );
966+
967+
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
968+
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
969+
964970
QgsGeometry extent = QgsGeometry::fromRect( mapSettings.visibleExtent() );
965971
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent" ), QVariant::fromValue( extent ), true ) );
966972
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_width" ), mapSettings.visibleExtent().width(), true ) );
967973
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_height" ), mapSettings.visibleExtent().height(), true ) );
974+
975+
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
976+
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
977+
968978
QgsGeometry centerPoint = QgsGeometry::fromPointXY( mapSettings.visibleExtent().center() );
969979
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_extent_center" ), QVariant::fromValue( centerPoint ), true ) );
970980

981+
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
982+
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
983+
971984
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs" ), mapSettings.destinationCrs().authid(), true ) );
972985
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_crs_definition" ), mapSettings.destinationCrs().toProj4(), true ) );
973986
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_units" ), QgsUnitTypes::toString( mapSettings.mapUnits() ), true ) );
974987

988+
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
989+
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
990+
975991
QVariantList layersIds;
976992
QVariantList layers;
977993
const QList<QgsMapLayer *> layersInMap = mapSettings.layers();
@@ -982,11 +998,21 @@ QgsExpressionContextScope *QgsExpressionContextUtils::mapSettingsScope( const Qg
982998
layersIds << layer->id();
983999
layers << QVariant::fromValue<QgsWeakMapLayerPointer>( QgsWeakMapLayerPointer( layer ) );
9841000
}
1001+
1002+
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
1003+
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
1004+
9851005
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layer_ids" ), layersIds, true ) );
9861006
scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "map_layers" ), layers, true ) );
9871007

1008+
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
1009+
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
1010+
9881011
scope->addFunction( QStringLiteral( "is_layer_visible" ), new GetLayerVisibility( mapSettings.layers() ) );
9891012

1013+
// IMPORTANT: ANY CHANGES HERE ALSO NEED TO BE MADE TO QgsLayoutItemMap::createExpressionContext()
1014+
// (rationale is described in QgsLayoutItemMap::createExpressionContext() )
1015+
9901016
return scope;
9911017
}
9921018

‎tests/src/core/testqgslayoutmap.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class TestQgsLayoutMap : public QObject
5757
void layersToRender();
5858
void mapRotation();
5959
void mapItemRotation();
60+
void expressionContext();
6061

6162
private:
6263
QgsRasterLayer *mRasterLayer = nullptr;
@@ -583,5 +584,56 @@ void TestQgsLayoutMap::mapItemRotation()
583584
QVERIFY( checker.testLayout( mReport, 0, 200 ) );
584585
}
585586

587+
void TestQgsLayoutMap::expressionContext()
588+
{
589+
QgsRectangle extent( 2000, 2800, 2500, 2900 );
590+
QgsLayout l( QgsProject::instance() );
591+
592+
QgsLayoutItemMap *map = new QgsLayoutItemMap( &l );
593+
map->setCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:4326" ) ) );
594+
map->attemptSetSceneRect( QRectF( 30, 60, 200, 100 ) );
595+
map->setExtent( extent );
596+
l.addLayoutItem( map );
597+
map->setId( QStringLiteral( "Map_id" ) );
598+
599+
QgsExpression e( QStringLiteral( "@map_scale" ) );
600+
601+
QgsExpressionContext c = map->createExpressionContext();
602+
QVariant r = e.evaluate( &c );
603+
QGSCOMPARENEAR( r.toDouble(), 184764103, 100 );
604+
605+
QgsExpression e2( QStringLiteral( "@map_crs" ) );
606+
r = e2.evaluate( &c );
607+
QCOMPARE( r.toString(), QString( "EPSG:4326" ) );
608+
609+
QgsExpression e3( QStringLiteral( "@map_crs_definition" ) );
610+
r = e3.evaluate( &c );
611+
QCOMPARE( r.toString(), QString( "+proj=longlat +datum=WGS84 +no_defs" ) );
612+
613+
QgsExpression e4( QStringLiteral( "@map_units" ) );
614+
r = e4.evaluate( &c );
615+
QCOMPARE( r.toString(), QString( "degrees" ) );
616+
617+
QgsVectorLayer *layer = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "A" ), QStringLiteral( "memory" ) );
618+
QgsVectorLayer *layer2 = new QgsVectorLayer( QStringLiteral( "Point?field=id_a:integer" ), QStringLiteral( "B" ), QStringLiteral( "memory" ) );
619+
map->setLayers( QList<QgsMapLayer *>() << layer << layer2 );
620+
QgsProject::instance()->addMapLayers( map->layers() );
621+
c = map->createExpressionContext();
622+
QgsExpression e5( QStringLiteral( "@map_layer_ids" ) );
623+
r = e5.evaluate( &c );
624+
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "%1,%2" ).arg( layer->id(), layer2->id() ) );
625+
e5 = QgsExpression( QStringLiteral( "array_foreach(@map_layers, layer_property(@element, 'name'))" ) );
626+
r = e5.evaluate( &c );
627+
QCOMPARE( r.toStringList().join( ',' ), QStringLiteral( "A,B" ) );
628+
629+
QgsExpression e6( QStringLiteral( "is_layer_visible( '%1' )" ).arg( layer->id() ) );
630+
r = e6.evaluate( &c );
631+
QCOMPARE( r.toBool(), true );
632+
633+
QgsExpression e7( QStringLiteral( "is_layer_visible( 'aaaaaa' )" ).arg( layer->id() ) );
634+
r = e7.evaluate( &c );
635+
QCOMPARE( r.toBool(), false );
636+
}
637+
586638
QGSTEST_MAIN( TestQgsLayoutMap )
587639
#include "testqgslayoutmap.moc"

0 commit comments

Comments
 (0)
Please sign in to comment.