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) |