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

Source Code for Module library.pyjamas.ui.DoubleControl

  1  """ Control Widgets.  Presently comprises a Vertical Slider Demo and derivatives. 
  2   
  3      HorizontalDemoSlider and HorizontalDemoSlider2 added by Bill Winder 
  4      AreaDemoSlider and AreaDemoSlider2 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  import math 
 18  from pyjamas import DOM 
 19  from pyjamas.ui.FocusWidget import FocusWidget 
 20  from pyjamas.ui import Focus 
 21  from pyjamas.ui.MouseListener import MouseHandler 
 22  from pyjamas.ui import KeyboardListener 
 23  from pyjamas.ui.Control import Control 
 24   
 25   
26 -class DoubleControl(Control):
27
28 - def __init__(self, element, min_value, max_value, 29 start_value_xy=None, step_xy=None, **kwargs):
30 31 self.min_value_x = min_value[0] 32 self.min_value_y = min_value[1] 33 34 self.max_value_x = max_value[0] 35 self.max_value_y = max_value[1] 36 37 if start_value_xy is None: 38 self.start_value_x = self.min_value_x 39 self.start_value_y = self.min_value_y 40 else: 41 self.start_value_x = start_value_xy[0] 42 self.start_value_y = start_value_xy[1] 43 44 if step_xy is None: 45 self.step_x = (self.max_value_x - self.min_value_x) / 20 46 self.step_y = (self.max_value_y - self.min_value_y) / 20 47 else: 48 self.step_x = step_xy[0] 49 self.step_y = step_xy[1] 50 51 self.value_x = self.start_value_x 52 self.value_y = self.start_value_y 53 54 self.valuechange_listeners = [] 55 self.dragging = False 56 self.drag_enabled = False 57 58 if not kwargs.has_key("TabIndex"): kwargs['TabIndex'] = 0 59 FocusWidget.__init__(self, element, **kwargs) 60 MouseHandler.__init__(self)
61
62 - def processValue(self, value):
63 64 """ rounds and limits the value to acceptable range 65 """ 66 value_x = value[0] 67 value_y = value[1] 68 69 value_x = math.floor((value_x - self.min_value_x) / self.step_x) 70 value_x = (value_x * self.step_x) + self.min_value_x 71 value_x = max(value_x, self.min_value_x) 72 value_x = min(value_x, self.max_value_x) 73 74 value_y = math.floor((value_y - self.min_value_y) / self.step_y) 75 value_y = (value_y * self.step_y) + self.min_value_y 76 value_y = max(value_y, self.min_value_y) 77 value_y = min(value_y, self.max_value_y) 78 79 return [value_x, value_y]
80
81 - def setValue(self, (new_value_x,new_value_y), notify=1):
82 83 old_value = [self.value_x, self.value_y] 84 85 self.value_x = new_value[0] 86 self.value_y = new_value[1] 87 88 if not notify: 89 return 90 91 for listener in self.valuechange_listeners: 92 listener.onControlValueChanged(self, old_value, new_value)
93
94 - def onControlValueChanged(self, value_old_xy, value_new_xy):
95 pass
96
97 - def onKeyDown(self, sender, keycode, modifiers):
98 if keycode == KeyboardListener.KEY_UP: 99 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 100 new_value = \ 101 self.processValue((self.value_x + self.step_x, 102 self.value_y + self.step_y)) 103 self.setControlPos(new_value) 104 self.setValue(new_value) 105 elif keycode == KeyboardListener.KEY_DOWN: 106 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 107 new_value = \ 108 self.processValue((self.value_x - self.step_x, 109 self.value_y-self.step_y)) 110 self.setControlPos(new_value) 111 self.setValue(new_value)
112 113 Factory.registerClass('pyjamas.ui.DoubleControl', 'DoubleControl', DoubleControl) 114