Skip to content

Commit

Permalink
Fix restricting model algorithm input types to valid types for alg (r…
Browse files Browse the repository at this point in the history
…efs #17030)
  • Loading branch information
nyalldawson committed Aug 23, 2017
1 parent 4511ea1 commit cb70aad
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 79 deletions.
66 changes: 38 additions & 28 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -1420,7 +1420,42 @@ class QgsProcessingParameterExpression : QgsProcessingParameterDefinition

};

class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition

class QgsProcessingParameterLimitedDataTypes
{
%Docstring
Can be inherited by parameters which require limits to their acceptable data types.
.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingParameterLimitedDataTypes( const QList< int > &types = QList< int >() );
%Docstring
Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data ``types``.
%End

QList< int > dataTypes() const;
%Docstring
Returns the geometry types for sources acceptable by the parameter.
.. seealso:: setDataTypes()
:rtype: list of int
%End

void setDataTypes( const QList< int > &types );
%Docstring
Sets the geometry ``types`` for sources acceptable by the parameter.
.. seealso:: dataTypes()
%End

protected:

};

class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition, QgsProcessingParameterLimitedDataTypes
{
%Docstring
A vector layer (with or without geometry) parameter for processing algorithms. Consider using
Expand Down Expand Up @@ -1455,19 +1490,6 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;


QList< int > dataTypes() const;
%Docstring
Returns the geometry types for sources acceptable by the parameter.
.. seealso:: setDataTypes()
:rtype: list of int
%End

void setDataTypes( const QList< int > &types );
%Docstring
Sets the geometry ``types`` for sources acceptable by the parameter.
.. seealso:: dataTypes()
%End

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );
Expand Down Expand Up @@ -1579,7 +1601,8 @@ class QgsProcessingParameterField : QgsProcessingParameterDefinition

};

class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition

class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition, QgsProcessingParameterLimitedDataTypes
{
%Docstring
An input feature source (such as vector layers) parameter for processing algorithms.
Expand Down Expand Up @@ -1613,19 +1636,6 @@ class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition
virtual QString asScriptCode() const;


QList< int > dataTypes() const;
%Docstring
Returns the geometry types for sources acceptable by the parameter.
.. seealso:: setDataTypes()
:rtype: list of int
%End

void setDataTypes( const QList< int > &types );
%Docstring
Sets the geometry ``types`` for sources acceptable by the parameter.
.. seealso:: dataTypes()
%End

virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/gui/wrappers.py
Expand Up @@ -897,7 +897,7 @@ def createWidget(self):
return widget
else:
self.combo = QComboBox()
layers = self.dialog.getAvailableValuesOfType((QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer), QgsProcessingOutputVectorLayer)
layers = self.dialog.getAvailableValuesOfType((QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer), QgsProcessingOutputVectorLayer, self.param.dataTypes())
self.combo.setEditable(True)
for layer in layers:
self.combo.addItem(self.dialog.resolveValueDescription(layer), layer)
Expand Down
18 changes: 13 additions & 5 deletions src/core/processing/models/qgsprocessingmodelalgorithm.cpp
Expand Up @@ -590,18 +590,24 @@ QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSo
continue;
}
}
else if ( def->type() == QgsProcessingParameterFeatureSource::typeName() )
else if ( def->type() == QgsProcessingParameterFeatureSource::typeName() || def->type() == QgsProcessingParameterVectorLayer::typeName() )
{
const QgsProcessingParameterFeatureSource *sourceDef = static_cast< const QgsProcessingParameterFeatureSource *>( def );
bool ok = !sourceDef->dataTypes().isEmpty();
const QgsProcessingParameterLimitedDataTypes *sourceDef = dynamic_cast< const QgsProcessingParameterLimitedDataTypes *>( def );
if ( !sourceDef )
continue;

bool ok = sourceDef->dataTypes().isEmpty();
Q_FOREACH ( int type, sourceDef->dataTypes() )
{
if ( dataTypes.contains( type ) || type == QgsProcessing::TypeMapLayer )
if ( dataTypes.contains( type ) || type == QgsProcessing::TypeMapLayer || type == QgsProcessing::TypeVector || type == QgsProcessing::TypeVectorAnyGeometry )
{
ok = true;
break;
}
}
if ( dataTypes.contains( QgsProcessing::TypeMapLayer ) || dataTypes.contains( QgsProcessing::TypeVector ) || dataTypes.contains( QgsProcessing::TypeVectorAnyGeometry ) )
ok = true;

if ( !ok )
continue;
}
Expand Down Expand Up @@ -636,7 +642,9 @@ QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSo
if ( out->type() == QgsProcessingOutputVectorLayer::typeName() )
{
const QgsProcessingOutputVectorLayer *vectorOut = static_cast< const QgsProcessingOutputVectorLayer *>( out );
if ( !( dataTypes.contains( vectorOut->dataType() ) || vectorOut->dataType() == QgsProcessing::TypeMapLayer ) )

if ( !( dataTypes.contains( vectorOut->dataType() ) || vectorOut->dataType() == QgsProcessing::TypeMapLayer || vectorOut->dataType() == QgsProcessing::TypeVector
|| vectorOut->dataType() == QgsProcessing::TypeVectorAnyGeometry ) )
{
continue;
}
Expand Down
16 changes: 6 additions & 10 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2184,7 +2184,7 @@ QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCo

QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mDataTypes( types )
, QgsProcessingParameterLimitedDataTypes( types )
{

}
Expand Down Expand Up @@ -2234,12 +2234,12 @@ QString QgsProcessingParameterVectorLayer::valueAsPythonString( const QVariant &
return layer ? QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' ) : QString();
}

QList<int> QgsProcessingParameterVectorLayer::dataTypes() const
QList<int> QgsProcessingParameterLimitedDataTypes::dataTypes() const
{
return mDataTypes;
}

void QgsProcessingParameterVectorLayer::setDataTypes( const QList<int> &types )
void QgsProcessingParameterLimitedDataTypes::setDataTypes( const QList<int> &types )
{
mDataTypes = types;
}
Expand Down Expand Up @@ -2483,7 +2483,7 @@ QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const

QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mDataTypes( types )
, QgsProcessingParameterLimitedDataTypes( types )
{

}
Expand Down Expand Up @@ -2603,14 +2603,10 @@ QString QgsProcessingParameterFeatureSource::asScriptCode() const
return code.trimmed();
}

QList< int > QgsProcessingParameterFeatureSource::dataTypes() const
QgsProcessingParameterLimitedDataTypes::QgsProcessingParameterLimitedDataTypes( const QList<int> &types )
: mDataTypes( types )
{
return mDataTypes;
}

void QgsProcessingParameterFeatureSource::setDataTypes( const QList<int> &types )
{
mDataTypes = types;
}

QVariantMap QgsProcessingParameterFeatureSource::toVariantMap() const
Expand Down
72 changes: 37 additions & 35 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -1350,14 +1350,48 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet

};


/**
* \class QgsProcessingParameterLimitedDataTypes
* \ingroup core
* Can be inherited by parameters which require limits to their acceptable data types.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterLimitedDataTypes
{
public:

/**
* Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data \a types.
*/
QgsProcessingParameterLimitedDataTypes( const QList< int > &types = QList< int >() );

/**
* Returns the geometry types for sources acceptable by the parameter.
* \see setDataTypes()
*/
QList< int > dataTypes() const;

/**
* Sets the geometry \a types for sources acceptable by the parameter.
* \see dataTypes()
*/
void setDataTypes( const QList< int > &types );

protected:

//! List of acceptable data types for the parameter
QList< int > mDataTypes;
};

/**
* \class QgsProcessingParameterVectorLayer
* \ingroup core
* A vector layer (with or without geometry) parameter for processing algorithms. Consider using
* the more versatile QgsProcessingParameterFeatureSource wherever possible.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParameterDefinition, public QgsProcessingParameterLimitedDataTypes
{
public:

Expand All @@ -1379,18 +1413,6 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;

/**
* Returns the geometry types for sources acceptable by the parameter.
* \see setDataTypes()
*/
QList< int > dataTypes() const;

/**
* Sets the geometry \a types for sources acceptable by the parameter.
* \see dataTypes()
*/
void setDataTypes( const QList< int > &types );

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

Expand All @@ -1399,11 +1421,6 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
*/
static QgsProcessingParameterVectorLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;

private:

QList< int > mDataTypes = QList< int >() << QgsProcessing::TypeVectorAnyGeometry;


};

/**
Expand Down Expand Up @@ -1497,13 +1514,14 @@ class CORE_EXPORT QgsProcessingParameterField : public QgsProcessingParameterDef

};


/**
* \class QgsProcessingParameterFeatureSource
* \ingroup core
* An input feature source (such as vector layers) parameter for processing algorithms.
* \since QGIS 3.0
*/
class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingParameterDefinition
class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingParameterDefinition, public QgsProcessingParameterLimitedDataTypes
{
public:

Expand All @@ -1524,18 +1542,6 @@ class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingPara
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asScriptCode() const override;

/**
* Returns the geometry types for sources acceptable by the parameter.
* \see setDataTypes()
*/
QList< int > dataTypes() const;

/**
* Sets the geometry \a types for sources acceptable by the parameter.
* \see dataTypes()
*/
void setDataTypes( const QList< int > &types );

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

Expand All @@ -1544,10 +1550,6 @@ class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingPara
*/
static QgsProcessingParameterFeatureSource *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;

private:

QList< int > mDataTypes = QList< int >() << QgsProcessing::TypeVectorAnyGeometry;

};

/**
Expand Down

0 comments on commit cb70aad

Please sign in to comment.