142
|
1 #!/usr/bin/python
|
|
2
|
133
|
3 import sys
|
343
|
4 from qtwrapper import QtGui, QtCore, QtWidgets, Qt, abspath, uic
|
333
|
5
|
132
|
6
|
279
|
7 BYTES_PER_LINE, GAP = 8, 12
|
136
|
8
|
|
9 def clamp(minimum, x, maximum):
|
343
|
10 return max(minimum, min(x, maximum))
|
136
|
11
|
|
12 def asciiChar(v):
|
343
|
13 if v < 0x20 or v > 0x7e:
|
|
14 return '.'
|
|
15 else:
|
|
16 return chr(v)
|
133
|
17
|
333
|
18 class BinViewer(QtWidgets.QWidget):
|
133
|
19 """ The view has an address, hex byte and ascii column """
|
134
|
20 def __init__(self, scrollArea):
|
135
|
21 super().__init__(scrollArea)
|
|
22 self.scrollArea = scrollArea
|
333
|
23 self.setFont(QtGui.QFont('Courier', 16))
|
133
|
24 self.setFocusPolicy(Qt.StrongFocus)
|
|
25 self.blinkcursor = False
|
136
|
26 self.cursorX = self.cursorY = 0
|
134
|
27 self.scrollArea = scrollArea
|
133
|
28 self.Data = bytearray()
|
136
|
29 self.Offset = 0
|
333
|
30 t = QtCore.QTimer(self)
|
133
|
31 t.timeout.connect(self.updateCursor)
|
|
32 t.setInterval(500)
|
|
33 t.start()
|
333
|
34
|
133
|
35 def updateCursor(self):
|
|
36 self.blinkcursor = not self.blinkcursor
|
|
37 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
|
333
|
38
|
133
|
39 def setCursorPosition(self, position):
|
136
|
40 position = clamp(0, int(position), len(self.Data) * 2 - 1)
|
133
|
41 self.cursorPosition = position
|
|
42 x = position % (2 * BYTES_PER_LINE)
|
137
|
43 x = x + int(x / 2) # Create a gap between hex values
|
133
|
44 self.cursorX = self.xposHex + x * self.charWidth
|
137
|
45 y = int(position / (2 * BYTES_PER_LINE))
|
|
46 self.cursorY = y * self.charHeight + 2
|
133
|
47 self.blinkcursor = True
|
|
48 self.update()
|
333
|
49
|
135
|
50 def getCursorPosition(self):
|
|
51 return self.cursorPosition
|
|
52 CursorPosition = property(getCursorPosition, setCursorPosition)
|
136
|
53 def setOffset(self, off):
|
|
54 self.offset = off
|
|
55 self.update()
|
333
|
56
|
139
|
57 Offset = property(lambda self: self.offset, setOffset)
|
133
|
58 def paintEvent(self, event):
|
136
|
59 # Helper variables:
|
|
60 er = event.rect()
|
|
61 chw, chh = self.charWidth, self.charHeight
|
333
|
62 painter = QtGui.QPainter(self)
|
133
|
63 # Background:
|
333
|
64 painter.fillRect(er, self.palette().color(QtGui.QPalette.Base))
|
|
65 painter.fillRect(QtCore.QRect(self.xposAddr, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
|
135
|
66 painter.setPen(Qt.gray)
|
|
67 x = self.xposAscii - (GAP / 2)
|
136
|
68 painter.drawLine(x, er.top(), x, er.bottom())
|
|
69 x = self.xposEnd - (GAP / 2)
|
|
70 painter.drawLine(x, er.top(), x, er.bottom())
|
133
|
71 # first and last index
|
136
|
72 firstIndex = max((int(er.top() / chh) - chh) * BYTES_PER_LINE, 0)
|
|
73 lastIndex = max((int(er.bottom() / chh) + chh) * BYTES_PER_LINE, 0)
|
|
74 yposStart = int(firstIndex / BYTES_PER_LINE) * chh + chh
|
|
75 # Draw contents:
|
|
76 painter.setPen(Qt.black)
|
133
|
77 ypos = yposStart
|
|
78 for index in range(firstIndex, lastIndex, BYTES_PER_LINE):
|
140
|
79 painter.setPen(Qt.black)
|
136
|
80 painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index + self.Offset))
|
133
|
81 xpos = self.xposHex
|
136
|
82 xposAscii = self.xposAscii
|
133
|
83 for colIndex in range(BYTES_PER_LINE):
|
|
84 if index + colIndex < len(self.Data):
|
135
|
85 b = self.Data[index + colIndex]
|
140
|
86 bo = self.originalData[index + colIndex]
|
|
87 if b == bo:
|
|
88 painter.setPen(Qt.black)
|
|
89 else:
|
|
90 painter.setPen(Qt.red)
|
136
|
91 painter.drawText(xpos, ypos, '{0:02X}'.format(b))
|
|
92 painter.drawText(xposAscii, ypos, asciiChar(b))
|
137
|
93 xpos += 3 * chw
|
136
|
94 xposAscii += chw
|
|
95 ypos += chh
|
133
|
96 # cursor
|
|
97 if self.blinkcursor:
|
136
|
98 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black)
|
333
|
99
|
133
|
100 def keyPressEvent(self, event):
|
343
|
101 if event.matches(QtGui.QKeySequence.MoveToNextChar):
|
135
|
102 self.CursorPosition += 1
|
343
|
103 if event.matches(QtGui.QKeySequence.MoveToPreviousChar):
|
135
|
104 self.CursorPosition -= 1
|
343
|
105 if event.matches(QtGui.QKeySequence.MoveToNextLine):
|
135
|
106 self.CursorPosition += 2 * BYTES_PER_LINE
|
343
|
107 if event.matches(QtGui.QKeySequence.MoveToPreviousLine):
|
135
|
108 self.CursorPosition -= 2 * BYTES_PER_LINE
|
343
|
109 if event.matches(QtGui.QKeySequence.MoveToNextPage):
|
136
|
110 rows = int(self.scrollArea.viewport().height() / self.charHeight)
|
|
111 self.CursorPosition += (rows - 1) * 2 * BYTES_PER_LINE
|
343
|
112 if event.matches(QtGui.QKeySequence.MoveToPreviousPage):
|
136
|
113 rows = int(self.scrollArea.viewport().height() / self.charHeight)
|
|
114 self.CursorPosition -= (rows - 1) * 2 * BYTES_PER_LINE
|
135
|
115 char = event.text().lower()
|
|
116 if char and char in '0123456789abcdef':
|
|
117 i = int(self.CursorPosition / 2)
|
|
118 hb = self.CursorPosition % 2
|
|
119 v = int(char, 16)
|
|
120 if hb == 0:
|
|
121 # high half byte
|
|
122 self.data[i] = (self.data[i] & 0xF) | (v << 4)
|
|
123 else:
|
|
124 self.data[i] = (self.data[i] & 0xF0) | v
|
|
125 self.CursorPosition += 1
|
137
|
126 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4)
|
134
|
127 self.update()
|
333
|
128
|
139
|
129 def setCursorPositionAt(self, pos):
|
135
|
130 """ Calculate cursor position at a certain point """
|
|
131 if pos.x() > self.xposHex and pos.x() < self.xposAscii:
|
139
|
132 x = round((2 * (pos.x() - self.xposHex)) / (self.charWidth * 3))
|
135
|
133 y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE
|
139
|
134 self.setCursorPosition(x + y)
|
333
|
135
|
135
|
136 def mousePressEvent(self, event):
|
139
|
137 self.setCursorPositionAt(event.pos())
|
333
|
138
|
133
|
139 def adjust(self):
|
|
140 self.charHeight = self.fontMetrics().height()
|
|
141 self.charWidth = self.fontMetrics().width('x')
|
136
|
142 self.xposAddr = GAP
|
133
|
143 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
|
137
|
144 self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP
|
136
|
145 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP
|
|
146 self.setMinimumWidth(self.xposEnd)
|
145
|
147 if self.isVisible():
|
|
148 sbw = self.scrollArea.verticalScrollBar().width()
|
|
149 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5)
|
139
|
150 r = len(self.Data) % BYTES_PER_LINE
|
|
151 r = 1 if r > 0 else 0
|
|
152 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + r) * self.charHeight + 4)
|
136
|
153 self.scrollArea.setMinimumHeight(self.charHeight * 8)
|
133
|
154 self.update()
|
333
|
155
|
145
|
156 def showEvent(self, e):
|
|
157 self.adjust()
|
|
158 super().showEvent(e)
|
333
|
159
|
133
|
160 def setData(self, d):
|
140
|
161 self.data = bytearray(d)
|
|
162 self.originalData = bytearray(d)
|
133
|
163 self.adjust()
|
|
164 self.setCursorPosition(0)
|
139
|
165 Data = property(lambda self: self.data, setData)
|
133
|
166
|
333
|
167
|
|
168 class HexEdit(QtWidgets.QScrollArea):
|
132
|
169 def __init__(self):
|
|
170 super().__init__()
|
134
|
171 self.bv = BinViewer(self)
|
133
|
172 self.setWidget(self.bv)
|
135
|
173 self.setWidgetResizable(True)
|
136
|
174 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
|
133
|
175 self.setFocusPolicy(Qt.NoFocus)
|
132
|
176
|
333
|
177
|
|
178 class HexEditor(QtWidgets.QMainWindow):
|
343
|
179 def __init__(self):
|
140
|
180 super().__init__()
|
343
|
181 uic.loadUi(abspath('hexeditor.ui'), baseinstance=self)
|
140
|
182 self.he = HexEdit()
|
|
183 self.setCentralWidget(self.he)
|
|
184 self.actionOpen.triggered.connect(self.doOpen)
|
|
185 self.actionSave.triggered.connect(self.doSave)
|
|
186 self.actionSaveAs.triggered.connect(self.doSaveAs)
|
|
187 self.fileName = None
|
|
188 self.updateControls()
|
343
|
189
|
|
190 def updateControls(self):
|
140
|
191 s = True if self.fileName else False
|
|
192 self.actionSave.setEnabled(s)
|
|
193 self.actionSaveAs.setEnabled(s)
|
343
|
194
|
|
195 def doOpen(self):
|
|
196 filename = QtWidgets.QFileDialog.getOpenFileName(self)
|
140
|
197 if filename:
|
|
198 with open(filename, 'rb') as f:
|
|
199 self.he.bv.Data = f.read()
|
|
200 self.fileName = filename
|
|
201 self.updateControls()
|
343
|
202
|
|
203 def doSave(self):
|
140
|
204 self.updateControls()
|
343
|
205
|
|
206 def doSaveAs(self):
|
140
|
207 filename = QFileDialog.getSaveFileName(self)
|
|
208 if filename:
|
|
209 with open(filename, 'wb') as f:
|
|
210 f.write(self.he.bv.Data)
|
|
211 self.fileName = filename
|
|
212 self.updateControls()
|
|
213
|
333
|
214
|
133
|
215 if __name__ == '__main__':
|
343
|
216 app = QtWidgets.QApplication(sys.argv)
|
|
217 he = HexEditor()
|
|
218 he.show()
|
|
219 #he.bv.Data = bytearray(range(100)) * 8 + b'x'
|
|
220 app.exec()
|