/usr/lib/python2.7/dist-packages/enable/events.py is in python-enable 4.5.1-4.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 | """
Define the event objects and traits used by Enable components.
For a list of all the possible event suffixes, see interactor.py.
"""
# Major library imports
from numpy import array, dot
# Enthought imports
from kiva import affine
from traits.api import (Any, Bool, Float, HasTraits, Int,
Event, List, ReadOnly)
class BasicEvent(HasTraits):
x = Float
y = Float
# True if the event has been handled.
handled = Bool(False)
# The AbstractWindow instance through/from which this event was fired.
# Can be None.
window = Any
# (x,y) position stack; initialized to an empty list
_pos_stack = List( () )
# Affine transform stack; initialized to an empty list
_transform_stack = List( () )
# This is a list of objects that have transformed the event's
# coordinates. This can be used to recreate the dispatch path
# that the event took.
dispatch_history = List()
def push_transform(self, transform, caller=None):
"""
Saves the current transform in a stack and sets the given transform
to be the active one.
"""
x, y = dot(array((self.x, self.y, 1)), transform)[:2]
self._pos_stack.append((self.x, self.y))
self._transform_stack.append(transform)
self.x = x
self.y = y
if caller is not None:
self.dispatch_history.append(caller)
return
def pop(self, count=1, caller=None):
"""
Restores a previous position of the event. If **count** is provided,
then pops **count** elements off of the event stack.
"""
for i in range(count-1):
self._pos_stack.pop()
self._transform_stack.pop()
self.x, self.y = self._pos_stack.pop()
self._transform_stack.pop()
if caller is not None:
if caller == self.dispatch_history[-1]:
self.dispatch_history.pop()
return
def offset_xy(self, origin_x, origin_y, caller=None):
"""
Shifts this event to be in the coordinate frame whose origin, specified
in the event's coordinate frame, is (origin_x, origin_y).
Basically, a component calls event.offset_xy(\*self.position) to shift
the event into its own coordinate frame.
"""
self.push_transform(affine.affine_from_translation(-origin_x, -origin_y))
if caller is not None:
self.dispatch_history.append(caller)
return
def scale_xy(self, scale_x, scale_y, caller=None):
"""
Scales the event to be in the scale specified.
A component calls event.scale_xy(scale) to scale the event into its own
coordinate frame when the ctm has been scaled. This operation is used
for zooming.
"""
# Note that the meaning of scale_x and scale_y for Enable
# is the inverted from the meaning for Kiva.affine.
# TODO: Fix this discrepancy.
self.push_transform(affine.affine_from_scale(1/scale_x, 1/scale_y))
if caller is not None:
self.dispatch_history.append(caller)
return
def net_transform(self):
"""
Returns a single transformation (currently only (dx,dy)) that reflects
the total amount of change from the original coordinates to the current
offset coordinates stored in self.x and self.y.
"""
if len(self._transform_stack) == 0:
return affine.affine_identity()
else:
return reduce(dot, self._transform_stack[::-1])
def current_pointer_position(self):
"""
Returns the current pointer position in the transformed coordinates
"""
window_pos = self.window.get_pointer_position()
return tuple(dot(array(window_pos + (1,)), self.net_transform())[:2])
def __repr__(self):
s = '%s(x=%r, y=%r, handled=%r)' % (self.__class__.__name__, self.x,
self.y, self.handled)
return s
class MouseEvent(BasicEvent):
alt_down = ReadOnly
control_down = ReadOnly
shift_down = ReadOnly
left_down = ReadOnly
middle_down = ReadOnly
right_down = ReadOnly
mouse_wheel = ReadOnly
mouse_event_trait = Event(MouseEvent)
class DragEvent(BasicEvent):
""" A system UI drag-and-drop operation. This is not the same as a
DragTool event.
"""
x0 = Float
y0 = Float
copy = ReadOnly
obj = ReadOnly
start_event = ReadOnly
def __repr__(self):
s = ('%s(x=%r, y=%r, x0=%r, y0=%r, handled=%r)' %
(self.__class__.__name__, self.x, self.y, self.x0, self.y0,
self.handled))
return s
drag_event_trait = Event(DragEvent)
class KeyEvent(BasicEvent):
event_type = ReadOnly # one of 'key_pressed', 'key_released' or 'character'
# 'character' is a single unicode character or is a string describing the
# high-bit and control characters. (See module enable.toolkit_constants)
# depending on the event type, it may represent the physical key pressed,
# or the text that was generated by a keystroke
character = ReadOnly
alt_down = ReadOnly
control_down = ReadOnly
shift_down = ReadOnly
event = ReadOnly # XXX the underlying toolkit's event object, remove?
def __repr__(self):
s = ('%s(event_type=%r, character=%r, alt_down=%r, control_down=%r, shift_down=%r, handled=%r)' %
(self.__class__.__name__, self.event_type, self.character, self.alt_down,
self.control_down, self.shift_down, self.handled))
return s
key_event_trait = Event( KeyEvent )
class BlobEvent(BasicEvent):
""" Represent a single pointer event from a multi-pointer event system.
Will be used with events:
blob_down
blob_move
blob_up
"""
# The ID of the pointer.
bid = Int(-1)
# If a blob_move event, then these will be the coordinates of the blob at
# the previous frame.
x0 = Float(0.0)
y0 = Float(0.0)
def push_transform(self, transform, caller=None):
""" Saves the current transform in a stack and sets the given transform
to be the active one.
This will also adjust x0 and y0.
"""
x, y = dot(array((self.x, self.y, 1)), transform)[:2]
self._pos_stack.append((self.x, self.y))
self._transform_stack.append(transform)
self.x = x
self.y = y
x0, y0 = dot(array((self.x0, self.y0, 1)), transform)[:2]
self.x0 = x0
self.y0 = y0
if caller is not None:
self.dispatch_history.append(caller)
def __repr__(self):
s = '%s(bid=%r, x=%r, y=%r, x0=%r, y0=%r, handled=%r)' % (self.__class__.__name__,
self.bid, self.x, self.y, self.x0, self.y0, self.handled)
return s
blob_event_trait = Event(BlobEvent)
class BlobFrameEvent(BasicEvent):
""" Represent the framing events for a multi-pointer event system.
Will be used with events:
blob_frame_begin
blob_frame_end
These can be used to synchronize the effects of multiple pointers.
The position traits are meaningless. These events will get passed down
through all components. Also, no component should mark it as handled. The
event must be dispatched through whether the component takes action based on
it or not.
NOTE: Frames without any blob events may or may not generate
BlobFrameEvents.
"""
# The ID number of the frame. This is generally implemented as a counter.
# Adjacent frames should have different frame IDs, but it is permitted for
# the counter to wrap around eventually or for the Enable application to
# disconnect and reconnect to a multi-pointer system and have the counter
# reset to 0.
fid = Int(-1)
# The timestamp of the frame in seconds from an unspecified origin.
t = Float(0.0)
# Never mark this event as handled. Let every component respond to it.
#handled = ReadOnly(False)
def __repr__(self):
s = '%s(fid=%r, t=%r)' % (self.__class__.__name__,
self.fid, self.t)
return s
blob_frame_event_trait = Event(BlobFrameEvent)
# EOF
|