Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[processing] add enum to expression parameter to make it possible
distinguish between native QGIS expressions and point cloud filter
expressions
  • Loading branch information
alexbruy authored and wonder-sk committed May 2, 2023
1 parent 3ab7530 commit d93111c
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 8 deletions.
Expand Up @@ -2887,9 +2887,16 @@ An expression parameter for processing algorithms.
%End
public:

enum Type
{
Qgis,
PointCloud,
};


QgsProcessingParameterExpression( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &parentLayerParameterName = QString(),
bool optional = false );
bool optional = false, Type type = Qgis );
%Docstring
Constructor for QgsProcessingParameterExpression.
%End
Expand Down Expand Up @@ -2920,6 +2927,24 @@ Returns the name of the parent layer parameter, or an empty string if this is no
Sets the name of the parent layer parameter. Use an empty string if this is not required.

.. seealso:: :py:func:`parentLayerParameterName`
%End

Type expressionType() const;
%Docstring
Returns the parameter's expression type.

.. seealso:: :py:func:`setExpressionType`

.. versionadded:: 3.32
%End

void setExpressionType( Type type );
%Docstring
Sets the parameter's expression ``type``.

.. seealso:: :py:func:`expressionType`

.. versionadded:: 3.32
%End

virtual QVariantMap toVariantMap() const;
Expand Down
30 changes: 27 additions & 3 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -5318,9 +5318,10 @@ QgsProcessingParameterAuthConfig *QgsProcessingParameterAuthConfig::fromScriptCo
// QgsProcessingParameterExpression
//

QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional )
QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional, Type type )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mParentLayerParameterName( parentLayerParameterName )
, mExpressionType( type )
{

}
Expand Down Expand Up @@ -5364,7 +5365,17 @@ QString QgsProcessingParameterExpression::asPythonString( const QgsProcessing::P
code += QStringLiteral( ", parentLayerParameterName='%1'" ).arg( mParentLayerParameterName );

QgsProcessingContext c;
code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );

switch ( mExpressionType )
{
case Qgis:
code += QStringLiteral( ", type=QgsProcessingParameterExpression.Qgis)" );
break;
case PointCloud:
code += QStringLiteral( ", type=QgsProcessingParameterExpression.PointCloud)" );
break;
}
return code;
}
}
Expand All @@ -5381,25 +5392,38 @@ void QgsProcessingParameterExpression::setParentLayerParameterName( const QStrin
mParentLayerParameterName = parentLayerParameterName;
}

QgsProcessingParameterExpression::Type QgsProcessingParameterExpression::expressionType() const
{
return mExpressionType;
}

void QgsProcessingParameterExpression::setExpressionType( Type expressionType )
{
mExpressionType = expressionType;
}

QVariantMap QgsProcessingParameterExpression::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
map.insert( QStringLiteral( "expression_type" ), mExpressionType );
return map;
}

bool QgsProcessingParameterExpression::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterDefinition::fromVariantMap( map );
mParentLayerParameterName = map.value( QStringLiteral( "parent_layer" ) ).toString();
mExpressionType = static_cast< Type >( map.value( QStringLiteral( "expression_type" ) ).toInt() );
return true;
}

QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional, Qgis );
}


QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, QgsProcessingParameterLimitedDataTypes( types )
Expand Down
28 changes: 26 additions & 2 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -2826,12 +2826,20 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet
{
public:

//! Expression type
enum Type
{
Qgis, //!< Native QGIS expression
PointCloud, //!< Point cloud expression
};


/**
* Constructor for QgsProcessingParameterExpression.
*/
QgsProcessingParameterExpression( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &parentLayerParameterName = QString(),
bool optional = false );
bool optional = false, Type type = Qgis );

/**
* Returns the type name for the parameter class.
Expand All @@ -2855,6 +2863,22 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet
*/
void setParentLayerParameterName( const QString &parentLayerParameterName );

/**
* Returns the parameter's expression type.
* \see setExpressionType()
*
* \since QGIS 3.32
*/
Type expressionType() const;

/**
* Sets the parameter's expression \a type.
* \see expressionType()
*
* \since QGIS 3.32
*/
void setExpressionType( Type type );

QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;

Expand All @@ -2866,7 +2890,7 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet
private:

QString mParentLayerParameterName;

Type mExpressionType = Qgis;
};


Expand Down
8 changes: 6 additions & 2 deletions tests/src/analysis/testqgsprocessing.cpp
Expand Up @@ -6482,7 +6482,7 @@ void TestQgsProcessing::parameterExpression()
QVERIFY( ok );

QString pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('non_optional', '', parentLayerParameterName='', defaultValue='1+1')" ) );
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('non_optional', '', parentLayerParameterName='', defaultValue='1+1', type=QgsProcessingParameterExpression.Qgis)" ) );

QString code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##non_optional=expression 1+1" ) );
Expand Down Expand Up @@ -6525,7 +6525,7 @@ void TestQgsProcessing::parameterExpression()
QCOMPARE( QgsProcessingParameters::parameterAsExpression( def.get(), params, context ), QString( "default" ) );

pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('optional', '', optional=True, parentLayerParameterName='', defaultValue='default')" ) );
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('optional', '', optional=True, parentLayerParameterName='', defaultValue='default', type=QgsProcessingParameterExpression.Qgis)" ) );

code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##optional=optional expression default" ) );
Expand All @@ -6543,6 +6543,10 @@ void TestQgsProcessing::parameterExpression()
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) ); // should NOT be acceptable, because it will fallback to invalid default value

// set point cloud expression type
def.reset( new QgsProcessingParameterExpression( "non_optional", QString(), QString( "default" ), QString(), false, QgsProcessingParameterExpression::PointCloud ) );
pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('non_optional', '', parentLayerParameterName='', defaultValue='default', type=QgsProcessingParameterExpression.PointCloud)" ) );
}

void TestQgsProcessing::parameterField()
Expand Down

0 comments on commit d93111c

Please sign in to comment.