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