|
29 | 29 | #include "qgsapplication.h" |
30 | 30 | #include "qgsmapsettings.h" |
31 | 31 | #include "qgsmaplayerlistutils.h" |
| 32 | +#include "qgsprocessingcontext.h" |
| 33 | +#include "qgsprocessingalgorithm.h" |
32 | 34 |
|
33 | 35 | #include <QSettings> |
34 | 36 | #include <QDir> |
@@ -707,6 +709,30 @@ class GetLayerVisibility : public QgsScopedExpressionFunction |
707 | 709 |
|
708 | 710 | }; |
709 | 711 |
|
| 712 | +class GetProcessingParameterValue : public QgsScopedExpressionFunction |
| 713 | +{ |
| 714 | + public: |
| 715 | + GetProcessingParameterValue( const QVariantMap ¶ms ) |
| 716 | + : QgsScopedExpressionFunction( QStringLiteral( "parameter" ), QgsExpressionFunction::ParameterList() << QgsExpressionFunction::Parameter( QStringLiteral( "name" ) ), QStringLiteral( "Processing" ) ) |
| 717 | + , mParams( params ) |
| 718 | + {} |
| 719 | + |
| 720 | + QVariant func( const QVariantList &values, const QgsExpressionContext *, QgsExpression * ) override |
| 721 | + { |
| 722 | + return mParams.value( values.at( 0 ).toString() ); |
| 723 | + } |
| 724 | + |
| 725 | + QgsScopedExpressionFunction *clone() const override |
| 726 | + { |
| 727 | + return new GetProcessingParameterValue( mParams ); |
| 728 | + } |
| 729 | + |
| 730 | + private: |
| 731 | + |
| 732 | + const QVariantMap mParams; |
| 733 | + |
| 734 | +}; |
| 735 | + |
710 | 736 | ///@endcond |
711 | 737 |
|
712 | 738 | QgsExpressionContextScope *QgsExpressionContextUtils::projectScope( const QgsProject *project ) |
@@ -1077,11 +1103,29 @@ QgsExpressionContext QgsExpressionContextUtils::createFeatureBasedContext( const |
1077 | 1103 | return QgsExpressionContext() << scope; |
1078 | 1104 | } |
1079 | 1105 |
|
| 1106 | +QgsExpressionContextScope *QgsExpressionContextUtils::processingAlgorithmScope( const QgsProcessingAlgorithm *algorithm, const QVariantMap ¶meters, QgsProcessingContext &context ) |
| 1107 | +{ |
| 1108 | + // set aside for future use |
| 1109 | + Q_UNUSED( context ); |
| 1110 | + |
| 1111 | + std::unique_ptr< QgsExpressionContextScope > scope( new QgsExpressionContextScope( QObject::tr( "Algorithm" ) ) ); |
| 1112 | + if ( !algorithm ) |
| 1113 | + return scope.release(); |
| 1114 | + |
| 1115 | + //add standard algorithm variables |
| 1116 | + scope->addVariable( QgsExpressionContextScope::StaticVariable( QStringLiteral( "algorithm_id" ), algorithm->id(), true ) ); |
| 1117 | + |
| 1118 | + scope->addFunction( QStringLiteral( "parameter" ), new GetProcessingParameterValue( parameters ) ); |
| 1119 | + |
| 1120 | + return scope.release(); |
| 1121 | +} |
| 1122 | + |
1080 | 1123 | void QgsExpressionContextUtils::registerContextFunctions() |
1081 | 1124 | { |
1082 | 1125 | QgsExpression::registerFunction( new GetNamedProjectColor( nullptr ) ); |
1083 | 1126 | QgsExpression::registerFunction( new GetComposerItemVariables( nullptr ) ); |
1084 | 1127 | QgsExpression::registerFunction( new GetLayerVisibility( QList<QgsMapLayer *>() ) ); |
| 1128 | + QgsExpression::registerFunction( new GetProcessingParameterValue( QVariantMap() ) ); |
1085 | 1129 | } |
1086 | 1130 |
|
1087 | 1131 | bool QgsScopedExpressionFunction::usesGeometry( const QgsExpressionNodeFunction *node ) const |
|
0 commit comments