Skip to content

Commit

Permalink
Add an UndoTool that reacts to key presses.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Jan 12, 2015
1 parent 40efe34 commit 2496492
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 7 deletions.
46 changes: 46 additions & 0 deletions enable/tools/apptools/undo_tool.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# (C) Copyright 2015 Enthought, Inc., Austin, TX
# All right reserved.
#
# This file is open source software distributed according to the terms in
# LICENSE.txt
#

from __future__ import (division, absolute_import, print_function,
unicode_literals)

# Enthought library imports
from traits.api import Instance, List

# Local library imports
from enable.base_tool import KeySpec
from .command_tool import BaseUndoTool


# default undo/redo/clear key specifications
ctrl_z = KeySpec('z', 'control', ignore=['shift', 'alt'])
ctrl_shift_z = KeySpec('z', 'control', 'shift', ignore=['alt'])


class UndoTool(BaseUndoTool):
""" Tool that triggers undo or redo when keys are pressed """

#: the key sequences which trigger undo actions
undo_keys = List(Instance(KeySpec), [ctrl_z])

#: the key sequences which trigger redo actions
redo_keys = List(Instance(KeySpec), [ctrl_shift_z])

def normal_key_pressed(self, event):
""" Respond to key presses which match either the undo or redo keys """
if self.undo_manager is not None:
for key in self.undo_keys:
if key.match(event):
self.undo_manager.undo()
event.handled = True
return
for key in self.redo_keys:
if key.match(event):
self.undo_manager.redo()
event.handled = True
return
28 changes: 21 additions & 7 deletions examples/enable/tools/apptools/undoable_move_tool.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,23 @@
from apptools.undo.api import (CommandStack, ICommandStack, IUndoManager,
UndoManager)
from apptools.undo.action.api import UndoAction, RedoAction
from pyface.api import ApplicationWindow, GUI
from pyface.action.api import Action, Group, MenuBarManager, MenuManager
from traits.api import Instance

from enable.api import Container, Window
from enable.api import Container, Window, KeySpec
from enable.example_application import DemoApplication, demo_main
from enable.primitives.api import Box
from enable.tools.apptools.move_command_tool import MoveCommandTool
from enable.tools.apptools.undo_tool import UndoTool


class UndoableMoveApplication(DemoApplication):
""" Example of using a MoveCommandTool with undo/redo support. """
""" Example of using a MoveCommandTool with undo/redo support.
You can use left/right arrow keys to step through the move history, or
use the standard undo/redo menu items.
"""

#: The apptools undo manager the application uses.
undo_manager = Instance(IUndoManager)
Expand All @@ -45,13 +50,22 @@ class UndoableMoveApplication(DemoApplication):

def _create_window(self):
box = Box(bounds=[100,100], position=[50,50], color='red')
tool = MoveCommandTool(component=box,
command_stack=self.command_stack)
box.tools.append(tool)

move_tool = MoveCommandTool(component=box,
command_stack=self.command_stack)
box.tools.append(move_tool)

container = Container(bounds=[600, 600])
container.add(box)
return Window(self.control, -1, component=container)

undo_tool = UndoTool(component=container,
undo_manager=self.undo_manager,
undo_keys=[KeySpec('Left')],
redo_keys=[KeySpec('Right')])
container.tools.append(undo_tool)

window = Window(self.control, -1, component=container)
return window

#-------------------------------------------------------------------------
# Traits handlers
Expand Down

0 comments on commit 2496492

Please sign in to comment.