src/eric7/HexEdit/HexEditUndoStack.py

branch
eric7
changeset 9221
bf71ee032bb4
parent 9209
b99e7fd55fd3
child 9473
3f23dbf37dbe
equal deleted inserted replaced
9220:e9e7eca7efee 9221:bf71ee032bb4
14 14
15 class HexEditCommand(enum.Enum): 15 class HexEditCommand(enum.Enum):
16 """ 16 """
17 Class implementing the edit commands. 17 Class implementing the edit commands.
18 """ 18 """
19
19 INSERT = 0 20 INSERT = 0
20 REMOVEAT = 1 21 REMOVEAT = 1
21 OVERWRITE = 2 22 OVERWRITE = 2
22 23
23 24
24 class HexEditUndoCommand(QUndoCommand): 25 class HexEditUndoCommand(QUndoCommand):
25 """ 26 """
26 Class implementing the Undo command. 27 Class implementing the Undo command.
27 """ 28 """
29
28 def __init__(self, chunks, cmd, pos, newByte, parent=None): 30 def __init__(self, chunks, cmd, pos, newByte, parent=None):
29 """ 31 """
30 Constructor 32 Constructor
31 33
32 @param chunks reference to the data container 34 @param chunks reference to the data container
33 @type HexEditChunks 35 @type HexEditChunks
34 @param cmd edit command 36 @param cmd edit command
35 @type HexEditCommand 37 @type HexEditCommand
36 @param pos edit position 38 @param pos edit position
39 @type int (range 0 to 255) 41 @type int (range 0 to 255)
40 @param parent reference to the parent command 42 @param parent reference to the parent command
41 @type QUndoCommand 43 @type QUndoCommand
42 """ 44 """
43 super().__init__(parent) 45 super().__init__(parent)
44 46
45 self.__chunks = chunks 47 self.__chunks = chunks
46 self._pos = pos 48 self._pos = pos
47 self._newByte = newByte 49 self._newByte = newByte
48 self._cmd = cmd 50 self._cmd = cmd
49 51
50 self.__wasChanged = False 52 self.__wasChanged = False
51 self.__oldByte = 0 53 self.__oldByte = 0
52 54
53 def undo(self): 55 def undo(self):
54 """ 56 """
55 Public method to undo the command. 57 Public method to undo the command.
56 """ 58 """
57 if self._cmd == HexEditCommand.INSERT: 59 if self._cmd == HexEditCommand.INSERT:
60 self.__chunks.overwrite(self._pos, self.__oldByte) 62 self.__chunks.overwrite(self._pos, self.__oldByte)
61 self.__chunks.setDataChanged(self._pos, self.__wasChanged) 63 self.__chunks.setDataChanged(self._pos, self.__wasChanged)
62 elif self._cmd == HexEditCommand.REMOVEAT: 64 elif self._cmd == HexEditCommand.REMOVEAT:
63 self.__chunks.insert(self._pos, self.__oldByte) 65 self.__chunks.insert(self._pos, self.__oldByte)
64 self.__chunks.setDataChanged(self._pos, self.__wasChanged) 66 self.__chunks.setDataChanged(self._pos, self.__wasChanged)
65 67
66 def redo(self): 68 def redo(self):
67 """ 69 """
68 Public method to redo the command. 70 Public method to redo the command.
69 """ 71 """
70 if self._cmd == HexEditCommand.INSERT: 72 if self._cmd == HexEditCommand.INSERT:
75 self.__chunks.overwrite(self._pos, self._newByte) 77 self.__chunks.overwrite(self._pos, self._newByte)
76 elif self._cmd == HexEditCommand.REMOVEAT: 78 elif self._cmd == HexEditCommand.REMOVEAT:
77 self.__oldByte = self.__chunks[self._pos] 79 self.__oldByte = self.__chunks[self._pos]
78 self.__wasChanged = self.__chunks.dataChanged(self._pos) 80 self.__wasChanged = self.__chunks.dataChanged(self._pos)
79 self.__chunks.removeAt(self._pos) 81 self.__chunks.removeAt(self._pos)
80 82
81 def mergeWith(self, command): 83 def mergeWith(self, command):
82 """ 84 """
83 Public method to merge this command with another one. 85 Public method to merge this command with another one.
84 86
85 @param command reference to the command to merge with 87 @param command reference to the command to merge with
86 @type QUndoCommand 88 @type QUndoCommand
87 @return flag indicating a successful merge 89 @return flag indicating a successful merge
88 @rtype bool 90 @rtype bool
89 """ 91 """
90 result = False 92 result = False
91 93
92 if ( 94 if (
93 self._cmd != HexEditCommand.REMOVEAT and 95 self._cmd != HexEditCommand.REMOVEAT
94 command._cmd == HexEditCommand.OVERWRITE and 96 and command._cmd == HexEditCommand.OVERWRITE
95 command._pos == self._pos 97 and command._pos == self._pos
96 ): 98 ):
97 self._newByte = command._newByte 99 self._newByte = command._newByte
98 result = True 100 result = True
99 101
100 return result 102 return result
101 103
102 def id(self): 104 def id(self):
103 """ 105 """
104 Public method to get the ID of this undo command class. 106 Public method to get the ID of this undo command class.
105 107
106 @return ID of the undo command class 108 @return ID of the undo command class
107 @rtype int 109 @rtype int
108 """ 110 """
109 return 4242 111 return 4242
110 112
111 113
112 class HexEditUndoStack(QUndoStack): 114 class HexEditUndoStack(QUndoStack):
113 """ 115 """
114 Class implementing an Undo stack for the hex edit widget. 116 Class implementing an Undo stack for the hex edit widget.
115 """ 117 """
118
116 def __init__(self, chunks, parent=None): 119 def __init__(self, chunks, parent=None):
117 """ 120 """
118 Constructor 121 Constructor
119 122
120 @param chunks reference to the data container 123 @param chunks reference to the data container
121 @type HexEditChunks 124 @type HexEditChunks
122 @param parent reference to the parent object 125 @param parent reference to the parent object
123 @type QObject 126 @type QObject
124 """ 127 """
125 super().__init__(parent) 128 super().__init__(parent)
126 129
127 self.__chunks = chunks 130 self.__chunks = chunks
128 self.__parent = parent 131 self.__parent = parent
129 132
130 def insert(self, pos, data): 133 def insert(self, pos, data):
131 """ 134 """
132 Public method to insert a byte. 135 Public method to insert a byte.
133 136
134 @param pos position to insert at 137 @param pos position to insert at
135 @type int 138 @type int
136 @param data byte to be inserted 139 @param data byte to be inserted
137 @type int (range 0 to 255) 140 @type int (range 0 to 255)
138 """ 141 """
139 if pos >= 0 and pos <= self.__chunks.size(): 142 if pos >= 0 and pos <= self.__chunks.size():
140 uc = HexEditUndoCommand( 143 uc = HexEditUndoCommand(self.__chunks, HexEditCommand.INSERT, pos, data)
141 self.__chunks, HexEditCommand.INSERT, pos, data)
142 self.push(uc) 144 self.push(uc)
143 145
144 def insertByteArray(self, pos, byteArray): 146 def insertByteArray(self, pos, byteArray):
145 """ 147 """
146 Public method to insert bytes. 148 Public method to insert bytes.
147 149
148 @param pos position to insert at 150 @param pos position to insert at
149 @type int 151 @type int
150 @param byteArray data to be inserted 152 @param byteArray data to be inserted
151 @type byteArray or QByteArray 153 @type byteArray or QByteArray
152 """ 154 """
153 ba = bytearray(byteArray) 155 ba = bytearray(byteArray)
154 156
155 if pos >= 0 and pos <= self.__chunks.size(): 157 if pos >= 0 and pos <= self.__chunks.size():
156 txt = self.tr("Inserting %n byte(s)", "", len(ba)) 158 txt = self.tr("Inserting %n byte(s)", "", len(ba))
157 self.beginMacro(txt) 159 self.beginMacro(txt)
158 for idx in range(len(ba)): 160 for idx in range(len(ba)):
159 uc = HexEditUndoCommand( 161 uc = HexEditUndoCommand(
160 self.__chunks, HexEditCommand.INSERT, pos + idx, ba[idx]) 162 self.__chunks, HexEditCommand.INSERT, pos + idx, ba[idx]
163 )
161 self.push(uc) 164 self.push(uc)
162 self.endMacro() 165 self.endMacro()
163 166
164 def removeAt(self, pos, length=1): 167 def removeAt(self, pos, length=1):
165 """ 168 """
166 Public method to remove bytes. 169 Public method to remove bytes.
167 170
168 @param pos position to remove bytes from 171 @param pos position to remove bytes from
169 @type int 172 @type int
170 @param length amount of bytes to remove 173 @param length amount of bytes to remove
171 @type int 174 @type int
172 """ 175 """
173 if pos >= 0 and pos <= self.__chunks.size(): 176 if pos >= 0 and pos <= self.__chunks.size():
174 if length == 1: 177 if length == 1:
175 uc = HexEditUndoCommand( 178 uc = HexEditUndoCommand(self.__chunks, HexEditCommand.REMOVEAT, pos, 0)
176 self.__chunks, HexEditCommand.REMOVEAT, pos, 0)
177 self.push(uc) 179 self.push(uc)
178 else: 180 else:
179 txt = self.tr("Deleting %n byte(s)", "", length) 181 txt = self.tr("Deleting %n byte(s)", "", length)
180 self.beginMacro(txt) 182 self.beginMacro(txt)
181 for _cnt in range(length): 183 for _cnt in range(length):
182 uc = HexEditUndoCommand( 184 uc = HexEditUndoCommand(
183 self.__chunks, HexEditCommand.REMOVEAT, pos, 0) 185 self.__chunks, HexEditCommand.REMOVEAT, pos, 0
186 )
184 self.push(uc) 187 self.push(uc)
185 self.endMacro() 188 self.endMacro()
186 189
187 def overwrite(self, pos, data): 190 def overwrite(self, pos, data):
188 """ 191 """
189 Public method to replace a byte. 192 Public method to replace a byte.
190 193
191 @param pos position to replace the byte at 194 @param pos position to replace the byte at
192 @type int 195 @type int
193 @param data byte to replace with 196 @param data byte to replace with
194 @type int (range 0 to 255) 197 @type int (range 0 to 255)
195 """ 198 """
196 if pos >= 0 and pos <= self.__chunks.size(): 199 if pos >= 0 and pos <= self.__chunks.size():
197 uc = HexEditUndoCommand( 200 uc = HexEditUndoCommand(self.__chunks, HexEditCommand.OVERWRITE, pos, data)
198 self.__chunks, HexEditCommand.OVERWRITE, pos, data)
199 self.push(uc) 201 self.push(uc)
200 202
201 def overwriteByteArray(self, pos, length, byteArray): 203 def overwriteByteArray(self, pos, length, byteArray):
202 """ 204 """
203 Public method to replace bytes. 205 Public method to replace bytes.
204 206
205 @param pos position to replace the bytes at 207 @param pos position to replace the bytes at
206 @type int 208 @type int
207 @param length amount of bytes to replace 209 @param length amount of bytes to replace
208 @type int 210 @type int
209 @param byteArray bytes to replace with 211 @param byteArray bytes to replace with
210 @type bytearray or QByteArray 212 @type bytearray or QByteArray
211 """ 213 """
212 ba = bytearray(byteArray) 214 ba = bytearray(byteArray)
213 215
214 if pos >= 0 and pos <= self.__chunks.size(): 216 if pos >= 0 and pos <= self.__chunks.size():
215 txt = self.tr("Inserting %n byte(s)", "", len(ba)) 217 txt = self.tr("Inserting %n byte(s)", "", len(ba))
216 self.beginMacro(txt) 218 self.beginMacro(txt)
217 self.removeAt(pos, length) 219 self.removeAt(pos, length)
218 self.insertByteArray(pos, ba) 220 self.insertByteArray(pos, ba)

eric ide

mercurial