133
|
1 import sys
|
132
|
2 from PyQt4.QtCore import *
|
|
3 from PyQt4.QtGui import *
|
|
4
|
133
|
5 BYTES_PER_LINE = 8
|
136
|
6 GAP = 12
|
|
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
|
|
22 self.setFont(QFont('Courier', 18))
|
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)
|
|
40 self.cursorX = self.xposHex + x * self.charWidth
|
136
|
41 self.cursorY = int(position / (2 * BYTES_PER_LINE)) * self.charHeight + 2
|
133
|
42 self.blinkcursor = True
|
|
43 self.update()
|
135
|
44 def getCursorPosition(self):
|
|
45 return self.cursorPosition
|
|
46 CursorPosition = property(getCursorPosition, setCursorPosition)
|
136
|
47 def getOffset(self):
|
|
48 return self.offset
|
|
49 def setOffset(self, off):
|
|
50 self.offset = off
|
|
51 self.update()
|
|
52 Offset = property(getOffset, 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):
|
136
|
74 painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index + self.Offset))
|
133
|
75 xpos = self.xposHex
|
136
|
76 xposAscii = self.xposAscii
|
133
|
77 for colIndex in range(BYTES_PER_LINE):
|
|
78 if index + colIndex < len(self.Data):
|
135
|
79 b = self.Data[index + colIndex]
|
136
|
80 painter.drawText(xpos, ypos, '{0:02X}'.format(b))
|
|
81 painter.drawText(xposAscii, ypos, asciiChar(b))
|
|
82 xpos += 2 * chw
|
|
83 xposAscii += chw
|
|
84 ypos += chh
|
133
|
85 # cursor
|
|
86 if self.blinkcursor:
|
136
|
87 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black)
|
133
|
88 def keyPressEvent(self, event):
|
|
89 if event.matches(QKeySequence.MoveToNextChar):
|
135
|
90 self.CursorPosition += 1
|
133
|
91 if event.matches(QKeySequence.MoveToPreviousChar):
|
135
|
92 self.CursorPosition -= 1
|
133
|
93 if event.matches(QKeySequence.MoveToNextLine):
|
135
|
94 self.CursorPosition += 2 * BYTES_PER_LINE
|
133
|
95 if event.matches(QKeySequence.MoveToPreviousLine):
|
135
|
96 self.CursorPosition -= 2 * BYTES_PER_LINE
|
136
|
97 if event.matches(QKeySequence.MoveToNextPage):
|
|
98 rows = int(self.scrollArea.viewport().height() / self.charHeight)
|
|
99 self.CursorPosition += (rows - 1) * 2 * BYTES_PER_LINE
|
|
100 if event.matches(QKeySequence.MoveToPreviousPage):
|
|
101 rows = int(self.scrollArea.viewport().height() / self.charHeight)
|
|
102 self.CursorPosition -= (rows - 1) * 2 * BYTES_PER_LINE
|
135
|
103 char = event.text().lower()
|
|
104 if char and char in '0123456789abcdef':
|
|
105 i = int(self.CursorPosition / 2)
|
|
106 hb = self.CursorPosition % 2
|
|
107 v = int(char, 16)
|
|
108 if hb == 0:
|
|
109 # high half byte
|
|
110 self.data[i] = (self.data[i] & 0xF) | (v << 4)
|
|
111 else:
|
|
112 self.data[i] = (self.data[i] & 0xF0) | v
|
|
113 self.CursorPosition += 1
|
|
114 self.scrollArea.ensureVisible(self.cursorX, self.cursorY, self.charWidth, self.charHeight + 2)
|
134
|
115 self.update()
|
135
|
116 def cursorPositionAt(self, pos):
|
|
117 """ Calculate cursor position at a certain point """
|
|
118 if pos.x() > self.xposHex and pos.x() < self.xposAscii:
|
|
119 x = (pos.x() - self.xposHex) / self.charWidth
|
|
120 y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE
|
|
121 return x + y
|
|
122 return 0
|
|
123 def mousePressEvent(self, event):
|
|
124 cpos = self.cursorPositionAt(event.pos())
|
|
125 self.setCursorPosition(cpos)
|
133
|
126 def adjust(self):
|
|
127 self.charHeight = self.fontMetrics().height()
|
|
128 self.charWidth = self.fontMetrics().width('x')
|
136
|
129 self.xposAddr = GAP
|
133
|
130 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
|
136
|
131 self.xposAscii = self.xposHex + BYTES_PER_LINE * 2 * self.charWidth + GAP
|
|
132 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP
|
|
133 self.setMinimumWidth(self.xposEnd)
|
|
134 sbw = self.scrollArea.verticalScrollBar().width()
|
|
135 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5)
|
133
|
136 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight)
|
136
|
137 self.scrollArea.setMinimumHeight(self.charHeight * 8)
|
133
|
138 self.update()
|
|
139 def getData(self):
|
|
140 return self.data
|
|
141 def setData(self, d):
|
|
142 self.data = d
|
|
143 self.adjust()
|
|
144 self.setCursorPosition(0)
|
|
145 Data = property(getData, setData)
|
|
146
|
132
|
147 class HexEdit(QScrollArea):
|
|
148 def __init__(self):
|
|
149 super().__init__()
|
134
|
150 self.bv = BinViewer(self)
|
133
|
151 self.setWidget(self.bv)
|
135
|
152 self.setWidgetResizable(True)
|
136
|
153 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
|
133
|
154 self.setFocusPolicy(Qt.NoFocus)
|
132
|
155
|
133
|
156 if __name__ == '__main__':
|
|
157 app = QApplication(sys.argv)
|
|
158 he = HexEdit()
|
|
159 he.show()
|
135
|
160 he.bv.Data = bytearray(range(100)) * 8 + b'hjkfd'
|
133
|
161 app.exec()
|
|
162
|