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

Source Code for Module library.pyjamas.ui.Control

  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 import Window 
 20  from pyjamas.ui.FocusWidget import FocusWidget 
 21  from pyjamas.ui.MouseListener import MouseHandler, MouseWheelHandler 
 22  from pyjamas.ui import KeyboardListener 
 23  from pyjamas.ui import GlassWidget 
 24   
 25   
26 -class Control(FocusWidget, MouseHandler, MouseWheelHandler):
27
28 - def __init__(self, element, min_value, max_value, 29 start_value=None, step=None, 30 **kwargs):
31 32 self.min_value = min_value 33 self.max_value = max_value 34 if start_value is None: 35 start_value = min_value 36 if step is None: 37 step = (self.max_value - self.min_value) / 20 38 self.step = step 39 self.value = start_value 40 self.valuechange_listeners = [] 41 self.dragging = False 42 self.drag_enabled = False 43 44 kwargs['TabIndex'] = kwargs.get("TabIndex", 0) 45 46 FocusWidget.__init__(self, element, **kwargs) 47 MouseHandler.__init__(self) 48 MouseWheelHandler.__init__(self, True) 49 self.addMouseWheelListener(self)
50
51 - def isDragable(self):
52 return self.drag_enabled
53
54 - def setDragable(self, dragable):
55 if self.drag_enabled == dragable: 56 return 57 if self.drag_enabled: 58 self.removeKeyboardListener(self) 59 else: 60 self.addKeyboardListener(self) 61 self.drag_enabled = dragable
62
63 - def onFocus(self, sender):
64 pass
65
66 - def onLostFocus(self, sender):
67 pass
68
69 - def onClick(self, sender=None):
70 pass
71
72 - def processValue(self, value):
73 """ rounds and limits the value to acceptable range 74 """ 75 value = math.floor((value - self.min_value) / self.step) 76 value = (value * self.step) + self.min_value 77 value = max(value, self.min_value) 78 value = min(value, self.max_value) 79 return value
80
81 - def setValue(self, new_value, notify=1):
82 83 old_value = self.value 84 self.value = new_value 85 86 if not notify: 87 return 88 89 for listener in self.valuechange_listeners: 90 listener.onControlValueChanged(self, old_value, new_value)
91
92 - def addControlValueListener(self, listener):
93 self.valuechange_listeners.append(listener)
94
95 - def removeControlValueListener(self, listener):
96 self.valuechange_listeners.remove(listener)
97
98 - def moveControl(self, x, y, first_move=False):
99 pass
100
101 - def onClick(self, sender=None):
102 self.setFocus(True) 103 # work out the relative position of cursor 104 event = DOM.eventGetCurrentEvent() 105 mouse_x = DOM.eventGetClientX(event) + Window.getScrollLeft() 106 mouse_y = DOM.eventGetClientY(event) + Window.getScrollTop() 107 self.moveControl(mouse_x - self.getAbsoluteLeft(), 108 mouse_y - self.getAbsoluteTop(), True)
109
110 - def onMouseWheel(self, sender, velocity):
111 print "mouse wheel", sender, velocity 112 if self.dragging: # don't accept wheel events if dragging! 113 return 114 new_value = self.processValue(self.value - self.step * velocity) 115 self.setControlPos(new_value) 116 self.setValue(new_value)
117
118 - def onMouseMove(self, sender, x, y):
119 if not self.dragging: 120 return 121 self.moveControl(x + Window.getScrollLeft(), y + Window.getScrollTop())
122
123 - def onLoseFocus(self, sender):
124 self.endDragging()
125
126 - def onMouseDown(self, sender, x, y):
127 # regardless of drag_enabled, onMouseDown must prevent 128 # default, in order to avoid losing focus. 129 self.setFocus(True) 130 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 131 if not self.drag_enabled: 132 return 133 self.dragging = True 134 GlassWidget.show(self) 135 self.moveControl(x + Window.getScrollLeft(), y + Window.getScrollTop(), 136 True)
137
138 - def onMouseEnter(self, sender):
139 pass
140
141 - def onMouseLeave(self, sender):
142 pass
143
144 - def onMouseUp(self, sender, x, y):
145 self.endDragging()
146
147 - def onMouseGlassEnter(self, sender):
148 pass
149
150 - def onMouseGlassLeave(self, sender):
151 self.endDragging()
152
153 - def endDragging(self):
154 if not self.dragging: 155 return 156 self.dragging = False 157 GlassWidget.hide()
158
159 - def onKeyDown(self, sender, keycode, modifiers):
160 if keycode == KeyboardListener.KEY_UP: 161 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 162 new_value = self.processValue(self.value + self.step) 163 self.setControlPos(new_value) 164 self.setValue(new_value) 165 elif keycode == KeyboardListener.KEY_DOWN: 166 DOM.eventPreventDefault(DOM.eventGetCurrentEvent()) 167 new_value = self.processValue(self.value - self.step) 168 self.setControlPos(new_value) 169 self.setValue(new_value)
170
171 - def onKeyUp(self, sender, keycode, modifiers):
172 pass
173
174 - def onKeyPress(self, sender, keycode, modifiers):
175 pass
176
177 - def _event_targets_control(self, event):
178 target = DOM.eventGetTarget(event) 179 return target and DOM.isOrHasChild(self.getElement(), target)
180
181 - def onEventPreview(self, event):
182 etype = DOM.eventGetType(event) 183 print "control preview", etype, self._event_targets_control(event), \ 184 DOM.getCaptureElement() is not None 185 if etype == "keydown": 186 return self._event_targets_control(event) 187 elif etype == "keyup": 188 return self._event_targets_control(event) 189 elif etype == "keypress": 190 return self._event_targets_control(event) 191 elif ( etype == "mousedown" 192 or etype == "blur" 193 ): 194 if DOM.getCaptureElement() is not None: 195 return True 196 if not self._event_targets_control(event): 197 return True 198 elif ( etype == "mouseup" 199 or etype == "click" 200 or etype == "mousemove" 201 or etype == "dblclick" 202 ): 203 if DOM.getCaptureElement() is not None: 204 return True 205 elif etype == "mouseout": 206 if DOM.getCaptureElement() is not None: 207 return False 208 return self._event_targets_control(event)
209 210 Factory.registerClass('pyjamas.ui.Control', 'Control', Control) 211