Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow setting layer type filter for QgsProcessingParameterVectorLayer
Turns out this is required for some algorithms
  • Loading branch information
nyalldawson committed Jul 27, 2017
1 parent 5585805 commit f7b25a1
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 10 deletions.
23 changes: 22 additions & 1 deletion python/core/processing/qgsprocessingparameters.sip
Expand Up @@ -1395,7 +1395,10 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
%End
public:

QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
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.
Expand All @@ -1412,6 +1415,24 @@ 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 );


static QgsProcessingParameterVectorLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
%Docstring
Creates a new parameter using the definition from a script code.
Expand Down
Expand Up @@ -62,7 +62,7 @@ def __init__(self):

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Layer to import')))
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File database'), False, False))
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File database'), [], False, False))
self.addParameter(QgsProcessingParameterString(self.TABLENAME, self.tr('Table to import to (leave blank to use layer name)'), optional=True))
self.addParameter(QgsProcessingParameterField(self.PRIMARY_KEY, self.tr('Primary key field'), self.INPUT, optional=True))
self.addParameter(QgsProcessingParameterString(self.GEOMETRY_COLUMN, self.tr('Geometry column'), 'geom'))
Expand Down
Expand Up @@ -47,7 +47,7 @@ def __init__(self):
super().__init__()

def initAlgorithm(self, config=None):
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File Database'), False, False))
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File Database'), [], False, False))
self.addParameter(QgsProcessingParameterString(self.SQL, self.tr('SQL query'), '', True))

def name(self):
Expand Down
12 changes: 11 additions & 1 deletion python/plugins/processing/gui/wrappers.py
Expand Up @@ -1067,7 +1067,17 @@ def createWidget(self):
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
self.combo.setAllowEmptyLayer(True)

self.combo.setFilters(QgsMapLayerProxyModel.VectorLayer)
filters = QgsMapLayerProxyModel.Filters()
if QgsProcessing.TypeVectorAny in self.param.dataTypes() or len(self.param.dataTypes()) == 0:
filters = QgsMapLayerProxyModel.VectorLayer
if QgsProcessing.TypeVectorPoint in self.param.dataTypes():
filters |= QgsMapLayerProxyModel.PointLayer
if QgsProcessing.TypeVectorLine in self.param.dataTypes():
filters |= QgsMapLayerProxyModel.LineLayer
if QgsProcessing.TypeVectorPolygon in self.param.dataTypes():
filters |= QgsMapLayerProxyModel.PolygonLayer
self.combo.setFilters(filters)

self.combo.setExcludedProviders(['grass'])
try:
if iface.activeLayer().type() == QgsMapLayer.VectorLayer:
Expand Down
39 changes: 37 additions & 2 deletions src/core/processing/qgsprocessingparameters.cpp
Expand Up @@ -2096,8 +2096,9 @@ QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCo
return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
}

QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, 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 )
, mDataTypes( types )
{

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

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

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

QVariantMap QgsProcessingParameterVectorLayer::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
QVariantList types;
Q_FOREACH ( int type, mDataTypes )
{
types << type;
}
map.insert( QStringLiteral( "data_types" ), types );
return map;
}

bool QgsProcessingParameterVectorLayer::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterDefinition::fromVariantMap( map );
mDataTypes.clear();
QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
Q_FOREACH ( const QVariant &val, values )
{
mDataTypes << val.toInt();
}
return true;
}

QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
return new QgsProcessingParameterVectorLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
}

QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional )
Expand Down
25 changes: 24 additions & 1 deletion src/core/processing/qgsprocessingparameters.h
Expand Up @@ -1341,7 +1341,10 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
/**
* Constructor for QgsProcessingParameterVectorLayer.
*/
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
QgsProcessingParameterVectorLayer( const QString &name,
const QString &description = QString(),
const QList< int > &types = QList< int >(),
const QVariant &defaultValue = QVariant(),
bool optional = false );

/**
Expand All @@ -1352,11 +1355,31 @@ 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;

/**
* Creates a new parameter using the definition from a script code.
*/
static QgsProcessingParameterVectorLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;

private:

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


};

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/src/core/testqgsprocessing.cpp
Expand Up @@ -3265,7 +3265,7 @@ void TestQgsProcessing::parameterVectorLayer()
context.setProject( &p );

// not optional!
std::unique_ptr< QgsProcessingParameterVectorLayer > def( new QgsProcessingParameterVectorLayer( "non_optional", QString(), QString( "somelayer" ), false ) );
std::unique_ptr< QgsProcessingParameterVectorLayer > def( new QgsProcessingParameterVectorLayer( "non_optional", QString(), QList< int >(), QString( "somelayer" ), false ) );
QVERIFY( !def->checkValueIsAcceptable( false ) );
QVERIFY( !def->checkValueIsAcceptable( true ) );
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
Expand Down Expand Up @@ -3331,7 +3331,7 @@ void TestQgsProcessing::parameterVectorLayer()
QVERIFY( dynamic_cast< QgsProcessingParameterVectorLayer *>( def.get() ) );

// optional
def.reset( new QgsProcessingParameterVectorLayer( "optional", QString(), v1->id(), true ) );
def.reset( new QgsProcessingParameterVectorLayer( "optional", QString(), QList< int >(), v1->id(), true ) );
params.insert( "optional", QVariant() );
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );
QVERIFY( def->checkValueIsAcceptable( false ) );
Expand All @@ -3353,7 +3353,7 @@ void TestQgsProcessing::parameterVectorLayer()
QCOMPARE( fromCode->defaultValue(), def->defaultValue() );

//optional with direct layer default
def.reset( new QgsProcessingParameterVectorLayer( "optional", QString(), QVariant::fromValue( v1 ), true ) );
def.reset( new QgsProcessingParameterVectorLayer( "optional", QString(), QList< int >(), QVariant::fromValue( v1 ), true ) );
QCOMPARE( QgsProcessingParameters::parameterAsVectorLayer( def.get(), params, context )->id(), v1->id() );
}

Expand Down

0 comments on commit f7b25a1

Please sign in to comment.