Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
move expression type enum to qgis.h
  • Loading branch information
alexbruy authored and wonder-sk committed May 2, 2023
1 parent 61cfdd3 commit 10b02e0
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 55 deletions.
6 changes: 6 additions & 0 deletions python/core/auto_additions/qgis.py
Expand Up @@ -3578,3 +3578,9 @@
Qgis.AttributeFormPythonInitCodeSource.__doc__ = 'The Python init code source for attribute forms.\n\n.. note::\n\n Prior to QGIS 3.32 this was available as :py:class:`QgsEditFormConfig`.PythonInitCodeSource.\n\n.. versionadded:: 3.32\n\n' + '* ``CodeSourceNone``: ' + Qgis.AttributeFormPythonInitCodeSource.NoSource.__doc__ + '\n' + '* ``CodeSourceFile``: ' + Qgis.AttributeFormPythonInitCodeSource.File.__doc__ + '\n' + '* ``CodeSourceDialog``: ' + Qgis.AttributeFormPythonInitCodeSource.Dialog.__doc__ + '\n' + '* ``CodeSourceEnvironment``: ' + Qgis.AttributeFormPythonInitCodeSource.Environment.__doc__
# --
Qgis.AttributeFormPythonInitCodeSource.baseClass = Qgis
# monkey patching scoped based enum
Qgis.ExpressionType.Qgis.__doc__ = "Native QGIS expression"
Qgis.ExpressionType.PointCloud.__doc__ = "Point cloud expression"
Qgis.ExpressionType.__doc__ = 'Expression types\n\n.. versionadded:: 3.32\n\n' + '* ``Qgis``: ' + Qgis.ExpressionType.Qgis.__doc__ + '\n' + '* ``PointCloud``: ' + Qgis.ExpressionType.PointCloud.__doc__
# --
Qgis.ExpressionType.baseClass = Qgis
Expand Up @@ -2887,16 +2887,9 @@ 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, Type type = Qgis );
bool optional = false, Qgis::ExpressionType type = Qgis::ExpressionType::Qgis );
%Docstring
Constructor for QgsProcessingParameterExpression.
%End
Expand Down Expand Up @@ -2929,7 +2922,7 @@ Sets the name of the parent layer parameter. Use an empty string if this is not
.. seealso:: :py:func:`parentLayerParameterName`
%End

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

Expand All @@ -2938,7 +2931,7 @@ Returns the parameter's expression type.
.. versionadded:: 3.32
%End

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

Expand Down
6 changes: 6 additions & 0 deletions python/core/auto_generated/qgis.sip.in
Expand Up @@ -2021,6 +2021,12 @@ The development version
Environment
};

enum class ExpressionType
{
Qgis,
PointCloud,
};

static const double DEFAULT_SEARCH_RADIUS_MM;

static const float DEFAULT_MAPTOPIXEL_THRESHOLD;
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/pdal/qgsalgorithmpdalfilter.cpp
Expand Up @@ -61,7 +61,7 @@ QgsPdalFilterAlgorithm *QgsPdalFilterAlgorithm::createInstance() const
void QgsPdalFilterAlgorithm::initAlgorithm( const QVariantMap & )
{
addParameter( new QgsProcessingParameterPointCloudLayer( QStringLiteral( "INPUT" ), QObject::tr( "Input layer" ) ) );
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "FILTER_EXPRESSION" ), QObject::tr( "Filter expression" ), QVariant(), QStringLiteral( "INPUT" ), false, QgsProcessingParameterExpression::PointCloud ) );
addParameter( new QgsProcessingParameterExpression( QStringLiteral( "FILTER_EXPRESSION" ), QObject::tr( "Filter expression" ), QVariant(), QStringLiteral( "INPUT" ), false, Qgis::ExpressionType::PointCloud ) );
addParameter( new QgsProcessingParameterExtent( QStringLiteral( "FILTER_EXTENT" ), QObject::tr( "Cropping extent" ), QVariant(), true ) );
addParameter( new QgsProcessingParameterPointCloudDestination( QStringLiteral( "OUTPUT" ), QObject::tr( "Filtered" ) ) );
}
Expand Down
2 changes: 1 addition & 1 deletion src/analysis/processing/pdal/qgspdalalgorithmbase.cpp
Expand Up @@ -48,7 +48,7 @@ QString QgsPdalAlgorithmBase::wrenchExecutableBinary() const

void QgsPdalAlgorithmBase::createCommonParameters()
{
std::unique_ptr< QgsProcessingParameterExpression > filterParam = std::make_unique< QgsProcessingParameterExpression >( QStringLiteral( "FILTER_EXPRESSION" ), QObject::tr( "Filter expression" ), QVariant(), QStringLiteral( "INPUT" ), true, QgsProcessingParameterExpression::PointCloud );
std::unique_ptr< QgsProcessingParameterExpression > filterParam = std::make_unique< QgsProcessingParameterExpression >( QStringLiteral( "FILTER_EXPRESSION" ), QObject::tr( "Filter expression" ), QVariant(), QStringLiteral( "INPUT" ), true, Qgis::ExpressionType::PointCloud );
filterParam->setFlags( filterParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( filterParam.release() );

Expand Down
21 changes: 8 additions & 13 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -5318,7 +5318,7 @@ QgsProcessingParameterAuthConfig *QgsProcessingParameterAuthConfig::fromScriptCo
// QgsProcessingParameterExpression
//

QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional, Type type )
QgsProcessingParameterExpression::QgsProcessingParameterExpression( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, bool optional, Qgis::ExpressionType type )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mParentLayerParameterName( parentLayerParameterName )
, mExpressionType( type )
Expand Down Expand Up @@ -5367,14 +5367,9 @@ QString QgsProcessingParameterExpression::asPythonString( const QgsProcessing::P
QgsProcessingContext c;
code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );

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

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

void QgsProcessingParameterExpression::setExpressionType( Type expressionType )
void QgsProcessingParameterExpression::setExpressionType( Qgis::ExpressionType expressionType )
{
mExpressionType = expressionType;
}
Expand All @@ -5406,21 +5401,21 @@ QVariantMap QgsProcessingParameterExpression::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
map.insert( QStringLiteral( "parent_layer" ), mParentLayerParameterName );
map.insert( QStringLiteral( "expression_type" ), mExpressionType );
map.insert( QStringLiteral( "expression_type" ), static_cast< int >( 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() );
mExpressionType = static_cast< Qgis::ExpressionType >( 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, Qgis );
return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional, Qgis::ExpressionType::Qgis );
}


Expand Down
16 changes: 4 additions & 12 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -2826,20 +2826,12 @@ 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, Type type = Qgis );
bool optional = false, Qgis::ExpressionType type = Qgis::ExpressionType::Qgis );

/**
* Returns the type name for the parameter class.
Expand Down Expand Up @@ -2869,15 +2861,15 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet
*
* \since QGIS 3.32
*/
Type expressionType() const;
Qgis::ExpressionType expressionType() const;

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

QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;
Expand All @@ -2890,7 +2882,7 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet
private:

QString mParentLayerParameterName;
Type mExpressionType = Qgis;
Qgis::ExpressionType mExpressionType = Qgis::ExpressionType::Qgis;
};


Expand Down
12 changes: 12 additions & 0 deletions src/core/qgis.h
Expand Up @@ -3508,6 +3508,18 @@ class CORE_EXPORT Qgis
};
Q_ENUM( AttributeFormPythonInitCodeSource )

/**
* Expression types
*
* \since QGIS 3.32
*/
enum class ExpressionType
{
Qgis, //!< Native QGIS expression
PointCloud, //!< Point cloud expression
};
Q_ENUM( ExpressionType )

/**
* Identify search radius in mm
* \since QGIS 2.3
Expand Down
18 changes: 9 additions & 9 deletions src/gui/processing/qgsprocessingwidgetwrapperimpl.cpp
Expand Up @@ -2125,8 +2125,8 @@ QgsProcessingExpressionParameterDefinitionWidget::QgsProcessingExpressionParamet

vlayout->addWidget( new QLabel( tr( "Expression type" ) ) );
mExpressionTypeComboBox = new QComboBox();
mExpressionTypeComboBox->addItem( tr( "Qgis" ), QgsProcessingParameterExpression::Qgis );
mExpressionTypeComboBox->addItem( tr( "Point cloud" ), QgsProcessingParameterExpression::PointCloud );
mExpressionTypeComboBox->addItem( tr( "QGIS" ), static_cast< int >( Qgis::ExpressionType::Qgis ) );
mExpressionTypeComboBox->addItem( tr( "Point Cloud" ), static_cast< int >( Qgis::ExpressionType::PointCloud ) );

connect( mExpressionTypeComboBox, static_cast<void ( QComboBox::* )( int )>( &QComboBox::currentIndexChanged ), this, [ = ]( int )
{
Expand All @@ -2137,15 +2137,15 @@ QgsProcessingExpressionParameterDefinitionWidget::QgsProcessingExpressionParamet
if ( const QgsProcessingParameterExpression *expParam = dynamic_cast<const QgsProcessingParameterExpression *>( definition ) )
initialParent = expParam->parentLayerParameterName();

QgsProcessingParameterExpression::Type exprType = static_cast< QgsProcessingParameterExpression::Type >( mExpressionTypeComboBox->currentData().toInt() );
Qgis::ExpressionType exprType = static_cast< Qgis::ExpressionType >( mExpressionTypeComboBox->currentData().toInt() );

if ( QgsProcessingModelAlgorithm *model = widgetContext.model() )
{
// populate combo box with other model input choices
const QMap<QString, QgsProcessingModelParameter> components = model->parameterComponents();
for ( auto it = components.constBegin(); it != components.constEnd(); ++it )
{
if ( exprType == QgsProcessingParameterExpression::Qgis )
if ( exprType == Qgis::ExpressionType::Qgis )
{
if ( const QgsProcessingParameterFeatureSource *definition = dynamic_cast< const QgsProcessingParameterFeatureSource * >( model->parameterDefinition( it.value().parameterName() ) ) )
{
Expand Down Expand Up @@ -2189,7 +2189,7 @@ QgsProcessingExpressionParameterDefinitionWidget::QgsProcessingExpressionParamet

mExpressionTypeComboBox->setCurrentIndex( -1 );
if ( const QgsProcessingParameterExpression *expParam = dynamic_cast<const QgsProcessingParameterExpression *>( definition ) )
mExpressionTypeComboBox->setCurrentIndex( mExpressionTypeComboBox->findData( expParam->expressionType() ) );
mExpressionTypeComboBox->setCurrentIndex( mExpressionTypeComboBox->findData( static_cast< int >( expParam->expressionType() ) ) );
else
mExpressionTypeComboBox->setCurrentIndex( 0 );

Expand All @@ -2200,7 +2200,7 @@ QgsProcessingExpressionParameterDefinitionWidget::QgsProcessingExpressionParamet

QgsProcessingParameterDefinition *QgsProcessingExpressionParameterDefinitionWidget::createParameter( const QString &name, const QString &description, QgsProcessingParameterDefinition::Flags flags ) const
{
QgsProcessingParameterExpression::Type expressionType = static_cast< QgsProcessingParameterExpression::Type >( mExpressionTypeComboBox->currentData().toInt() );
Qgis::ExpressionType expressionType = static_cast< Qgis::ExpressionType >( mExpressionTypeComboBox->currentData().toInt() );
auto param = std::make_unique< QgsProcessingParameterExpression >( name, description, mDefaultLineEdit->text(), mParentLayerComboBox->currentData().toString(), false, expressionType );
param->setFlags( flags );
return param.release();
Expand Down Expand Up @@ -2235,7 +2235,7 @@ QWidget *QgsProcessingExpressionWidgetWrapper::createWidget()
}
else
{
if ( expParam->expressionType() == QgsProcessingParameterExpression::PointCloud )
if ( expParam->expressionType() == Qgis::ExpressionType::PointCloud )
{
mPointCloudExpLineEdit = new QgsProcessingPointCloudExpressionLineEdit();
mPointCloudExpLineEdit->setToolTip( parameterDefinition()->toolTip() );
Expand Down Expand Up @@ -2337,7 +2337,7 @@ void QgsProcessingExpressionWidgetWrapper::setParentLayerWrapperValue( const Qgs
const QgsProcessingParameterExpression *expParam = dynamic_cast< const QgsProcessingParameterExpression *>( parameterDefinition() );
switch ( expParam->expressionType() )
{
case QgsProcessingParameterExpression::Qgis:
case Qgis::ExpressionType::Qgis:
{
if ( val.userType() == QMetaType::type( "QgsProcessingFeatureSourceDefinition" ) )
{
Expand Down Expand Up @@ -2380,7 +2380,7 @@ void QgsProcessingExpressionWidgetWrapper::setParentLayerWrapperValue( const Qgs

break;
}
case QgsProcessingParameterExpression::PointCloud:
case Qgis::ExpressionType::PointCloud:
{
QgsPointCloudLayer *layer = QgsProcessingParameters::parameterAsPointCloudLayer( parentWrapper->parameterDefinition(), val, *context, QgsProcessing::LayerOptionsFlag::SkipIndexGeneration );
if ( !layer )
Expand Down
8 changes: 4 additions & 4 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', type=QgsProcessingParameterExpression.Qgis)" ) );
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('non_optional', '', parentLayerParameterName='', defaultValue='1+1')" ) );

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', type=QgsProcessingParameterExpression.Qgis)" ) );
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('optional', '', optional=True, parentLayerParameterName='', defaultValue='default')" ) );

code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##optional=optional expression default" ) );
Expand All @@ -6544,9 +6544,9 @@ void TestQgsProcessing::parameterExpression()
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 ) );
def.reset( new QgsProcessingParameterExpression( "non_optional", QString(), QString( "default" ), QString(), false, Qgis::ExpressionType::PointCloud ) );
pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('non_optional', '', parentLayerParameterName='', defaultValue='default', type=QgsProcessingParameterExpression.PointCloud)" ) );
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterExpression('non_optional', '', parentLayerParameterName='', defaultValue='default', type=Qgis.ExpressionType.PointCloud)" ) );
}

void TestQgsProcessing::parameterField()
Expand Down
10 changes: 5 additions & 5 deletions tests/src/gui/testprocessinggui.cpp
Expand Up @@ -2956,7 +2956,7 @@ void TestProcessingGui::testExpressionWrapper()
//
// point cloud expression
//
param.setExpressionType( QgsProcessingParameterExpression::PointCloud );
param.setExpressionType( Qgis::ExpressionType::PointCloud );
param.setParentLayerParameterName( QStringLiteral( "pointcloud" ) );
QgsProcessingExpressionWidgetWrapper wrapper3( &param, type );
w = wrapper3.createWrappedWidget( context );
Expand Down Expand Up @@ -3052,7 +3052,7 @@ void TestProcessingGui::testExpressionWrapper()
QCOMPARE( def->name(), QStringLiteral( "param_name" ) );
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) ); // should default to mandatory
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) );
QCOMPARE( static_cast< QgsProcessingParameterExpression * >( def.get() )->expressionType(), QgsProcessingParameterExpression::Qgis );
QCOMPARE( static_cast< QgsProcessingParameterExpression * >( def.get() )->expressionType(), Qgis::ExpressionType::Qgis );

// using a parameter definition as initial values
QgsProcessingParameterExpression exprParam( QStringLiteral( "n" ), QStringLiteral( "test desc" ), QVariant(), QStringLiteral( "parent" ) );
Expand All @@ -3063,9 +3063,9 @@ void TestProcessingGui::testExpressionWrapper()
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagOptional ) );
QVERIFY( !( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced ) );
QCOMPARE( static_cast< QgsProcessingParameterExpression * >( def.get() )->parentLayerParameterName(), QStringLiteral( "parent" ) );
QCOMPARE( static_cast< QgsProcessingParameterExpression * >( def.get() )->expressionType(), QgsProcessingParameterExpression::Qgis );
QCOMPARE( static_cast< QgsProcessingParameterExpression * >( def.get() )->expressionType(), Qgis::ExpressionType::Qgis );
exprParam.setFlags( QgsProcessingParameterDefinition::FlagAdvanced | QgsProcessingParameterDefinition::FlagOptional );
exprParam.setExpressionType( QgsProcessingParameterExpression::PointCloud );
exprParam.setExpressionType( Qgis::ExpressionType::PointCloud );
exprParam.setParentLayerParameterName( QString() );
widget = std::make_unique< QgsProcessingParameterDefinitionWidget >( QStringLiteral( "expression" ), context, widgetContext, &exprParam );
def.reset( widget->createParameter( QStringLiteral( "param_name" ) ) );
Expand All @@ -3074,7 +3074,7 @@ void TestProcessingGui::testExpressionWrapper()
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagOptional );
QVERIFY( def->flags() & QgsProcessingParameterDefinition::FlagAdvanced );
QVERIFY( static_cast< QgsProcessingParameterExpression * >( def.get() )->parentLayerParameterName().isEmpty() );
QCOMPARE( static_cast< QgsProcessingParameterExpression * >( def.get() )->expressionType(), QgsProcessingParameterExpression::PointCloud );
QCOMPARE( static_cast< QgsProcessingParameterExpression * >( def.get() )->expressionType(), Qgis::ExpressionType::PointCloud );
}

void TestProcessingGui::testFieldSelectionPanel()
Expand Down

0 comments on commit 10b02e0

Please sign in to comment.