Show More
@@ -1,93 +1,97 | |||
|
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, Tuple |
|
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 | 36 | self._validate('value', None, self.value) |
|
37 | 37 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
38 | 38 | |
|
39 | 39 | def _validate(self, name, old, new): |
|
40 | 40 | """Validate value, max, min.""" |
|
41 | 41 | if self.min > new or new > self.max: |
|
42 | 42 | self.value = min(max(new, self.min), self.max) |
|
43 | 43 | |
|
44 | 44 | |
|
45 | 45 | class FloatTextWidget(_FloatWidget): |
|
46 | 46 | _view_name = Unicode('FloatTextView', sync=True) |
|
47 | 47 | |
|
48 | 48 | |
|
49 | 49 | class BoundedFloatTextWidget(_BoundedFloatWidget): |
|
50 | 50 | _view_name = Unicode('FloatTextView', sync=True) |
|
51 | 51 | |
|
52 | 52 | |
|
53 | 53 | class FloatSliderWidget(_BoundedFloatWidget): |
|
54 | 54 | _view_name = Unicode('FloatSliderView', sync=True) |
|
55 | 55 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
56 | 56 | help="Vertical or horizontal.", sync=True) |
|
57 | 57 | range = Bool(False, help="Display a range selector", sync=True) |
|
58 | 58 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
59 | 59 | |
|
60 | 60 | |
|
61 | 61 | class FloatProgressWidget(_BoundedFloatWidget): |
|
62 | 62 | _view_name = Unicode('ProgressView', sync=True) |
|
63 | 63 | |
|
64 | 64 | class _FloatRangeWidget(_FloatWidget): |
|
65 | 65 | value = Tuple(CFloat, CFloat, default_value=(0.0, 1.0), help="Low and high float values", sync=True) |
|
66 | 66 | |
|
67 | 67 | class _BoundedFloatRangeWidget(_FloatRangeWidget): |
|
68 | 68 | step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
69 | 69 | max = CFloat(100.0, help="Max value", sync=True) |
|
70 | 70 | min = CFloat(0.0, help="Min value", sync=True) |
|
71 | 71 | |
|
72 | 72 | def __init__(self, *pargs, **kwargs): |
|
73 | """Constructor""" | |
|
73 | set_value = 'value' not in kwargs | |
|
74 | 74 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
75 | if set_value: | |
|
76 | # if no value is set, use 25-75% to avoid the handles overlapping | |
|
77 | self.value = (0.75*self.min + 0.25*self.max, | |
|
78 | 0.25*self.min + 0.75*self.max) | |
|
75 | 79 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
76 | 80 | |
|
77 | 81 | def _validate(self, name, old, new): |
|
78 | 82 | """Validate min <= low <= high <= max""" |
|
79 | 83 | if name == "value": |
|
80 | 84 | low, high = new |
|
81 | 85 | else: |
|
82 | 86 | low, high = self.value |
|
83 | 87 | low = max(low, self.min) |
|
84 | 88 | high = min(high, self.max) |
|
85 | 89 | self.value = (min(low, high), max(low, high)) |
|
86 | 90 | |
|
87 | 91 | |
|
88 | 92 | class FloatRangeSliderWidget(_BoundedFloatRangeWidget): |
|
89 | 93 | _view_name = Unicode('FloatSliderView', sync=True) |
|
90 | 94 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
91 | 95 | help="Vertical or horizontal.", sync=True) |
|
92 | 96 | range = Bool(True, help="Display a range selector", sync=True) |
|
93 | 97 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
@@ -1,92 +1,96 | |||
|
1 | 1 | """IntWidget class. |
|
2 | 2 | |
|
3 | 3 | Represents an unbounded int 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, CInt, Bool, Enum, Tuple |
|
18 | 18 | |
|
19 | 19 | #----------------------------------------------------------------------------- |
|
20 | 20 | # Classes |
|
21 | 21 | #----------------------------------------------------------------------------- |
|
22 | 22 | class _IntWidget(DOMWidget): |
|
23 | 23 | value = CInt(0, help="Int 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 _BoundedIntWidget(_IntWidget): |
|
29 | 29 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
30 | 30 | max = CInt(100, help="Max value", sync=True) |
|
31 | 31 | min = CInt(0, help="Min value", sync=True) |
|
32 | 32 | |
|
33 | 33 | def __init__(self, *pargs, **kwargs): |
|
34 | 34 | """Constructor""" |
|
35 | 35 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
36 | 36 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
37 | 37 | |
|
38 | 38 | def _validate(self, name, old, new): |
|
39 | 39 | """Validate value, max, min.""" |
|
40 | 40 | if self.min > new or new > self.max: |
|
41 | 41 | self.value = min(max(new, self.min), self.max) |
|
42 | 42 | |
|
43 | 43 | |
|
44 | 44 | class IntTextWidget(_IntWidget): |
|
45 | 45 | _view_name = Unicode('IntTextView', sync=True) |
|
46 | 46 | |
|
47 | 47 | |
|
48 | 48 | class BoundedIntTextWidget(_BoundedIntWidget): |
|
49 | 49 | _view_name = Unicode('IntTextView', sync=True) |
|
50 | 50 | |
|
51 | 51 | |
|
52 | 52 | class IntSliderWidget(_BoundedIntWidget): |
|
53 | 53 | _view_name = Unicode('IntSliderView', sync=True) |
|
54 | 54 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
55 | 55 | help="Vertical or horizontal.", sync=True) |
|
56 | 56 | range = Bool(False, help="Display a range selector", sync=True) |
|
57 | 57 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
|
58 | 58 | |
|
59 | 59 | |
|
60 | 60 | class IntProgressWidget(_BoundedIntWidget): |
|
61 | 61 | _view_name = Unicode('ProgressView', sync=True) |
|
62 | 62 | |
|
63 | 63 | class _IntRangeWidget(_IntWidget): |
|
64 | 64 | value = Tuple(CInt, CInt, default_value=(0, 1), help="Low and high int values", sync=True) |
|
65 | 65 | |
|
66 | 66 | class _BoundedIntRangeWidget(_IntRangeWidget): |
|
67 | 67 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
68 | 68 | max = CInt(100, help="Max value", sync=True) |
|
69 | 69 | min = CInt(0, help="Min value", sync=True) |
|
70 | 70 | |
|
71 | 71 | def __init__(self, *pargs, **kwargs): |
|
72 | """Constructor""" | |
|
72 | set_value = 'value' not in kwargs | |
|
73 | 73 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
74 | if set_value: | |
|
75 | # if no value is set, use 25-75% to avoid the handles overlapping | |
|
76 | self.value = (0.75*self.min + 0.25*self.max, | |
|
77 | 0.25*self.min + 0.75*self.max) | |
|
74 | 78 | self.on_trait_change(self._validate, ['value', 'min', 'max']) |
|
75 | 79 | |
|
76 | 80 | def _validate(self, name, old, new): |
|
77 | 81 | """Validate min <= low <= high <= max""" |
|
78 | 82 | if name == "value": |
|
79 | 83 | low, high = new |
|
80 | 84 | else: |
|
81 | 85 | low, high = self.value |
|
82 | 86 | low = max(low, self.min) |
|
83 | 87 | high = min(high, self.max) |
|
84 | 88 | self.value = (min(low, high), max(low, high)) |
|
85 | 89 | |
|
86 | 90 | |
|
87 | 91 | class IntRangeSliderWidget(_BoundedIntRangeWidget): |
|
88 | 92 | _view_name = Unicode('IntSliderView', sync=True) |
|
89 | 93 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
90 | 94 | help="Vertical or horizontal.", sync=True) |
|
91 | 95 | range = Bool(True, help="Display a range selector", sync=True) |
|
92 | 96 | 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