annotate python/hexedit.py @ 134:1a50d6db24ed

Changes on hexedit
author Windel Bouwman
date Mon, 21 Jan 2013 20:24:40 +0100
parents a816c683c1c0
children 01d88140dc03
rev   line source
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
1 import sys
132
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
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
5 BYTES_PER_LINE = 8
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
6 GAP = 16
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
7
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
8 class BinViewer(QWidget):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
9 """ The view has an address, hex byte and ascii column """
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
10 def __init__(self, scrollArea):
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
11 super().__init__()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
12 self.setFont(QFont('Courier', 10))
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
13 self.setFocusPolicy(Qt.StrongFocus)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
14 self.blinkcursor = False
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
15 self.cursorX = 0
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
16 self.cursorY = 0
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
17 self.scrollArea = scrollArea
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
18 self.Data = bytearray()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
19 t = QTimer(self)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
20 t.timeout.connect(self.updateCursor)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
21 t.setInterval(500)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
22 t.start()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
23 def updateCursor(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
24 self.blinkcursor = not self.blinkcursor
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
25 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
26 def setCursorPosition(self, position):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
27 if position > len(self.Data) * 2 - 1:
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
28 position = len(self.Data) * 2 - 1
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
29 if position < 0:
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
30 position = 0
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
31 self.cursorPosition = position
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
32 x = position % (2 * BYTES_PER_LINE)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
33 self.cursorX = self.xposHex + x * self.charWidth
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
34 self.cursorY = int(position / (2 * BYTES_PER_LINE)) * self.charHeight + 4
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
35 self.blinkcursor = True
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
36 self.update()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
37 def paintEvent(self, event):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
38 painter = QPainter(self)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
39 # Background:
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
40 painter.fillRect(event.rect(), self.palette().color(QPalette.Base))
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
41 painter.fillRect(QRect(self.xposAddr, event.rect().top(), self.xposHex, self.height()), Qt.yellow)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
42
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
43 painter.setPen(Qt.black)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
44 # first and last index
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
45 firstIndex = (int(event.rect().top() / self.charHeight) - self.charHeight) * BYTES_PER_LINE
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
46 if firstIndex < 0:
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
47 firstIndex = 0
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
48 lastIndex = (int(event.rect().bottom() / self.charHeight) + self.charHeight) * BYTES_PER_LINE
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
49 if lastIndex < 0:
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
50 lastIndex = 0
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
51 yposStart = int(firstIndex / BYTES_PER_LINE) * self.charHeight + self.charHeight
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
52
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
53 ypos = yposStart
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
54 for index in range(firstIndex, lastIndex, BYTES_PER_LINE):
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
55 painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index))
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
56
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
57 xpos = self.xposHex
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
58 for colIndex in range(BYTES_PER_LINE):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
59 if index + colIndex < len(self.Data):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
60 b = '{0:02X}'.format(self.Data[index + colIndex])
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
61 painter.drawText(xpos, ypos, b)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
62 xpos += 2 * self.charWidth
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
63 ypos += self.charHeight
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
64
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
65 # cursor
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
66 if self.blinkcursor:
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
67 painter.fillRect(self.cursorX, self.cursorY + self.charHeight - 2, self.charWidth, 2, Qt.blue)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
68 def keyPressEvent(self, event):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
69 if event.matches(QKeySequence.MoveToNextChar):
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
70 self.setCursorPosition(self.cursorPosition + 1)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
71 if event.matches(QKeySequence.MoveToPreviousChar):
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
72 self.setCursorPosition(self.cursorPosition - 1)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
73 if event.matches(QKeySequence.MoveToNextLine):
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
74 self.setCursorPosition(self.cursorPosition + 2 * BYTES_PER_LINE)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
75 if event.matches(QKeySequence.MoveToPreviousLine):
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
76 self.setCursorPosition(self.cursorPosition - 2 * BYTES_PER_LINE)
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
77 self.ensureVisible()
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
78 self.update()
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
79 def ensureVisible(self):
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
80 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight, 3, self.charHeight / 2 + 2)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
81 def adjust(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
82 self.charHeight = self.fontMetrics().height()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
83 self.charWidth = self.fontMetrics().width('x')
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
84 self.xposAddr = 2
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
85 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
86 self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
87 self.setMinimumWidth(self.xposAscii + self.charWidth * BYTES_PER_LINE)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
88 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
89 self.update()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
90 def getBytes(self, offset):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
91 return bytes()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
92 def setHexFile(self, hf):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
93 self.hexfile = hf
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
94 self.update()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
95 def getData(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
96 return self.data
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
97 def setData(self, d):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
98 self.data = d
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
99 self.adjust()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
100 self.setCursorPosition(0)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
101 Data = property(getData, setData)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
102
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
103 class HexEdit(QScrollArea):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
104 def __init__(self):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
105 super().__init__()
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
106 self.setWidgetResizable(True)
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
107 self.bv = BinViewer(self)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
108 self.setWidget(self.bv)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
109 self.setFocusPolicy(Qt.NoFocus)
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
110
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
111 if __name__ == '__main__':
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
112 app = QApplication(sys.argv)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
113 he = HexEdit()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
114 he.show()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
115 he.bv.Data = bytearray(range(100)) * 8
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
116 app.exec()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
117