annotate python/hexviewer.py @ 109:ad14c7c52589

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