Skip to content
Permalink
Browse files

Merge pull request #35076 from alexbruy/maplayer-datatype

Data type support for QgsProcessingParameterMapLayer
  • Loading branch information
alexbruy committed Mar 15, 2020
2 parents 1c5fa2d + 539a7ff commit 47070a7d7bb6296ff0ff4c4e5f04234a079e398c
@@ -1336,44 +1336,6 @@ Creates a new parameter using the definition from a script code.

};

class QgsProcessingParameterMapLayer : QgsProcessingParameterDefinition
{
%Docstring
A map layer parameter for processing algorithms.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingParameterMapLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false );
%Docstring
Constructor for QgsProcessingParameterMapLayer.
%End

static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End
virtual QgsProcessingParameterDefinition *clone() const /Factory/;

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

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;


static QgsProcessingParameterMapLayer *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.
%End

};

class QgsProcessingParameterExtent : QgsProcessingParameterDefinition
{
%Docstring
@@ -2453,6 +2415,54 @@ Creates a new parameter using the definition from a script code.
%End
};

class QgsProcessingParameterMapLayer : QgsProcessingParameterDefinition, QgsProcessingParameterLimitedDataTypes
{
%Docstring
A map layer parameter for processing algorithms.

.. versionadded:: 3.0
%End

%TypeHeaderCode
#include "qgsprocessingparameters.h"
%End
public:

QgsProcessingParameterMapLayer( const QString &name, const QString &description = QString(), const QVariant &defaultValue = QVariant(),
bool optional = false,
const QList< int > &types = QList< int >() );
%Docstring
Constructor for QgsProcessingParameterMapLayer.
%End

static QString typeName();
%Docstring
Returns the type name for the parameter class.
%End
virtual QgsProcessingParameterDefinition *clone() const /Factory/;

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

virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

virtual QString asScriptCode() const;

virtual QString asPythonString( QgsProcessing::PythonOutputType outputType = QgsProcessing::PythonQgsProcessingAlgorithmSubclass ) const;


virtual QVariantMap toVariantMap() const;

virtual bool fromVariantMap( const QVariantMap &map );


static QgsProcessingParameterMapLayer *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.
%End

};

class QgsProcessingParameterField : QgsProcessingParameterDefinition
{
%Docstring
@@ -78,7 +78,7 @@
PARAMETER_MULTIPLE = 'multilayer'
PARAMETER_BAND = 'band'
PARAMETER_LAYOUTITEM = 'layoutitem'
PARAMETER_MAP_LAYER = 'Map Layer'
PARAMETER_MAP_LAYER = 'layer'
PARAMETER_RANGE = 'range'
PARAMETER_ENUM = 'enum'
PARAMETER_MATRIX = 'matrix'
@@ -117,6 +117,10 @@ def getParameterFromString(s, context=''):
elif clazz == QgsProcessingParameterMapLayer:
if len(params) > 3:
params[3] = True if params[3].lower() == 'true' else False
try:
params[4] = [int(p) for p in params[4].split(';')]
except:
params[4] = [getattr(QgsProcessing, p.split(".")[1]) for p in params[4].split(';')]
elif clazz == QgsProcessingParameterBoolean:
if len(params) > 2:
params[2] = True if params[2].lower() == 'true' else False
@@ -91,7 +91,8 @@ def use_legacy_dialog(param=None, paramType=None):
parameters.PARAMETER_POINT,
parameters.PARAMETER_CRS,
parameters.PARAMETER_ENUM,
parameters.PARAMETER_MATRIX):
parameters.PARAMETER_MATRIX,
parameters.PARAMETER_MAP_LAYER):
return True
elif isinstance(param, (QgsProcessingParameterField,
QgsProcessingParameterBand,
@@ -106,6 +107,7 @@ def use_legacy_dialog(param=None, paramType=None):
QgsProcessingParameterCrs,
QgsProcessingParameterEnum,
QgsProcessingParameterMatrix,
QgsProcessingParameterMapLayer,
QgsProcessingDestinationParameter)):
return True

@@ -241,6 +243,20 @@ def setupUi(self):
if self.param is not None:
self.datatypeCombo.setCurrentIndex(self.datatypeCombo.findData(self.param.layerType()))
self.verticalLayout.addWidget(self.datatypeCombo)
elif (self.paramType == parameters.PARAMETER_MAP_LAYER or
isinstance(self.param, QgsProcessingParameterMapLayer)):
self.verticalLayout.addWidget(QLabel(self.tr('Data type')))
self.datatypeCombo = QComboBox()
self.datatypeCombo.addItem(self.tr('Any Map Layer'), QgsProcessing.TypeMapLayer)
self.datatypeCombo.addItem(self.tr('Vector (Point)'), QgsProcessing.TypeVectorPoint)
self.datatypeCombo.addItem(self.tr('Vector (Line)'), QgsProcessing.TypeVectorLine)
self.datatypeCombo.addItem(self.tr('Vector (Polygon)'), QgsProcessing.TypeVectorPolygon)
self.datatypeCombo.addItem(self.tr('Vector (Any Geometry Type)'), QgsProcessing.TypeVectorAnyGeometry)
self.datatypeCombo.addItem(self.tr('Raster'), QgsProcessing.TypeRaster)
self.datatypeCombo.addItem(self.tr('Mesh'), QgsProcessing.TypeMesh)
if self.param is not None:
self.datatypeCombo.setCurrentIndex(self.datatypeCombo.findData(self.param.dataTypes()[0]))
self.verticalLayout.addWidget(self.datatypeCombo)
elif (self.paramType in (parameters.PARAMETER_NUMBER, parameters.PARAMETER_DISTANCE, parameters.PARAMETER_SCALE)
or isinstance(self.param, (QgsProcessingParameterNumber, QgsProcessingParameterDistance, QgsProcessingParameterScale))):

@@ -453,7 +469,7 @@ def accept(self):
elif (self.paramType == parameters.PARAMETER_MAP_LAYER
or isinstance(self.param, QgsProcessingParameterMapLayer)):
self.param = QgsProcessingParameterMapLayer(
name, description)
name, description, types=[self.datatypeCombo.currentData()])
elif (self.paramType == parameters.PARAMETER_RASTER
or isinstance(self.param, QgsProcessingParameterRasterLayer)):
self.param = QgsProcessingParameterRasterLayer(
@@ -2246,8 +2246,9 @@ QgsProcessingParameterCrs *QgsProcessingParameterCrs::fromScriptCode( const QStr
return new QgsProcessingParameterCrs( name, description, definition.compare( QLatin1String( "none" ), Qt::CaseInsensitive ) == 0 ? QVariant() : definition, isOptional );
}

QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
QgsProcessingParameterMapLayer::QgsProcessingParameterMapLayer( const QString &name, const QString &description, const QVariant &defaultValue, bool optional, const QList<int> &types )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
, QgsProcessingParameterLimitedDataTypes( types )
{

}
@@ -2303,9 +2304,149 @@ QString QgsProcessingParameterMapLayer::valueAsPythonString( const QVariant &val
: QgsProcessingUtils::stringToPythonLiteral( val.toString() );
}

QString QgsProcessingParameterMapLayer::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags & FlagOptional )
code += QStringLiteral( "optional " );
code += QStringLiteral( "layer " );

for ( int type : mDataTypes )
{
switch ( type )
{
case QgsProcessing::TypeVectorAnyGeometry:
code += QStringLiteral( "hasgeometry " );
break;

case QgsProcessing::TypeVectorPoint:
code += QStringLiteral( "point " );
break;

case QgsProcessing::TypeVectorLine:
code += QStringLiteral( "line " );
break;

case QgsProcessing::TypeVectorPolygon:
code += QStringLiteral( "polygon " );
break;

case QgsProcessing::TypeRaster:
code += QStringLiteral( "raster " );
break;

case QgsProcessing::TypeMesh:
code += QStringLiteral( "mesh " );
break;
}
}

code += mDefault.toString();
return code.trimmed();
}

QgsProcessingParameterMapLayer *QgsProcessingParameterMapLayer::fromScriptCode( const QString &name, const QString &description, bool isOptional, const QString &definition )
{
return new QgsProcessingParameterMapLayer( name, description, definition, isOptional );
QList< int > types;
QString def = definition;
while ( true )
{
if ( def.startsWith( QLatin1String( "hasgeometry" ), Qt::CaseInsensitive ) )
{
types << QgsProcessing::TypeVectorAnyGeometry;
def = def.mid( 12 );
continue;
}
else if ( def.startsWith( QLatin1String( "point" ), Qt::CaseInsensitive ) )
{
types << QgsProcessing::TypeVectorPoint;
def = def.mid( 6 );
continue;
}
else if ( def.startsWith( QLatin1String( "line" ), Qt::CaseInsensitive ) )
{
types << QgsProcessing::TypeVectorLine;
def = def.mid( 5 );
continue;
}
else if ( def.startsWith( QLatin1String( "polygon" ), Qt::CaseInsensitive ) )
{
types << QgsProcessing::TypeVectorPolygon;
def = def.mid( 8 );
continue;
}
else if ( def.startsWith( QLatin1String( "raster" ), Qt::CaseInsensitive ) )
{
types << QgsProcessing::TypeRaster;
def = def.mid( 7 );
continue;
}
else if ( def.startsWith( QLatin1String( "mesh" ), Qt::CaseInsensitive ) )
{
types << QgsProcessing::TypeMesh;
def = def.mid( 5 );
continue;
}
break;
}

return new QgsProcessingParameterMapLayer( name, description, def, isOptional, types );
}

QString QgsProcessingParameterMapLayer::asPythonString( const QgsProcessing::PythonOutputType outputType ) const
{
switch ( outputType )
{
case QgsProcessing::PythonQgsProcessingAlgorithmSubclass:
{
QString code = QStringLiteral( "QgsProcessingParameterMapLayer('%1', '%2'" ).arg( name(), description() );
if ( mFlags & FlagOptional )
code += QStringLiteral( ", optional=True" );

QgsProcessingContext c;
code += QStringLiteral( ", defaultValue=%1" ).arg( valueAsPythonString( mDefault, c ) );

if ( !mDataTypes.empty() )
{
QStringList options;
options.reserve( mDataTypes.size() );
for ( int t : mDataTypes )
options << QStringLiteral( "QgsProcessing.%1" ).arg( QgsProcessing::sourceTypeToString( static_cast< QgsProcessing::SourceType >( t ) ) );
code += QStringLiteral( ", types=[%1])" ).arg( options.join( ',' ) );
}
else
{
code += QStringLiteral( ")" );
}

return code;
}
}
return QString();
}

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

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

QgsProcessingParameterExtent::QgsProcessingParameterExtent( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
@@ -3950,8 +4091,7 @@ QVariantMap QgsProcessingParameterVectorLayer::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
QVariantList types;
const auto constMDataTypes = mDataTypes;
for ( int type : constMDataTypes )
for ( int type : mDataTypes )
{
types << type;
}
@@ -3963,9 +4103,8 @@ bool QgsProcessingParameterVectorLayer::fromVariantMap( const QVariantMap &map )
{
QgsProcessingParameterDefinition::fromVariantMap( map );
mDataTypes.clear();
QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
const auto constValues = values;
for ( const QVariant &val : constValues )
const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
for ( const QVariant &val : values )
{
mDataTypes << val.toInt();
}
@@ -4459,8 +4598,7 @@ QString QgsProcessingParameterFeatureSource::asScriptCode() const
code += QStringLiteral( "optional " );
code += QStringLiteral( "source " );

const auto constMDataTypes = mDataTypes;
for ( int type : constMDataTypes )
for ( int type : mDataTypes )
{
switch ( type )
{
@@ -4520,8 +4658,7 @@ QVariantMap QgsProcessingParameterFeatureSource::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
QVariantList types;
const auto constMDataTypes = mDataTypes;
for ( int type : constMDataTypes )
for ( int type : mDataTypes )
{
types << type;
}
@@ -4533,9 +4670,8 @@ bool QgsProcessingParameterFeatureSource::fromVariantMap( const QVariantMap &map
{
QgsProcessingParameterDefinition::fromVariantMap( map );
mDataTypes.clear();
QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
const auto constValues = values;
for ( const QVariant &val : constValues )
const QVariantList values = map.value( QStringLiteral( "data_types" ) ).toList();
for ( const QVariant &val : values )
{
mDataTypes << val.toInt();
}

0 comments on commit 47070a7

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