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