Skip to content

Commit

Permalink
[processing] some fixes for supporting optional parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
volaya committed Nov 4, 2015
1 parent 0cc92ff commit 8187ad5
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 49 deletions.
60 changes: 27 additions & 33 deletions python/plugins/processing/core/parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Parameter:
take as input.
"""

def __init__(self, name='', description=''):
def __init__(self, name='', description='', optional=False):
self.name = name
self.description = description
self.value = None
Expand All @@ -68,6 +68,7 @@ def __init__(self, name='', description=''):
# It can be used as any other parameter, but it will not be
# shown to the user
self.hidden = False
self.optional = optional

def setValue(self, obj):
"""
Expand Down Expand Up @@ -109,8 +110,8 @@ def tr(self, string, context=''):

class ParameterBoolean(Parameter):

def __init__(self, name='', description='', default=True):
Parameter.__init__(self, name, description)
def __init__(self, name='', description='', default=True, optional=False):
Parameter.__init__(self, name, description, optional)
self.default = parseBool(default)
self.value = None

Expand All @@ -129,12 +130,12 @@ def setValue(self, value):

class ParameterCrs(Parameter):

def __init__(self, name='', description='', default='EPSG:4326'):
def __init__(self, name='', description='', default='EPSG:4326', optional=False):
'''The value is a string that uniquely identifies the
coordinate reference system. Typically it is the auth id of the CRS
(if the authority is EPSG) or proj4 string of the CRS (in case
of other authorities or user defined projections).'''
Parameter.__init__(self, name, description)
Parameter.__init__(self, name, description, optional)
self.value = None
self.default = default

Expand Down Expand Up @@ -168,8 +169,8 @@ class ParameterExtent(Parameter):

USE_MIN_COVERING_EXTENT = 'USE_MIN_COVERING_EXTENT'

def __init__(self, name='', description='', default='0,1,0,1'):
Parameter.__init__(self, name, description)
def __init__(self, name='', description='', default='0,1,0,1', optional=False):
Parameter.__init__(self, name, description, optional)
self.default = default
# The value is a string in the form "xmin, xmax, ymin, ymax"
self.value = None
Expand Down Expand Up @@ -200,11 +201,10 @@ def getValueAsCommandLineParameter(self):
class ParameterFile(Parameter):

def __init__(self, name='', description='', isFolder=False, optional=True, ext=None):
Parameter.__init__(self, name, description)
Parameter.__init__(self, name, description, parseBool(optional))
self.value = None
self.ext = ext
self.isFolder = parseBool(isFolder)
self.optional = parseBool(optional)

def getValueAsCommandLineParameter(self):
return '"' + unicode(self.value) + '"'
Expand All @@ -229,8 +229,8 @@ def typeName(self):
class ParameterFixedTable(Parameter):

def __init__(self, name='', description='', numRows=3,
cols=['value'], fixedNumOfRows=False):
Parameter.__init__(self, name, description)
cols=['value'], fixedNumOfRows=False, optional=False):
Parameter.__init__(self, name, description, optional)
self.cols = cols
if isinstance(cols, basestring):
self.cols = self.cols.split(";")
Expand Down Expand Up @@ -282,9 +282,8 @@ class ParameterMultipleInput(ParameterDataObject):
TYPE_FILE = 4

def __init__(self, name='', description='', datatype=-1, optional=False):
ParameterDataObject.__init__(self, name, description)
ParameterDataObject.__init__(self, name, description, optional)
self.datatype = int(float(datatype))
self.optional = parseBool(optional)
self.value = None
self.exported = None

Expand Down Expand Up @@ -411,8 +410,8 @@ def dataType(self):
class ParameterNumber(Parameter):

def __init__(self, name='', description='', minValue=None, maxValue=None,
default=0.0):
Parameter.__init__(self, name, description)
default=0.0, optional=False):
Parameter.__init__(self, name, description, optional)
try:
self.default = int(unicode(default))
self.isInteger = True
Expand All @@ -433,7 +432,7 @@ def setValue(self, n):
if n is None:
if not self.optional:
return False
self.value = None
self.value = self.default
return True
try:
if float(n) - int(float(n)) == 0:
Expand All @@ -454,8 +453,8 @@ def setValue(self, n):

class ParameterRange(Parameter):

def __init__(self, name='', description='', default='0,1'):
Parameter.__init__(self, name, description)
def __init__(self, name='', description='', default='0,1', optional=False):
Parameter.__init__(self, name, description, optional)
self.default = default
self.value = None

Expand Down Expand Up @@ -491,9 +490,8 @@ def getValueAsCommandLineParameter(self):
class ParameterRaster(ParameterDataObject):

def __init__(self, name='', description='', optional=False, showSublayersDialog=True):
ParameterDataObject.__init__(self, name, description)
ParameterDataObject.__init__(self, name, description, optional)
self.showSublayersDialog = parseBool(showSublayersDialog)
self.optional = parseBool(optional)
self.value = None
self.exported = None

Expand Down Expand Up @@ -548,8 +546,9 @@ def getFileFilter(self):

class ParameterSelection(Parameter):

def __init__(self, name='', description='', options=[], default=0, isSource=False):
Parameter.__init__(self, name, description)
def __init__(self, name='', description='', options=[], default=0, isSource=False,
optional=False):
Parameter.__init__(self, name, description, optional)
self.options = options
if isSource:
self.options = []
Expand Down Expand Up @@ -588,16 +587,15 @@ class ParameterString(Parameter):

def __init__(self, name='', description='', default='', multiline=False,
optional=False):
Parameter.__init__(self, name, description)
Parameter.__init__(self, name, description, optional)
self.default = default
self.value = None
self.multiline = parseBool(multiline)
self.optional = parseBool(optional)

def setValue(self, obj):
if obj is None:
if not self.optional:
return false
return False
self.value = ''
return True
self.value = unicode(obj).replace(
Expand All @@ -614,8 +612,7 @@ def getValueAsCommandLineParameter(self):
class ParameterTable(ParameterDataObject):

def __init__(self, name='', description='', optional=False):
ParameterDataObject.__init__(self, name, description)
self.optional = parseBool(optional)
ParameterDataObject.__init__(self, name, description, optional)
self.value = None
self.exported = None

Expand Down Expand Up @@ -685,11 +682,10 @@ class ParameterTableField(Parameter):

def __init__(self, name='', description='', parent=None, datatype=-1,
optional=False):
Parameter.__init__(self, name, description)
Parameter.__init__(self, name, description, optional)
self.parent = parent
self.value = None
self.datatype = int(datatype)
self.optional = parseBool(optional)

def getValueAsCommandLineParameter(self):
return '"' + unicode(self.value) + '"'
Expand Down Expand Up @@ -728,8 +724,7 @@ class ParameterVector(ParameterDataObject):

def __init__(self, name='', description='', shapetype=[-1],
optional=False):
ParameterDataObject.__init__(self, name, description)
self.optional = parseBool(optional)
ParameterDataObject.__init__(self, name, description, optional)
if isinstance(shapetype, int):
shapetype = [shapetype]
elif isinstance(shapetype, basestring):
Expand Down Expand Up @@ -818,12 +813,11 @@ class ParameterGeometryPredicate(Parameter):

def __init__(self, name='', description='', left=None, right=None,
optional=False, enabledPredicates=None):
Parameter.__init__(self, name, description)
Parameter.__init__(self, name, description, optional)
self.left = left
self.right = right
self.value = None
self.default = []
self.optional = parseBool(optional)
self.enabledPredicates = enabledPredicates
if self.enabledPredicates is None:
self.enabledPredicates = self.predicates
Expand Down
44 changes: 28 additions & 16 deletions python/plugins/processing/modeler/ModelerParametersDialog.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,10 +552,15 @@ def setParamFileValue(self, alg, param, widget):
def setParamNumberValue(self, alg, param, widget):
idx = widget.findText(widget.currentText())
if idx < 0:
s = widget.currentText()
try:
value = float(s)
except:
s = widget.currentText().strip()
if s:
try:
value = float(s)
except:
return False
elif param.optional:
value = None
else:
return False
else:
value = widget.itemData(widget.currentIndex())
Expand All @@ -565,14 +570,19 @@ def setParamNumberValue(self, alg, param, widget):
def setParamExtentValue(self, alg, param, widget):
idx = widget.findText(widget.currentText())
if idx < 0:
s = unicode(widget.currentText())
try:
tokens = s.split(',')
if len(tokens) != 4:
s = unicode(widget.currentText()).strip()
if s:
try:
tokens = s.split(',')
if len(tokens) != 4:
return False
for token in tokens:
float(token)
except:
return False
for token in tokens:
float(token)
except:
elif param.optional:
s = None
else:
return False
alg.params[param.name] = [s]
else:
Expand Down Expand Up @@ -607,13 +617,15 @@ def setParamValue(self, alg, param, widget):
return True
elif isinstance(param, ParameterCrs):
authid = widget.getValue()
if authid is None:
alg.params[param.name] = None
else:
alg.params[param.name] = authid
if authid is None and not param.optional:
return False
alg.params[param.name] = authid
return True
elif isinstance(param, ParameterFixedTable):
alg.params[param.name] = ParameterFixedTable.tableToString(widget.table)
table = widget.table
if not bool(table) and not param.optional:
return False
alg.params[param.name] = ParameterFixedTable.tableToString(table)
return True
elif isinstance(param, ParameterTableField):
return self.setParamTableFieldValue(alg, param, widget)
Expand Down

0 comments on commit 8187ad5

Please sign in to comment.