/usr/lib/python2.7/dist-packages/chaco/tools/better_zoom.py is in python-chaco 4.4.1-1.2.
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 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | import numpy
from chaco.grid_mapper import GridMapper
from enable.api import BaseTool, KeySpec
from traits.api import Enum, Float, Instance, Bool, HasTraits, List
from tool_history_mixin import ToolHistoryMixin
from tool_states import ZoomState, PanState, GroupedToolState, ToolState
class BetterZoom(BaseTool, ToolHistoryMixin):
# Keys to zoom in/out
zoom_in_key = Instance(KeySpec, args=("+",), kw={'ignore': ['shift']})
zoom_out_key = Instance(KeySpec, args=("-",))
# Keys to zoom in/out in x direction only
zoom_in_x_key = Instance(KeySpec, args=("Right", "shift"))
zoom_out_x_key = Instance(KeySpec, args=("Left", "shift"))
# Keys to zoom in/out in y direction only
zoom_in_y_key = Instance(KeySpec, args=("Up", "shift"))
zoom_out_y_key = Instance(KeySpec, args=("Down", "shift"))
# Key to go to the previous state in the history.
prev_state_key = Instance(KeySpec, args=("z", "control"))
# Key to go to the next state in the history.
next_state_key = Instance(KeySpec, args=("y", "control"))
# Enable the mousewheel for zooming?
enable_wheel = Bool(True)
# if the mouse pointer should be used to control the center
# of the zoom action
zoom_to_mouse = Bool(True)
# if the mouse pointer should be used to control the center
# of the zoom action even for key events based zoom
keys_zoom_to_mouse = Bool(True)
# The axis to which the selection made by this tool is perpendicular. This
# only applies in 'range' mode.
axis = Enum("both", "index", "value")
# The maximum ratio between the original data space bounds and the zoomed-in
# data space bounds. If No limit is desired, set to inf
x_max_zoom_factor = Float(1e5)
y_max_zoom_factor = Float(1e5)
# The maximum ratio between the zoomed-out data space bounds and the original
# bounds. If No limit is desired, set to -inf
x_min_zoom_factor = Float(1e-5)
y_min_zoom_factor = Float(1e-5)
# The amount to zoom in by. The zoom out will be inversely proportional
zoom_factor = Float(2.0)
# The zoom factor on each axis
_index_factor = Float(1.0)
_value_factor = Float(1.0)
# inherited from ToolHistoryMixin, but requires instances of ZoomState
_history = List(ToolState, [ZoomState((1.0, 1.0), (1.0, 1.0))])
def _do_zoom(self, new_index_factor, new_value_factor):
if self.zoom_to_mouse:
location = self.position
x_map = self._get_x_mapper()
y_map = self._get_y_mapper()
cx = (x_map.range.high + x_map.range.low)/2
if self._index_factor == new_index_factor:
nextx = cx
else:
x = x_map.map_data(location[0])
nextx = x + (cx - x)*(self._index_factor/new_index_factor)
cy = (y_map.range.high + y_map.range.low)/2
if self._value_factor == new_value_factor:
nexty = cy
else:
y = y_map.map_data(location[1])
nexty = y + (cy - y)*(self._value_factor/new_value_factor)
pan_state = PanState((cx,cy), (nextx, nexty))
zoom_state = ZoomState((self._index_factor, self._value_factor),
(new_index_factor, new_value_factor))
states = GroupedToolState([pan_state, zoom_state])
states.apply(self)
self._append_state(states)
else:
zoom_state = ZoomState((self._index_factor, self._value_factor),
(new_index_factor, new_value_factor))
zoom_state.apply(self)
self._append_state(zoom_state)
#--------------------------------------------------------------------------
# public interface
#--------------------------------------------------------------------------
def zoom_in(self, factor=0):
if factor == 0:
factor = self.zoom_factor
new_index_factor = self._index_factor * factor
new_value_factor = self._value_factor * factor
if self.axis == 'value':
new_index_factor = self._index_factor
elif self.axis == 'index':
new_value_factor = self._value_factor
if self.component.orientation == 'h':
if self._zoom_limit_reached(new_index_factor, 'x'):
return
if self._zoom_limit_reached(new_value_factor, 'y'):
return
else:
if self._zoom_limit_reached(new_index_factor, 'y'):
return
if self._zoom_limit_reached(new_value_factor, 'x'):
return
self._do_zoom(new_index_factor, new_value_factor)
def zoom_out(self, factor=0):
if factor == 0:
factor = self.zoom_factor
new_index_factor = self._index_factor / factor
new_value_factor = self._value_factor / factor
if self.axis == 'value':
new_index_factor = self._index_factor
elif self.axis == 'index':
new_value_factor = self._value_factor
if self.component.orientation == 'h':
if self._zoom_limit_reached(new_index_factor, 'x'):
return
if self._zoom_limit_reached(new_value_factor, 'y'):
return
else:
if self._zoom_limit_reached(new_index_factor, 'y'):
return
if self._zoom_limit_reached(new_value_factor, 'x'):
return
self._do_zoom(new_index_factor, new_value_factor)
def zoom_in_x(self, factor=0):
if factor == 0:
factor = self.zoom_factor
if self.component.orientation == 'h':
new_index_factor = self._index_factor * factor
new_value_factor = self._value_factor
if self._zoom_limit_reached(new_index_factor, 'x'):
return
else:
new_index_factor = self._index_factor
new_value_factor = self._value_factor * factor
if self._zoom_limit_reached(new_value_factor, 'x'):
return
self._do_zoom(new_index_factor, new_value_factor)
def zoom_out_x(self, factor=0):
if factor == 0:
factor = self.zoom_factor
if self.component.orientation == 'h':
new_index_factor = self._index_factor / factor
new_value_factor = self._value_factor
if self._zoom_limit_reached(new_index_factor, 'x'):
return
else:
new_index_factor = self._index_factor
new_value_factor = self._value_factor / factor
if self._zoom_limit_reached(new_value_factor, 'x'):
return
self._do_zoom(new_index_factor, new_value_factor)
def zoom_in_y(self, factor=0):
if factor == 0:
factor = self.zoom_factor
if self.component.orientation == 'v':
new_index_factor = self._index_factor * factor
new_value_factor = self._value_factor
if self._zoom_limit_reached(new_index_factor, 'y'):
return
else:
new_index_factor = self._index_factor
new_value_factor = self._value_factor * factor
if self._zoom_limit_reached(new_value_factor, 'y'):
return
self._do_zoom(new_index_factor, new_value_factor)
def zoom_out_y(self, factor=0):
if factor == 0:
factor = self.zoom_factor
if self.component.orientation == 'v':
new_index_factor = self._index_factor / factor
new_value_factor = self._value_factor
if self._zoom_limit_reached(new_index_factor, 'y'):
return
else:
new_index_factor = self._index_factor
new_value_factor = self._value_factor / factor
if self._zoom_limit_reached(new_value_factor, 'y'):
return
self._do_zoom(new_index_factor, new_value_factor)
#--------------------------------------------------------------------------
# BaseTool interface
#--------------------------------------------------------------------------
def normal_key_pressed(self, event):
""" Handles a key being pressed when the tool is in the 'normal'
state.
"""
if not self.keys_zoom_to_mouse:
self.position = self._center_screen()
if self.zoom_in_key.match(event):
self.zoom_in()
event.handled = True
elif self.zoom_out_key.match(event):
self.zoom_out()
event.handled = True
elif self.zoom_in_x_key.match(event):
self.zoom_in_x(self.zoom_factor)
event.handled = True
elif self.zoom_out_x_key.match(event):
self.zoom_out_x(self.zoom_factor)
event.handled = True
elif self.zoom_in_y_key.match(event):
self.zoom_in_y(self.zoom_factor)
event.handled = True
elif self.zoom_out_y_key.match(event):
self.zoom_out_y(self.zoom_factor)
event.handled = True
ToolHistoryMixin.normal_key_pressed(self, event)
return
def normal_mouse_wheel(self, event):
if not self.enable_wheel:
return
if event.mouse_wheel != 0:
if event.mouse_wheel > 0:
self.zoom_in()
else:
self.zoom_out()
event.handled = True
def normal_mouse_move(self, event):
self.position = (event.x, event.y)
def normal_mouse_enter(self, event):
""" Try to set the focus to the window when the mouse enters, otherwise
the keypress events will not be triggered.
"""
if self.component._window is not None:
self.component._window._set_focus()
#--------------------------------------------------------------------------
# private interface
#--------------------------------------------------------------------------
def _center_screen(self):
return self.component.bounds[0]/2, self.component.bounds[1]/2
def _zoom_limit_reached(self, factor, xy_axis):
""" Returns True if the new low and high exceed the maximum zoom
limits
"""
if xy_axis == 'x':
if factor <= self.x_max_zoom_factor and factor >= self.x_min_zoom_factor:
return False
return True
else:
if factor <= self.y_max_zoom_factor and factor >= self.y_min_zoom_factor:
return False
return True
def _zoom_in_mapper(self, mapper, factor):
high = mapper.range.high
low = mapper.range.low
range = high-low
center = (low + high)/2.0
new_range = range/factor
mapper.range.high = center + new_range/2
mapper.range.low = center - new_range/2
def _get_x_mapper(self):
if isinstance(self.component.index_mapper, GridMapper):
if self.component.orientation == "h":
return self.component.index_mapper._xmapper
return self.component.index_mapper._ymapper
else:
if self.component.orientation == "h":
return self.component.index_mapper
return self.component.value_mapper
def _get_y_mapper(self):
if isinstance(self.component.index_mapper, GridMapper):
if self.component.orientation == "h":
return self.component.index_mapper._ymapper
return self.component.index_mapper._xmapper
else:
if self.component.orientation == "h":
return self.component.value_mapper
return self.component.index_mapper
#--------------------------------------------------------------------------
# ToolHistoryMixin interface
#--------------------------------------------------------------------------
def _next_state_pressed(self):
""" Called when the tool needs to advance to the next state in the
stack.
The **_history_index** will have already been set to the index
corresponding to the next state.
"""
self._current_state().apply(self)
def _prev_state_pressed(self):
""" Called when the tool needs to advance to the previous state in the
stack.
The **_history_index** will have already been set to the index
corresponding to the previous state.
"""
self._history[self._history_index+1].revert(self)
def _reset_state_pressed(self):
""" Called when the tool needs to reset its history.
The history index will have already been set to 0.
"""
for state in self._history[::-1]:
state.revert(self)
self._history = []
|