comparison python/hexviewer.py @ 104:ed230e947dc6

Added hexviewer
author windel
date Sun, 30 Dec 2012 22:31:55 +0100
parents
children 6a303f835c6d
comparison
equal deleted inserted replaced
103:28a35161ef23 104:ed230e947dc6
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()
29 byteWidth = self.fontWidth * 16 * 3
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)
35 for r in range(1, 10):
36 addr = '{0:08X}'.format(r << 16)
37 painter.drawText(2, 2 + self.fontHeight * r, addr)
38 bts = bytes(range(16))
39 x = addressWidth + 4
40 for b in bts:
41 b = '{0:02X}'.format(b)
42 painter.drawText(x, 2 + self.fontHeight * r, b)
43 x += 3 * self.fontWidth
44 x = addressWidth + byteWidth + 4
45 for b in bts:
46 b = '{0}'.format(chr(b))
47 painter.drawText(x, 2 + self.fontHeight * r, b)
48 x += 1 * self.fontWidth
49 def setHexFile(self, hf):
50 self.hexFile = hf
51 self.update()
52
53 class BinViewMain(QMainWindow):
54 def __init__(self):
55 super().__init__()
56 self.bv = BinViewer()
57 self.setCentralWidget(self.bv)
58 self.bv.setHexFile(hexfile.HexFile('audio.hex'))
59 mb = self.menuBar()
60 fileMenu = mb.addMenu("File")
61
62 def addMenuEntry(name, menu, callback, shortcut=None):
63 a = QAction(name, self)
64 menu.addAction(a)
65 a.triggered.connect(callback)
66 if shortcut: a.setShortcut(shortcut)
67 addMenuEntry("Open", fileMenu, self.openFile, QKeySequence(QKeySequence.Open))
68 def openFile(self):
69 filename = QFileDialog.getOpenFileName(self, "Open hex file...", "*.hex", "Intel hexfiles (*.hex)")
70 if filename:
71 h = hexfile.HexFile(filename)
72
73 if __name__ == '__main__':
74 app = QApplication(sys.argv)
75 bv = BinViewMain()
76 bv.show()
77 app.exec_()
78