Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 1 addition & 62 deletions apptools/undo/abstract_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,65 +12,4 @@
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

# Enthought library imports.
from traits.api import Any, HasTraits, Str, provides

# Local imports.
from .i_command import ICommand


@provides(ICommand)
class AbstractCommand(HasTraits):
"""The AbstractCommand class is an abstract base class that implements the
ICommand interface.
"""

#### 'ICommand' interface #################################################

# This is the data on which the command operates.
data = Any

# This is the name of the command as it will appear in any GUI element. It
# may include '&' which will be automatically removed whenever it is
# inappropriate.
name = Str

###########################################################################
# 'ICommand' interface.
###########################################################################

def do(self):
"""This is called by the command stack to do the command and to return
any value. The command must save any state necessary for the 'redo()'
and 'undo()' methods to work. The class's __init__() must also ensure
that deep copies of any arguments are made if appropriate. It is
guaranteed that this will only ever be called once and that it will be
called before any call to 'redo()' or 'undo()'.
"""

raise NotImplementedError

def merge(self, other):
"""This is called by the command stack to try and merge another
command with this one. True is returned if the commands were merged.
'other' is the command that is about to be executed. If the commands
are merged then 'other' will discarded and not placed on the command
stack. A subsequent undo or redo of this modified command must have
the same effect as the two original commands.
"""

# By default merges never happen.
return False

def redo(self):
"""This is called by the command stack to redo the command. Any
returned value will replace the value that the command stack references
from the original call to 'do()' or previous call to 'redo()'.
"""

raise NotImplementedError

def undo(self):
""" This is called by the command stack to undo the command. """

raise NotImplementedError
from pyface.undo.api import AbstractCommand
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For maintaining backward compatibility, I'd think the safest thing to do would be from pyface.undo.abstract_command import *, assuming the module is moved to pyface as is. e.g. this breaks from apptools.undo.abstract_command import ICommand. We believe no one does that, but it costs little to do the safer approach. 🤞 this does not break anything.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rahulporuri should we make this change before I push ahead with the release?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'd actually prefer breaking such imports now - which will make the eventual transition to pyface trivial - and cleaner/better because those imports are antipatterns anyway.

72 changes: 3 additions & 69 deletions apptools/undo/action/abstract_command_stack_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,6 @@
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

# Enthought library imports.
from pyface.action.api import Action
from traits.api import Instance

# Local library imports
from ..i_undo_manager import IUndoManager


class AbstractCommandStackAction(Action):
"""The abstract base class for all actions that operate on a command
stack.
"""

#### 'AbstractCommandStackAction' interface ###############################

# The undo manager.
undo_manager = Instance(IUndoManager)

###########################################################################
# 'object' interface.
###########################################################################

def __init__(self, **traits):
""" Initialise the instance. """

super(AbstractCommandStackAction, self).__init__(**traits)

self.undo_manager.on_trait_event(
self._on_stack_updated, "stack_updated"
)

# Update the action to initialise it.
self._update_action()

###########################################################################
# 'Action' interface.
###########################################################################

def destroy(self):
"""Called when the action is no longer required.

By default this method does nothing, but this would be a great place to
unhook trait listeners etc.

"""

self.undo_manager.on_trait_event(
self._on_stack_updated, "stack_updated", remove=True
)

###########################################################################
# Protected interface.
###########################################################################

def _update_action(self):
""" Update the state of the action. """

raise NotImplementedError

###########################################################################
# Private interface.
###########################################################################

def _on_stack_updated(self, stack):
""" Handle changes to the state of a command stack. """

# Ignore unless it is the active stack.
if stack is self.undo_manager.active_stack:
self._update_action()
from pyface.undo.action.abstract_command_stack_action import (
AbstractCommandStackAction
)
8 changes: 5 additions & 3 deletions apptools/undo/action/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

from .command_action import CommandAction
from .redo_action import RedoAction
from .undo_action import UndoAction
from pyface.undo.action.command_action import (
CommandAction,
RedoAction,
UndoAction,
)
45 changes: 1 addition & 44 deletions apptools/undo/action/command_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,47 +12,4 @@
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

# Enthought library imports.
from pyface.action.api import Action
from traits.api import Any, Callable, Instance
from ..i_command_stack import ICommandStack


class CommandAction(Action):
"""The CommandAction class is an Action class that wraps undo/redo
commands. It is only useful for commands that do not take any arguments or
return any result.
"""

#### 'CommandAction' interface ############################################

# The command to create when the action is performed.
command = Callable

# The command stack onto which the command will be pushed when the action
# is performed.
command_stack = Instance(ICommandStack)

# This is the data on which the command operates.
data = Any

###########################################################################
# 'Action' interface.
###########################################################################

def perform(self, event):
"""This is reimplemented to push a new command instance onto the
command stack.
"""

self.command_stack.push(self.command(data=self.data))

def _name_default(self):
""" This gets the action name from the command. """

if self.command:
name = self.command().name
else:
name = ""

return name
from pyface.undo.action.api import CommandAction
36 changes: 1 addition & 35 deletions apptools/undo/action/redo_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,38 +12,4 @@
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

# Local imports.
from .abstract_command_stack_action import AbstractCommandStackAction


class RedoAction(AbstractCommandStackAction):
"""An action that redos the last command undone of the active command
stack.
"""

###########################################################################
# 'Action' interface.
###########################################################################

def perform(self, event):
""" Perform the action. """

self.undo_manager.redo()

###########################################################################
# 'AbstractUndoAction' interface.
###########################################################################

def _update_action(self):
""" Update the state of the action. """

name = self.undo_manager.redo_name

if name:
name = "&Redo " + name
self.enabled = True
else:
name = "&Redo"
self.enabled = False

self.name = name
from pyface.undo.action.api import RedoAction
34 changes: 1 addition & 33 deletions apptools/undo/action/undo_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,4 @@
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

# Local imports.
from .abstract_command_stack_action import AbstractCommandStackAction


class UndoAction(AbstractCommandStackAction):
""" An action that undos the last command of the active command stack. """

###########################################################################
# 'Action' interface.
###########################################################################

def perform(self, event):
""" Perform the action. """

self.undo_manager.undo()

###########################################################################
# 'AbstractUndoAction' interface.
###########################################################################

def _update_action(self):
""" Update the state of the action. """

name = self.undo_manager.undo_name

if name:
name = "&Undo " + name
self.enabled = True
else:
name = "&Undo"
self.enabled = False

self.name = name
from pyface.undo.action.api import UndoAction
14 changes: 8 additions & 6 deletions apptools/undo/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

from .abstract_command import AbstractCommand
from .command_stack import CommandStack
from .i_command import ICommand
from .i_command_stack import ICommandStack
from .i_undo_manager import IUndoManager
from .undo_manager import UndoManager
from pyface.undo.api import (
AbstractCommand,
CommandStack,
ICommand,
ICommandStack,
IUndoManager,
UndoManager,
)
Loading