##// END OF EJS Templates
Fix validate logic if min/max are changed
Gordon Ball -
Show More
@@ -1,91 +1,93 b''
1 """FloatWidget class.
1 """FloatWidget class.
2
2
3 Represents an unbounded float using a widget.
3 Represents an unbounded float using a widget.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from .widget import DOMWidget
16 from .widget import DOMWidget
17 from IPython.utils.traitlets import Unicode, CFloat, Bool, Enum, Tuple
17 from IPython.utils.traitlets import Unicode, CFloat, Bool, Enum, Tuple
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Classes
20 # Classes
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 class _FloatWidget(DOMWidget):
22 class _FloatWidget(DOMWidget):
23 value = CFloat(0.0, help="Float value", sync=True)
23 value = CFloat(0.0, help="Float value", sync=True)
24 disabled = Bool(False, help="Enable or disable user changes", sync=True)
24 disabled = Bool(False, help="Enable or disable user changes", sync=True)
25 description = Unicode(help="Description of the value this widget represents", sync=True)
25 description = Unicode(help="Description of the value this widget represents", sync=True)
26
26
27
27
28 class _BoundedFloatWidget(_FloatWidget):
28 class _BoundedFloatWidget(_FloatWidget):
29 max = CFloat(100.0, help="Max value", sync=True)
29 max = CFloat(100.0, help="Max value", sync=True)
30 min = CFloat(0.0, help="Min value", sync=True)
30 min = CFloat(0.0, help="Min value", sync=True)
31 step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True)
31 step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True)
32
32
33 def __init__(self, *pargs, **kwargs):
33 def __init__(self, *pargs, **kwargs):
34 """Constructor"""
34 """Constructor"""
35 DOMWidget.__init__(self, *pargs, **kwargs)
35 DOMWidget.__init__(self, *pargs, **kwargs)
36 self._validate('value', None, self.value)
36 self._validate('value', None, self.value)
37 self.on_trait_change(self._validate, ['value', 'min', 'max'])
37 self.on_trait_change(self._validate, ['value', 'min', 'max'])
38
38
39 def _validate(self, name, old, new):
39 def _validate(self, name, old, new):
40 """Validate value, max, min."""
40 """Validate value, max, min."""
41 if self.min > new or new > self.max:
41 if self.min > new or new > self.max:
42 self.value = min(max(new, self.min), self.max)
42 self.value = min(max(new, self.min), self.max)
43
43
44
44
45 class FloatTextWidget(_FloatWidget):
45 class FloatTextWidget(_FloatWidget):
46 _view_name = Unicode('FloatTextView', sync=True)
46 _view_name = Unicode('FloatTextView', sync=True)
47
47
48
48
49 class BoundedFloatTextWidget(_BoundedFloatWidget):
49 class BoundedFloatTextWidget(_BoundedFloatWidget):
50 _view_name = Unicode('FloatTextView', sync=True)
50 _view_name = Unicode('FloatTextView', sync=True)
51
51
52
52
53 class FloatSliderWidget(_BoundedFloatWidget):
53 class FloatSliderWidget(_BoundedFloatWidget):
54 _view_name = Unicode('FloatSliderView', sync=True)
54 _view_name = Unicode('FloatSliderView', sync=True)
55 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
55 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
56 help="Vertical or horizontal.", sync=True)
56 help="Vertical or horizontal.", sync=True)
57 range = Bool(False, help="Display a range selector", sync=True)
57 range = Bool(False, help="Display a range selector", sync=True)
58 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
58 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
59
59
60
60
61 class FloatProgressWidget(_BoundedFloatWidget):
61 class FloatProgressWidget(_BoundedFloatWidget):
62 _view_name = Unicode('ProgressView', sync=True)
62 _view_name = Unicode('ProgressView', sync=True)
63
63
64 class _FloatRangeWidget(_FloatWidget):
64 class _FloatRangeWidget(_FloatWidget):
65 value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Low and high float values", sync=True)
65 value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Low and high float values", sync=True)
66
66
67 class _BoundedFloatRangeWidget(_FloatRangeWidget):
67 class _BoundedFloatRangeWidget(_FloatRangeWidget):
68 step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True)
68 step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True)
69 max = CFloat(100.0, help="Max value", sync=True)
69 max = CFloat(100.0, help="Max value", sync=True)
70 min = CFloat(0.0, help="Min value", sync=True)
70 min = CFloat(0.0, help="Min value", sync=True)
71
71
72 def __init__(self, *pargs, **kwargs):
72 def __init__(self, *pargs, **kwargs):
73 """Constructor"""
73 """Constructor"""
74 DOMWidget.__init__(self, *pargs, **kwargs)
74 DOMWidget.__init__(self, *pargs, **kwargs)
75 self.on_trait_change(self._validate, ['value', 'min', 'max'])
75 self.on_trait_change(self._validate, ['value', 'min', 'max'])
76
76
77 def _validate(self, name, old, new):
77 def _validate(self, name, old, new):
78 """Validate min <= low <= high <= max"""
78 """Validate min <= low <= high <= max"""
79 if name == "value":
79 if name == "value":
80 low, high = new
80 low, high = new
81 low = max(low, self.min)
81 else:
82 high = min(high, self.max)
82 low, high = self.value
83 self.value = (min(low, high), max(low, high))
83 low = max(low, self.min)
84 high = min(high, self.max)
85 self.value = (min(low, high), max(low, high))
84
86
85
87
86 class FloatRangeSliderWidget(_BoundedFloatRangeWidget):
88 class FloatRangeSliderWidget(_BoundedFloatRangeWidget):
87 _view_name = Unicode('FloatSliderView', sync=True)
89 _view_name = Unicode('FloatSliderView', sync=True)
88 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
90 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
89 help="Vertical or horizontal.", sync=True)
91 help="Vertical or horizontal.", sync=True)
90 range = Bool(True, help="Display a range selector", sync=True)
92 range = Bool(True, help="Display a range selector", sync=True)
91 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
93 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
@@ -1,90 +1,92 b''
1 """IntWidget class.
1 """IntWidget class.
2
2
3 Represents an unbounded int using a widget.
3 Represents an unbounded int using a widget.
4 """
4 """
5 #-----------------------------------------------------------------------------
5 #-----------------------------------------------------------------------------
6 # Copyright (c) 2013, the IPython Development Team.
6 # Copyright (c) 2013, the IPython Development Team.
7 #
7 #
8 # Distributed under the terms of the Modified BSD License.
8 # Distributed under the terms of the Modified BSD License.
9 #
9 #
10 # The full license is in the file COPYING.txt, distributed with this software.
10 # The full license is in the file COPYING.txt, distributed with this software.
11 #-----------------------------------------------------------------------------
11 #-----------------------------------------------------------------------------
12
12
13 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
14 # Imports
14 # Imports
15 #-----------------------------------------------------------------------------
15 #-----------------------------------------------------------------------------
16 from .widget import DOMWidget
16 from .widget import DOMWidget
17 from IPython.utils.traitlets import Unicode, CInt, Bool, Enum, Tuple
17 from IPython.utils.traitlets import Unicode, CInt, Bool, Enum, Tuple
18
18
19 #-----------------------------------------------------------------------------
19 #-----------------------------------------------------------------------------
20 # Classes
20 # Classes
21 #-----------------------------------------------------------------------------
21 #-----------------------------------------------------------------------------
22 class _IntWidget(DOMWidget):
22 class _IntWidget(DOMWidget):
23 value = CInt(0, help="Int value", sync=True)
23 value = CInt(0, help="Int value", sync=True)
24 disabled = Bool(False, help="Enable or disable user changes", sync=True)
24 disabled = Bool(False, help="Enable or disable user changes", sync=True)
25 description = Unicode(help="Description of the value this widget represents", sync=True)
25 description = Unicode(help="Description of the value this widget represents", sync=True)
26
26
27
27
28 class _BoundedIntWidget(_IntWidget):
28 class _BoundedIntWidget(_IntWidget):
29 step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True)
29 step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True)
30 max = CInt(100, help="Max value", sync=True)
30 max = CInt(100, help="Max value", sync=True)
31 min = CInt(0, help="Min value", sync=True)
31 min = CInt(0, help="Min value", sync=True)
32
32
33 def __init__(self, *pargs, **kwargs):
33 def __init__(self, *pargs, **kwargs):
34 """Constructor"""
34 """Constructor"""
35 DOMWidget.__init__(self, *pargs, **kwargs)
35 DOMWidget.__init__(self, *pargs, **kwargs)
36 self.on_trait_change(self._validate, ['value', 'min', 'max'])
36 self.on_trait_change(self._validate, ['value', 'min', 'max'])
37
37
38 def _validate(self, name, old, new):
38 def _validate(self, name, old, new):
39 """Validate value, max, min."""
39 """Validate value, max, min."""
40 if self.min > new or new > self.max:
40 if self.min > new or new > self.max:
41 self.value = min(max(new, self.min), self.max)
41 self.value = min(max(new, self.min), self.max)
42
42
43
43
44 class IntTextWidget(_IntWidget):
44 class IntTextWidget(_IntWidget):
45 _view_name = Unicode('IntTextView', sync=True)
45 _view_name = Unicode('IntTextView', sync=True)
46
46
47
47
48 class BoundedIntTextWidget(_BoundedIntWidget):
48 class BoundedIntTextWidget(_BoundedIntWidget):
49 _view_name = Unicode('IntTextView', sync=True)
49 _view_name = Unicode('IntTextView', sync=True)
50
50
51
51
52 class IntSliderWidget(_BoundedIntWidget):
52 class IntSliderWidget(_BoundedIntWidget):
53 _view_name = Unicode('IntSliderView', sync=True)
53 _view_name = Unicode('IntSliderView', sync=True)
54 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
54 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
55 help="Vertical or horizontal.", sync=True)
55 help="Vertical or horizontal.", sync=True)
56 range = Bool(False, help="Display a range selector", sync=True)
56 range = Bool(False, help="Display a range selector", sync=True)
57 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
57 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
58
58
59
59
60 class IntProgressWidget(_BoundedIntWidget):
60 class IntProgressWidget(_BoundedIntWidget):
61 _view_name = Unicode('ProgressView', sync=True)
61 _view_name = Unicode('ProgressView', sync=True)
62
62
63 class _IntRangeWidget(_IntWidget):
63 class _IntRangeWidget(_IntWidget):
64 value = Tuple(CInt, CInt, default_value=(0, 1), help="Low and high int values", sync=True)
64 value = Tuple(CInt, CInt, default_value=(0, 1), help="Low and high int values", sync=True)
65
65
66 class _BoundedIntRangeWidget(_IntRangeWidget):
66 class _BoundedIntRangeWidget(_IntRangeWidget):
67 step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True)
67 step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True)
68 max = CInt(100, help="Max value", sync=True)
68 max = CInt(100, help="Max value", sync=True)
69 min = CInt(0, help="Min value", sync=True)
69 min = CInt(0, help="Min value", sync=True)
70
70
71 def __init__(self, *pargs, **kwargs):
71 def __init__(self, *pargs, **kwargs):
72 """Constructor"""
72 """Constructor"""
73 DOMWidget.__init__(self, *pargs, **kwargs)
73 DOMWidget.__init__(self, *pargs, **kwargs)
74 self.on_trait_change(self._validate, ['value', 'min', 'max'])
74 self.on_trait_change(self._validate, ['value', 'min', 'max'])
75
75
76 def _validate(self, name, old, new):
76 def _validate(self, name, old, new):
77 """Validate min <= low <= high <= max"""
77 """Validate min <= low <= high <= max"""
78 if name == "value":
78 if name == "value":
79 low, high = new
79 low, high = new
80 low = max(low, self.min)
80 else:
81 high = min(high, self.max)
81 low, high = self.value
82 self.value = (min(low, high), max(low, high))
82 low = max(low, self.min)
83 high = min(high, self.max)
84 self.value = (min(low, high), max(low, high))
83
85
84
86
85 class IntRangeSliderWidget(_BoundedIntRangeWidget):
87 class IntRangeSliderWidget(_BoundedIntRangeWidget):
86 _view_name = Unicode('IntSliderView', sync=True)
88 _view_name = Unicode('IntSliderView', sync=True)
87 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
89 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
88 help="Vertical or horizontal.", sync=True)
90 help="Vertical or horizontal.", sync=True)
89 range = Bool(True, help="Display a range selector", sync=True)
91 range = Bool(True, help="Display a range selector", sync=True)
90 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
92 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
General Comments 0
You need to be logged in to leave comments. Login now