1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 """Defines a component for selecting a numerical value within a range."""
17
18 from muntjac.ui.abstract_field import AbstractField
19
20
22 """A component for selecting a numerical value within a range.
23
24 @author: Vaadin Ltd.
25 @author: Richard Lincoln
26 """
27
28 CLIENT_WIDGET = None
29
30 ORIENTATION_HORIZONTAL = 0
31
32 ORIENTATION_VERTICAL = 1
33
34
35
36
37
38 STYLE_SCROLLBAR = 'scrollbar'
39
40
42 """Slider constructor.
43
44 @param args: tuple of the form
45 - ()
46 - (caption)
47 1. The caption for this Slider (e.g. "Volume").
48 - (min, max, resolution)
49 - (min, max)
50 - (caption, min, max)
51 """
52
53 self._min = 0
54
55
56 self._max = 100
57
58
59
60 self._resolution = 0
61
62
63 self._orientation = self.ORIENTATION_HORIZONTAL
64
65
66
67
68
69
70 self._size = -1
71
72
73
74
75
76
77
78 self._handleSize = -1
79
80
81
82
83 self._arrows = False
84
85 args = args
86 nargs = len(args)
87 if nargs == 0:
88 super(Slider, self).__init__()
89 super(Slider, self).setValue(float(self._min))
90 elif nargs == 1:
91 caption, = args
92 Slider.__init__(self)
93 self.setCaption(caption)
94 elif nargs == 2:
95 minn, maxx = args
96 Slider.__init__(self)
97 self.setMin(minn)
98 self.setMax(maxx)
99 self.setResolution(0)
100 elif nargs == 3:
101 if isinstance(args[0], float):
102 minn, maxx, resolution = args
103 Slider.__init__(self)
104 self.setMin(minn)
105 self.setMax(maxx)
106 self.setResolution(resolution)
107 else:
108 caption, minn, maxx = args
109 Slider.__init__(self, minn, maxx)
110 self.setCaption(caption)
111 else:
112 raise ValueError, 'too many arguments'
113
114
116 """Gets the biggest possible value in Sliders range.
117
118 @return: the biggest value slider can have
119 """
120 return self._max
121
122
124 """Set the maximum value of the Slider. If the current value of
125 the Slider is out of new bounds, the value is set to new minimum.
126
127 @param maximum: new maximum value of the Slider
128 """
129 self._max = maximum
130
131 try:
132 if float(str( self.getValue() )) > maximum:
133 super(Slider, self).setValue( float(maximum) )
134 except ValueError:
135
136
137
138 super(Slider, self).setValue( float(maximum) )
139
140 self.requestRepaint()
141
142
144 """Gets the minimum value in Sliders range.
145
146 @return: the smallest value slider can have
147 """
148 return self._min
149
150
152 """Set the minimum value of the Slider. If the current value of
153 the Slider is out of new bounds, the value is set to new minimum.
154
155 @param minimum:
156 New minimum value of the Slider.
157 """
158 self._min = minimum
159
160 try:
161 if float( str(self.getValue()) ) < minimum:
162 super(Slider, self).setValue(float(minimum))
163 except ValueError:
164
165
166
167 super(Slider, self).setValue(float(minimum))
168
169 self.requestRepaint()
170
171
173 """Get the current orientation of the Slider (horizontal or vertical).
174
175 @return: orientation
176 """
177 return self._orientation
178
179
181 """Set the orientation of the Slider.
182 """
183 self._orientation = orientation
184 self.requestRepaint()
185
186
188 """Get the current resolution of the Slider.
189
190 @return: resolution
191 """
192 return self._resolution
193
194
196 """Set a new resolution for the Slider.
197 """
198 if resolution < 0:
199 return
200 self._resolution = resolution
201 self.requestRepaint()
202
203
204 - def setValue(self, value, repaintIsNotNeeded=False):
205 """Set the value of this Slider.
206
207 @param value:
208 New value of Slider. Must be within Sliders range
209 (min - max), otherwise throws an exception.
210 @param repaintIsNotNeeded:
211 If true, client-side is not requested to repaint itself.
212 @raise ValueOutOfBoundsException:
213 """
214 v = value
215
216 if self._resolution > 0:
217
218 newValue = v * 10**self._resolution
219 newValue = newValue / 10**self._resolution
220 if (self._min > newValue) or (self._max < newValue):
221 raise ValueOutOfBoundsException(value)
222 else:
223 newValue = v
224 if (self._min > newValue) or (self._max < newValue):
225 raise ValueOutOfBoundsException(value)
226
227 super(Slider, self).setValue(float(newValue), repaintIsNotNeeded)
228
229
231 """Get the current Slider size.
232
233 @return: size in pixels or -1 for auto sizing.
234 @deprecated: use standard getWidth/getHeight instead
235 """
236 return self._size
237
238
254
255
257 """Get the handle size of this Slider.
258
259 @return: handle size in percentages.
260 @deprecated: The size is dictated by the current theme.
261 """
262 return self._handleSize
263
264
266 """Set the handle size of this Slider.
267
268 @param handleSize:
269 in percentages relative to slider base size.
270 @deprecated: The size is dictated by the current theme.
271 """
272 if handleSize < 0:
273 self._handleSize = -1
274 elif handleSize > 99:
275 self._handleSize = 99
276 elif handleSize < 1:
277 self._handleSize = 1
278 else:
279 self._handleSize = handleSize
280
281 self.requestRepaint()
282
283
284 - def paintContent(self, target):
285 super(Slider, self).paintContent(target)
286
287 target.addAttribute('min', self._min)
288 if self._max > self._min:
289 target.addAttribute('max', self._max)
290 else:
291 target.addAttribute('max', self._min)
292
293 target.addAttribute('resolution', self._resolution)
294
295 if self._resolution > 0:
296 target.addVariable(self, 'value', float( self.getValue() ))
297 else:
298 target.addVariable(self, 'value', int( self.getValue() ))
299
300 if self._orientation == self.ORIENTATION_VERTICAL:
301 target.addAttribute('vertical', True)
302
303 if self._arrows:
304 target.addAttribute('arrows', True)
305
306 if self._size > -1:
307 target.addAttribute('size', self._size)
308
309 if self._min != self._max and self._min < self._max:
310 target.addAttribute('hsize', self._handleSize)
311 else:
312 target.addAttribute('hsize', 100)
313
314
316 """Invoked when the value of a variable has changed. Slider listeners
317 are notified if the slider value has changed.
318 """
319 super(Slider, self).changeVariables(source, variables)
320
321 if 'value' in variables:
322 value = variables.get('value')
323 newValue = float(str(value))
324 if (newValue is not None and newValue != self.getValue()
325 and newValue != self.getValue()):
326
327 try:
328 self.setValue(newValue, True)
329 except ValueOutOfBoundsException, e:
330 out = float( e.getValue() )
331 if out < self._min:
332 out = self._min
333 if out > self._max:
334 out = self._max
335 super(Slider, self).setValue(float(out), False)
336
337
340
341
343 """ValueOutOfBoundsException
344
345 @author: Vaadin Ltd.
346 @author: Richard Lincoln
347 """
348
350 """Constructs an C{ValueOutOfBoundsException} with
351 the specified detail message.
352 """
353 self._value = valueOutOfBounds
354
355
358