/usr/lib/python2.7/dist-packages/chaco/tools/pan_tool.py is in python-chaco 4.5.0-1.
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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | """ Defines the PanTool class.
"""
from numpy import inf
# Enthought library imports
from enable.api import BaseTool, Pointer, KeySpec
from traits.api import Bool, Enum, Float, Tuple, Instance
class PanTool(BaseTool):
""" A tool that enables the user to pan a plot by clicking a mouse
button and dragging.
"""
# The mouse button that initiates the drag operation.
drag_button = Enum("left", "middle", "right")
# The cursor to use when panning.
drag_pointer = Pointer("hand")
# Scaling factor on the panning "speed".
speed = Float(1.0)
# The modifier key that, if depressed when the drag is initiated, constrains
# the panning to happen in the only direction of largest initial motion.
# It is possible to permanently restrict this tool to always drag along one
# direction. To do so, set constrain=True, constrain_key=None, and
# constrain_direction to the desired direction.
constrain_key = Enum(None, "shift", "control", "alt")
# Keys to Pan via keyboard
pan_right_key = Instance(KeySpec, args=("Right",))
pan_left_key = Instance(KeySpec, args=("Left",))
pan_up_key = Instance(KeySpec, args=("Up",))
pan_down_key = Instance(KeySpec, args=("Down",))
# number of pixels the keys should pan
# disabled if 0.0
pan_keys_step = Float(0.0)
# Constrain the panning to one direction?
constrain = Bool(False)
# The direction of constrained draw. A value of None means that the user
# has initiated the drag and pressed the constrain_key, but hasn't moved
# the mouse yet; the magnitude of the components of the next mouse_move
# event will determine the constrain_direction.
constrain_direction = Enum(None, "x", "y")
# Restrict to the bounds of the plot data
restrict_to_data = Bool(False)
# (x,y) of the point where the mouse button was pressed.
_original_xy = Tuple
# Data coordinates of **_original_xy**. This may be either (index,value)
# or (value,index) depending on the component's orientation.
_original_data = Tuple
# Was constrain=True triggered by the **contrain_key**? If False, it was
# set programmatically.
_auto_constrain = Bool(False)
#------------------------------------------------------------------------
# Inherited BaseTool traits
#------------------------------------------------------------------------
# The tool does not have a visual representation (overrides
# BaseTool).
draw_mode = "none"
# The tool is not visible (overrides BaseTool).
visible = False
# The possible event states of this tool (overrides enable.Interactor).
event_state = Enum("normal", "panning")
def normal_key_pressed(self, event):
""" Handles a key being pressed when the tool is in the 'normal'
state.
"""
if self.pan_keys_step == 0.0:
return
src = self.component.bounds[0]/2, self.component.bounds[1]/2
dest = src
if self.pan_left_key.match(event):
dest = (src[0] - self.pan_keys_step,
src[1])
elif self.pan_right_key.match(event):
dest = (src[0] + self.pan_keys_step,
src[1])
elif self.pan_down_key.match(event):
dest = (src[0],
src[1] - self.pan_keys_step)
elif self.pan_up_key.match(event):
dest = (src[0],
src[1] + self.pan_keys_step)
if src != dest:
self._original_xy = src
event.x = dest[0]
event.y = dest[1]
self.panning_mouse_move(event)
return
def normal_left_down(self, event):
""" Handles the left mouse button being pressed when the tool is in
the 'normal' state.
Starts panning if the left mouse button is the drag button.
"""
if self.drag_button == "left":
self._start_pan(event)
return
def normal_right_down(self, event):
""" Handles the right mouse button being pressed when the tool is in
the 'normal' state.
Starts panning if the right mouse button is the drag button.
"""
if self.drag_button == "right":
self._start_pan(event)
return
def normal_middle_down(self, event):
""" Handles the middle mouse button being pressed when the tool is in
the 'normal' state.
Starts panning if the middle mouse button is the drag button.
"""
if self.drag_button == "middle":
self._start_pan(event)
return
def panning_left_up(self, event):
""" Handles the left mouse button coming up when the tool is in the
'panning' state.
Stops panning if the left mouse button is the drag button.
"""
if self.drag_button == "left":
self._end_pan(event)
return
def panning_right_up(self, event):
""" Handles the right mouse button coming up when the tool is in the
'panning' state.
Stops panning if the right mouse button is the drag button.
"""
if self.drag_button == "right":
self._end_pan(event)
return
def panning_middle_up(self, event):
""" Handles the middle mouse button coming up when the tool is in the
'panning' state.
Stops panning if the middle mouse button is the drag button.
"""
if self.drag_button == "middle":
self._end_pan(event)
return
def panning_mouse_move(self, event):
""" Handles the mouse being moved when the tool is in the 'panning'
state.
"""
plot = self.component
if self._auto_constrain and self.constrain_direction is None:
# Determine the constraint direction
x_orig, y_orig = self._original_xy
if abs(event.x - x_orig) > abs(event.y - y_orig):
self.constrain_direction = "x"
else:
self.constrain_direction = "y"
direction_info = [("x", "width", 0), ("y", "height", 1)]
for direction, bound_name, index in direction_info:
if not self.constrain or self.constrain_direction == direction:
mapper = getattr(plot, direction + "_mapper")
domain_min, domain_max = mapper.domain_limits
eventpos = getattr(event, direction)
origpos = self._original_xy[index]
screenlow, screenhigh = mapper.screen_bounds
screendelta = self.speed * (eventpos - origpos)
newlow = mapper.map_data(screenlow - screendelta)
newhigh = mapper.map_data(screenhigh - screendelta)
# Don't set the range in this dimension if the panning
# would exceed the domain limits.
# To do this offset properly, we would need to iteratively
# solve for a root using map_data on successive trial
# values. As a first approximation, we're just going to
# use a linear approximation, which works perfectly for
# linear mappers (which is used 99% of the time).
data = [arr for arr in
(source.get_data() for source in mapper.range.sources)
if arr.size > 0]
if domain_min is None:
if self.restrict_to_data:
domain_min = min([arr.min() for arr in data])
else:
domain_min = -inf
if domain_max is None:
if self.restrict_to_data:
domain_max = max([arr.max() for arr in data])
else:
domain_max = inf
if (newlow <= domain_min) and (newhigh >= domain_max):
# Don't do anything; effectively, freeze the pan
continue
if newlow <= domain_min:
newlow = domain_min
# Calculate delta in screen space, which is always linear.
screen_delta = mapper.map_screen(domain_min) - screenlow
newhigh = mapper.map_data(screenhigh + screen_delta)
elif newhigh >= domain_max:
newhigh = domain_max
# Calculate delta in screen space, which is always linear.
screen_delta = mapper.map_screen(domain_max) - screenhigh
newlow = mapper.map_data(screenlow + screen_delta)
# Use .set_bounds() so that we don't generate two range_changed
# events on the DataRange
mapper.range.set_bounds(newlow, newhigh)
event.handled = True
self._original_xy = (event.x, event.y)
plot.request_redraw()
return
def panning_mouse_leave(self, event):
""" Handles the mouse leaving the plot when the tool is in the 'panning'
state.
Ends panning.
"""
return self._end_pan(event)
def _start_pan(self, event, capture_mouse=True):
self._original_xy = (event.x, event.y)
if self.constrain_key is not None:
if getattr(event, self.constrain_key + "_down"):
self.constrain = True
self._auto_constrain = True
self.constrain_direction = None
self.event_state = "panning"
if capture_mouse:
event.window.set_pointer(self.drag_pointer)
event.window.set_mouse_owner(self, event.net_transform())
event.handled = True
return
def _end_pan(self, event):
if self._auto_constrain:
self.constrain = False
self.constrain_direction = None
self.event_state = "normal"
event.window.set_pointer("arrow")
if event.window.mouse_owner == self:
event.window.set_mouse_owner(None)
event.handled = True
return
# EOF
|