Skip to content

Commit cb70aad

Browse files
committed
Fix restricting model algorithm input types to valid types for alg (refs #17030)
1 parent 4511ea1 commit cb70aad

File tree

6 files changed

+206
-79
lines changed

6 files changed

+206
-79
lines changed

python/core/processing/qgsprocessingparameters.sip

+38-28
Original file line numberDiff line numberDiff line change
@@ -1420,7 +1420,42 @@ class QgsProcessingParameterExpression : QgsProcessingParameterDefinition
14201420

14211421
};
14221422

1423-
class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
1423+
1424+
class QgsProcessingParameterLimitedDataTypes
1425+
{
1426+
%Docstring
1427+
Can be inherited by parameters which require limits to their acceptable data types.
1428+
.. versionadded:: 3.0
1429+
%End
1430+
1431+
%TypeHeaderCode
1432+
#include "qgsprocessingparameters.h"
1433+
%End
1434+
public:
1435+
1436+
QgsProcessingParameterLimitedDataTypes( const QList< int > &types = QList< int >() );
1437+
%Docstring
1438+
Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data ``types``.
1439+
%End
1440+
1441+
QList< int > dataTypes() const;
1442+
%Docstring
1443+
Returns the geometry types for sources acceptable by the parameter.
1444+
.. seealso:: setDataTypes()
1445+
:rtype: list of int
1446+
%End
1447+
1448+
void setDataTypes( const QList< int > &types );
1449+
%Docstring
1450+
Sets the geometry ``types`` for sources acceptable by the parameter.
1451+
.. seealso:: dataTypes()
1452+
%End
1453+
1454+
protected:
1455+
1456+
};
1457+
1458+
class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition, QgsProcessingParameterLimitedDataTypes
14241459
{
14251460
%Docstring
14261461
A vector layer (with or without geometry) parameter for processing algorithms. Consider using
@@ -1455,19 +1490,6 @@ class QgsProcessingParameterVectorLayer : QgsProcessingParameterDefinition
14551490
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;
14561491

14571492

1458-
QList< int > dataTypes() const;
1459-
%Docstring
1460-
Returns the geometry types for sources acceptable by the parameter.
1461-
.. seealso:: setDataTypes()
1462-
:rtype: list of int
1463-
%End
1464-
1465-
void setDataTypes( const QList< int > &types );
1466-
%Docstring
1467-
Sets the geometry ``types`` for sources acceptable by the parameter.
1468-
.. seealso:: dataTypes()
1469-
%End
1470-
14711493
virtual QVariantMap toVariantMap() const;
14721494

14731495
virtual bool fromVariantMap( const QVariantMap &map );
@@ -1579,7 +1601,8 @@ class QgsProcessingParameterField : QgsProcessingParameterDefinition
15791601

15801602
};
15811603

1582-
class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition
1604+
1605+
class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition, QgsProcessingParameterLimitedDataTypes
15831606
{
15841607
%Docstring
15851608
An input feature source (such as vector layers) parameter for processing algorithms.
@@ -1613,19 +1636,6 @@ class QgsProcessingParameterFeatureSource : QgsProcessingParameterDefinition
16131636
virtual QString asScriptCode() const;
16141637

16151638

1616-
QList< int > dataTypes() const;
1617-
%Docstring
1618-
Returns the geometry types for sources acceptable by the parameter.
1619-
.. seealso:: setDataTypes()
1620-
:rtype: list of int
1621-
%End
1622-
1623-
void setDataTypes( const QList< int > &types );
1624-
%Docstring
1625-
Sets the geometry ``types`` for sources acceptable by the parameter.
1626-
.. seealso:: dataTypes()
1627-
%End
1628-
16291639
virtual QVariantMap toVariantMap() const;
16301640

16311641
virtual bool fromVariantMap( const QVariantMap &map );

python/plugins/processing/gui/wrappers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,7 @@ def createWidget(self):
897897
return widget
898898
else:
899899
self.combo = QComboBox()
900-
layers = self.dialog.getAvailableValuesOfType((QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer), QgsProcessingOutputVectorLayer)
900+
layers = self.dialog.getAvailableValuesOfType((QgsProcessingParameterFeatureSource, QgsProcessingParameterVectorLayer), QgsProcessingOutputVectorLayer, self.param.dataTypes())
901901
self.combo.setEditable(True)
902902
for layer in layers:
903903
self.combo.addItem(self.dialog.resolveValueDescription(layer), layer)

src/core/processing/models/qgsprocessingmodelalgorithm.cpp

+13-5
Original file line numberDiff line numberDiff line change
@@ -590,18 +590,24 @@ QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSo
590590
continue;
591591
}
592592
}
593-
else if ( def->type() == QgsProcessingParameterFeatureSource::typeName() )
593+
else if ( def->type() == QgsProcessingParameterFeatureSource::typeName() || def->type() == QgsProcessingParameterVectorLayer::typeName() )
594594
{
595-
const QgsProcessingParameterFeatureSource *sourceDef = static_cast< const QgsProcessingParameterFeatureSource *>( def );
596-
bool ok = !sourceDef->dataTypes().isEmpty();
595+
const QgsProcessingParameterLimitedDataTypes *sourceDef = dynamic_cast< const QgsProcessingParameterLimitedDataTypes *>( def );
596+
if ( !sourceDef )
597+
continue;
598+
599+
bool ok = sourceDef->dataTypes().isEmpty();
597600
Q_FOREACH ( int type, sourceDef->dataTypes() )
598601
{
599-
if ( dataTypes.contains( type ) || type == QgsProcessing::TypeMapLayer )
602+
if ( dataTypes.contains( type ) || type == QgsProcessing::TypeMapLayer || type == QgsProcessing::TypeVector || type == QgsProcessing::TypeVectorAnyGeometry )
600603
{
601604
ok = true;
602605
break;
603606
}
604607
}
608+
if ( dataTypes.contains( QgsProcessing::TypeMapLayer ) || dataTypes.contains( QgsProcessing::TypeVector ) || dataTypes.contains( QgsProcessing::TypeVectorAnyGeometry ) )
609+
ok = true;
610+
605611
if ( !ok )
606612
continue;
607613
}
@@ -636,7 +642,9 @@ QgsProcessingModelChildParameterSources QgsProcessingModelAlgorithm::availableSo
636642
if ( out->type() == QgsProcessingOutputVectorLayer::typeName() )
637643
{
638644
const QgsProcessingOutputVectorLayer *vectorOut = static_cast< const QgsProcessingOutputVectorLayer *>( out );
639-
if ( !( dataTypes.contains( vectorOut->dataType() ) || vectorOut->dataType() == QgsProcessing::TypeMapLayer ) )
645+
646+
if ( !( dataTypes.contains( vectorOut->dataType() ) || vectorOut->dataType() == QgsProcessing::TypeMapLayer || vectorOut->dataType() == QgsProcessing::TypeVector
647+
|| vectorOut->dataType() == QgsProcessing::TypeVectorAnyGeometry ) )
640648
{
641649
continue;
642650
}

src/core/processing/qgsprocessingparameters.cpp

+6-10
Original file line numberDiff line numberDiff line change
@@ -2184,7 +2184,7 @@ QgsProcessingParameterExpression *QgsProcessingParameterExpression::fromScriptCo
21842184

21852185
QgsProcessingParameterVectorLayer::QgsProcessingParameterVectorLayer( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
21862186
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2187-
, mDataTypes( types )
2187+
, QgsProcessingParameterLimitedDataTypes( types )
21882188
{
21892189

21902190
}
@@ -2234,12 +2234,12 @@ QString QgsProcessingParameterVectorLayer::valueAsPythonString( const QVariant &
22342234
return layer ? QgsProcessingUtils::normalizeLayerSource( layer->source() ).prepend( '\'' ).append( '\'' ) : QString();
22352235
}
22362236

2237-
QList<int> QgsProcessingParameterVectorLayer::dataTypes() const
2237+
QList<int> QgsProcessingParameterLimitedDataTypes::dataTypes() const
22382238
{
22392239
return mDataTypes;
22402240
}
22412241

2242-
void QgsProcessingParameterVectorLayer::setDataTypes( const QList<int> &types )
2242+
void QgsProcessingParameterLimitedDataTypes::setDataTypes( const QList<int> &types )
22432243
{
22442244
mDataTypes = types;
22452245
}
@@ -2483,7 +2483,7 @@ QgsProcessingParameterField *QgsProcessingParameterField::fromScriptCode( const
24832483

24842484
QgsProcessingParameterFeatureSource::QgsProcessingParameterFeatureSource( const QString &name, const QString &description, const QList<int> &types, const QVariant &defaultValue, bool optional )
24852485
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
2486-
, mDataTypes( types )
2486+
, QgsProcessingParameterLimitedDataTypes( types )
24872487
{
24882488

24892489
}
@@ -2603,14 +2603,10 @@ QString QgsProcessingParameterFeatureSource::asScriptCode() const
26032603
return code.trimmed();
26042604
}
26052605

2606-
QList< int > QgsProcessingParameterFeatureSource::dataTypes() const
2606+
QgsProcessingParameterLimitedDataTypes::QgsProcessingParameterLimitedDataTypes( const QList<int> &types )
2607+
: mDataTypes( types )
26072608
{
2608-
return mDataTypes;
2609-
}
26102609

2611-
void QgsProcessingParameterFeatureSource::setDataTypes( const QList<int> &types )
2612-
{
2613-
mDataTypes = types;
26142610
}
26152611

26162612
QVariantMap QgsProcessingParameterFeatureSource::toVariantMap() const

src/core/processing/qgsprocessingparameters.h

+37-35
Original file line numberDiff line numberDiff line change
@@ -1350,14 +1350,48 @@ class CORE_EXPORT QgsProcessingParameterExpression : public QgsProcessingParamet
13501350

13511351
};
13521352

1353+
1354+
/**
1355+
* \class QgsProcessingParameterLimitedDataTypes
1356+
* \ingroup core
1357+
* Can be inherited by parameters which require limits to their acceptable data types.
1358+
* \since QGIS 3.0
1359+
*/
1360+
class CORE_EXPORT QgsProcessingParameterLimitedDataTypes
1361+
{
1362+
public:
1363+
1364+
/**
1365+
* Constructor for QgsProcessingParameterLimitedDataTypes, with a list of acceptable data \a types.
1366+
*/
1367+
QgsProcessingParameterLimitedDataTypes( const QList< int > &types = QList< int >() );
1368+
1369+
/**
1370+
* Returns the geometry types for sources acceptable by the parameter.
1371+
* \see setDataTypes()
1372+
*/
1373+
QList< int > dataTypes() const;
1374+
1375+
/**
1376+
* Sets the geometry \a types for sources acceptable by the parameter.
1377+
* \see dataTypes()
1378+
*/
1379+
void setDataTypes( const QList< int > &types );
1380+
1381+
protected:
1382+
1383+
//! List of acceptable data types for the parameter
1384+
QList< int > mDataTypes;
1385+
};
1386+
13531387
/**
13541388
* \class QgsProcessingParameterVectorLayer
13551389
* \ingroup core
13561390
* A vector layer (with or without geometry) parameter for processing algorithms. Consider using
13571391
* the more versatile QgsProcessingParameterFeatureSource wherever possible.
13581392
* \since QGIS 3.0
13591393
*/
1360-
class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParameterDefinition
1394+
class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParameterDefinition, public QgsProcessingParameterLimitedDataTypes
13611395
{
13621396
public:
13631397

@@ -1379,18 +1413,6 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
13791413
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
13801414
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
13811415

1382-
/**
1383-
* Returns the geometry types for sources acceptable by the parameter.
1384-
* \see setDataTypes()
1385-
*/
1386-
QList< int > dataTypes() const;
1387-
1388-
/**
1389-
* Sets the geometry \a types for sources acceptable by the parameter.
1390-
* \see dataTypes()
1391-
*/
1392-
void setDataTypes( const QList< int > &types );
1393-
13941416
QVariantMap toVariantMap() const override;
13951417
bool fromVariantMap( const QVariantMap &map ) override;
13961418

@@ -1399,11 +1421,6 @@ class CORE_EXPORT QgsProcessingParameterVectorLayer : public QgsProcessingParame
13991421
*/
14001422
static QgsProcessingParameterVectorLayer *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;
14011423

1402-
private:
1403-
1404-
QList< int > mDataTypes = QList< int >() << QgsProcessing::TypeVectorAnyGeometry;
1405-
1406-
14071424
};
14081425

14091426
/**
@@ -1497,13 +1514,14 @@ class CORE_EXPORT QgsProcessingParameterField : public QgsProcessingParameterDef
14971514

14981515
};
14991516

1517+
15001518
/**
15011519
* \class QgsProcessingParameterFeatureSource
15021520
* \ingroup core
15031521
* An input feature source (such as vector layers) parameter for processing algorithms.
15041522
* \since QGIS 3.0
15051523
*/
1506-
class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingParameterDefinition
1524+
class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingParameterDefinition, public QgsProcessingParameterLimitedDataTypes
15071525
{
15081526
public:
15091527

@@ -1524,18 +1542,6 @@ class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingPara
15241542
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
15251543
QString asScriptCode() const override;
15261544

1527-
/**
1528-
* Returns the geometry types for sources acceptable by the parameter.
1529-
* \see setDataTypes()
1530-
*/
1531-
QList< int > dataTypes() const;
1532-
1533-
/**
1534-
* Sets the geometry \a types for sources acceptable by the parameter.
1535-
* \see dataTypes()
1536-
*/
1537-
void setDataTypes( const QList< int > &types );
1538-
15391545
QVariantMap toVariantMap() const override;
15401546
bool fromVariantMap( const QVariantMap &map ) override;
15411547

@@ -1544,10 +1550,6 @@ class CORE_EXPORT QgsProcessingParameterFeatureSource : public QgsProcessingPara
15441550
*/
15451551
static QgsProcessingParameterFeatureSource *fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition ) SIP_FACTORY;
15461552

1547-
private:
1548-
1549-
QList< int > mDataTypes = QList< int >() << QgsProcessing::TypeVectorAnyGeometry;
1550-
15511553
};
15521554

15531555
/**

0 commit comments

Comments
 (0)