annotate python/hexedit.py @ 133:a816c683c1c0

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