[API][processing] New parameter type for coordinate operations

Allows selection of the proj coordinate operation to use when
reprojecting between two CRSes
This commit is contained in:
Nyall Dawson 2019-12-19 14:19:29 +10:00
parent c04d4faf7f
commit fe622dd24a
6 changed files with 535 additions and 0 deletions

View File

@ -191,6 +191,8 @@ their acceptable ranges, defaults, etc.
sipType = sipType_QgsProcessingParameterLayoutItem;
else if ( sipCpp->type() == QgsProcessingParameterColor::typeName() )
sipType = sipType_QgsProcessingParameterColor;
else if ( sipCpp->type() == QgsProcessingParameterCoordinateOperation::typeName() )
sipType = sipType_QgsProcessingParameterCoordinateOperation;
else
sipType = nullptr;
%End
@ -3210,6 +3212,128 @@ Creates a new parameter using the definition from a script code.
};
class QgsProcessingParameterCoordinateOperation : QgsProcessingParameterDefinition
{
%Docstring
A coordinate operation parameter for processing algorithms, for selection between available
coordinate operations to use when projecting between a source and destination coordinate reference system.
.. versionadded:: 3.12
%End
%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:
QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &sourceCrsParameterName = QString(), const QString &destinationCrsParameterName = QString(),
const QVariant &staticSourceCrs = QVariant(), const QVariant &staticDestinationCrs = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterCoordinateOperation.
%End
static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End
virtual QgsProcessingParameterDefinition *clone() const /Factory/;
virtual QString type() const;
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
virtual QString asScriptCode() const;
virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;
virtual QVariantMap toVariantMap() const;
virtual bool fromVariantMap( const QVariantMap &map );
static QgsProcessingParameterCoordinateOperation *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
%Docstring
Creates a new parameter using the definition from a script code.
%End
QString sourceCrsParameterName() const;
%Docstring
Returns the name of the source CRS parameter, or an empty string if this is not set.
.. seealso:: :py:func:`setSourceCrsParameterName`
.. seealso:: :py:func:`destinationCrsParameterName`
%End
void setSourceCrsParameterName( const QString &name );
%Docstring
Sets the ``name`` of the source CRS parameter. Use an empty string if this is not required.
.. seealso:: :py:func:`sourceCrsParameterName`
.. seealso:: :py:func:`setDestinationCrsParameterName`
%End
QString destinationCrsParameterName() const;
%Docstring
Returns the name of the destination CRS parameter, or an empty string if this is not set.
.. seealso:: :py:func:`setDestinationCrsParameterName`
.. seealso:: :py:func:`sourceCrsParameterName`
%End
void setDestinationCrsParameterName( const QString &name );
%Docstring
Sets the ``name`` of the destination CRS parameter. Use an empty string if this is not required.
.. seealso:: :py:func:`destinationCrsParameterName`
.. seealso:: :py:func:`setSourceCrsParameterName`
%End
QVariant sourceCrs() const;
%Docstring
Returns the static source CRS, or an invalid value if this is not set.
.. seealso:: :py:func:`setSourceCrs`
.. seealso:: :py:func:`destinationCrs`
%End
void setSourceCrs( const QVariant &crs );
%Docstring
Sets the static source ``crs``.
.. seealso:: :py:func:`sourceCrs`
.. seealso:: :py:func:`setDestinationCrs`
%End
QVariant destinationCrs() const;
%Docstring
Returns the static destination CRS, or an invalid value if this is not set.
.. seealso:: :py:func:`setDestinationCrs`
.. seealso:: :py:func:`sourceCrs`
%End
void setDestinationCrs( const QVariant &crs );
%Docstring
Sets the static destination ``crs``.
.. seealso:: :py:func:`destinationCrs`
.. seealso:: :py:func:`setSourceCrs`
%End
};
/************************************************************************

View File

@ -1747,6 +1747,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromVariantM
def.reset( new QgsProcessingParameterLayoutItem( name ) );
else if ( type == QgsProcessingParameterColor::typeName() )
def.reset( new QgsProcessingParameterColor( name ) );
else if ( type == QgsProcessingParameterCoordinateOperation::typeName() )
def.reset( new QgsProcessingParameterCoordinateOperation( name ) );
else
{
QgsProcessingParameterType *paramType = QgsApplication::instance()->processingRegistry()->parameterType( type );
@ -1841,6 +1843,8 @@ QgsProcessingParameterDefinition *QgsProcessingParameters::parameterFromScriptCo
return QgsProcessingParameterLayoutItem::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "color" ) )
return QgsProcessingParameterColor::fromScriptCode( name, description, isOptional, definition );
else if ( type == QStringLiteral( "coordinateoperation" ) )
return QgsProcessingParameterCoordinateOperation::fromScriptCode( name, description, isOptional, definition );
return nullptr;
}
@ -5923,3 +5927,121 @@ QgsProcessingParameterColor *QgsProcessingParameterColor::fromScriptCode( const
return new QgsProcessingParameterColor( name, description, defaultValue, allowOpacity, isOptional );
}
//
// QgsProcessingParameterCoordinateOperation
//
QgsProcessingParameterCoordinateOperation::QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description, const QVariant &defaultValue, const QString &sourceCrsParameterName, const QString &destinationCrsParameterName, const QVariant &staticSourceCrs, const QVariant &staticDestinationCrs, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mSourceParameterName( sourceCrsParameterName )
, mDestParameterName( destinationCrsParameterName )
, mSourceCrs( staticSourceCrs )
, mDestCrs( staticDestinationCrs )
{
}
QgsProcessingParameterDefinition *QgsProcessingParameterCoordinateOperation::clone() const
{
return new QgsProcessingParameterCoordinateOperation( * this );
}
QString QgsProcessingParameterCoordinateOperation::valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const
{
if ( !value.isValid() || value.isNull() )
return QStringLiteral( "None" );
if ( value.canConvert<QgsCoordinateReferenceSystem>() )
{
if ( !value.value< QgsCoordinateReferenceSystem >().isValid() )
return QStringLiteral( "QgsCoordinateReferenceSystem()" );
else
return QStringLiteral( "QgsCoordinateReferenceSystem('%1')" ).arg( value.value< QgsCoordinateReferenceSystem >().authid() );
}
if ( value.canConvert<QgsProperty>() )
return QStringLiteral( "QgsProperty.fromExpression('%1')" ).arg( value.value< QgsProperty >().asExpression() );
QVariantMap p;
p.insert( name(), value );
QgsMapLayer *layer = QgsProcessingParameters::parameterAsLayer( this, p, context );
if ( layer )
return QgsProcessingUtils::stringToPythonLiteral( QgsProcessingUtils::normalizeLayerSource( layer->source() ) );
QString s = value.toString();
return QgsProcessingUtils::stringToPythonLiteral( s );
}
QString QgsProcessingParameterCoordinateOperation::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags & FlagOptional )
code += QStringLiteral( "optional " );
code += QStringLiteral( "coordinateoperation " );
code += mDefault.toString();
return code.trimmed();
}
QString QgsProcessingParameterCoordinateOperation::asPythonString( QgsProcessing::PythonOutputType outputType ) const
{
switch ( outputType )
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
{
QgsProcessingContext c;
QString code = QStringLiteral( "QgsProcessingParameterCoordinateOperation('%1', '%2'" ).arg( name(), description() );
if ( mFlags & FlagOptional )
code += QStringLiteral( ", optional=True" );
if ( !mSourceParameterName.isEmpty() )
code += QStringLiteral( ", sourceCrsParameterName=%1" ).arg( valueAsPythonString( mSourceParameterName, c ) );
if ( !mDestParameterName.isEmpty() )
code += QStringLiteral( ", destinationCrsParameterName=%1" ).arg( valueAsPythonString( mDestParameterName, c ) );
if ( mSourceCrs.isValid() )
code += QStringLiteral( ", staticSourceCrs=%1" ).arg( valueAsPythonString( mSourceCrs, c ) );
if ( mDestCrs.isValid() )
code += QStringLiteral( ", staticDestinationCrs=%1" ).arg( valueAsPythonString( mDestCrs, c ) );
code += QStringLiteral( ", defaultValue=%1)" ).arg( valueAsPythonString( mDefault, c ) );
return code;
}
}
return QString();
}
QVariantMap QgsProcessingParameterCoordinateOperation::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
map.insert( QStringLiteral( "source_crs_parameter_name" ), mSourceParameterName );
map.insert( QStringLiteral( "dest_crs_parameter_name" ), mDestParameterName );
map.insert( QStringLiteral( "static_source_crs" ), mSourceCrs );
map.insert( QStringLiteral( "static_dest_crs" ), mDestCrs );
return map;
}
bool QgsProcessingParameterCoordinateOperation::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterDefinition::fromVariantMap( map );
mSourceParameterName = map.value( QStringLiteral( "source_crs_parameter_name" ) ).toString();
mDestParameterName = map.value( QStringLiteral( "dest_crs_parameter_name" ) ).toString();
mSourceCrs = map.value( QStringLiteral( "static_source_crs" ) );
mDestCrs = map.value( QStringLiteral( "static_dest_crs" ) );
return true;
}
QgsProcessingParameterCoordinateOperation *QgsProcessingParameterCoordinateOperation::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
QString def = definition;
if ( def.startsWith( '"' ) || def.startsWith( '\'' ) )
def = def.mid( 1 );
if ( def.endsWith( '"' ) || def.endsWith( '\'' ) )
def.chop( 1 );
QVariant defaultValue = def;
if ( def == QStringLiteral( "None" ) )
defaultValue = QVariant();
return new QgsProcessingParameterCoordinateOperation( name, description, defaultValue, QString(), QString(), QVariant(), QVariant(), isOptional );
}

View File

@ -268,6 +268,8 @@ class CORE_EXPORT QgsProcessingParameterDefinition
sipType = sipType_QgsProcessingParameterLayoutItem;
else if ( sipCpp->type() == QgsProcessingParameterColor::typeName() )
sipType = sipType_QgsProcessingParameterColor;
else if ( sipCpp->type() == QgsProcessingParameterCoordinateOperation::typeName() )
sipType = sipType_QgsProcessingParameterCoordinateOperation;
else
sipType = nullptr;
SIP_END
@ -3001,6 +3003,110 @@ class CORE_EXPORT QgsProcessingParameterColor : public QgsProcessingParameterDef
};
/**
* \class QgsProcessingParameterCoordinateOperation
* \ingroup core
* A coordinate operation parameter for processing algorithms, for selection between available
* coordinate operations to use when projecting between a source and destination coordinate reference system.
* \since QGIS 3.12
*/
class CORE_EXPORT QgsProcessingParameterCoordinateOperation : public QgsProcessingParameterDefinition
{
public:
/**
* Constructor for QgsProcessingParameterCoordinateOperation.
*/
QgsProcessingParameterCoordinateOperation( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
const QString &sourceCrsParameterName = QString(), const QString &destinationCrsParameterName = QString(),
const QVariant &staticSourceCrs = QVariant(), const QVariant &staticDestinationCrs = QVariant(),
bool optional = false );
/**
* Returns the type name for the parameter class.
*/
static QString typeName() { return QStringLiteral( "coordinateoperation" ); }
QgsProcessingParameterDefinition *clone() const override SIP_FACTORY;
QString type() const override { return typeName(); }
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asScriptCode() const override;
QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const override;
QVariantMap toVariantMap() const override;
bool fromVariantMap( const QVariantMap &map ) override;
/**
* Creates a new parameter using the definition from a script code.
*/
static QgsProcessingParameterCoordinateOperation *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;
/**
* Returns the name of the source CRS parameter, or an empty string if this is not set.
* \see setSourceCrsParameterName()
* \see destinationCrsParameterName()
*/
QString sourceCrsParameterName() const { return mSourceParameterName; }
/**
* Sets the \a name of the source CRS parameter. Use an empty string if this is not required.
* \see sourceCrsParameterName()
* \see setDestinationCrsParameterName()
*/
void setSourceCrsParameterName( const QString &name ) { mSourceParameterName = name; }
/**
* Returns the name of the destination CRS parameter, or an empty string if this is not set.
* \see setDestinationCrsParameterName()
* \see sourceCrsParameterName()
*/
QString destinationCrsParameterName() const { return mDestParameterName; }
/**
* Sets the \a name of the destination CRS parameter. Use an empty string if this is not required.
* \see destinationCrsParameterName()
* \see setSourceCrsParameterName()
*/
void setDestinationCrsParameterName( const QString &name ) { mDestParameterName = name; }
/**
* Returns the static source CRS, or an invalid value if this is not set.
* \see setSourceCrs()
* \see destinationCrs()
*/
QVariant sourceCrs() const { return mSourceCrs; }
/**
* Sets the static source \a crs.
* \see sourceCrs()
* \see setDestinationCrs()
*/
void setSourceCrs( const QVariant &crs ) { mSourceCrs = crs; }
/**
* Returns the static destination CRS, or an invalid value if this is not set.
* \see setDestinationCrs()
* \see sourceCrs()
*/
QVariant destinationCrs() const { return mDestCrs; }
/**
* Sets the static destination \a crs.
* \see destinationCrs()
* \see setSourceCrs()
*/
void setDestinationCrs( const QVariant &crs ) { mDestCrs = crs; }
private:
QString mSourceParameterName;
QString mDestParameterName;
QVariant mSourceCrs;
QVariant mDestCrs;
};
// clazy:excludeall=qstring-allocations
#endif // QGSPROCESSINGPARAMETERS_H

View File

@ -1677,5 +1677,57 @@ class CORE_EXPORT QgsProcessingParameterTypeColor : public QgsProcessingParamete
}
};
/**
* A coordinate operation parameter for Processing algorithms.
*
* \ingroup core
* \note No Python bindings available. Get your copy from QgsApplication.processingRegistry().parameterType('coordinateoperation')
* \since QGIS 3.12
*/
class CORE_EXPORT QgsProcessingParameterTypeCoordinateOperation : public QgsProcessingParameterType
{
QgsProcessingParameterDefinition *create( const QString &name ) const override SIP_FACTORY
{
return new QgsProcessingParameterCoordinateOperation( name );
}
QString description() const override
{
return QCoreApplication::translate( "Processing", "A coordinate operation parameter." );
}
QString name() const override
{
return QCoreApplication::translate( "Processing", "Coordinate Operation" );
}
QString id() const override
{
return QStringLiteral( "coordinateoperation" );
}
QString pythonImportString() const override
{
return QStringLiteral( "from qgis.core import QgsProcessingParameterCoordinateOperation" );
}
QString className() const override
{
return QStringLiteral( "QgsProcessingParameterCoordinateOperation" );
}
QStringList acceptedPythonTypes() const override
{
return QStringList() << QObject::tr( "str: string representation of a Proj coordinate operation" );
}
QStringList acceptedStringValues() const override
{
return QStringList() << QObject::tr( "String representation of Proj coordinate operation" );
}
};
#endif // QGSPROCESSINGPARAMETERTYPEIMPL_H

View File

@ -52,6 +52,7 @@ QgsProcessingRegistry::QgsProcessingRegistry( QObject *parent SIP_TRANSFERTHIS )
addParameterType( new QgsProcessingParameterTypeLayout() );
addParameterType( new QgsProcessingParameterTypeLayoutItem() );
addParameterType( new QgsProcessingParameterTypeColor() );
addParameterType( new QgsProcessingParameterTypeCoordinateOperation() );
}
QgsProcessingRegistry::~QgsProcessingRegistry()

View File

@ -566,6 +566,7 @@ class TestQgsProcessing: public QObject
void parameterLayout();
void parameterLayoutItem();
void parameterColor();
void parameterCoordinateOperation();
void checkParamValues();
void combineLayerExtent();
void processingFeatureSource();
@ -6532,6 +6533,135 @@ void TestQgsProcessing::parameterColor()
QVERIFY( def->checkValueIsAcceptable( QVariant() ) ); // should be valid, falls back to valid default
}
void TestQgsProcessing::parameterCoordinateOperation()
{
QgsProcessingContext context;
// not optional!
std::unique_ptr< QgsProcessingParameterCoordinateOperation > def( new QgsProcessingParameterCoordinateOperation( "non_optional", QString(), QString(), QString(), QString(), QVariant(), QVariant(), false ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( "test" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
def->setSourceCrsParameterName( QStringLiteral( "src" ) );
QCOMPARE( def->sourceCrsParameterName(), QStringLiteral( "src" ) );
def->setDestinationCrsParameterName( QStringLiteral( "dest" ) );
QCOMPARE( def->destinationCrsParameterName(), QStringLiteral( "dest" ) );
def->setSourceCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:7855" ) ) );
QCOMPARE( def->sourceCrs().value< QgsCoordinateReferenceSystem >().authid(), QStringLiteral( "EPSG:7855" ) );
def->setDestinationCrs( QgsCoordinateReferenceSystem( QStringLiteral( "EPSG:28355" ) ) );
QCOMPARE( def->destinationCrs().value< QgsCoordinateReferenceSystem >().authid(), QStringLiteral( "EPSG:28355" ) );
// string value
QVariantMap params;
params.insert( "non_optional", QStringLiteral( "abcdef" ) );
QCOMPARE( QgsProcessingParameters::parameterAsString( def.get(), params, context ), QString( "abcdef" ) );
QCOMPARE( def->valueAsPythonString( QVariant(), context ), QStringLiteral( "None" ) );
QCOMPARE( def->valueAsPythonString( 5, context ), QStringLiteral( "'5'" ) );
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc" ), context ), QStringLiteral( "'abc'" ) );
QCOMPARE( def->valueAsPythonString( QStringLiteral( "abc\ndef" ), context ), QStringLiteral( "'abc\\ndef'" ) );
QCOMPARE( def->valueAsPythonString( QVariant::fromValue( QgsProperty::fromExpression( "\"a\"=1" ) ), context ), QStringLiteral( "QgsProperty.fromExpression('\"a\"=1')" ) );
QCOMPARE( def->valueAsPythonString( "uri='complex' username=\"complex\"", context ), QStringLiteral( "'uri=\\'complex\\' username=\\\"complex\\\"'" ) );
QCOMPARE( def->valueAsPythonString( QStringLiteral( "c:\\test\\new data\\test.dat" ), context ), QStringLiteral( "'c:\\\\test\\\\new data\\\\test.dat'" ) );
QString pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterCoordinateOperation('non_optional', '', sourceCrsParameterName='src', destinationCrsParameterName='dest', staticSourceCrs=QgsCoordinateReferenceSystem('EPSG:7855'), staticDestinationCrs=QgsCoordinateReferenceSystem('EPSG:28355'), defaultValue=None)" ) );
QString code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##non_optional=coordinateoperation" ) );
std::unique_ptr< QgsProcessingParameterCoordinateOperation > fromCode( dynamic_cast< QgsProcessingParameterCoordinateOperation * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
QVariantMap map = def->toVariantMap();
QgsProcessingParameterCoordinateOperation fromMap( "x" );
QVERIFY( fromMap.fromVariantMap( map ) );
QCOMPARE( fromMap.name(), def->name() );
QCOMPARE( fromMap.description(), def->description() );
QCOMPARE( fromMap.flags(), def->flags() );
QCOMPARE( fromMap.defaultValue(), def->defaultValue() );
QCOMPARE( fromMap.sourceCrsParameterName(), def->sourceCrsParameterName() );
QCOMPARE( fromMap.destinationCrsParameterName(), def->destinationCrsParameterName() );
QCOMPARE( fromMap.sourceCrs().value< QgsCoordinateReferenceSystem >().authid(), def->sourceCrs().value< QgsCoordinateReferenceSystem >().authid() );
QCOMPARE( fromMap.destinationCrs().value< QgsCoordinateReferenceSystem >().authid(), def->destinationCrs().value< QgsCoordinateReferenceSystem >().authid() );
def.reset( dynamic_cast< QgsProcessingParameterCoordinateOperation *>( QgsProcessingParameters::parameterFromVariantMap( map ) ) );
QVERIFY( dynamic_cast< QgsProcessingParameterCoordinateOperation *>( def.get() ) );
fromCode.reset( dynamic_cast< QgsProcessingParameterCoordinateOperation * >( QgsProcessingParameters::parameterFromScriptCode( QStringLiteral( "##non_optional=coordinateoperation None" ) ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QVERIFY( !fromCode->defaultValue().isValid() );
fromCode.reset( dynamic_cast< QgsProcessingParameterCoordinateOperation * >( QgsProcessingParameters::parameterFromScriptCode( QStringLiteral( "##non_optional=coordinateoperation it's mario" ) ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue().toString(), QStringLiteral( "it's mario" ) );
def->setDefaultValue( QStringLiteral( "it's mario" ) );
pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterCoordinateOperation('non_optional', '', sourceCrsParameterName='src', destinationCrsParameterName='dest', staticSourceCrs=QgsCoordinateReferenceSystem('EPSG:7855'), staticDestinationCrs=QgsCoordinateReferenceSystem('EPSG:28355'), defaultValue='it\\'s mario')" ) );
code = def->asScriptCode();
fromCode.reset( dynamic_cast< QgsProcessingParameterCoordinateOperation * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
fromCode.reset( dynamic_cast< QgsProcessingParameterCoordinateOperation * >( QgsProcessingParameters::parameterFromScriptCode( QStringLiteral( "##non_optional=coordinateoperation 'my val'" ) ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue().toString(), QStringLiteral( "my val" ) );
fromCode.reset( dynamic_cast< QgsProcessingParameterCoordinateOperation * >( QgsProcessingParameters::parameterFromScriptCode( QStringLiteral( "##non_optional=coordinateoperation \"my val\"" ) ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "non optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue().toString(), QStringLiteral( "my val" ) );
// optional
def.reset( new QgsProcessingParameterCoordinateOperation( "optional", QString(), QString( "default" ), QString(), QString(), QVariant(), QVariant(), true ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( "test" ) );
QVERIFY( def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) );
params.insert( "optional", QVariant() );
QCOMPARE( QgsProcessingParameters::parameterAsString( def.get(), params, context ), QString( "default" ) );
params.insert( "optional", QString() ); //empty string should not result in default value
QCOMPARE( QgsProcessingParameters::parameterAsString( def.get(), params, context ), QString() );
pythonCode = def->asPythonString();
QCOMPARE( pythonCode, QStringLiteral( "QgsProcessingParameterCoordinateOperation('optional', '', optional=True, defaultValue='default')" ) );
code = def->asScriptCode();
QCOMPARE( code, QStringLiteral( "##optional=optional coordinateoperation default" ) );
fromCode.reset( dynamic_cast< QgsProcessingParameterCoordinateOperation * >( QgsProcessingParameters::parameterFromScriptCode( code ) ) );
QVERIFY( fromCode.get() );
QCOMPARE( fromCode->name(), def->name() );
QCOMPARE( fromCode->description(), QStringLiteral( "optional" ) );
QCOMPARE( fromCode->flags(), def->flags() );
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );
// not optional, valid default!
def.reset( new QgsProcessingParameterCoordinateOperation( "non_optional", QString(), QString( "def" ), QString(), QString(), QVariant(), QVariant(), false ) );
QVERIFY( def->checkValueIsAcceptable( 1 ) );
QVERIFY( def->checkValueIsAcceptable( "test" ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( def->checkValueIsAcceptable( QVariant() ) ); // should be valid, falls back to valid default
}
void TestQgsProcessing::checkParamValues()
{
DummyAlgorithm a( "asd" );