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