Show More
@@ -1,165 +1,164 b'' | |||
|
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="Tuple of (lower, upper) bounds", sync=True) |
|
66 | 66 | lower = CFloat(0.0, help="Lower bound", sync=False) |
|
67 | 67 | upper = CFloat(1.0, help="Upper bound", sync=False) |
|
68 | 68 | |
|
69 | 69 | def __init__(self, *pargs, **kwargs): |
|
70 | 70 | if 'value' in kwargs and ('lower' in kwargs or 'upper' in kwargs): |
|
71 | 71 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") |
|
72 | 72 | |
|
73 | 73 | value_given = 'value' in kwargs |
|
74 | 74 | |
|
75 | 75 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
76 | 76 | |
|
77 | 77 | # ensure the traits match, preferring whichever (if any) was given in kwargs |
|
78 | 78 | if value_given: |
|
79 | 79 | self.lower, self.upper = self.value |
|
80 | 80 | else: |
|
81 | 81 | self.value = (self.lower, self.upper) |
|
82 | 82 | |
|
83 | 83 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) |
|
84 | 84 | |
|
85 | 85 | def _validate(self, name, old, new): |
|
86 | print '_IntRangeWidget::_validate', name, old, new | |
|
87 | 86 | if name == 'value': |
|
88 | 87 | self.lower, self.upper = min(new), max(new) |
|
89 | 88 | elif name == 'lower': |
|
90 | 89 | self.value = (new, self.value[1]) |
|
91 | 90 | elif name == 'upper': |
|
92 | 91 | self.value = (self.value[0], new) |
|
93 | 92 | |
|
94 | 93 | class _BoundedFloatRangeWidget(_FloatRangeWidget): |
|
95 | 94 | step = CFloat(1.0, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
96 | 95 | max = CFloat(100.0, help="Max value", sync=True) |
|
97 | 96 | min = CFloat(0.0, help="Min value", sync=True) |
|
98 | 97 | |
|
99 | 98 | def __init__(self, *pargs, **kwargs): |
|
100 | 99 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs |
|
101 | 100 | _FloatRangeWidget.__init__(self, *pargs, **kwargs) |
|
102 | 101 | |
|
103 | 102 | # ensure a minimal amount of sanity |
|
104 | 103 | if self.min > self.max: |
|
105 | 104 | raise ValueError("min must be <= max") |
|
106 | 105 | |
|
107 | 106 | # ensure the initial values within bounds |
|
108 | 107 | if self.lower < self.min: |
|
109 | 108 | self.lower = self.min |
|
110 | 109 | |
|
111 | 110 | if self.upper > self.max: |
|
112 | 111 | self.upper = self.max |
|
113 | 112 | |
|
114 | 113 | # if no value is set, use 25-75% to avoid the handles overlapping |
|
115 | 114 | if not any_value_given: |
|
116 | 115 | self.value = (0.75*self.min + 0.25*self.max, |
|
117 | 116 | 0.25*self.min + 0.75*self.max) |
|
118 | 117 | # callback already set for 'value', 'lower', 'upper' |
|
119 | 118 | self.on_trait_change(self._validate, ['min', 'max']) |
|
120 | 119 | |
|
121 | 120 | |
|
122 | 121 | def _validate(self, name, old, new): |
|
123 | 122 | if name == "min": |
|
124 | 123 | if new > self.max: |
|
125 | 124 | raise ValueError("setting min > max") |
|
126 | 125 | self.min = new |
|
127 | 126 | elif name == "max": |
|
128 | 127 | if new < self.min: |
|
129 | 128 | raise ValueError("setting max < min") |
|
130 | 129 | self.max = new |
|
131 | 130 | |
|
132 | 131 | low, high = self.value |
|
133 | 132 | if name == "value": |
|
134 | 133 | low, high = min(new), max(new) |
|
135 | 134 | elif name == "upper": |
|
136 | 135 | if new < self.lower: |
|
137 | 136 | raise ValueError("setting upper < lower") |
|
138 | 137 | high = new |
|
139 | 138 | elif name == "lower": |
|
140 | 139 | if new > self.upper: |
|
141 | 140 | raise ValueError("setting lower > upper") |
|
142 | 141 | low = new |
|
143 | 142 | |
|
144 | 143 | low = max(self.min, min(low, self.max)) |
|
145 | 144 | high = min(self.max, max(high, self.min)) |
|
146 | 145 | |
|
147 | 146 | # determine the order in which we should update the |
|
148 | 147 | # lower, upper traits to avoid a temporary inverted overlap |
|
149 | 148 | lower_first = high < self.lower |
|
150 | 149 | |
|
151 | 150 | self.value = (low, high) |
|
152 | 151 | if lower_first: |
|
153 | 152 | self.lower = low |
|
154 | 153 | self.upper = high |
|
155 | 154 | else: |
|
156 | 155 | self.upper = high |
|
157 | 156 | self.lower = low |
|
158 | 157 | |
|
159 | 158 | |
|
160 | 159 | class FloatRangeSliderWidget(_BoundedFloatRangeWidget): |
|
161 | 160 | _view_name = Unicode('FloatSliderView', sync=True) |
|
162 | 161 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
163 | 162 | help="Vertical or horizontal.", sync=True) |
|
164 | 163 | range = Bool(True, help="Display a range selector", sync=True) |
|
165 | 164 | readout = Bool(True, help="Display the current value of the slider next to it.", sync=True) |
@@ -1,162 +1,161 b'' | |||
|
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="Tuple of (lower, upper) bounds", sync=True) |
|
65 | 65 | lower = CInt(0, help="Lower bound", sync=False) |
|
66 | 66 | upper = CInt(1, help="Upper bound", sync=False) |
|
67 | 67 | |
|
68 | 68 | def __init__(self, *pargs, **kwargs): |
|
69 | 69 | if 'value' in kwargs and ('lower' in kwargs or 'upper' in kwargs): |
|
70 | 70 | raise ValueError("Cannot specify both 'value' and 'lower'/'upper' for range widget") |
|
71 | 71 | |
|
72 | 72 | value_given = 'value' in kwargs |
|
73 | 73 | |
|
74 | 74 | DOMWidget.__init__(self, *pargs, **kwargs) |
|
75 | 75 | |
|
76 | 76 | # ensure the traits match, preferring whichever (if any) was given in kwargs |
|
77 | 77 | if value_given: |
|
78 | 78 | self.lower, self.upper = self.value |
|
79 | 79 | else: |
|
80 | 80 | self.value = (self.lower, self.upper) |
|
81 | 81 | |
|
82 | 82 | self.on_trait_change(self._validate, ['value', 'upper', 'lower']) |
|
83 | 83 | |
|
84 | 84 | def _validate(self, name, old, new): |
|
85 | print '_IntRangeWidget::_validate', name, old, new | |
|
86 | 85 | if name == 'value': |
|
87 | 86 | self.lower, self.upper = min(new), max(new) |
|
88 | 87 | elif name == 'lower': |
|
89 | 88 | self.value = (new, self.value[1]) |
|
90 | 89 | elif name == 'upper': |
|
91 | 90 | self.value = (self.value[0], new) |
|
92 | 91 | |
|
93 | 92 | class _BoundedIntRangeWidget(_IntRangeWidget): |
|
94 | 93 | step = CInt(1, help="Minimum step that the value can take (ignored by some views)", sync=True) |
|
95 | 94 | max = CInt(100, help="Max value", sync=True) |
|
96 | 95 | min = CInt(0, help="Min value", sync=True) |
|
97 | 96 | |
|
98 | 97 | def __init__(self, *pargs, **kwargs): |
|
99 | 98 | any_value_given = 'value' in kwargs or 'upper' in kwargs or 'lower' in kwargs |
|
100 | 99 | _IntRangeWidget.__init__(self, *pargs, **kwargs) |
|
101 | 100 | |
|
102 | 101 | # ensure a minimal amount of sanity |
|
103 | 102 | if self.min > self.max: |
|
104 | 103 | raise ValueError("min must be <= max") |
|
105 | 104 | |
|
106 | 105 | # ensure the initial values within bounds |
|
107 | 106 | if self.lower < self.min: |
|
108 | 107 | self.lower = self.min |
|
109 | 108 | |
|
110 | 109 | if self.upper > self.max: |
|
111 | 110 | self.upper = self.max |
|
112 | 111 | |
|
113 | 112 | # if no value (or upper/lower) is set, use 25-75% to avoid the handles overlapping |
|
114 | 113 | if not any_value_given: |
|
115 | 114 | self.value = (0.75*self.min + 0.25*self.max, |
|
116 | 115 | 0.25*self.min + 0.75*self.max) |
|
117 | 116 | # callback already set for 'value', 'lower', 'upper' |
|
118 | 117 | self.on_trait_change(self._validate, ['min', 'max']) |
|
119 | 118 | |
|
120 | 119 | def _validate(self, name, old, new): |
|
121 | 120 | if name == "min": |
|
122 | 121 | if new > self.max: |
|
123 | 122 | raise ValueError("setting min > max") |
|
124 | 123 | self.min = new |
|
125 | 124 | elif name == "max": |
|
126 | 125 | if new < self.min: |
|
127 | 126 | raise ValueError("setting max < min") |
|
128 | 127 | self.max = new |
|
129 | 128 | |
|
130 | 129 | low, high = self.value |
|
131 | 130 | if name == "value": |
|
132 | 131 | low, high = min(new), max(new) |
|
133 | 132 | elif name == "upper": |
|
134 | 133 | if new < self.lower: |
|
135 | 134 | raise ValueError("setting upper < lower") |
|
136 | 135 | high = new |
|
137 | 136 | elif name == "lower": |
|
138 | 137 | if new > self.upper: |
|
139 | 138 | raise ValueError("setting lower > upper") |
|
140 | 139 | low = new |
|
141 | 140 | |
|
142 | 141 | low = max(self.min, min(low, self.max)) |
|
143 | 142 | high = min(self.max, max(high, self.min)) |
|
144 | 143 | |
|
145 | 144 | # determine the order in which we should update the |
|
146 | 145 | # lower, upper traits to avoid a temporary inverted overlap |
|
147 | 146 | lower_first = high < self.lower |
|
148 | 147 | |
|
149 | 148 | self.value = (low, high) |
|
150 | 149 | if lower_first: |
|
151 | 150 | self.lower = low |
|
152 | 151 | self.upper = high |
|
153 | 152 | else: |
|
154 | 153 | self.upper = high |
|
155 | 154 | self.lower = low |
|
156 | 155 | |
|
157 | 156 | class IntRangeSliderWidget(_BoundedIntRangeWidget): |
|
158 | 157 | _view_name = Unicode('IntSliderView', sync=True) |
|
159 | 158 | orientation = Enum([u'horizontal', u'vertical'], u'horizontal', |
|
160 | 159 | help="Vertical or horizontal.", sync=True) |
|
161 | 160 | range = Bool(True, help="Display a range selector", sync=True) |
|
162 | 161 | 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