Package pyjamas :: Package ui :: Module HorizontalSlider
[hide private]
[frames] | no frames]

Source Code for Module pyjamas.ui.HorizontalSlider

 1  """ Control Widgets.  Presently comprises a Vertical Slider and derivatives. 
 2   
 3      HorizontalSlider and HorizontalSlider2 added by Bill Winder 
 4      AreaSlider and AreaSlider2 added by Bill Winder 
 5   
 6      Copyright (C) 2008, 2009, 2010 Luke Kenneth Casson Leighton <lkcl@lkcl.net> 
 7      Copyright (C) 2010 - Cedric Gestes <gestes@aldebaran-robotics.com> 
 8      Copyright (C) 2009, 2010 - Bill Winder <wgwinder@gmail.com> 
 9   
10   
11      To do: All controls with draggable=True do not fire the OnFocus methon on single click. 
12      the control does not activate the OnFocus method. Clicking the handle does fire OnFocus, however. 
13   
14  """ 
15   
16  from pyjamas import Factory 
17  from pyjamas import DOM 
18  from pyjamas.ui.VerticalSlider import VerticalSlider 
19   
20   
21 -class HorizontalSlider(VerticalSlider):
22
23 - def __init__(self, min_value, max_value, start_value=None, step=None, 24 **kwargs):
25 26 VerticalSlider.__init__(self, min_value, max_value, start_value, 27 step, **kwargs) 28 self.setHandleStyle(None, "10px", "100%", None)
29
30 - def moveControl(self, mouse_x, mouse_y, first_move=False):
31 handle_width = DOM.getIntAttribute(self.handle, "offsetWidth") 32 widget_width = self.getOffsetWidth() 33 length_range = widget_width - 10 # handle width is hard-coded 34 relative_x = mouse_x - (handle_width / 2) 35 if relative_x < 0: 36 relative_x = 0 37 if relative_x >= length_range: 38 relative_x = length_range 39 40 val_diff = self.max_value - self.min_value 41 new_value = ((val_diff * relative_x) / length_range) + self.min_value 42 new_value = self.processValue(new_value) 43 44 self.setControlPos(new_value) 45 self.setValue(new_value)
46
47 - def setControlPos(self, value):
48 widget_width = self.getOffsetWidth() 49 length_range = widget_width - 10 # handle width is hard-coded 50 val_diff = self.max_value - self.min_value 51 relative_x = length_range * (value - self.min_value) / val_diff 52 53 # limit the position to be in the widget! 54 if relative_x < 0: 55 relative_x = 0 56 if relative_x >= length_range: 57 relative_x = length_range 58 59 # move the handle 60 DOM.setStyleAttribute(self.handle, "left", "%dpx" % relative_x) 61 DOM.setStyleAttribute(self.handle, "position", "absolute")
62 63 Factory.registerClass('pyjamas.ui.HorizontalSlider', 'HorizontalSlider', HorizontalSlider) 64