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