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