104
|
1 #!/usr/bin/python
|
|
2
|
|
3 from PyQt4.QtCore import *
|
|
4 from PyQt4.QtGui import *
|
109
|
5 from qtpropertyviewer import QtPropertyViewer
|
104
|
6 import sys
|
|
7 import hexfile
|
|
8
|
|
9 class BinViewer(QWidget):
|
|
10 """
|
|
11 The binviewer consists of a hex view, a ascii view
|
|
12 and perhaps others..
|
|
13 """
|
|
14 def __init__(self):
|
|
15 super().__init__()
|
|
16 self.setFont(QFont('Courier'))
|
|
17 self.fontHeight = self.fontMetrics().height()
|
|
18 self.fontWidth = self.fontMetrics().width('x')
|
|
19 self.hexfile = hexfile.HexFile()
|
|
20 def paintEvent(self, event):
|
|
21 painter = QPainter(self)
|
|
22 br = QBrush(Qt.lightGray)
|
|
23 painter.setBrush(br)
|
|
24 h = self.height()
|
|
25 addressWidth = self.fontWidth * 8
|
|
26 painter.drawRect(2, 2, addressWidth, h)
|
|
27 br2 = QBrush(Qt.yellow)
|
|
28 painter.setBrush(br2)
|
|
29 w = self.width()
|
105
|
30 byteWidth = self.fontWidth * (16 * 3 - 1)
|
104
|
31 painter.drawRect(addressWidth + 4, 2, byteWidth, h)
|
|
32 asciiWidth = self.fontWidth * 16
|
|
33 br2 = QBrush(Qt.gray)
|
|
34 painter.setBrush(br2)
|
|
35 painter.drawRect(addressWidth + byteWidth + 4, 2, asciiWidth, h)
|
105
|
36 rows = int(h / self.fontHeight) + 1
|
|
37 offset = 0
|
|
38 for r in range(1, rows):
|
|
39 bts = self.getBytes(offset + r - 1)
|
104
|
40 addr = '{0:08X}'.format(r << 16)
|
|
41 painter.drawText(2, 2 + self.fontHeight * r, addr)
|
|
42 x = addressWidth + 4
|
|
43 for b in bts:
|
|
44 b = '{0:02X}'.format(b)
|
|
45 painter.drawText(x, 2 + self.fontHeight * r, b)
|
|
46 x += 3 * self.fontWidth
|
|
47 x = addressWidth + byteWidth + 4
|
|
48 for b in bts:
|
|
49 b = '{0}'.format(chr(b))
|
|
50 painter.drawText(x, 2 + self.fontHeight * r, b)
|
|
51 x += 1 * self.fontWidth
|
105
|
52 def getBytes(self, offset):
|
|
53 if self.hexfile.regions:
|
|
54 r = self.hexfile.regions[0]
|
|
55 chunks = [r.data[p:p+16] for p in range(0, len(r.data), 16)]
|
|
56 if len(chunks) > offset:
|
|
57 return chunks[offset]
|
|
58 return bytes()
|
104
|
59 def setHexFile(self, hf):
|
105
|
60 self.hexfile = hf
|
104
|
61 self.update()
|
|
62
|
|
63 class BinViewMain(QMainWindow):
|
|
64 def __init__(self):
|
|
65 super().__init__()
|
|
66 self.bv = BinViewer()
|
|
67 self.setCentralWidget(self.bv)
|
|
68 mb = self.menuBar()
|
|
69 fileMenu = mb.addMenu("File")
|
|
70
|
|
71 def addMenuEntry(name, menu, callback, shortcut=None):
|
|
72 a = QAction(name, self)
|
|
73 menu.addAction(a)
|
|
74 a.triggered.connect(callback)
|
|
75 if shortcut: a.setShortcut(shortcut)
|
|
76 addMenuEntry("Open", fileMenu, self.openFile, QKeySequence(QKeySequence.Open))
|
|
77 def openFile(self):
|
|
78 filename = QFileDialog.getOpenFileName(self, "Open hex file...", "*.hex", "Intel hexfiles (*.hex)")
|
|
79 if filename:
|
|
80 h = hexfile.HexFile(filename)
|
109
|
81 @pyqtProperty(str)
|
|
82 def leetValue(self):
|
|
83 return '1337'
|
104
|
84
|
|
85 if __name__ == '__main__':
|
|
86 app = QApplication(sys.argv)
|
|
87 bv = BinViewMain()
|
|
88 bv.show()
|
105
|
89 bv.bv.setHexFile(hexfile.HexFile('audio.hex'))
|
109
|
90 qpv = QtPropertyViewer()
|
|
91 qpv.propertyModel.InspectedWidget = bv
|
|
92 qpv.show()
|
104
|
93 app.exec_()
|
|
94
|