eric6/HexEdit/HexEditUndoStack.py

changeset 8265
0090cfa83159
parent 8222
5994b80b8760
equal deleted inserted replaced
8264:f95dde35d0ab 8265:0090cfa83159
5 5
6 """ 6 """
7 Module implementing the Undo stack for the hex edit widget. 7 Module implementing the Undo stack for the hex edit widget.
8 """ 8 """
9 9
10 from enum import Enum 10 import enum
11 11
12 from PyQt5.QtWidgets import QUndoStack, QUndoCommand 12 from PyQt5.QtWidgets import QUndoStack, QUndoCommand
13 13
14 14
15 class HexEditCommand(Enum): 15 class HexEditCommand(enum.Enum):
16 """ 16 """
17 Class implementing the edit comands. 17 Class implementing the edit commands.
18 """ 18 """
19 Insert = 0 19 INSERT = 0
20 RemoveAt = 1 20 REMOVEAT = 1
21 Overwrite = 2 21 OVERWRITE = 2
22 22
23 23
24 class HexEditUndoCommand(QUndoCommand): 24 class HexEditUndoCommand(QUndoCommand):
25 """ 25 """
26 Class implementing the Undo command. 26 Class implementing the Undo command.
52 52
53 def undo(self): 53 def undo(self):
54 """ 54 """
55 Public method to undo the command. 55 Public method to undo the command.
56 """ 56 """
57 if self._cmd == HexEditCommand.Insert: 57 if self._cmd == HexEditCommand.INSERT:
58 self.__chunks.removeAt(self._pos) 58 self.__chunks.removeAt(self._pos)
59 elif self._cmd == HexEditCommand.Overwrite: 59 elif self._cmd == HexEditCommand.OVERWRITE:
60 self.__chunks.overwrite(self._pos, self.__oldByte) 60 self.__chunks.overwrite(self._pos, self.__oldByte)
61 self.__chunks.setDataChanged(self._pos, self.__wasChanged) 61 self.__chunks.setDataChanged(self._pos, self.__wasChanged)
62 elif self._cmd == HexEditCommand.RemoveAt: 62 elif self._cmd == HexEditCommand.REMOVEAT:
63 self.__chunks.insert(self._pos, self.__oldByte) 63 self.__chunks.insert(self._pos, self.__oldByte)
64 self.__chunks.setDataChanged(self._pos, self.__wasChanged) 64 self.__chunks.setDataChanged(self._pos, self.__wasChanged)
65 65
66 def redo(self): 66 def redo(self):
67 """ 67 """
68 Public method to redo the command. 68 Public method to redo the command.
69 """ 69 """
70 if self._cmd == HexEditCommand.Insert: 70 if self._cmd == HexEditCommand.INSERT:
71 self.__chunks.insert(self._pos, self._newByte) 71 self.__chunks.insert(self._pos, self._newByte)
72 elif self._cmd == HexEditCommand.Overwrite: 72 elif self._cmd == HexEditCommand.OVERWRITE:
73 self.__oldByte = self.__chunks[self._pos] 73 self.__oldByte = self.__chunks[self._pos]
74 self.__wasChanged = self.__chunks.dataChanged(self._pos) 74 self.__wasChanged = self.__chunks.dataChanged(self._pos)
75 self.__chunks.overwrite(self._pos, self._newByte) 75 self.__chunks.overwrite(self._pos, self._newByte)
76 elif self._cmd == HexEditCommand.RemoveAt: 76 elif self._cmd == HexEditCommand.REMOVEAT:
77 self.__oldByte = self.__chunks[self._pos] 77 self.__oldByte = self.__chunks[self._pos]
78 self.__wasChanged = self.__chunks.dataChanged(self._pos) 78 self.__wasChanged = self.__chunks.dataChanged(self._pos)
79 self.__chunks.removeAt(self._pos) 79 self.__chunks.removeAt(self._pos)
80 80
81 def mergeWith(self, command): 81 def mergeWith(self, command):
88 @rtype bool 88 @rtype bool
89 """ 89 """
90 result = False 90 result = False
91 91
92 if ( 92 if (
93 self._cmd != HexEditCommand.RemoveAt and 93 self._cmd != HexEditCommand.REMOVEAT and
94 command._cmd == HexEditCommand.Overwrite and 94 command._cmd == HexEditCommand.OVERWRITE and
95 command._pos == self._pos 95 command._pos == self._pos
96 ): 96 ):
97 self._newByte = command._newByte 97 self._newByte = command._newByte
98 result = True 98 result = True
99 99
136 @param data byte to be inserted 136 @param data byte to be inserted
137 @type int (range 0 to 255) 137 @type int (range 0 to 255)
138 """ 138 """
139 if pos >= 0 and pos <= self.__chunks.size(): 139 if pos >= 0 and pos <= self.__chunks.size():
140 uc = HexEditUndoCommand( 140 uc = HexEditUndoCommand(
141 self.__chunks, HexEditCommand.Insert, pos, data) 141 self.__chunks, HexEditCommand.INSERT, pos, data)
142 self.push(uc) 142 self.push(uc)
143 143
144 def insertByteArray(self, pos, byteArray): 144 def insertByteArray(self, pos, byteArray):
145 """ 145 """
146 Public method to insert bytes. 146 Public method to insert bytes.
155 if pos >= 0 and pos <= self.__chunks.size(): 155 if pos >= 0 and pos <= self.__chunks.size():
156 txt = self.tr("Inserting %n byte(s)", "", len(ba)) 156 txt = self.tr("Inserting %n byte(s)", "", len(ba))
157 self.beginMacro(txt) 157 self.beginMacro(txt)
158 for idx in range(len(ba)): 158 for idx in range(len(ba)):
159 uc = HexEditUndoCommand( 159 uc = HexEditUndoCommand(
160 self.__chunks, HexEditCommand.Insert, pos + idx, ba[idx]) 160 self.__chunks, HexEditCommand.INSERT, pos + idx, ba[idx])
161 self.push(uc) 161 self.push(uc)
162 self.endMacro() 162 self.endMacro()
163 163
164 def removeAt(self, pos, length=1): 164 def removeAt(self, pos, length=1):
165 """ 165 """
171 @type int 171 @type int
172 """ 172 """
173 if pos >= 0 and pos <= self.__chunks.size(): 173 if pos >= 0 and pos <= self.__chunks.size():
174 if length == 1: 174 if length == 1:
175 uc = HexEditUndoCommand( 175 uc = HexEditUndoCommand(
176 self.__chunks, HexEditCommand.RemoveAt, pos, 0) 176 self.__chunks, HexEditCommand.REMOVEAT, pos, 0)
177 self.push(uc) 177 self.push(uc)
178 else: 178 else:
179 txt = self.tr("Deleting %n byte(s)", "", length) 179 txt = self.tr("Deleting %n byte(s)", "", length)
180 self.beginMacro(txt) 180 self.beginMacro(txt)
181 for _cnt in range(length): 181 for _cnt in range(length):
182 uc = HexEditUndoCommand( 182 uc = HexEditUndoCommand(
183 self.__chunks, HexEditCommand.RemoveAt, pos, 0) 183 self.__chunks, HexEditCommand.REMOVEAT, pos, 0)
184 self.push(uc) 184 self.push(uc)
185 self.endMacro() 185 self.endMacro()
186 186
187 def overwrite(self, pos, data): 187 def overwrite(self, pos, data):
188 """ 188 """
193 @param data byte to replace with 193 @param data byte to replace with
194 @type int (range 0 to 255) 194 @type int (range 0 to 255)
195 """ 195 """
196 if pos >= 0 and pos <= self.__chunks.size(): 196 if pos >= 0 and pos <= self.__chunks.size():
197 uc = HexEditUndoCommand( 197 uc = HexEditUndoCommand(
198 self.__chunks, HexEditCommand.Overwrite, pos, data) 198 self.__chunks, HexEditCommand.OVERWRITE, pos, data)
199 self.push(uc) 199 self.push(uc)
200 200
201 def overwriteByteArray(self, pos, length, byteArray): 201 def overwriteByteArray(self, pos, length, byteArray):
202 """ 202 """
203 Public method to replace bytes. 203 Public method to replace bytes.

eric ide

mercurial