Skip to content
Permalink
Browse files

Improvements to layer handling in expressions

  • Loading branch information
m-kuhn committed Jul 7, 2017
1 parent 066528d commit 0e19995762f2e3aa6f0f64901de848d5772070bf
Showing with 28 additions and 43 deletions.
  1. +12 −27 src/core/expression/qgsexpressionfunction.cpp
  2. +16 −16 src/core/expression/qgsexpressionutils.h
@@ -3319,23 +3319,19 @@ static QVariant fcnGetFeatureById( const QVariantList &values, const QgsExpressi
{
QVariant result;
QgsVectorLayer *vl = QgsExpressionUtils::getVectorLayer( values.at( 0 ), parent );
if ( !vl )
return result;
if ( vl )
{
QgsFeatureId fid = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );

QgsFeatureId fid = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
QgsFeatureRequest req;
req.setFilterFid( fid );
req.setLimit( 1 );
QgsFeatureIterator fIt = vl->getFeatures( req );

QgsFeatureRequest req;
req.setFilterFid( fid );
req.setLimit( 1 );
if ( !parent->needsGeometry() )
{
req.setFlags( QgsFeatureRequest::NoGeometry );
QgsFeature fet;
if ( fIt.nextFeature( fet ) )
result = QVariant::fromValue( fet );
}
QgsFeatureIterator fIt = vl->getFeatures( req );

QgsFeature fet;
if ( fIt.nextFeature( fet ) )
result = QVariant::fromValue( fet );

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

static QVariant fcnGetLayerProperty( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent )
{
QString layerIdOrName = QgsExpressionUtils::getStringValue( values.at( 0 ), parent );

//try to find a matching layer by name
QgsMapLayer *layer = QgsProject::instance()->mapLayer( layerIdOrName ); //search by id first
if ( !layer )
{
QList<QgsMapLayer *> layersByName = QgsProject::instance()->mapLayersByName( layerIdOrName );
if ( !layersByName.isEmpty() )
{
layer = layersByName.at( 0 );
}
}
QgsMapLayer *layer = QgsExpressionUtils::getMapLayer( values.at( 0 ), parent );

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

QgsWithVariableExpressionFunction::QgsWithVariableExpressionFunction()
: QgsExpressionFunction( QStringLiteral("with_variable"), 3, QCoreApplication::tr( "General" ) )
: QgsExpressionFunction( QStringLiteral( "with_variable" ), 3, QCoreApplication::tr( "General" ) )
{

}
@@ -333,26 +333,26 @@ class QgsExpressionUtils
return nullptr;
}

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

if ( !vl )
{
QList<QgsMapLayer *> layersByName = QgsProject::instance()->mapLayersByName( layerString );
if ( !layersByName.isEmpty() )
{
vl = qobject_cast<QgsVectorLayer *>( layersByName.at( 0 ) );
}
}
}
// No pointer yet, maybe it's a layer id?
if ( !ml )
ml = project->mapLayer( value.toString() );

return vl;
// Still nothing? Check for layer name
if ( !ml )
ml = project->mapLayersByName( value.toString() ).value( 0 );

return ml;
}

static QgsVectorLayer *getVectorLayer( const QVariant &value, QgsExpression *e )
{
return qobject_cast<QgsVectorLayer *>( getMapLayer( value, e ) );
}


0 comments on commit 0e19995

Please sign in to comment.
You can’t perform that action at this time.