##// END OF EJS Templates
ensure range widgets get value that is on a step
MinRK -
Show More
@@ -40,7 +40,7 b' def _matches(o, pattern):'
40 return all(isinstance(obj,kind) for obj,kind in comps)
40 return all(isinstance(obj,kind) for obj,kind in comps)
41
41
42
42
43 def _get_min_max_value(min, max, value):
43 def _get_min_max_value(min, max, value=None, step=None):
44 """Return min, max, value given input values with possible None."""
44 """Return min, max, value given input values with possible None."""
45 if value is None:
45 if value is None:
46 if not max > min:
46 if not max > min:
@@ -60,6 +60,10 b' def _get_min_max_value(min, max, value):'
60 raise TypeError('expected a number, got: %r' % value)
60 raise TypeError('expected a number, got: %r' % value)
61 else:
61 else:
62 raise ValueError('unable to infer range, value from: ({0}, {1}, {2})'.format(min, max, value))
62 raise ValueError('unable to infer range, value from: ({0}, {1}, {2})'.format(min, max, value))
63 if step:
64 # ensure value is on a step
65 r = (value - min) % step
66 value = value - r
63 return min, max, value
67 return min, max, value
64
68
65 def _widget_abbrev_single_value(o):
69 def _widget_abbrev_single_value(o):
@@ -85,19 +89,19 b' def _widget_abbrev(o):'
85 """Make widgets from abbreviations: single values, lists or tuples."""
89 """Make widgets from abbreviations: single values, lists or tuples."""
86 if isinstance(o, (list, tuple)):
90 if isinstance(o, (list, tuple)):
87 if _matches(o, (int, int)):
91 if _matches(o, (int, int)):
88 min, max, value = _get_min_max_value(o[0], o[1], None)
92 min, max, value = _get_min_max_value(o[0], o[1])
89 return IntSliderWidget(value=value, min=min, max=max)
93 return IntSliderWidget(value=value, min=min, max=max)
90 elif _matches(o, (int, int, int)):
94 elif _matches(o, (int, int, int)):
91 min, max, value = _get_min_max_value(o[0], o[1], None)
95 min, max, value = _get_min_max_value(o[0], o[1], step=o[2])
92 return IntSliderWidget(value=value, min=min, max=max, step=o[2])
96 return IntSliderWidget(value=value, min=min, max=max, step=o[2])
93 elif _matches(o, (float, float)):
97 elif _matches(o, (float, float)):
94 min, max, value = _get_min_max_value(o[0], o[1], None)
98 min, max, value = _get_min_max_value(o[0], o[1])
95 return FloatSliderWidget(value=value, min=min, max=max)
99 return FloatSliderWidget(value=value, min=min, max=max)
96 elif _matches(o, (float, float, float)):
100 elif _matches(o, (float, float, float)):
97 min, max, value = _get_min_max_value(o[0], o[1], None)
101 min, max, value = _get_min_max_value(o[0], o[1], step=o[2])
98 return FloatSliderWidget(value=value, min=min, max=max, step=o[2])
102 return FloatSliderWidget(value=value, min=min, max=max, step=o[2])
99 elif _matches(o, (float, float, int)):
103 elif _matches(o, (float, float, int)):
100 min, max, value = _get_min_max_value(o[0], o[1], None)
104 min, max, value = _get_min_max_value(o[0], o[1], step=o[2])
101 return FloatSliderWidget(value=value, min=min, max=max, step=float(o[2]))
105 return FloatSliderWidget(value=value, min=min, max=max, step=float(o[2]))
102 elif all(isinstance(x, string_types) for x in o):
106 elif all(isinstance(x, string_types) for x in o):
103 return DropdownWidget(value=unicode_type(o[0]),
107 return DropdownWidget(value=unicode_type(o[0]),
General Comments 0
You need to be logged in to leave comments. Login now