Skip to content
Permalink
Browse files

[processing] add parameterAsInts function

  • Loading branch information
nirvn committed Sep 4, 2018
1 parent 97adef2 commit 48b18d4c6c74e30859377cd2450bccb560e3ee20
@@ -583,6 +583,13 @@ Evaluates the parameter with matching ``name`` to a static double value.
int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
%Docstring
Evaluates the parameter with matching ``name`` to a static integer value.
%End

QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
%Docstring
Evaluates the parameter with matching ``name`` to a list of integer values.

.. versionadded:: 3.4
%End

int parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;
@@ -533,6 +533,20 @@ Evaluates the parameter with matching ``definition`` to a static integer value.
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a static integer value.

.. versionadded:: 3.4
%End

static QList<int> parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` to a list of integer values.

.. versionadded:: 3.4
%End

static QList<int> parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context );
%Docstring
Evaluates the parameter with matching ``definition`` and ``value`` to a list of integer values.

.. versionadded:: 3.4
%End

@@ -117,7 +117,7 @@ def getConsoleCommands(self, parameters, context, feedback, executing=True):

arguments = []

bands = self.parameterAsFields(parameters, self.BANDS, context)
bands = self.parameterAsInts(parameters, self.BANDS, context)
for band in bands:
arguments.append('-b {}'.format(band))

@@ -565,6 +565,11 @@ int QgsProcessingAlgorithm::parameterAsInt( const QVariantMap &parameters, const
return QgsProcessingParameters::parameterAsInt( parameterDefinition( name ), parameters, context );
}

QList<int> QgsProcessingAlgorithm::parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
{
return QgsProcessingParameters::parameterAsInts( parameterDefinition( name ), parameters, context );
}

int QgsProcessingAlgorithm::parameterAsEnum( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const
{
return QgsProcessingParameters::parameterAsEnum( parameterDefinition( name ), parameters, context );
@@ -582,6 +582,12 @@ class CORE_EXPORT QgsProcessingAlgorithm
*/
int parameterAsInt( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;

/**
* Evaluates the parameter with matching \a name to a list of integer values.
* \since QGIS 3.4
*/
QList<int> parameterAsInts( const QVariantMap &parameters, const QString &name, const QgsProcessingContext &context ) const;

/**
* Evaluates the parameter with matching \a name to a enum value.
*/
@@ -178,6 +178,63 @@ int QgsProcessingParameters::parameterAsInt( const QgsProcessingParameterDefinit
return val.toInt();
}

QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
{
if ( !definition )
return QList< int >();

return parameterAsInts( definition, parameters.value( definition->name() ), context );
}

QList< int > QgsProcessingParameters::parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context )
{
if ( !definition )
return QList< int >();

QList< int > resultList;
QVariant val = value;
if ( val.isValid() )
{
if ( val.canConvert<QgsProperty>() )
resultList << val.value< QgsProperty >().valueAsInt( context.expressionContext(), definition->defaultValue().toInt() );
else if ( val.type() == QVariant::List )
{
QVariantList list = val.toList();
for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
resultList << it->toInt();
}
else
{
QStringList parts = val.toString().split( ';' );
for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
resultList << it->toInt();
}
}

if ( ( resultList.isEmpty() || resultList.at( 0 ) == 0 ) )
{
resultList.clear();
// check default
if ( definition->defaultValue().isValid() )
{
if ( definition->defaultValue().type() == QVariant::List )
{
QVariantList list = definition->defaultValue().toList();
for ( auto it = list.constBegin(); it != list.constEnd(); ++it )
resultList << it->toInt();
}
else
{
QStringList parts = definition->defaultValue().toString().split( ';' );
for ( auto it = parts.constBegin(); it != parts.constEnd(); ++it )
resultList << it->toInt();
}
}
}

return resultList;
}

int QgsProcessingParameters::parameterAsEnum( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context )
{
if ( !definition )
@@ -4342,7 +4399,7 @@ QString QgsProcessingParameterBand::valueAsPythonString( const QVariant &value,
QVariantList values = value.toList();
for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
{
parts << QgsProcessingUtils::stringToPythonLiteral( it->toString() );
parts << QString::number( static_cast< int >( it->toDouble() ) );
}
return parts.join( ',' ).prepend( '[' ).append( ']' );
}
@@ -4352,7 +4409,7 @@ QString QgsProcessingParameterBand::valueAsPythonString( const QVariant &value,
QStringList values = value.toStringList();
for ( auto it = values.constBegin(); it != values.constEnd(); ++it )
{
parts << QgsProcessingUtils::stringToPythonLiteral( *it );
parts << QString::number( static_cast< int >( it->toDouble() ) );
}
return parts.join( ',' ).prepend( '[' ).append( ']' );
}
@@ -602,6 +602,18 @@ class CORE_EXPORT QgsProcessingParameters
*/
static int parameterAsInt( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context );

/**
* Evaluates the parameter with matching \a definition to a list of integer values.
* \since QGIS 3.4
*/
static QList<int> parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariantMap &parameters, const QgsProcessingContext &context );

/**
* Evaluates the parameter with matching \a definition and \a value to a list of integer values.
* \since QGIS 3.4
*/
static QList<int> parameterAsInts( const QgsProcessingParameterDefinition *definition, const QVariant &value, const QgsProcessingContext &context );

/**
* Evaluates the parameter with matching \a definition to a enum value.
*/
@@ -5105,23 +5105,23 @@ void TestQgsProcessing::parameterBand()

// multiple
def.reset( new QgsProcessingParameterBand( "non_optional", QString(), QVariant(), QString(), false, true ) );
QVERIFY( def->checkValueIsAcceptable( QStringList() << "Band 1" << "Band 2" ) );
QVERIFY( def->checkValueIsAcceptable( QVariantList() << "Band 1" << "Band 2" ) );
QVERIFY( def->checkValueIsAcceptable( QStringList() << "1" << "2" ) );
QVERIFY( def->checkValueIsAcceptable( QVariantList() << 1 << 2 ) );
QVERIFY( !def->checkValueIsAcceptable( "" ) );
QVERIFY( !def->checkValueIsAcceptable( QVariant() ) );
QVERIFY( !def->checkValueIsAcceptable( QStringList() ) );
QVERIFY( !def->checkValueIsAcceptable( QVariantList() ) );

params.insert( "non_optional", QString( "Band 1;Band 2" ) );
QStringList bands = QgsProcessingParameters::parameterAsFields( def.get(), params, context );
QCOMPARE( bands, QStringList() << "Band 1" << "Band 2" );
params.insert( "non_optional", QVariantList() << "Band 1" << "Band 2" );
bands = QgsProcessingParameters::parameterAsFields( def.get(), params, context );
QCOMPARE( bands, QStringList() << "Band 1" << "Band 2" );
params.insert( "non_optional", QString( "1;2" ) );
QList<int> bands = QgsProcessingParameters::parameterAsInts( def.get(), params, context );
QCOMPARE( bands, QList<int>() << 1 << 2 );
params.insert( "non_optional", QVariantList() << 1 << 2 );
bands = QgsProcessingParameters::parameterAsInts( def.get(), params, context );
QCOMPARE( bands, QList<int>() << 1 << 2 );

QCOMPARE( def->valueAsPythonString( QVariant(), context ), QStringLiteral( "None" ) );
QCOMPARE( def->valueAsPythonString( QStringList() << "Band 1" << "Band 2", context ), QStringLiteral( "['Band 1','Band 2']" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << "Band 1" << "Band 2", context ), QStringLiteral( "['Band 1','Band 2']" ) );
QCOMPARE( def->valueAsPythonString( QStringList() << "1" << "2", context ), QStringLiteral( "[1,2]" ) );
QCOMPARE( def->valueAsPythonString( QVariantList() << 1 << 2, context ), QStringLiteral( "[1,2]" ) );

QVariantMap map = def->toVariantMap();
QgsProcessingParameterBand fromMap( "x" );

0 comments on commit 48b18d4

Please sign in to comment.
You can’t perform that action at this time.