Skip to content

Commit

Permalink
[processing] Fix optional numeric parameters cannot be cleared
Browse files Browse the repository at this point in the history
Without this change optional numeric parameters have no way to
be cleared in the GUI - they are always forced to have a value

Fixes #17471 - but I've noticed that many optional numeric
GRASS parameters have a non-null default value. These may
need to be investigated and manually changed to None defaults
in the description files.
  • Loading branch information
nyalldawson committed Dec 1, 2017
1 parent db85502 commit 1c1de3a
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions python/plugins/processing/gui/NumberInputPanel.py
Expand Up @@ -36,6 +36,7 @@
from qgis.core import (QgsExpression,
QgsProcessingParameterNumber,
QgsProcessingOutputNumber,
QgsProcessingParameterDefinition,
QgsProcessingModelChildParameterSource)
from qgis.gui import QgsExpressionBuilderDialog
from processing.tools.dataobjects import createExpressionContext, createContext
Expand Down Expand Up @@ -154,20 +155,31 @@ def __init__(self, param):
else:
self.spnValue.setMinimum(-999999999)

self.allowing_null = False
# set default value
if param.flags() & QgsProcessingParameterDefinition.FlagOptional:
self.spnValue.setShowClearButton(True)
min = self.spnValue.minimum() - 1
self.spnValue.setMinimum(min)
self.spnValue.setValue(min)
self.spnValue.setSpecialValueText(self.tr('Not set'))
self.allowing_null = True

if param.defaultValue() is not None:
self.setValue(param.defaultValue())
try:
self.spnValue.setClearValue(float(param.defaultValue()))
except:
pass
if not self.allowing_null:
try:
self.spnValue.setClearValue(float(param.defaultValue()))
except:
pass
elif self.param.minimum() is not None:
try:
self.setValue(float(self.param.minimum()))
self.spnValue.setClearValue(float(self.param.minimum()))
if not self.allowing_null:
self.spnValue.setClearValue(float(self.param.minimum()))
except:
pass
else:
elif not self.allowing_null:
self.setValue(0)
self.spnValue.setClearValue(0)

Expand All @@ -179,7 +191,10 @@ def __init__(self, param):
self.spnValue.valueChanged.connect(lambda: self.hasChanged.emit())

def getValue(self):
return self.spnValue.value()
if self.allowing_null and self.spnValue.value() == self.spnValue.minimum():
return None
else:
return self.spnValue.value()

def setValue(self, value):
try:
Expand Down

0 comments on commit 1c1de3a

Please sign in to comment.