annotate python/hexviewer.py @ 105:6a303f835c6d

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