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