Package muntjac :: Package ui :: Module slider
[hide private]
[frames] | no frames]

Source Code for Module muntjac.ui.slider

  1  # Copyright (C) 2012 Vaadin Ltd.  
  2  # Copyright (C) 2012 Richard Lincoln 
  3  #  
  4  # Licensed under the Apache License, Version 2.0 (the "License");  
  5  # you may not use this file except in compliance with the License.  
  6  # You may obtain a copy of the License at  
  7  #  
  8  #     http://www.apache.org/licenses/LICENSE-2.0  
  9  #  
 10  # Unless required by applicable law or agreed to in writing, software  
 11  # distributed under the License is distributed on an "AS IS" BASIS,  
 12  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
 13  # See the License for the specific language governing permissions and  
 14  # limitations under the License. 
 15   
 16  """Defines a component for selecting a numerical value within a range.""" 
 17   
 18  from muntjac.ui.abstract_field import AbstractField 
 19   
 20   
21 -class Slider(AbstractField):
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 #ClientWidget(VSlider, LoadStyle.LAZY) 29 30 ORIENTATION_HORIZONTAL = 0 31 32 ORIENTATION_VERTICAL = 1 33 34 #: Style constant representing a scrollbar styled slider. Use this with 35 # L{#addStyleName(String)}. Default styling usually represents a 36 # common slider found e.g. in Adobe Photoshop. The client side 37 # implementation dictates how different styles will look. 38 STYLE_SCROLLBAR = 'scrollbar' 39 40
41 - def __init__(self, *args):
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 #: Minimum value of slider 53 self._min = 0 54 55 #: Maximum value of slider 56 self._max = 100 57 58 #: Resolution, how many digits are considered relevant after the 59 # decimal point. Must be a non-negative value 60 self._resolution = 0 61 62 #: Slider orientation (horizontal/vertical), defaults . 63 self._orientation = self.ORIENTATION_HORIZONTAL 64 65 #: Slider size in pixels. In horizontal mode, if set to -1, allow 66 # 100% width of container. In vertical mode, if set to -1, default 67 # height is determined by the client-side implementation. 68 # 69 # @deprecated 70 self._size = -1 71 72 #: Handle (draggable control element) size in percents relative to 73 # base size. Must be a value between 1-99. Other values are converted 74 # to nearest bound. A negative value sets the width to auto 75 # (client-side implementation calculates). 76 # 77 # @deprecated: The size is dictated by the current theme. 78 self._handleSize = -1 79 80 #: Show arrows that can be pressed to slide the handle in some 81 # increments (client-side implementation decides the increment, 82 # usually somewhere between 5-10% of slide range). 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
115 - def getMax(self):
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
123 - def setMax(self, maximum):
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: # ClassCastException 135 # FIXME: Handle exception 136 # Where does ClassCastException come from? Can't see any casts 137 # above 138 super(Slider, self).setValue( float(maximum) ) 139 140 self.requestRepaint()
141 142
143 - def getMin(self):
144 """Gets the minimum value in Sliders range. 145 146 @return: the smallest value slider can have 147 """ 148 return self._min
149 150
151 - def setMin(self, minimum):
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: # ClassCastException 164 # FIXME: Handle exception 165 # Where does ClassCastException come from? Can't see any casts 166 # above 167 super(Slider, self).setValue(float(minimum)) 168 169 self.requestRepaint()
170 171
172 - def getOrientation(self):
173 """Get the current orientation of the Slider (horizontal or vertical). 174 175 @return: orientation 176 """ 177 return self._orientation
178 179
180 - def setOrientation(self, orientation):
181 """Set the orientation of the Slider. 182 """ 183 self._orientation = orientation 184 self.requestRepaint()
185 186
187 - def getResolution(self):
188 """Get the current resolution of the Slider. 189 190 @return: resolution 191 """ 192 return self._resolution
193 194
195 - def setResolution(self, resolution):
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 # Round up to resolution 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
230 - def getSize(self):
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
239 - def setSize(self, size):
240 """Set the size for this Slider. 241 242 @param size: 243 in pixels, or -1 auto sizing. 244 @deprecated: use standard setWidth/setHeight instead 245 """ 246 self._size = size 247 248 if self._orientation == self.ORIENTATION_HORIZONTAL: 249 self.setWidth(size, self.UNITS_PIXELS) 250 else: 251 self.setHeight(size, self.UNITS_PIXELS) 252 253 self.requestRepaint()
254 255
256 - def getHandleSize(self):
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
265 - def setHandleSize(self, handleSize):
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
315 - def changeVariables(self, source, variables):
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 # Convert to nearest bound 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
338 - def getType(self):
339 return float
340 341
342 -class ValueOutOfBoundsException(Exception):
343 """ValueOutOfBoundsException 344 345 @author: Vaadin Ltd. 346 @author: Richard Lincoln 347 """ 348
349 - def __init__(self, valueOutOfBounds):
350 """Constructs an C{ValueOutOfBoundsException} with 351 the specified detail message. 352 """ 353 self._value = valueOutOfBounds
354 355
356 - def getValue(self):
357 return self._value
358