Skip to content
Permalink
Browse files

QgsProcessingParameterVectorLayer accepts lists of vector layer types

  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent bdf051a commit a951424287f28a1f426d80cc9b380f0319bda232
@@ -168,10 +168,13 @@ class QgsProcessingParameterDefinition
@see flags()
%End

virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;
%Docstring
Checks whether the specified ``input`` value is acceptable for the
parameter. Returns true if the value can be accepted.
The optional ``context`` parameter can be specified to allow a more stringent
check to be performed, capable of checking for the presence of required
layers and other factors within the context.
:rtype: bool
%End

@@ -384,6 +387,8 @@ class QgsProcessingParameterCrs : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

};

class QgsProcessingParameterMapLayer : QgsProcessingParameterDefinition
@@ -405,6 +410,8 @@ class QgsProcessingParameterMapLayer : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

};

class QgsProcessingParameterExtent : QgsProcessingParameterDefinition
@@ -426,6 +433,8 @@ class QgsProcessingParameterExtent : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


};

@@ -449,7 +458,7 @@ class QgsProcessingParameterPoint : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


};
@@ -479,7 +488,7 @@ class QgsProcessingParameterFile : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


Behavior behavior() const;
@@ -531,7 +540,7 @@ class QgsProcessingParameterMatrix : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


QStringList headers() const;
@@ -601,7 +610,7 @@ class QgsProcessingParameterMultipleLayers : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


QgsProcessingParameterDefinition::LayerType layerType() const;
@@ -664,7 +673,7 @@ class QgsProcessingParameterNumber : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


double minimum() const;
@@ -729,7 +738,7 @@ class QgsProcessingParameterRange : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


QgsProcessingParameterNumber::Type dataType() const;
@@ -766,6 +775,8 @@ class QgsProcessingParameterRasterLayer : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


};

@@ -790,7 +801,7 @@ class QgsProcessingParameterEnum : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


QStringList options() const;
@@ -945,7 +956,7 @@ class QgsProcessingParameterTableField : QgsProcessingParameterDefinition
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input ) const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


QString parentLayerParameter() const;
@@ -1001,26 +1012,28 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
%End
public:

QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(),
const QList< int > &types = QList< int >(),
const QVariant &defaultValue = QVariant(), bool optional = false );
%Docstring
Constructor for QgsProcessingParameterVectorLayer.
%End

virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


QgsProcessingParameterDefinition::LayerType dataType() const;
QList< int > dataTypes() const;
%Docstring
Returns the layer type for layers acceptable by the parameter.
.. seealso:: setDataType()
:rtype: QgsProcessingParameterDefinition.LayerType
Returns the layer types for layers acceptable by the parameter.
.. seealso:: setDataTypes()
:rtype: list of int
%End

void setDataType( QgsProcessingParameterDefinition::LayerType type );
void setDataTypes( const QList< int > &types );
%Docstring
Sets the layer ``type`` for layers acceptable by the parameter.
.. seealso:: dataType()
Sets the layer ``types`` for layers acceptable by the parameter.
.. seealso:: dataTypes()
%End

};
@@ -1048,6 +1061,8 @@ class QgsProcessingParameterOutputVectorLayer : QgsProcessingParameterDefinition

virtual QString type() const;
virtual bool isDestination() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;


QgsProcessingParameterDefinition::LayerType dataType() const;
%Docstring
@@ -30,6 +30,7 @@
from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterOutputVectorLayer,
QgsProcessingOutputVectorLayer)
@@ -52,17 +53,10 @@ class Boundary(QgisAlgorithm):

def __init__(self):
super().__init__()
self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT_LAYER, self.tr('Input layer')))

self.addParameter(QgsProcessingParameterVectorLayer(self.INPUT_LAYER, self.tr('Input layer'), [QgsProcessingParameterDefinition.TypeVectorLine, QgsProcessingParameterDefinition.TypeVectorPolygon]))
self.addParameter(QgsProcessingParameterOutputVectorLayer(self.OUTPUT_LAYER, self.tr('Boundary')))

self.addOutput(QgsProcessingOutputVectorLayer(self.OUTPUT_LAYER, self.tr("Boundaries")))

# self.addParameter(ParameterVector(self.INPUT_LAYER,
# self.tr('Input layer'), [dataobjects.TYPE_VECTOR_LINE,
# dataobjects.TYPE_VECTOR_POLYGON]))
# self.addOutput(OutputVector(self.OUTPUT_LAYER, self.tr('Boundary')))

def icon(self):
return QIcon(os.path.join(pluginPath, 'images', 'ftools', 'convex_hull.png'))

@@ -763,13 +763,13 @@ def createWidget(self):
widget.setLayout(layout)

filters = QgsMapLayerProxyModel.Filters()
if QgsProcessingParameterDefinition.TypeVectorAny == self.param.dataType():
if QgsProcessingParameterDefinition.TypeVectorAny in self.param.dataTypes():
filters = QgsMapLayerProxyModel.HasGeometry
if QgsProcessingParameterDefinition.TypeVectorPoint == self.param.dataType():
if QgsProcessingParameterDefinition.TypeVectorPoint in self.param.dataTypes():
filters |= QgsMapLayerProxyModel.PointLayer
if QgsProcessingParameterDefinition.TypeVectorLine == self.param.dataType():
if QgsProcessingParameterDefinition.TypeVectorLine in self.param.dataTypes():
filters |= QgsMapLayerProxyModel.LineLayer
if QgsProcessingParameterDefinition.TypeVectorPolygon == self.param.dataType():
if QgsProcessingParameterDefinition.TypeVectorPolygon in self.param.dataTypes():
filters |= QgsMapLayerProxyModel.PolygonLayer

try:
@@ -783,7 +783,8 @@ def createWidget(self):
if ProcessingConfig.getSetting(ProcessingConfig.SHOW_CRS_DEF):
self.combo.setShowCrs(True)

self.combo.setFilters(filters)
if filters:
self.combo.setFilters(filters)
self.combo.setExcludedProviders(['grass'])

self.combo.currentIndexChanged.connect(lambda: self.widgetValueHasChanged.emit(self))
@@ -1130,9 +1130,9 @@ void QgsProcessingParameterTableField::setAllowMultiple( bool allowMultiple )
mAllowMultiple = allowMultiple;
}

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

}
@@ -1163,18 +1163,17 @@ bool QgsProcessingParameterVectorLayer::checkValueIsAcceptable( const QVariant &
return false;
}

QgsProcessingParameterDefinition::LayerType QgsProcessingParameterVectorLayer::dataType() const
QList< int > QgsProcessingParameterVectorLayer::dataTypes() const
{
return mDataType;
return mDataTypes;
}

void QgsProcessingParameterVectorLayer::setDataType( QgsProcessingParameterDefinition::LayerType dataType )
void QgsProcessingParameterVectorLayer::setDataTypes( const QList<int> &types )
{
mDataType = dataType;
mDataTypes = types;
}



QgsProcessingParameterOutputVectorLayer::QgsProcessingParameterOutputVectorLayer( const QString &name, const QString &description, QgsProcessingParameterDefinition::LayerType type, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, mDataType( type )
@@ -999,29 +999,30 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
public:

/**
* Constructor for QgsProcessingParameterVectorLayer.
*/
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), QgsProcessingParameterDefinition::LayerType type = QgsProcessingParameterDefinition::TypeVectorAny, const QVariant &defaultValue = QVariant(),
bool optional = false );
* Constructor for QgsProcessingParameterVectorLayer.
*/
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(),
const QList< int > &types = QList< int >(),
const QVariant &defaultValue = QVariant(), bool optional = false );

QString type() const override { return QStringLiteral( "vector" ); }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;

/**
* Returns the layer type for layers acceptable by the parameter.
* \see setDataType()
* Returns the layer types for layers acceptable by the parameter.
* \see setDataTypes()
*/
QgsProcessingParameterDefinition::LayerType dataType() const;
QList< int > dataTypes() const;

/**
* Sets the layer \a type for layers acceptable by the parameter.
* \see dataType()
* Sets the layer \a types for layers acceptable by the parameter.
* \see dataTypes()
*/
void setDataType( QgsProcessingParameterDefinition::LayerType type );
void setDataTypes( const QList< int > &types );

private:

QgsProcessingParameterDefinition::LayerType mDataType = QgsProcessingParameterDefinition::TypeVectorAny;
QList< int > mDataTypes = QList< int >() << QgsProcessingParameterDefinition::TypeVectorAny;

};

@@ -2098,7 +2098,7 @@ void TestQgsProcessing::parameterVectorLayer()
context.setProject( &p );

// not optional!
QgsProcessingParameterVectorLayer *def = new QgsProcessingParameterVectorLayer( "non_optional", QString(), QgsProcessingParameterDefinition::TypeVectorAny, QString( "EPSG:3113" ), false );
QgsProcessingParameterVectorLayer *def = new QgsProcessingParameterVectorLayer( "non_optional", QString(), QList< int >() << QgsProcessingParameterDefinition::TypeVectorAny, QString( "EPSG:3113" ), false );
QVERIFY( !def->checkValueIsAcceptable( false ) );
QVERIFY( !def->checkValueIsAcceptable( true ) );
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
@@ -2130,7 +2130,7 @@ void TestQgsProcessing::parameterVectorLayer()

// optional
delete def;
def = new QgsProcessingParameterVectorLayer( "optional", QString(), QgsProcessingParameterDefinition::TypeVectorAny, v1->id(), true );
def = new QgsProcessingParameterVectorLayer( "optional", QString(), QList< int >() << QgsProcessingParameterDefinition::TypeVectorAny, v1->id(), true );
params.insert( "optional", QVariant() );
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def, params, QStringLiteral( "optional" ), context )->id(), v1->id() );
QVERIFY( def->checkValueIsAcceptable( false ) );

0 comments on commit a951424

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