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
|
from traits.api import Bool, Enum, Tuple
from drag_tool import DragTool
class MoveTool(DragTool):
""" Generic tool for moving a component's position relative to its container
"""
drag_button = Enum("left", "right")
# Should the moved component be raised to the top of its container's
# list of components? This is only recommended for overlaying containers
# and canvases, but generally those are the only ones in which the
# MoveTool will be useful.
auto_raise = Bool(True)
# The last cursor position we saw; used during drag to compute deltas
_prev_pos = Tuple(0, 0)
def is_draggable(self, x, y):
if self.component:
c = self.component
return (c.x <= x <= c.x2) and (c.y <= y <= c.y2)
else:
return False
def drag_start(self, event):
if self.component:
self._prev_pos = (event.x, event.y)
self.component._layout_needed = True
if self.auto_raise:
# Push the component to the top of its container's list
self.component.container.raise_component(self.component)
event.window.set_mouse_owner(self, event.net_transform())
event.handled = True
return
def dragging(self, event):
if self.component:
dx = event.x - self._prev_pos[0]
dy = event.y - self._prev_pos[1]
pos = self.component.position
self.component.position = [pos[0] + dx, pos[1] + dy]
self.component._layout_needed = True
self.component.request_redraw()
self._prev_pos = (event.x, event.y)
event.handled = True
return
|