File: UndoRedoStack.py

package info (click to toggle)
asymptote 3.09%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 41,392 kB
  • sloc: cpp: 282,597; ansic: 69,697; python: 15,252; sh: 5,888; perl: 3,006; makefile: 1,719; lisp: 1,507; yacc: 614; lex: 449; xml: 182; javascript: 79; asm: 8
file content (112 lines) | stat: -rw-r--r-- 3,252 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
#!/usr/bin/env python3
###########################################################################
#
# UndoRedoStack implements the usual undo/redo capabilities of a GUI
#
# Author: Orest Shardt
# Created: July 23, 2007
#
###########################################################################


class action:
    def __init__(self, actions):
        act, inv = actions
        self.act = act
        self.inv = inv

    def undo(self):
        # print ("Undo:",self)
        self.inv()

    def redo(self):
        # print ("Redo:",self)
        self.act()

    def __str__(self):
        return "A generic action"


class beginActionGroup:
    pass


class endActionGroup:
    pass


class actionStack:
    def __init__(self):
        self.clear()
        self.redoStack = []

    def add(self, action_to_add):
        self.undoStack.append(action_to_add)
        # print ("Added",action)
        self.redoStack.clear()

    def undo(self):
        if len(self.undoStack) > 0:
            op = self.undoStack.pop()
            if op is beginActionGroup:
                level = 1
                self.redoStack.append(endActionGroup)
                while level > 0:
                    op = self.undoStack.pop()
                    if op is endActionGroup:
                        level -= 1
                        self.redoStack.append(beginActionGroup)
                    elif op is beginActionGroup:
                        level += 1
                        self.redoStack.append(endActionGroup)
                    else:
                        op.undo()
                        self.redoStack.append(op)
            elif op is endActionGroup:
                raise Exception("endActionGroup without previous beginActionGroup")
            else:
                self.redoStack.append(op)
                op.undo()
                # print ("undid",op)
        else:
            pass  # print ("nothing to undo")

    def redo(self):
        if len(self.redoStack) > 0:
            op = self.redoStack.pop()
            if op is beginActionGroup:
                level = 1
                self.undoStack.append(endActionGroup)
                while level > 0:
                    op = self.redoStack.pop()
                    if op is endActionGroup:
                        level -= 1
                        self.undoStack.append(beginActionGroup)
                    elif op is beginActionGroup:
                        level += 1
                        self.undoStack.append(endActionGroup)
                    else:
                        op.redo()
                        self.undoStack.append(op)
            elif op is endActionGroup:
                raise Exception("endActionGroup without previous beginActionGroup")
            else:
                self.undoStack.append(op)
                op.redo()
                # print ("redid",op)
        else:
            pass  # print ("nothing to redo")

    def setCommitLevel(self):
        self.commitLevel = len(self.undoStack)

    def changesMade(self):
        if len(self.undoStack) != self.commitLevel:
            return True
        else:
            return False

    def clear(self):
        self.redoStack = []
        self.undoStack = []
        self.commitLevel = 0