Skip to content

Commit f7b25a1

Browse files
committedJul 27, 2017
Allow setting layer type filter for QgsProcessingParameterVectorLayer
Turns out this is required for some algorithms
1 parent 5585805 commit f7b25a1

File tree

7 files changed

+99
-10
lines changed

7 files changed

+99
-10
lines changed
 

‎python/core/processing/qgsprocessingparameters.sip

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1395,7 +1395,10 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
13951395
%End
13961396
public:
13971397

1398-
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
1398+
QgsProcessingParameterVectorLayer( const QString &name,
1399+
const QString &description = QString(),
1400+
const QList< int > &types = QList< int >(),
1401+
const QVariant &defaultValue = QVariant(),
13991402
bool optional = false );
14001403
%Docstring
14011404
Constructor for QgsProcessingParameterVectorLayer.
@@ -1412,6 +1415,24 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
14121415
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
14131416

14141417

1418+
QList< int > dataTypes() const;
1419+
%Docstring
1420+
Returns the geometry types for sources acceptable by the parameter.
1421+
.. seealso:: setDataTypes()
1422+
:rtype: list of int
1423+
%End
1424+
1425+
void setDataTypes( const QList< int > &types );
1426+
%Docstring
1427+
Sets the geometry ``types`` for sources acceptable by the parameter.
1428+
.. seealso:: dataTypes()
1429+
%End
1430+
1431+
virtual QVariantMap toVariantMap() const;
1432+
1433+
virtual bool fromVariantMap( const QVariantMap &map );
1434+
1435+
14151436
static QgsProcessingParameterVectorLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) /Factory/;
14161437
%Docstring
14171438
Creates a new parameter using the definition from a script code.

‎python/plugins/processing/algs/qgis/ImportIntoSpatialite.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(self):
6262

6363
def initAlgorithm(self, config=None):
6464
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT, self.tr('Layer to import')))
65-
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File database'), False, False))
65+
self.addParameter(QgsProcessingParameterVectorLayer(self.DATABASE, self.tr('File database'), [], False, False))
6666
self.addParameter(QgsProcessingParameterString(self.TABLENAME, self.tr('Table to import to (leave blank to use layer name)'), optional=True))
6767
self.addParameter(QgsProcessingParameterField(self.PRIMARY_KEY, self.tr('Primary key field'), self.INPUT, optional=True))
6868
self.addParameter(QgsProcessingParameterString(self.GEOMETRY_COLUMN, self.tr('Geometry column'), 'geom'))

‎python/plugins/processing/algs/qgis/SpatialiteExecuteSQL.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(self):
4747
super().__init__()
4848

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

5353
def name(self):

‎python/plugins/processing/gui/wrappers.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,17 @@ def createWidget(self):
10671067
if self.param.flags() & QgsProcessingParameterDefinition.FlagOptional:
10681068
self.combo.setAllowEmptyLayer(True)
10691069

1070-
self.combo.setFilters(QgsMapLayerProxyModel.VectorLayer)
1070+
filters = QgsMapLayerProxyModel.Filters()
1071+
if QgsProcessing.TypeVectorAny in self.param.dataTypes() or len(self.param.dataTypes()) == 0:
1072+
filters = QgsMapLayerProxyModel.VectorLayer
1073+
if QgsProcessing.TypeVectorPoint in self.param.dataTypes():
1074+
filters |= QgsMapLayerProxyModel.PointLayer
1075+
if QgsProcessing.TypeVectorLine in self.param.dataTypes():
1076+
filters |= QgsMapLayerProxyModel.LineLayer
1077+
if QgsProcessing.TypeVectorPolygon in self.param.dataTypes():
1078+
filters |= QgsMapLayerProxyModel.PolygonLayer
1079+
self.combo.setFilters(filters)
1080+
10711081
self.combo.setExcludedProviders(['grass'])
10721082
try:
10731083
if iface.activeLayer().type() == QgsMapLayer.VectorLayer:

‎src/core/processing/qgsprocessingparameters.cpp

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,8 +2096,9 @@ QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCo
20962096
return new QgsProcessingParameterExpression( name, description, definition, QString(), isOptional );
20972097
}
20982098

2099-
QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
2099+
QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
21002100
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2101+
, mDataTypes( types )
21012102
{
21022103

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

2146+
QList<int> QgsProcessingParameterVectorLayer::dataTypes() const
2147+
{
2148+
return mDataTypes;
2149+
}
2150+
2151+
void QgsProcessingParameterVectorLayer::setDataTypes( const QList<int> &types )
2152+
{
2153+
mDataTypes = types;
2154+
}
2155+
2156+
QVariantMap QgsProcessingParameterVectorLayer::toVariantMap() const
2157+
{
2158+
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
2159+
QVariantList types;
2160+
Q_FOREACH ( int type, mDataTypes )
2161+
{
2162+
types << type;
2163+
}
2164+
map.insert( QStringLiteral( "data_types" ), types );
2165+
return map;
2166+
}
2167+
2168+
bool QgsProcessingParameterVectorLayer::fromVariantMap( const QVariantMap &map )
2169+
{
2170+
QgsProcessingParameterDefinition::fromVariantMap( map );
2171+
mDataTypes.clear();
2172+
QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
2173+
Q_FOREACH ( const QVariant &val, values )
2174+
{
2175+
mDataTypes << val.toInt();
2176+
}
2177+
return true;
2178+
}
2179+
21452180
QgsProcessingParameterVectorLayer *QgsProcessingParameterVectorLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
21462181
{
2147-
return new QgsProcessingParameterVectorLayer( name, description, definition.isEmpty() ? QVariant() : definition, isOptional );
2182+
return new QgsProcessingParameterVectorLayer( name, description, QList< int>(), definition.isEmpty() ? QVariant() : definition, isOptional );
21482183
}
21492184

21502185
QgsProcessingParameterField::QgsProcessingParameterField( const QString &name, const QString &description, const QVariant &defaultValue, const QString &parentLayerParameterName, DataType type, bool allowMultiple, bool optional )

‎src/core/processing/qgsprocessingparameters.h

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1341,7 +1341,10 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
13411341
/**
13421342
* Constructor for QgsProcessingParameterVectorLayer.
13431343
*/
1344-
QgsProcessingParameterVectorLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
1344+
QgsProcessingParameterVectorLayer( const QString &name,
1345+
const QString &description = QString(),
1346+
const QList< int > &types = QList< int >(),
1347+
const QVariant &defaultValue = QVariant(),
13451348
bool optional = false );
13461349

13471350
/**
@@ -1352,11 +1355,31 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
13521355
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
13531356
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
13541357

1358+
/**
1359+
* Returns the geometry types for sources acceptable by the parameter.
1360+
* \see setDataTypes()
1361+
*/
1362+
QList< int > dataTypes() const;
1363+
1364+
/**
1365+
* Sets the geometry \a types for sources acceptable by the parameter.
1366+
* \see dataTypes()
1367+
*/
1368+
void setDataTypes( const QList< int > &types );
1369+
1370+
QVariantMap toVariantMap() const override;
1371+
bool fromVariantMap( const QVariantMap &map ) override;
1372+
13551373
/**
13561374
* Creates a new parameter using the definition from a script code.
13571375
*/
13581376
static QgsProcessingParameterVectorLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;
13591377

1378+
private:
1379+
1380+
QList< int > mDataTypes = QList< int >() << QgsProcessing::TypeVectorAny;
1381+
1382+
13601383
};
13611384

13621385
/**

‎tests/src/core/testqgsprocessing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3265,7 +3265,7 @@ void TestQgsProcessing::parameterVectorLayer()
32653265
context.setProject( &p );
32663266

32673267
// not optional!
3268-
std::unique_ptr< QgsProcessingParameterVectorLayer > def( new QgsProcessingParameterVectorLayer( "non_optional", QString(), QString( "somelayer" ), false ) );
3268+
std::unique_ptr< QgsProcessingParameterVectorLayer > def( new QgsProcessingParameterVectorLayer( "non_optional", QString(), QList< int >(), QString( "somelayer" ), false ) );
32693269
QVERIFY( !def->checkValueIsAcceptable( false ) );
32703270
QVERIFY( !def->checkValueIsAcceptable( true ) );
32713271
QVERIFY( !def->checkValueIsAcceptable( 5 ) );
@@ -3331,7 +3331,7 @@ void TestQgsProcessing::parameterVectorLayer()
33313331
QVERIFY( dynamic_cast< QgsProcessingParameterVectorLayer *>( def.get() ) );
33323332

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

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

0 commit comments

Comments
 (0)
Please sign in to comment.