[processing] Allow case-different duplicate parameter names

But always prefer case-exact matches for parameter names. Turns
out the grass provider requires use of parameters with the same
name but different case, so we need to be able to handle this.
This commit is contained in:
Nyall Dawson 2018-01-29 09:53:04 +10:00
parent 28ff28a223
commit fc68bb25d7
4 changed files with 22 additions and 4 deletions

View File

@ -224,7 +224,7 @@ Returns an ordered list of parameter definitions utilized by the algorithm.
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;
%Docstring
Returns a matching parameter by ``name``. Matching is done in a case-insensitive
manner.
manner, but exact case matches will be preferred.
.. seealso:: :py:func:`parameterDefinitions`
%End

View File

@ -243,7 +243,8 @@ bool QgsProcessingAlgorithm::addParameter( QgsProcessingParameterDefinition *def
return false;
// check for duplicate named parameters
if ( QgsProcessingAlgorithm::parameterDefinition( definition->name() ) )
const QgsProcessingParameterDefinition *existingDef = QgsProcessingAlgorithm::parameterDefinition( definition->name() );
if ( existingDef && existingDef->name() == definition->name() ) // parameterDefinition is case-insensitive, but we DO allow case-different duplicate names
{
QgsLogger::warning( QStringLiteral( "Duplicate parameter %1 registered for alg %2" ).arg( definition->name(), id() ) );
return false;
@ -300,7 +301,15 @@ QVariantMap QgsProcessingAlgorithm::postProcessAlgorithm( QgsProcessingContext &
const QgsProcessingParameterDefinition *QgsProcessingAlgorithm::parameterDefinition( const QString &name ) const
{
Q_FOREACH ( const QgsProcessingParameterDefinition *def, mParameters )
// first pass - case sensitive match
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->name() == name )
return def;
}
// second pass - case insensitive
for ( const QgsProcessingParameterDefinition *def : mParameters )
{
if ( def->name().compare( name, Qt::CaseInsensitive ) == 0 )
return def;

View File

@ -247,7 +247,7 @@ class CORE_EXPORT QgsProcessingAlgorithm
/**
* Returns a matching parameter by \a name. Matching is done in a case-insensitive
* manner.
* manner, but exact case matches will be preferred.
* \see parameterDefinitions()
*/
const QgsProcessingParameterDefinition *parameterDefinition( const QString &name ) const;

View File

@ -147,6 +147,15 @@ class DummyAlgorithm : public QgsProcessingAlgorithm
QCOMPARE( rasterParam->defaultFileExtension(), QStringLiteral( "tif" ) ); // before alg is accessible
addParameter( rasterParam );
QCOMPARE( rasterParam->defaultFileExtension(), QStringLiteral( "tif" ) );
// should allow parameters with same name but different case (required for grass provider)
QgsProcessingParameterBoolean *p1C = new QgsProcessingParameterBoolean( "P1" );
QVERIFY( addParameter( p1C ) );
QCOMPARE( parameterDefinitions().count(), 8 );
// parameterDefinition should be case insensitive, but prioritise correct case matches
QCOMPARE( parameterDefinition( "p1" ), parameterDefinitions().at( 0 ) );
QCOMPARE( parameterDefinition( "P1" ), parameterDefinitions().at( 7 ) );
}
void runParameterChecks2()