Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improvements to layer handling in expressions
  • Loading branch information
m-kuhn committed Jul 7, 2017
1 parent 066528d commit 0e19995
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 43 deletions.
39 changes: 12 additions & 27 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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" ) )
{

}
Expand Down
32 changes: 16 additions & 16 deletions src/core/expression/qgsexpressionutils.h
Expand Up @@ -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 ) );
}


Expand Down

0 comments on commit 0e19995

Please sign in to comment.