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