Skip to content
Permalink
Browse files

[processing] Fix optional numeric parameters cannot be cleared

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 1c1de3a88bed861da53b859bf80062fd2aab3659
Showing with 22 additions and 7 deletions.
  1. +22 −7 python/plugins/processing/gui/NumberInputPanel.py
@@ -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
@@ -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)

@@ -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:

0 comments on commit 1c1de3a

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