Skip to content
Permalink
Browse files

Fix restricting model algorithm input types to valid types for alg (r…

…efs #17030)
  • Loading branch information
nyalldawson committed Aug 23, 2017
1 parent 4511ea1 commit cb70aad7a36c5051e0ba3b5656a144faf62bddc2
@@ -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
@@ -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 );
@@ -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.
@@ -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 );
@@ -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)
@@ -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;
}
@@ -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;
}
@@ -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 )
{

}
@@ -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;
}
@@ -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 )
{

}
@@ -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
@@ -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:

@@ -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;

@@ -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;


};

/**
@@ -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:

@@ -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;

@@ -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;

};

/**

0 comments on commit cb70aad

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