File: undo_manager.py

package info (click to toggle)
python-pyface 8.0.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 13,944 kB
  • sloc: python: 54,107; makefile: 82
file content (134 lines) | stat: -rw-r--r-- 4,477 bytes parent folder | download
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# (C) Copyright 2008-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

# ------------------------------------------------------------------------------
# Copyright (c) 2008, Riverbank Computing Limited
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in enthought/LICENSE.txt and may be redistributed only
# under the conditions described in the aforementioned license.  The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
# Thanks for using Enthought open source!
#
# Author: Riverbank Computing Limited
# Description: <Enthought undo package component>
# ------------------------------------------------------------------------------

# Enthought library imports.
from traits.api import (
    Bool,
    Event,
    HasTraits,
    Instance,
    Int,
    Property,
    Str,
    provides,
    observe,
)

# Local imports.
from .i_undo_manager import IUndoManager


@provides(IUndoManager)
class UndoManager(HasTraits):
    """The UndoManager class is the default implementation of the
    IUndoManager interface.
    """

    #### 'IUndoManager' interface #############################################

    #: This is the currently active command stack and may be None. Typically it
    #: is set when some sort of editor becomes active.
    active_stack = Instance("pyface.undo.api.ICommandStack")

    #: This reflects the clean state of the currently active command stack. It
    #: is intended to support a "document modified" indicator in the GUI. It is
    #: maintained by the undo manager.
    active_stack_clean = Property(Bool)

    #: This is the name of the command that can be redone. It will be empty if
    #: there is no command that can be redone. It is maintained by the undo
    #: manager.
    redo_name = Property(Str)

    #: This is the sequence number of the next command to be performed. It is
    #: incremented immediately before a command is invoked (by its 'do()'
    #: method).
    sequence_nr = Int()

    #: This event is fired when the index of a command stack changes. The value
    #: of the event is the stack that has changed. Note that it may not be the
    #: active stack.
    stack_updated = Event()

    #: This is the name of the command that can be undone. It will be empty if
    #: there is no command that can be undone. It is maintained by the undo
    #: manager.
    undo_name = Property(Str)

    ###########################################################################
    # 'IUndoManager' interface.
    ###########################################################################

    def redo(self):
        """ Redo the last undone command of the active command stack. """

        if self.active_stack is not None:
            self.active_stack.redo()

    def undo(self):
        """ Undo the last command of the active command stack. """

        if self.active_stack is not None:
            self.active_stack.undo()

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

    @observe("active_stack")
    def _update_stack_updated(self, event):
        """ Handle a different stack becoming active. """
        new = event.new
        # Pretend that the stack contents have changed.
        self.stack_updated = new

    def _get_active_stack_clean(self):
        """ Get the current clean state. """

        if self.active_stack is None:
            active_stack_clean = True
        else:
            active_stack_clean = self.active_stack.clean

        return active_stack_clean

    def _get_redo_name(self):
        """ Get the current redo name. """

        if self.active_stack is None:
            redo_name = ""
        else:
            redo_name = self.active_stack.redo_name

        return redo_name

    def _get_undo_name(self):
        """ Get the current undo name. """

        if self.active_stack is None:
            undo_name = ""
        else:
            undo_name = self.active_stack.undo_name

        return undo_name