Skip to content

Commit 0e19995

Browse files
committed
Improvements to layer handling in expressions
1 parent 066528d commit 0e19995

File tree

2 files changed

+28
-43
lines changed

2 files changed

+28
-43
lines changed

src/core/expression/qgsexpressionfunction.cpp

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3319,23 +3319,19 @@ static QVariant fcnGetFeatureById( const QVariantList &values, const QgsExpressi
33193319
{
33203320
QVariant result;
33213321
QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
3322-
if ( !vl )
3323-
return result;
3322+
if ( vl )
3323+
{
3324+
QgsFeatureId fid = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
33243325

3325-
QgsFeatureId fid = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
3326+
QgsFeatureRequest req;
3327+
req.setFilterFid( fid );
3328+
req.setLimit( 1 );
3329+
QgsFeatureIterator fIt = vl->getFeatures( req );
33263330

3327-
QgsFeatureRequest req;
3328-
req.setFilterFid( fid );
3329-
req.setLimit( 1 );
3330-
if ( !parent->needsGeometry() )
3331-
{
3332-
req.setFlags( QgsFeatureRequest::NoGeometry );
3331+
QgsFeature fet;
3332+
if ( fIt.nextFeature( fet ) )
3333+
result = QVariant::fromValue( fet );
33333334
}
3334-
QgsFeatureIterator fIt = vl->getFeatures( req );
3335-
3336-
QgsFeature fet;
3337-
if ( fIt.nextFeature( fet ) )
3338-
result = QVariant::fromValue( fet );
33393335

33403336
return result;
33413337
}
@@ -3378,18 +3374,7 @@ static QVariant fcnGetFeature( const QVariantList &values, const QgsExpressionCo
33783374

33793375
static QVariant fcnGetLayerProperty( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
33803376
{
3381-
QString layerIdOrName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );
3382-
3383-
//try to find a matching layer by name
3384-
QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerIdOrName ); //search by id first
3385-
if ( !layer )
3386-
{
3387-
QList<QgsMapLayer *> layersByName = QgsProject::instance()->mapLayersByName( layerIdOrName );
3388-
if ( !layersByName.isEmpty() )
3389-
{
3390-
layer = layersByName.at( 0 );
3391-
}
3392-
}
3377+
QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );
33933378

33943379
if ( !layer )
33953380
return QVariant();
@@ -4303,7 +4288,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
43034288
}
43044289

43054290
QgsWithVariableExpressionFunction::QgsWithVariableExpressionFunction()
4306-
: QgsExpressionFunction( QStringLiteral("with_variable"), 3, QCoreApplication::tr( "General" ) )
4291+
: QgsExpressionFunction( QStringLiteral( "with_variable" ), 3, QCoreApplication::tr( "General" ) )
43074292
{
43084293

43094294
}

src/core/expression/qgsexpressionutils.h

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -333,26 +333,26 @@ class QgsExpressionUtils
333333
return nullptr;
334334
}
335335

336-
static QgsVectorLayer *getVectorLayer( const QVariant &value, QgsExpression * )
336+
static QgsMapLayer *getMapLayer( const QVariant &value, QgsExpression * )
337337
{
338+
// First check if we already received a layer pointer
338339
QgsMapLayer *ml = value.value< QgsWeakMapLayerPointer >().data();
339-
QgsVectorLayer *vl = qobject_cast<QgsVectorLayer *>( ml );
340-
if ( !vl )
341-
{
342-
QString layerString = value.toString();
343-
vl = qobject_cast<QgsVectorLayer *>( QgsProject::instance()->mapLayer( layerString ) ); //search by id first
340+
QgsProject *project = QgsProject::instance();
344341

345-
if ( !vl )
346-
{
347-
QList<QgsMapLayer *> layersByName = QgsProject::instance()->mapLayersByName( layerString );
348-
if ( !layersByName.isEmpty() )
349-
{
350-
vl = qobject_cast<QgsVectorLayer *>( layersByName.at( 0 ) );
351-
}
352-
}
353-
}
342+
// No pointer yet, maybe it's a layer id?
343+
if ( !ml )
344+
ml = project->mapLayer( value.toString() );
354345

355-
return vl;
346+
// Still nothing? Check for layer name
347+
if ( !ml )
348+
ml = project->mapLayersByName( value.toString() ).value( 0 );
349+
350+
return ml;
351+
}
352+
353+
static QgsVectorLayer *getVectorLayer( const QVariant &value, QgsExpression *e )
354+
{
355+
return qobject_cast<QgsVectorLayer *>( getMapLayer( value, e ) );
356356
}
357357

358358

0 commit comments

Comments
 (0)