Skip to content
Permalink
Browse files

Port parameter conversion to script code to c++

  • Loading branch information
nyalldawson committed Jun 26, 2017
1 parent 23ddace commit 178a415c9fa4e724d5887e3e5bf53882ec3050c6
@@ -299,6 +299,13 @@ class QgsProcessingParameterDefinition
:rtype: str
%End

virtual QString asScriptCode() const;
%Docstring
Returns the parameter definition encoded in a string which can be used within a
Python processing script.
:rtype: str
%End

virtual QVariantMap toVariantMap() const;
%Docstring
Saves this parameter to a QVariantMap. Subclasses should ensure that they call the base class
@@ -571,6 +578,8 @@ class QgsProcessingParameterBoolean : QgsProcessingParameterDefinition
virtual QString type() const;
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

virtual QString asScriptCode() const;

};

class QgsProcessingParameterCrs : QgsProcessingParameterDefinition
@@ -701,6 +710,8 @@ class QgsProcessingParameterFile : QgsProcessingParameterDefinition
virtual QString type() const;
virtual bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = 0 ) const;

virtual QString asScriptCode() const;


Behavior behavior() const;
%Docstring
@@ -837,6 +848,8 @@ class QgsProcessingParameterMultipleLayers : QgsProcessingParameterDefinition

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

virtual QString asScriptCode() const;


QgsProcessingParameterDefinition::LayerType layerType() const;
%Docstring
@@ -102,13 +102,6 @@ class ParameterBoolean(Parameter):
def __init__(self, name='', description='', default=None, optional=False, metadata={}):
Parameter.__init__(self, name, description, parseBool(default), optional, metadata)

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'boolean '
return '##' + self.name() + '=' + param_type + str(self.defaultValue())

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -137,13 +130,6 @@ def __init__(self, name='', description='', default=None, optional=False, metada
if self.value == 'ProjectCrs':
self.value = QgsProject.instance().crs().authid()

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'crs '
return '##' + self.name() + '=' + param_type + str(self.defaultValue())

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -167,13 +153,6 @@ def __init__(self, name='', description='', default=None, optional=True):
# The value is a string in the form "xmin, xmax, ymin, ymax"
self.skip_crs_check = False

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'extent'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -189,13 +168,6 @@ def __init__(self, name='', description='', default=None, optional=False):
Parameter.__init__(self, name, description, default, optional)
# The value is a string in the form "x, y"

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'point'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -212,16 +184,6 @@ def __init__(self, name='', description='', isFolder=False, optional=True, ext=N
self.ext = ext
self.isFolder = parseBool(isFolder)

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
if self.isFolder:
param_type += 'folder'
else:
param_type += 'file'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -250,13 +212,6 @@ def tableToString(table):
tablestring = tablestring[:-1]
return tablestring

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'fixedtable'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -405,18 +360,6 @@ def dataType(self):
else:
return 'any vectors'

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
if self.datatype == dataobjects.TYPE_RASTER:
param_type += 'multiple raster'
if self.datatype == dataobjects.TYPE_FILE:
param_type += 'multiple file'
else:
param_type += 'multiple vector'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -455,16 +398,6 @@ def __init__(self, name='', description='', minValue=None, maxValue=None,
self.max = None
self.value = self.default

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'number'
code = '##' + self.name() + '=' + param_type
if self.default:
code += str(self.default)
return code

@classmethod
def fromScriptCode(self, line):

@@ -539,13 +472,6 @@ def __init__(self, name='', description='', optional=False, showSublayersDialog=
Parameter.__init__(self, name, description, None, optional)
self.showSublayersDialog = parseBool(showSublayersDialog)

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'raster'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -621,13 +547,6 @@ def __init__(self, name='', description='', default=None, multiline=False,
Parameter.__init__(self, name, description, default, optional, metadata)
self.multiline = parseBool(multiline)

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'string '
return '##' + self.name() + '=' + param_type + repr(self.defaultValue())

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -656,13 +575,6 @@ def __init__(self, name='', description='', default=None, optional=False, parent
Parameter.__init__(self, name, description, default, optional)
self.parent_layer = parent_layer

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'expression '
return '##' + self.name() + '=' + param_type + str(self.defaultValue())

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -711,13 +623,6 @@ def getSafeExportedTable(self):
self.exported = self.value
return self.exported

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'table'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -758,13 +663,6 @@ def dataType(self):
else:
return 'any'

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'field'
return '##' + self.name() + '=' + param_type + str(self.parent)

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -835,13 +733,6 @@ def getSafeExportedLayer(self):
def dataType(self):
return dataobjects.vectorDataType(self)

def getAsScriptCode(self):
param_type = ''
if self.flags() & QgsProcessingParameterDefinition.FlagOptional:
param_type += 'optional '
param_type += 'vector'
return '##' + self.name() + '=' + param_type

@classmethod
def fromScriptCode(self, line):
isOptional, name, definition = _splitParameterOptions(line)
@@ -227,7 +227,7 @@ def resolveValue(self, value, param):
def toPython(self):
s = ['##%s=name' % self.name()]
for param in list(self.parameterComponents().values()):
s.append(param.param.getAsScriptCode())
s.append(param.param.asScriptCode())
for alg in list(self.algs.values()):
for name, out in list(alg.modelOutputs().items()):
s.append('##%s=%s' % (safeName(out.description()).lower(), alg.getOutputType(name)))
@@ -800,6 +800,16 @@ QString QgsProcessingParameterDefinition::valueAsPythonString( const QVariant &v
return value.toString().prepend( '\'' ).append( '\'' );
}

QString QgsProcessingParameterDefinition::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags && FlagOptional )
code += QStringLiteral( "optional " );
code += type() + ' ';
code += mDefault.toString();
return code.trimmed();
}

QVariantMap QgsProcessingParameterDefinition::toVariantMap() const
{
QVariantMap map;
@@ -833,6 +843,16 @@ QString QgsProcessingParameterBoolean::valueAsPythonString( const QVariant &val,
return val.toBool() ? QStringLiteral( "True" ) : QStringLiteral( "False" );
}

QString QgsProcessingParameterBoolean::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags && FlagOptional )
code += QStringLiteral( "optional " );
code += type() + ' ';
code += mDefault.toBool() ? QStringLiteral( "true" ) : QStringLiteral( "false" );
return code.trimmed();
}

QgsProcessingParameterCrs::QgsProcessingParameterCrs( const QString &name, const QString &description, const QVariant &defaultValue, bool optional )
: QgsProcessingParameterDefinition( name, description, defaultValue, optional )
{
@@ -1051,6 +1071,16 @@ bool QgsProcessingParameterFile::checkValueIsAcceptable( const QVariant &input,
return true;
}

QString QgsProcessingParameterFile::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags && FlagOptional )
code += QStringLiteral( "optional " );
code += ( mBehavior == File ? QStringLiteral( "file" ) : QStringLiteral( "folder" ) ) + ' ';
code += mDefault.toString();
return code.trimmed();
}

QVariantMap QgsProcessingParameterFile::toVariantMap() const
{
QVariantMap map = QgsProcessingParameterDefinition::toVariantMap();
@@ -1272,6 +1302,46 @@ QString QgsProcessingParameterMultipleLayers::valueAsPythonString( const QVarian
return QgsProcessingParameterDefinition::valueAsPythonString( value, context );
}

QString QgsProcessingParameterMultipleLayers::asScriptCode() const
{
QString code = QStringLiteral( "##%1=" ).arg( mName );
if ( mFlags && FlagOptional )
code += QStringLiteral( "optional " );
switch ( mLayerType )
{
case TypeRaster:
code += QStringLiteral( "multiple raster" );
break;

case TypeFile:
code += QStringLiteral( "multiple file" );
break;

default:
code += QStringLiteral( "multiple vector" );
break;
}
code += ' ';
if ( mDefault.type() == QVariant::List )
{
QStringList parts;
Q_FOREACH ( const QVariant &var, mDefault.toList() )
{
parts << var.toString();
}
code += parts.join( ',' );
}
else if ( mDefault.type() == QVariant::StringList )
{
code += mDefault.toStringList().join( ',' );
}
else
{
code += mDefault.toString();
}
return code.trimmed();
}

QgsProcessingParameterDefinition::LayerType QgsProcessingParameterMultipleLayers::layerType() const
{
return mLayerType;
@@ -331,6 +331,12 @@ class CORE_EXPORT QgsProcessingParameterDefinition
*/
virtual QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const;

/**
* Returns the parameter definition encoded in a string which can be used within a
* Python processing script.
*/
virtual QString asScriptCode() const;

/**
* Saves this parameter to a QVariantMap. Subclasses should ensure that they call the base class
* method and then extend the result with additional properties.
@@ -589,6 +595,7 @@ class CORE_EXPORT QgsProcessingParameterBoolean : public QgsProcessingParameterD

QString type() const override { return QStringLiteral( "boolean" ); }
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asScriptCode() const override;
};

/**
@@ -702,6 +709,7 @@ class CORE_EXPORT QgsProcessingParameterFile : public QgsProcessingParameterDefi

QString type() const override { return QStringLiteral( "file" ); }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString asScriptCode() const override;

/**
* Returns the parameter behavior (e.g. File or Folder).
@@ -831,6 +839,7 @@ class CORE_EXPORT QgsProcessingParameterMultipleLayers : public QgsProcessingPar
QString type() const override { return QStringLiteral( "multilayer" ); }
bool checkValueIsAcceptable( const QVariant &input, QgsProcessingContext *context = nullptr ) const override;
QString valueAsPythonString( const QVariant &value, QgsProcessingContext &context ) const override;
QString asScriptCode() const override;

/**
* Returns the layer type for layers acceptable by the parameter.

0 comments on commit 178a415

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