Top

undo_manager module

class UndoManager(object):
    '''
    UndoManager is a simplistic reusable component to support an undo-redo mechanism.
    UndoableActions can be added in the manager, which gives a centered interface for
    performing their undo and redo actions.
    '''

        
    def __init__(self):
        # Undo and redo stacks which contain the UndoableAction objects
        # When a new action is made it is put in the undo stack. When an operation
        # is undone, it is places in the redo stack.
        self.undo_stack = []
        self.redo_stack = []
    
    
    def can_undo(self):
        '''
        Tests if an undo operation can be performed at this moment.

        @return: boolean value telling if an undo is possible to perform.
        '''
        if self.undo_stack:
            return True
        return False 

    
    def can_redo(self):
        '''
        Tests if an redo operation can be performed at this moment.

        @return: boolean value telling if a redo is possible to perform.
        '''
        if self.redo_stack:
            return True
        return False 

    
    def add_action(self, action ):
        '''
        Adds a new undoable action into this Undo Manager.

        @param action: the UndoableAction to be added.
        '''
        self.redo_stack  = []       
        self.undo_stack.append(action)
    
    def undo(self):
        '''
        Undoes one action from the undo stack. The undone action then is moved to
        the redo stack.
        '''
        action = self.undo_stack.pop()
        action.undo()
        self.redo_stack.append(action)

    def redo(self):
        '''
        Redoes one action from the redo stack. The redone action then is moved to
        the undo stack.
        '''
        action = self.redo_stack.pop()
        action.redo()
        self.undo_stack.append(action)
        

    def describe_undoable_action(self):
        '''
        Describes an available undoable action.

        @return: A description of the undoable action for menus etc.
        '''
        if self.can_undo():
            return "Undo "+self.undo_stack[-1].get_description()
        return "Nothing to undo."
    

    def describe_redoable_action(self):
        '''
        Describes an available redoable action.

        @return: A description of the redoable action for menus etc.
        '''
        if self.can_redo():
            return "Redo "+self.redo_stack[-1].get_description()
        return "Nothing to redo."

Classes

class UndoManager

UndoManager is a simplistic reusable component to support an undo-redo mechanism. UndoableActions can be added in the manager, which gives a centered interface for performing their undo and redo actions.

class UndoManager(object):
    '''
    UndoManager is a simplistic reusable component to support an undo-redo mechanism.
    UndoableActions can be added in the manager, which gives a centered interface for
    performing their undo and redo actions.
    '''

        
    def __init__(self):
        # Undo and redo stacks which contain the UndoableAction objects
        # When a new action is made it is put in the undo stack. When an operation
        # is undone, it is places in the redo stack.
        self.undo_stack = []
        self.redo_stack = []
    
    
    def can_undo(self):
        '''
        Tests if an undo operation can be performed at this moment.

        @return: boolean value telling if an undo is possible to perform.
        '''
        if self.undo_stack:
            return True
        return False 

    
    def can_redo(self):
        '''
        Tests if an redo operation can be performed at this moment.

        @return: boolean value telling if a redo is possible to perform.
        '''
        if self.redo_stack:
            return True
        return False 

    
    def add_action(self, action ):
        '''
        Adds a new undoable action into this Undo Manager.

        @param action: the UndoableAction to be added.
        '''
        self.redo_stack  = []       
        self.undo_stack.append(action)
    
    def undo(self):
        '''
        Undoes one action from the undo stack. The undone action then is moved to
        the redo stack.
        '''
        action = self.undo_stack.pop()
        action.undo()
        self.redo_stack.append(action)

    def redo(self):
        '''
        Redoes one action from the redo stack. The redone action then is moved to
        the undo stack.
        '''
        action = self.redo_stack.pop()
        action.redo()
        self.undo_stack.append(action)
        

    def describe_undoable_action(self):
        '''
        Describes an available undoable action.

        @return: A description of the undoable action for menus etc.
        '''
        if self.can_undo():
            return "Undo "+self.undo_stack[-1].get_description()
        return "Nothing to undo."
    

    def describe_redoable_action(self):
        '''
        Describes an available redoable action.

        @return: A description of the redoable action for menus etc.
        '''
        if self.can_redo():
            return "Redo "+self.redo_stack[-1].get_description()
        return "Nothing to redo."

Ancestors (in MRO)

Instance variables

var redo_stack

var undo_stack

Methods

def __init__(

self)

def __init__(self):
    # Undo and redo stacks which contain the UndoableAction objects
    # When a new action is made it is put in the undo stack. When an operation
    # is undone, it is places in the redo stack.
    self.undo_stack = []
    self.redo_stack = []

def add_action(

self, action)

Adds a new undoable action into this Undo Manager.

@param action: the UndoableAction to be added.

def add_action(self, action ):
    '''
    Adds a new undoable action into this Undo Manager.
    @param action: the UndoableAction to be added.
    '''
    self.redo_stack  = []       
    self.undo_stack.append(action)

def can_redo(

self)

Tests if an redo operation can be performed at this moment.

@return: boolean value telling if a redo is possible to perform.

def can_redo(self):
    '''
    Tests if an redo operation can be performed at this moment.
    @return: boolean value telling if a redo is possible to perform.
    '''
    if self.redo_stack:
        return True
    return False 

def can_undo(

self)

Tests if an undo operation can be performed at this moment.

@return: boolean value telling if an undo is possible to perform.

def can_undo(self):
    '''
    Tests if an undo operation can be performed at this moment.
    @return: boolean value telling if an undo is possible to perform.
    '''
    if self.undo_stack:
        return True
    return False 

def describe_redoable_action(

self)

Describes an available redoable action.

@return: A description of the redoable action for menus etc.

def describe_redoable_action(self):
    '''
    Describes an available redoable action.
    @return: A description of the redoable action for menus etc.
    '''
    if self.can_redo():
        return "Redo "+self.redo_stack[-1].get_description()
    return "Nothing to redo."

def describe_undoable_action(

self)

Describes an available undoable action.

@return: A description of the undoable action for menus etc.

def describe_undoable_action(self):
    '''
    Describes an available undoable action.
    @return: A description of the undoable action for menus etc.
    '''
    if self.can_undo():
        return "Undo "+self.undo_stack[-1].get_description()
    return "Nothing to undo."

def redo(

self)

Redoes one action from the redo stack. The redone action then is moved to the undo stack.

def redo(self):
    '''
    Redoes one action from the redo stack. The redone action then is moved to
    the undo stack.
    '''
    action = self.redo_stack.pop()
    action.redo()
    self.undo_stack.append(action)

def undo(

self)

Undoes one action from the undo stack. The undone action then is moved to the redo stack.

def undo(self):
    '''
    Undoes one action from the undo stack. The undone action then is moved to
    the redo stack.
    '''
    action = self.undo_stack.pop()
    action.undo()
    self.redo_stack.append(action)