@@ -312,14 +312,19 @@ static QgsExpression::Node* getNode( const QVariant& value, QgsExpression* paren
312
312
313
313
QgsVectorLayer* getVectorLayer ( const QVariant& value, QgsExpression* )
314
314
{
315
- QString layerString = value.toString ();
316
- QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance ()->mapLayer ( layerString ) ); // search by id first
315
+ QgsVectorLayer* vl = value.value <QgsVectorLayer*>();
317
316
if ( !vl )
318
317
{
319
- QList<QgsMapLayer *> layersByName = QgsMapLayerRegistry::instance ()->mapLayersByName ( layerString );
320
- if ( !layersByName.isEmpty () )
318
+ QString layerString = value.toString ();
319
+ vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance ()->mapLayer ( layerString ) ); // search by id first
320
+
321
+ if ( !vl )
321
322
{
322
- vl = qobject_cast<QgsVectorLayer*>( layersByName.at ( 0 ) );
323
+ QList<QgsMapLayer *> layersByName = QgsMapLayerRegistry::instance ()->mapLayersByName ( layerString );
324
+ if ( !layersByName.isEmpty () )
325
+ {
326
+ vl = qobject_cast<QgsVectorLayer*>( layersByName.at ( 0 ) );
327
+ }
323
328
}
324
329
}
325
330
@@ -707,8 +712,7 @@ static QVariant fcnAggregateRelation( const QVariantList& values, const QgsExpre
707
712
}
708
713
709
714
// first step - find current layer
710
- QString layerId = context->variable ( QStringLiteral ( " layer_id" ) ).toString ();
711
- QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance ()->mapLayer ( layerId ) );
715
+ QgsVectorLayer* vl = getVectorLayer ( context->variable ( " layer" ), parent );
712
716
if ( !vl )
713
717
{
714
718
parent->setEvalErrorString ( QObject::tr ( " Cannot use relation aggregate function in this context" ) );
@@ -810,8 +814,7 @@ static QVariant fcnAggregateGeneric( QgsAggregateCalculator::Aggregate aggregate
810
814
}
811
815
812
816
// first step - find current layer
813
- QString layerId = context->variable ( QStringLiteral ( " layer_id" ) ).toString ();
814
- QgsVectorLayer* vl = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance ()->mapLayer ( layerId ) );
817
+ QgsVectorLayer* vl = getVectorLayer ( context->variable ( " layer" ), parent );
815
818
if ( !vl )
816
819
{
817
820
parent->setEvalErrorString ( QObject::tr ( " Cannot use aggregate function in this context" ) );
@@ -1358,6 +1361,63 @@ static QVariant fcnAttribute( const QVariantList& values, const QgsExpressionCon
1358
1361
1359
1362
return feat.attribute ( attr );
1360
1363
}
1364
+
1365
+ static QVariant fcnIsSelected ( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent )
1366
+ {
1367
+ QgsVectorLayer* layer = nullptr ;
1368
+ QgsFeature feature;
1369
+
1370
+ if ( values.isEmpty () )
1371
+ {
1372
+ feature = context->feature ();
1373
+ layer = getVectorLayer ( context->variable ( " layer" ), parent );
1374
+ }
1375
+ else if ( values.size () == 1 )
1376
+ {
1377
+ layer = getVectorLayer ( context->variable ( " layer" ), parent );
1378
+ feature = getFeature ( values.at ( 0 ), parent );
1379
+ }
1380
+ else if ( values.size () == 2 )
1381
+ {
1382
+ layer = getVectorLayer ( values.at ( 0 ), parent );
1383
+ feature = getFeature ( values.at ( 1 ), parent );
1384
+ }
1385
+ else
1386
+ {
1387
+ parent->setEvalErrorString ( QObject::tr ( " Function `is_selected` requires no more than two parameters. %1 given." ).arg ( values.length () ) );
1388
+ return QVariant ();
1389
+ }
1390
+
1391
+ if ( !layer || !feature.isValid () )
1392
+ {
1393
+ return QVariant ( QVariant::Bool );
1394
+ }
1395
+
1396
+ return layer->selectedFeaturesIds ().contains ( feature.id () );
1397
+ }
1398
+
1399
+ static QVariant fcnNumSelected ( const QVariantList& values, const QgsExpressionContext* context, QgsExpression* parent )
1400
+ {
1401
+ QgsVectorLayer* layer = nullptr ;
1402
+
1403
+ if ( values.isEmpty () )
1404
+ layer = getVectorLayer ( context->variable ( " layer" ), parent );
1405
+ else if ( values.count () == 1 )
1406
+ layer = getVectorLayer ( values.at ( 0 ), parent );
1407
+ else
1408
+ {
1409
+ parent->setEvalErrorString ( QObject::tr ( " Function `num_selected` requires no more than one parameter. %1 given." ).arg ( values.length () ) );
1410
+ return QVariant ();
1411
+ }
1412
+
1413
+ if ( !layer )
1414
+ {
1415
+ return QVariant ( QVariant::LongLong );
1416
+ }
1417
+
1418
+ return layer->selectedFeatureCount ();
1419
+ }
1420
+
1361
1421
static QVariant fcnConcat ( const QVariantList& values, const QgsExpressionContext*, QgsExpression *parent )
1362
1422
{
1363
1423
QString concat;
@@ -3665,10 +3725,37 @@ const QList<QgsExpression::Function*>& QgsExpression::Functions()
3665
3725
<< Parameter ( QStringLiteral ( " vertex" ) ), fcnAngleAtVertex, QStringLiteral ( " GeometryGroup" ) )
3666
3726
<< new StaticFunction ( QStringLiteral ( " distance_to_vertex" ), ParameterList () << Parameter ( QStringLiteral ( " geometry" ) )
3667
3727
<< Parameter ( QStringLiteral ( " vertex" ) ), fcnDistanceToVertex, QStringLiteral ( " GeometryGroup" ) )
3728
+
3729
+
3730
+ // **Record** functions
3731
+
3668
3732
<< new StaticFunction ( QStringLiteral ( " $id" ), 0 , fcnFeatureId, QStringLiteral ( " Record" ) )
3669
3733
<< new StaticFunction ( QStringLiteral ( " $currentfeature" ), 0 , fcnFeature, QStringLiteral ( " Record" ) )
3670
3734
<< new StaticFunction ( QStringLiteral ( " uuid" ), 0 , fcnUuid, QStringLiteral ( " Record" ), QString (), false , QSet<QString>(), false , QStringList () << QStringLiteral ( " $uuid" ) )
3671
3735
<< new StaticFunction ( QStringLiteral ( " get_feature" ), 3 , fcnGetFeature, QStringLiteral ( " Record" ), QString (), false , QSet<QString>(), false , QStringList () << QStringLiteral ( " getFeature" ) )
3736
+
3737
+ << new StaticFunction (
3738
+ QStringLiteral ( " is_selected" ),
3739
+ -1 ,
3740
+ fcnIsSelected,
3741
+ QStringLiteral ( " Record" ),
3742
+ QString (),
3743
+ false ,
3744
+ QSet<QString>()
3745
+ )
3746
+
3747
+ << new StaticFunction (
3748
+ QStringLiteral ( " num_selected" ),
3749
+ -1 ,
3750
+ fcnNumSelected,
3751
+ QStringLiteral ( " Record" ),
3752
+ QString (),
3753
+ false ,
3754
+ QSet<QString>()
3755
+ )
3756
+
3757
+ // **General** functions
3758
+
3672
3759
<< new StaticFunction ( QStringLiteral ( " layer_property" ), 2 , fcnGetLayerProperty, QStringLiteral ( " General" ) )
3673
3760
<< new StaticFunction ( QStringLiteral ( " var" ), 1 , fcnGetVariable, QStringLiteral ( " General" ) )
3674
3761
@@ -5290,6 +5377,7 @@ void QgsExpression::initVariableHelp()
5290
5377
// layer variables
5291
5378
gVariableHelpTexts .insert ( QStringLiteral ( " layer_name" ), QCoreApplication::translate ( " variable_help" , " Name of current layer." ) );
5292
5379
gVariableHelpTexts .insert ( QStringLiteral ( " layer_id" ), QCoreApplication::translate ( " variable_help" , " ID of current layer." ) );
5380
+ gVariableHelpTexts .insert ( QStringLiteral ( " layer" ), QCoreApplication::translate ( " variable_help" , " The current layer." ) );
5293
5381
5294
5382
// composition variables
5295
5383
gVariableHelpTexts .insert ( QStringLiteral ( " layout_numpages" ), QCoreApplication::translate ( " variable_help" , " Number of pages in composition." ) );
0 commit comments