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