##// END OF EJS Templates
Validate initial value of "_BoundedFloatWidget".
Raffaele De Feo -
Show More
@@ -1,60 +1,61
1 1 """FloatWidget class.
2 2
3 3 Represents an unbounded float using a widget.
4 4 """
5 5 #-----------------------------------------------------------------------------
6 6 # Copyright (c) 2013, the IPython Development Team.
7 7 #
8 8 # Distributed under the terms of the Modified BSD License.
9 9 #
10 10 # The full license is in the file COPYING.txt, distributed with this software.
11 11 #-----------------------------------------------------------------------------
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Imports
15 15 #-----------------------------------------------------------------------------
16 16 from .widget import DOMWidget
17 17 from IPython.utils.traitlets import Unicode, CFloat, Bool, Enum
18 18
19 19 #-----------------------------------------------------------------------------
20 20 # Classes
21 21 #-----------------------------------------------------------------------------
22 22 class _FloatWidget(DOMWidget):
23 23 value = CFloat(0.0, help="Float value", sync=True)
24 24 disabled = Bool(False, help="Enable or disable user changes", sync=True)
25 25 description = Unicode(help="Description of the value this widget represents", sync=True)
26 26
27 27
28 28 class _BoundedFloatWidget(_FloatWidget):
29 29 max = CFloat(100.0, help="Max value", sync=True)
30 30 min = CFloat(0.0, help="Min value", sync=True)
31 31 step = CFloat(0.1, help="Minimum step that the value can take (ignored by some views)", sync=True)
32 32
33 33 def __init__(self, *pargs, **kwargs):
34 34 """Constructor"""
35 35 DOMWidget.__init__(self, *pargs, **kwargs)
36 self._validate('value', None, self.value)
36 37 self.on_trait_change(self._validate, ['value', 'min', 'max'])
37 38
38 39 def _validate(self, name, old, new):
39 40 """Validate value, max, min."""
40 41 if self.min > new or new > self.max:
41 42 self.value = min(max(new, self.min), self.max)
42 43
43 44
44 45 class FloatTextWidget(_FloatWidget):
45 46 _view_name = Unicode('FloatTextView', sync=True)
46 47
47 48
48 49 class BoundedFloatTextWidget(_BoundedFloatWidget):
49 50 _view_name = Unicode('FloatTextView', sync=True)
50 51
51 52
52 53 class FloatSliderWidget(_BoundedFloatWidget):
53 54 _view_name = Unicode('FloatSliderView', sync=True)
54 55 orientation = Enum([u'horizontal', u'vertical'], u'horizontal',
55 56 help="Vertical or horizontal.", sync=True)
56 57 readout = Bool(True, help="Display the current value of the slider next to it.", sync=True)
57 58
58 59
59 60 class FloatProgressWidget(_BoundedFloatWidget):
60 61 _view_name = Unicode('ProgressView', sync=True)
General Comments 0
You need to be logged in to leave comments. Login now