Skip to content

Commit

Permalink
QgsProcessingParameterVectorLayer accepts lists of vector layer types
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jun 5, 2017
1 parent bdf051a commit a951424
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 51 deletions.
51 changes: 33 additions & 18 deletions python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -426,6 +433,8 @@ class QgsProcessingParameterExtent : QgsProcessingParameterDefinition
%End

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


};

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


};
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -766,6 +775,8 @@ class QgsProcessingParameterRasterLayer : QgsProcessingParameterDefinition
%End

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


};

Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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

};
Expand Down Expand Up @@ -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
Expand Down
10 changes: 2 additions & 8 deletions python/plugins/processing/algs/qgis/Boundary.py
Expand Up @@ -30,6 +30,7 @@
from qgis.core import (QgsGeometry,
QgsWkbTypes,
QgsProcessingUtils,
QgsProcessingParameterDefinition,
QgsProcessingParameterVectorLayer,
QgsProcessingParameterOutputVectorLayer,
QgsProcessingOutputVectorLayer)
Expand All @@ -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'))

Expand Down
11 changes: 6 additions & 5 deletions python/plugins/processing/gui/wrappers.py
Expand Up @@ -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:
Expand All @@ -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))
Expand Down
13 changes: 6 additions & 7 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -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 )
{

}
Expand Down Expand Up @@ -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 )
Expand Down
23 changes: 12 additions & 11 deletions src/core/processing/qgsprocessingparameters.h
Expand Up @@ -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;

};

Expand Down
4 changes: 2 additions & 2 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 ) );
Expand Down

0 comments on commit a951424

Please sign in to comment.