annotate python/hexedit.py @ 132:205578c96a79

Moved hexview to seperate class
author Windel Bouwman
date Sun, 20 Jan 2013 17:45:45 +0100
parents
children a816c683c1c0
rev   line source
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
1
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
2 from PyQt4.QtCore import *
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
3 from PyQt4.QtGui import *
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
4
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
5 class HexEdit(QScrollArea):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
6 def __init__(self):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
7 super().__init__()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
8
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
9 class BinViewer(QWidget):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
10 """
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
11 The binviewer consists of a hex view, a ascii view
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
12 and perhaps others..
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
13 """
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
14 def __init__(self):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
15 super().__init__()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
16 self.setFont(QFont('Courier'))
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
17 self.fontHeight = self.fontMetrics().height()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
18 self.fontWidth = self.fontMetrics().width('x')
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
19 self.hexfile = hexfile.HexFile()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
20 def paintEvent(self, event):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
21 painter = QPainter(self)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
22 br = QBrush(Qt.lightGray)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
23 painter.setBrush(br)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
24 h = self.height()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
25 addressWidth = self.fontWidth * 8
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
26 painter.drawRect(2, 2, addressWidth, h)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
27 br2 = QBrush(Qt.yellow)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
28 painter.setBrush(br2)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
29 w = self.width()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
30 byteWidth = self.fontWidth * (16 * 3 - 1)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
31 painter.drawRect(addressWidth + 4, 2, byteWidth, h)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
32 asciiWidth = self.fontWidth * 16
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
33 br2 = QBrush(Qt.gray)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
34 painter.setBrush(br2)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
35 painter.drawRect(addressWidth + byteWidth + 4, 2, asciiWidth, h)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
36 rows = int(h / self.fontHeight) + 1
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
37 offset = 0
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
38 for r in range(1, rows):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
39 bts = self.getBytes(offset + r - 1)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
40 addr = '{0:08X}'.format(r << 16)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
41 painter.drawText(2, 2 + self.fontHeight * r, addr)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
42 x = addressWidth + 4
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
43 for b in bts:
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
44 b = '{0:02X}'.format(b)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
45 painter.drawText(x, 2 + self.fontHeight * r, b)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
46 x += 3 * self.fontWidth
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
47 x = addressWidth + byteWidth + 4
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
48 for b in bts:
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
49 b = '{0}'.format(chr(b))
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
50 painter.drawText(x, 2 + self.fontHeight * r, b)
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
51 x += 1 * self.fontWidth
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
52 def getBytes(self, offset):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
53 if self.hexfile.regions:
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
54 r = self.hexfile.regions[0]
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
55 chunks = [r.data[p:p+16] for p in range(0, len(r.data), 16)]
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
56 if len(chunks) > offset:
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
57 return chunks[offset]
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
58 return bytes()
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
59 def setHexFile(self, hf):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
60 self.hexfile = hf
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
61 self.update()