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