Skip to content
Permalink
Browse files

[processing] add option to add validators to settings

  • Loading branch information
volaya committed Nov 6, 2015
1 parent b3b3379 commit edb3cf350ef6ee357172a10d4910d0a7b8050497
Showing with 49 additions and 13 deletions.
  1. +43 −3 python/plugins/processing/core/ProcessingConfig.py
  2. +6 −10 python/plugins/processing/gui/ConfigDialog.py
@@ -192,7 +192,7 @@ def getSetting(name):
@staticmethod
def setSettingValue(name, value):
if name in ProcessingConfig.settings.keys():
ProcessingConfig.settings[name].value = value
ProcessingConfig.settings[name].setValue(value)
ProcessingConfig.settings[name].save()

@staticmethod
@@ -210,17 +210,52 @@ class Setting:
FILE = 1
FOLDER = 2
SELECTION = 3
FLOAT = 4
INT = 5

def __init__(self, group, name, description, default, hidden=False, valuetype=None, options=None):
def __init__(self, group, name, description, default, hidden=False, valuetype=None,
validator=None, options=None):
self.group = group
self.name = name
self.qname = "Processing/Configuration/" + self.name
self.description = description
self.default = default
self.value = default
self.hidden = hidden
if valuetype is None:
if isinstance(default, (int, long)):
valuetype = self.INT
elif isinstance(default, float):
valuetype = self.FLOAT
self.valuetype = valuetype
self.options = options
if validator is None:
if valuetype == self.FLOAT:
def checkFloat(v):
try:
float(v)
except ValueError:
raise ValueError(self.tr('Wrong parameter value:\n%s') % unicode(v))
validator = checkFloat
elif valuetype == self.INT:
def checkInt(v):
try:
int(v)
except ValueError:
raise ValueError(self.tr('Wrong parameter value:\n%s') % unicode(v))
validator = checkInt
elif valuetype in [self.FILE, self.FOLDER]:
def checkFileOrFolder(v):
if v and not os.path.exists(v):
raise ValueError(self.tr('Specified path does not exist:\n%s') % unicode(v))
validator = checkFileOrFolder
else:
validator = lambda x: True
self.validator = validator
self.value = default

def setValue(self, value):
self.validator(value)
self._value = value

def read(self):
qsettings = QSettings()
@@ -235,3 +270,8 @@ def save(self):

def __str__(self):
return self.name + '=' + unicode(self.value)

def tr(self, string, context=''):
if context == '':
context = 'ProcessingConfig'
return QCoreApplication.translate(context, string)
@@ -140,18 +140,14 @@ def fillTree(self):
def accept(self):
for setting in self.items.keys():
if isinstance(setting.value, bool):
setting.value = self.items[setting].checkState() == Qt.Checked
elif isinstance(setting.value, (float, int, long)):
value = unicode(self.items[setting].text())
setting.setValue(self.items[setting].checkState() == Qt.Checked)
else:
try:
value = float(value)
setting.value = value
except ValueError:
QMessageBox.critical(self, self.tr('Wrong value'),
self.tr('Wrong parameter value:\n%1') % value)
setting.setValue(unicode(self.items[setting].text()))
except ValueError, e:
QMessageBox.warning(self, self.tr('Wrong value'),
self.tr('Wrong value for parameter "%s":\n\n%s' %(setting.description, unicode(e))))
return
else:
setting.value = unicode(self.items[setting].text())
setting.save()
Processing.updateAlgsList()

0 comments on commit edb3cf3

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