-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcommand_manager.py
More file actions
102 lines (86 loc) · 3.44 KB
/
command_manager.py
File metadata and controls
102 lines (86 loc) · 3.44 KB
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
from collections import deque
from PyQt5.QtCore import QObject
class Command:
def execute(self):
"""执行命令,子类必须实现此方法"""
raise NotImplementedError
def undo(self):
"""撤销命令,子类必须实现此方法"""
raise NotImplementedError
def redo(self):
"""重做命令,默认调用execute"""
self.execute()
class CommandManager(QObject):
def __init__(self, max_history=100):
super().__init__()
self.max_history = max_history
self.undo_stack = [] # 撤销栈,保存已执行的命令
self.redo_stack = [] # 重做栈,保存已撤销的命令
self.undo_stack_changed = None # 钩子函数,撤销栈变化时调用
self.redo_stack_changed = None # 钩子函数,重做栈变化时调用
def execute_command(self, command):
"""执行一个新命令"""
command.execute()
self.undo_stack.append(command)
# 限制撤销栈大小
if len(self.undo_stack) > self.max_history:
self.undo_stack.pop(0) # 移除最早的命令
# 清空重做栈
self.redo_stack.clear()
# 强制更新UI状态
print(f"执行命令 - 撤销栈: {len(self.undo_stack)}, 重做栈: {len(self.redo_stack)}")
# 确保回调被调用
if self.undo_stack_changed:
self.undo_stack_changed()
if self.redo_stack_changed:
self.redo_stack_changed()
def undo(self):
"""撤销上一个命令"""
if not self.undo_stack:
print("没有操作可以撤销")
return
command = self.undo_stack.pop()
command.undo()
self.redo_stack.append(command)
# 立即更新UI
print(f"撤销操作 - 撤销栈: {len(self.undo_stack)}, 重做栈: {len(self.redo_stack)}")
# 确保回调被立即调用
if self.undo_stack_changed:
self.undo_stack_changed()
if self.redo_stack_changed:
self.redo_stack_changed()
def redo(self):
"""重做上一个撤销的命令"""
if not self.redo_stack:
print("没有操作可以重做")
return
command = self.redo_stack.pop()
# 确保调用的是redo方法而不是execute方法
if hasattr(command, "redo") and callable(command.redo):
command.redo() # 使用redo方法重做操作
else:
# 如果没有redo方法,则使用execute作为后备
command.execute() # 使用execute方法作为后备
self.undo_stack.append(command)
# 立即更新UI
print(f"重做操作 - 撤销栈: {len(self.undo_stack)}, 重做栈: {len(self.redo_stack)}")
# 确保回调被立即调用
if self.undo_stack_changed:
self.undo_stack_changed()
if self.redo_stack_changed:
self.redo_stack_changed()
def clear(self):
"""清空命令栈"""
self.undo_stack.clear()
self.redo_stack.clear()
# 触发回调
if callable(self.undo_stack_changed):
self.undo_stack_changed()
if callable(self.redo_stack_changed):
self.redo_stack_changed()
def can_undo(self):
"""是否可以撤销"""
return len(self.undo_stack) > 0
def can_redo(self):
"""是否可以重做"""
return len(self.redo_stack) > 0