diff --git a/examples/SpinBox.py b/examples/SpinBox.py index 2faf10ee..ef1d0fc5 100644 --- a/examples/SpinBox.py +++ b/examples/SpinBox.py @@ -26,7 +26,7 @@ spins = [ ("Float with SI-prefixed units
(n, u, m, k, M, etc)", pg.SpinBox(value=0.9, suffix='V', siPrefix=True)), ("Float with SI-prefixed units,
dec step=0.1, minStep=0.1", - pg.SpinBox(value=1.0, suffix='V', siPrefix=True, dec=True, step=0.1, minStep=0.1)), + pg.SpinBox(value=1.0, suffix='PSI', siPrefix=True, dec=True, step=0.1, minStep=0.1)), ("Float with SI-prefixed units,
dec step=0.5, minStep=0.01", pg.SpinBox(value=1.0, suffix='V', siPrefix=True, dec=True, step=0.5, minStep=0.01)), ("Float with SI-prefixed units,
dec step=1.0, minStep=0.001", diff --git a/pyqtgraph/functions.py b/pyqtgraph/functions.py index 7ad603f7..0495a00c 100644 --- a/pyqtgraph/functions.py +++ b/pyqtgraph/functions.py @@ -110,7 +110,7 @@ def siFormat(x, precision=3, suffix='', space=True, error=None, minVal=1e-25, al return fmt % (x*p, pref, suffix, plusminus, siFormat(error, precision=precision, suffix=suffix, space=space, minVal=minVal)) -def siParse(s, regex=FLOAT_REGEX): +def siParse(s, regex=FLOAT_REGEX, suffix=None): """Convert a value written in SI notation to a tuple (number, si_prefix, suffix). Example:: @@ -118,6 +118,12 @@ def siParse(s, regex=FLOAT_REGEX): siParse('100 μV") # returns ('100', 'μ', 'V') """ s = asUnicode(s) + s = s.strip() + if suffix is not None and len(suffix) > 0: + if s[-len(suffix):] != suffix: + raise ValueError("String '%s' does not have the expected suffix '%s'" % (s, suffix)) + s = s[:-len(suffix)] + 'X' # add a fake suffix so the regex still picks up the si prefix + m = regex.match(s) if m is None: raise ValueError('Cannot parse number "%s"' % s) @@ -126,15 +132,18 @@ def siParse(s, regex=FLOAT_REGEX): except IndexError: sip = '' - try: - suf = m.group('suffix') - except IndexError: - suf = '' + if suffix is None: + try: + suf = m.group('suffix') + except IndexError: + suf = '' + else: + suf = suffix return m.group('number'), '' if sip is None else sip, '' if suf is None else suf -def siEval(s, typ=float, regex=FLOAT_REGEX): +def siEval(s, typ=float, regex=FLOAT_REGEX, suffix=None): """ Convert a value written in SI notation to its equivalent prefixless value. @@ -142,9 +151,9 @@ def siEval(s, typ=float, regex=FLOAT_REGEX): siEval("100 μV") # returns 0.0001 """ - val, siprefix, suffix = siParse(s, regex) + val, siprefix, suffix = siParse(s, regex, suffix=suffix) v = typ(val) - return siApply(val, siprefix) + return siApply(v, siprefix) def siApply(val, siprefix): diff --git a/pyqtgraph/widgets/SpinBox.py b/pyqtgraph/widgets/SpinBox.py index b8066cd7..17caea32 100644 --- a/pyqtgraph/widgets/SpinBox.py +++ b/pyqtgraph/widgets/SpinBox.py @@ -518,7 +518,7 @@ class SpinBox(QtGui.QAbstractSpinBox): # tokenize into numerical value, si prefix, and suffix try: - val, siprefix, suffix = fn.siParse(strn, self.opts['regex']) + val, siprefix, suffix = fn.siParse(strn, self.opts['regex'], suffix=self.opts['suffix']) except Exception: return False diff --git a/pyqtgraph/widgets/tests/test_spinbox.py b/pyqtgraph/widgets/tests/test_spinbox.py index 10087881..cff97da7 100644 --- a/pyqtgraph/widgets/tests/test_spinbox.py +++ b/pyqtgraph/widgets/tests/test_spinbox.py @@ -18,6 +18,8 @@ def test_spinbox_formatting(): (12345678955, '12345678955', dict(int=True, decimals=100)), (1.45e-9, '1.45e-09 A', dict(int=False, decimals=6, suffix='A', siPrefix=False)), (1.45e-9, '1.45 nA', dict(int=False, decimals=6, suffix='A', siPrefix=True)), + (1.45, '1.45 PSI', dict(int=False, decimals=6, suffix='PSI', siPrefix=True)), + (1.45e-3, '1.45 mPSI', dict(int=False, decimals=6, suffix='PSI', siPrefix=True)), (-2500.3427, '$-2500.34', dict(int=False, format='${value:0.02f}')), ] @@ -26,3 +28,14 @@ def test_spinbox_formatting(): sb.setValue(value) assert sb.value() == value assert pg.asUnicode(sb.text()) == text + + # test setting value + if not opts.get('int', False): + suf = sb.opts['suffix'] + sb.lineEdit().setText('0.1' + suf) + sb.editingFinishedEvent() + assert sb.value() == 0.1 + if suf != '': + sb.lineEdit().setText('0.1 m' + suf) + sb.editingFinishedEvent() + assert sb.value() == 0.1e-3