@@ -4730,6 +4730,7 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
4730
4730
4731
4731
// functions for arrays
4732
4732
<< new QgsArrayForeachExpressionFunction ()
4733
+ << new QgsArrayFilterExpressionFunction ()
4733
4734
<< new QgsStaticExpressionFunction ( QStringLiteral ( " array" ), -1 , fcnArray, QStringLiteral ( " Arrays" ), QString (), false , QSet<QString>(), false , QStringList (), true )
4734
4735
<< new QgsStaticExpressionFunction ( QStringLiteral ( " array_length" ), 1 , fcnArrayLength, QStringLiteral ( " Arrays" ) )
4735
4736
<< new QgsStaticExpressionFunction ( QStringLiteral ( " array_contains" ), QgsExpressionFunction::ParameterList () << QgsExpressionFunction::Parameter ( QStringLiteral ( " array" ) ) << QgsExpressionFunction::Parameter ( QStringLiteral ( " value" ) ), fcnArrayContains, QStringLiteral ( " Arrays" ) )
@@ -4869,6 +4870,100 @@ bool QgsArrayForeachExpressionFunction::prepare( const QgsExpressionNodeFunction
4869
4870
return true ;
4870
4871
}
4871
4872
4873
+ QgsArrayFilterExpressionFunction::QgsArrayFilterExpressionFunction ()
4874
+ : QgsExpressionFunction( QStringLiteral( " array_filter" ), QgsExpressionFunction::ParameterList()
4875
+ << QgsExpressionFunction::Parameter( QStringLiteral( " array" ) )
4876
+ << QgsExpressionFunction::Parameter( QStringLiteral( " expression" ) ),
4877
+ QCoreApplication::tr( " Arrays" ) )
4878
+ {
4879
+
4880
+ }
4881
+
4882
+ bool QgsArrayFilterExpressionFunction::isStatic ( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const
4883
+ {
4884
+ bool isStatic = false ;
4885
+
4886
+ QgsExpressionNode::NodeList *args = node->args ();
4887
+
4888
+ if ( args->count () < 2 )
4889
+ return false ;
4890
+
4891
+ if ( args->at ( 0 )->isStatic ( parent, context ) && args->at ( 1 )->isStatic ( parent, context ) )
4892
+ {
4893
+ isStatic = true ;
4894
+ }
4895
+ return isStatic;
4896
+ }
4897
+
4898
+ QVariant QgsArrayFilterExpressionFunction::run ( QgsExpressionNode::NodeList *args, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
4899
+ {
4900
+ Q_UNUSED ( node )
4901
+ QVariantList result;
4902
+
4903
+ if ( args->count () < 2 )
4904
+ // error
4905
+ return result;
4906
+
4907
+ const QVariantList array = args->at ( 0 )->eval ( parent, context ).toList ();
4908
+
4909
+ QgsExpressionContext *subContext = const_cast <QgsExpressionContext *>( context );
4910
+ std::unique_ptr< QgsExpressionContext > tempContext;
4911
+ if ( !subContext )
4912
+ {
4913
+ tempContext = qgis::make_unique< QgsExpressionContext >();
4914
+ subContext = tempContext.get ();
4915
+ }
4916
+
4917
+ QgsExpressionContextScope *subScope = new QgsExpressionContextScope ();
4918
+ subContext->appendScope ( subScope );
4919
+
4920
+ for ( const QVariant &value : array )
4921
+ {
4922
+ subScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " element" ), value, true ) );
4923
+ if ( args->at ( 1 )->eval ( parent, subContext ).toBool () )
4924
+ result << value;
4925
+ }
4926
+
4927
+ if ( context )
4928
+ delete subContext->popScope ();
4929
+
4930
+ return result;
4931
+ }
4932
+
4933
+ QVariant QgsArrayFilterExpressionFunction::func ( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction *node )
4934
+ {
4935
+ // This is a dummy function, all the real handling is in run
4936
+ Q_UNUSED ( values )
4937
+ Q_UNUSED ( context )
4938
+ Q_UNUSED ( parent )
4939
+ Q_UNUSED ( node )
4940
+
4941
+ Q_ASSERT ( false );
4942
+ return QVariant ();
4943
+ }
4944
+
4945
+ bool QgsArrayFilterExpressionFunction::prepare ( const QgsExpressionNodeFunction *node, QgsExpression *parent, const QgsExpressionContext *context ) const
4946
+ {
4947
+ QgsExpressionNode::NodeList *args = node->args ();
4948
+
4949
+ if ( args->count () < 2 )
4950
+ // error
4951
+ return false ;
4952
+
4953
+ args->at ( 0 )->prepare ( parent, context );
4954
+
4955
+ QgsExpressionContext subContext;
4956
+ if ( context )
4957
+ subContext = *context;
4958
+
4959
+ QgsExpressionContextScope *subScope = new QgsExpressionContextScope ();
4960
+ subScope->addVariable ( QgsExpressionContextScope::StaticVariable ( QStringLiteral ( " element" ), QVariant (), true ) );
4961
+ subContext.appendScope ( subScope );
4962
+
4963
+ args->at ( 1 )->prepare ( parent, &subContext );
4964
+
4965
+ return true ;
4966
+ }
4872
4967
QgsWithVariableExpressionFunction::QgsWithVariableExpressionFunction ()
4873
4968
: QgsExpressionFunction( QStringLiteral( " with_variable" ), QgsExpressionFunction::ParameterList() <<
4874
4969
QgsExpressionFunction::Parameter( QStringLiteral( " name" ) )
0 commit comments