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):
|
133
|
11 super().__init__()
|
|
12 self.setFont(QFont('Courier', 10))
|
|
13 self.setFocusPolicy(Qt.StrongFocus)
|
|
14 self.blinkcursor = False
|
134
|
15 self.cursorX = 0
|
|
16 self.cursorY = 0
|
|
17 self.scrollArea = scrollArea
|
133
|
18 self.Data = bytearray()
|
|
19 t = QTimer(self)
|
|
20 t.timeout.connect(self.updateCursor)
|
|
21 t.setInterval(500)
|
|
22 t.start()
|
|
23 def updateCursor(self):
|
|
24 self.blinkcursor = not self.blinkcursor
|
|
25 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
|
|
26 def setCursorPosition(self, position):
|
|
27 if position > len(self.Data) * 2 - 1:
|
|
28 position = len(self.Data) * 2 - 1
|
|
29 if position < 0:
|
|
30 position = 0
|
|
31 self.cursorPosition = position
|
|
32 x = position % (2 * BYTES_PER_LINE)
|
|
33 self.cursorX = self.xposHex + x * self.charWidth
|
|
34 self.cursorY = int(position / (2 * BYTES_PER_LINE)) * self.charHeight + 4
|
|
35 self.blinkcursor = True
|
|
36 self.update()
|
|
37 def paintEvent(self, event):
|
|
38 painter = QPainter(self)
|
|
39 # Background:
|
|
40 painter.fillRect(event.rect(), self.palette().color(QPalette.Base))
|
|
41 painter.fillRect(QRect(self.xposAddr, event.rect().top(), self.xposHex, self.height()), Qt.yellow)
|
|
42
|
|
43 painter.setPen(Qt.black)
|
|
44 # first and last index
|
|
45 firstIndex = (int(event.rect().top() / self.charHeight) - self.charHeight) * BYTES_PER_LINE
|
|
46 if firstIndex < 0:
|
|
47 firstIndex = 0
|
|
48 lastIndex = (int(event.rect().bottom() / self.charHeight) + self.charHeight) * BYTES_PER_LINE
|
|
49 if lastIndex < 0:
|
|
50 lastIndex = 0
|
|
51 yposStart = int(firstIndex / BYTES_PER_LINE) * self.charHeight + self.charHeight
|
|
52
|
|
53 ypos = yposStart
|
|
54 for index in range(firstIndex, lastIndex, BYTES_PER_LINE):
|
134
|
55 painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index))
|
|
56
|
133
|
57 xpos = self.xposHex
|
|
58 for colIndex in range(BYTES_PER_LINE):
|
|
59 if index + colIndex < len(self.Data):
|
|
60 b = '{0:02X}'.format(self.Data[index + colIndex])
|
|
61 painter.drawText(xpos, ypos, b)
|
|
62 xpos += 2 * self.charWidth
|
|
63 ypos += self.charHeight
|
|
64
|
|
65 # cursor
|
|
66 if self.blinkcursor:
|
|
67 painter.fillRect(self.cursorX, self.cursorY + self.charHeight - 2, self.charWidth, 2, Qt.blue)
|
|
68 def keyPressEvent(self, event):
|
|
69 if event.matches(QKeySequence.MoveToNextChar):
|
134
|
70 self.setCursorPosition(self.cursorPosition + 1)
|
133
|
71 if event.matches(QKeySequence.MoveToPreviousChar):
|
134
|
72 self.setCursorPosition(self.cursorPosition - 1)
|
133
|
73 if event.matches(QKeySequence.MoveToNextLine):
|
134
|
74 self.setCursorPosition(self.cursorPosition + 2 * BYTES_PER_LINE)
|
133
|
75 if event.matches(QKeySequence.MoveToPreviousLine):
|
134
|
76 self.setCursorPosition(self.cursorPosition - 2 * BYTES_PER_LINE)
|
|
77 self.ensureVisible()
|
|
78 self.update()
|
|
79 def ensureVisible(self):
|
|
80 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight, 3, self.charHeight / 2 + 2)
|
133
|
81 def adjust(self):
|
|
82 self.charHeight = self.fontMetrics().height()
|
|
83 self.charWidth = self.fontMetrics().width('x')
|
|
84 self.xposAddr = 2
|
|
85 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
|
|
86 self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP
|
|
87 self.setMinimumWidth(self.xposAscii + self.charWidth * BYTES_PER_LINE)
|
|
88 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight)
|
|
89 self.update()
|
|
90 def getBytes(self, offset):
|
|
91 return bytes()
|
|
92 def setHexFile(self, hf):
|
|
93 self.hexfile = hf
|
|
94 self.update()
|
|
95 def getData(self):
|
|
96 return self.data
|
|
97 def setData(self, d):
|
|
98 self.data = d
|
|
99 self.adjust()
|
|
100 self.setCursorPosition(0)
|
|
101 Data = property(getData, setData)
|
|
102
|
132
|
103 class HexEdit(QScrollArea):
|
|
104 def __init__(self):
|
|
105 super().__init__()
|
133
|
106 self.setWidgetResizable(True)
|
134
|
107 self.bv = BinViewer(self)
|
133
|
108 self.setWidget(self.bv)
|
|
109 self.setFocusPolicy(Qt.NoFocus)
|
132
|
110
|
133
|
111 if __name__ == '__main__':
|
|
112 app = QApplication(sys.argv)
|
|
113 he = HexEdit()
|
|
114 he.show()
|
|
115 he.bv.Data = bytearray(range(100)) * 8
|
|
116 app.exec()
|
|
117
|