Skip to content

Commit 0f62685

Browse files
committed
Fix is_layer_visible to allow direct layer objects and handle removed layers
1 parent f29c48b commit 0f62685

File tree

4 files changed

+68
-40
lines changed

4 files changed

+68
-40
lines changed

python/core/auto_generated/qgsexpressioncontext.sip.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,6 +1075,7 @@ Creates a new scope which contains variables and functions relating to provider
10751075
Registers all known core functions provided by QgsExpressionContextScope objects.
10761076
%End
10771077

1078+
public:
10781079
};
10791080

10801081
/************************************************************************

src/core/qgsexpressioncontext.cpp

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "qgslayout.h"
3333
#include "qgslayoutpagecollection.h"
3434
#include "qgslayoutreportcontext.h"
35+
#include "qgsexpressionutils.h"
3536

3637
#include <QSettings>
3738
#include <QDir>
@@ -712,44 +713,6 @@ class GetLayoutItemVariables : public QgsScopedExpressionFunction
712713

713714
};
714715

715-
class GetLayerVisibility : public QgsScopedExpressionFunction
716-
{
717-
public:
718-
GetLayerVisibility( const QList<QgsMapLayer *> &layers )
719-
: QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
720-
, mLayers( layers )
721-
{}
722-
723-
QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override
724-
{
725-
if ( mLayers.isEmpty() )
726-
{
727-
return QVariant( false );
728-
}
729-
730-
QgsMapLayer *layer = _qgis_findLayer( mLayers, values.at( 0 ).toString() );
731-
if ( layer )
732-
{
733-
return QVariant( true );
734-
}
735-
else
736-
{
737-
return QVariant( false );
738-
}
739-
}
740-
741-
QgsScopedExpressionFunction *clone() const override
742-
{
743-
return new GetLayerVisibility( mLayers );
744-
}
745-
746-
private:
747-
748-
const QList<QgsMapLayer *> mLayers;
749-
750-
};
751-
752-
753716
class GetCurrentFormFieldValue : public QgsScopedExpressionFunction
754717
{
755718
public:
@@ -1340,3 +1303,35 @@ bool QgsScopedExpressionFunction::isStatic( const QgsExpressionNodeFunction *nod
13401303
{
13411304
return allParamsStatic( node, parent, context );
13421305
}
1306+
1307+
//
1308+
// GetLayerVisibility
1309+
//
1310+
1311+
QgsExpressionContextUtils::GetLayerVisibility::GetLayerVisibility( const QList<QgsMapLayer *> &layers )
1312+
: QgsScopedExpressionFunction( QStringLiteral( "is_layer_visible" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "id" ) ), QStringLiteral( "General" ) )
1313+
, mLayers( _qgis_listRawToQPointer( layers ) )
1314+
{}
1315+
1316+
QVariant QgsExpressionContextUtils::GetLayerVisibility::func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
1317+
{
1318+
if ( mLayers.isEmpty() )
1319+
{
1320+
return false;
1321+
}
1322+
1323+
QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
1324+
if ( layer )
1325+
{
1326+
return mLayers.contains( layer );
1327+
}
1328+
else
1329+
{
1330+
return false;
1331+
}
1332+
}
1333+
1334+
QgsScopedExpressionFunction *QgsExpressionContextUtils::GetLayerVisibility::clone() const
1335+
{
1336+
return new GetLayerVisibility( _qgis_listQPointerToRaw( mLayers ) );
1337+
}

src/core/qgsexpressioncontext.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,23 @@ class CORE_EXPORT QgsExpressionContextUtils
988988
*/
989989
static void registerContextFunctions();
990990

991+
private:
992+
993+
class GetLayerVisibility : public QgsScopedExpressionFunction
994+
{
995+
public:
996+
GetLayerVisibility( const QList<QgsMapLayer *> &layers );
997+
QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression *, const QgsExpressionNodeFunction * ) override;
998+
QgsScopedExpressionFunction *clone() const override;
999+
1000+
private:
1001+
1002+
const QList< QPointer< QgsMapLayer > > mLayers;
1003+
1004+
};
1005+
1006+
friend class QgsLayoutItemMap; // needs access to GetLayerVisibility
1007+
9911008
};
9921009

9931010
#endif // QGSEXPRESSIONCONTEXT_H

tests/src/core/testqgsmapsettings.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ void TestQgsMapSettings::testIsLayerVisible()
176176
QList<QgsMapLayer *> layers;
177177
layers << vlA << vlB;
178178

179+
QgsProject::instance()->addMapLayers( layers );
180+
179181
QgsMapSettings ms;
180182
ms.setLayers( layers );
181183
QgsExpressionContext context;
@@ -186,6 +188,11 @@ void TestQgsMapSettings::testIsLayerVisible()
186188
QVariant r = e.evaluate( &context );
187189
QCOMPARE( r.toBool(), true );
188190

191+
// test checking for visible layer by direct map layer object
192+
QgsExpression e4( QStringLiteral( "is_layer_visible(array_get( @map_layers, 0 ) )" ) );
193+
r = e4.evaluate( &context );
194+
QCOMPARE( r.toBool(), true );
195+
189196
// test checking for visible layer by name
190197
QgsExpression e2( QStringLiteral( "is_layer_visible( '%1' )" ).arg( vlB-> name() ) );
191198
r = e2.evaluate( &context );
@@ -196,8 +203,16 @@ void TestQgsMapSettings::testIsLayerVisible()
196203
r = e3.evaluate( &context );
197204
QCOMPARE( r.toBool(), false );
198205

199-
delete vlA;
200-
delete vlB;
206+
QgsProject::instance()->removeMapLayer( vlA );
207+
r = e.evaluate( &context );
208+
QCOMPARE( r.toBool(), false ); // layer is deleted
209+
r = e2.evaluate( &context );
210+
QCOMPARE( r.toBool(), true ); // layer still exists
211+
212+
QgsProject::instance()->removeMapLayer( vlB );
213+
r = e2.evaluate( &context );
214+
QCOMPARE( r.toBool(), false ); // layer is deleted
215+
201216
}
202217

203218
void TestQgsMapSettings::testMapLayerListUtils()

0 commit comments

Comments
 (0)