Add basic type checking to parameters

This commit is contained in:
Luke Campagnola 2017-05-01 17:09:15 -07:00
parent 20e821c45e
commit 7761b9a23c
2 changed files with 39 additions and 1 deletions

View File

@ -255,6 +255,7 @@ class Parameter(QtCore.QObject):
try:
if blockSignal is not None:
self.sigValueChanged.disconnect(blockSignal)
value = self._interpretValue(value)
if self.opts['value'] == value:
return value
self.opts['value'] = value
@ -265,6 +266,9 @@ class Parameter(QtCore.QObject):
return value
def _interpretValue(self, v):
return v
def value(self):
"""
Return the value of this Parameter.

View File

@ -4,10 +4,11 @@ from .Parameter import Parameter, registerParameterType
from .ParameterItem import ParameterItem
from ..widgets.SpinBox import SpinBox
from ..widgets.ColorButton import ColorButton
from ..colormap import ColorMap
#from ..widgets.GradientWidget import GradientWidget ## creates import loop
from .. import pixmaps as pixmaps
from .. import functions as fn
import os
import os, sys
from ..pgcollections import OrderedDict
class WidgetParameterItem(ParameterItem):
@ -320,6 +321,39 @@ class SimpleParameter(Parameter):
state['value'] = fn.colorTuple(self.value())
return state
def _interpretValue(self, v):
fn = {
'int': int,
'float': float,
'bool': bool,
'str': self._interpStr,
'color': self._interpColor,
'colormap': self._interpColormap,
}[self.opts['type']]
return fn(v)
def _interpStr(self, v):
if sys.version[0] == '2':
if isinstance(v, QtCore.QString):
v = unicode(v)
elif not isinstance(v, basestring):
raise TypeError("Cannot set str parmeter from object %r" % v)
else:
if isinstance(v, QtCore.QString):
v = str(v)
elif not isinstance(v, str):
raise TypeError("Cannot set str parmeter from object %r" % v)
return v
def _interpColor(self, v):
return fn.mkColor(v)
def _interpColormap(self, v):
if not isinstance(v, ColorMap):
raise TypeError("Cannot set colormap parameter from object %r" % v)
return v
registerParameterType('int', SimpleParameter, override=True)
registerParameterType('float', SimpleParameter, override=True)