-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an UndoTool that reacts to key presses.
- Loading branch information
1 parent
40efe34
commit 2496492
Showing
2 changed files
with
67 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters