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