annotate python/hexedit.py @ 136:9af544be5d2a

Added hexfile edit
author Windel Bouwman
date Wed, 23 Jan 2013 21:54:14 +0100
parents 01d88140dc03
children 0a540ce31cd5
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
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
6 GAP = 12
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
7
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
8 def clamp(minimum, x, maximum):
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
9 return max(minimum, min(x, maximum))
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
10
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
11 def asciiChar(v):
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
12 if v < 0x20 or v > 0x7e:
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
13 return '.'
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
14 else:
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
15 return chr(v)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
16
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
17 class BinViewer(QWidget):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
18 """ The view has an address, hex byte and ascii column """
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
19 def __init__(self, scrollArea):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
20 super().__init__(scrollArea)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
21 self.scrollArea = scrollArea
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
22 self.setFont(QFont('Courier', 18))
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
23 self.setFocusPolicy(Qt.StrongFocus)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
24 self.blinkcursor = False
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
25 self.cursorX = self.cursorY = 0
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
26 self.scrollArea = scrollArea
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
27 self.Data = bytearray()
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
28 self.Offset = 0
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
29 t = QTimer(self)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
30 t.timeout.connect(self.updateCursor)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
31 t.setInterval(500)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
32 t.start()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
33 def updateCursor(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
34 self.blinkcursor = not self.blinkcursor
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
35 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
36 def setCursorPosition(self, position):
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
37 position = clamp(0, int(position), len(self.Data) * 2 - 1)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
38 self.cursorPosition = position
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
39 x = position % (2 * BYTES_PER_LINE)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
40 self.cursorX = self.xposHex + x * self.charWidth
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
41 self.cursorY = int(position / (2 * BYTES_PER_LINE)) * self.charHeight + 2
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
42 self.blinkcursor = True
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
43 self.update()
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
44 def getCursorPosition(self):
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
45 return self.cursorPosition
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
46 CursorPosition = property(getCursorPosition, setCursorPosition)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
47 def getOffset(self):
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
48 return self.offset
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
49 def setOffset(self, off):
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
50 self.offset = off
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
51 self.update()
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
52 Offset = property(getOffset, setOffset)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
53 def paintEvent(self, event):
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
54 # Helper variables:
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
55 er = event.rect()
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
56 chw, chh = self.charWidth, self.charHeight
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
57 painter = QPainter(self)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
58 # Background:
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
59 painter.fillRect(er, self.palette().color(QPalette.Base))
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
60 painter.fillRect(QRect(self.xposAddr, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
61 painter.setPen(Qt.gray)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
62 x = self.xposAscii - (GAP / 2)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
63 painter.drawLine(x, er.top(), x, er.bottom())
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
64 x = self.xposEnd - (GAP / 2)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
65 painter.drawLine(x, er.top(), x, er.bottom())
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
66 # first and last index
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
67 firstIndex = max((int(er.top() / chh) - chh) * BYTES_PER_LINE, 0)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
68 lastIndex = max((int(er.bottom() / chh) + chh) * BYTES_PER_LINE, 0)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
69 yposStart = int(firstIndex / BYTES_PER_LINE) * chh + chh
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
70 # Draw contents:
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
71 painter.setPen(Qt.black)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
72 ypos = yposStart
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
73 for index in range(firstIndex, lastIndex, BYTES_PER_LINE):
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
74 painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index + self.Offset))
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
75 xpos = self.xposHex
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
76 xposAscii = self.xposAscii
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
77 for colIndex in range(BYTES_PER_LINE):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
78 if index + colIndex < len(self.Data):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
79 b = self.Data[index + colIndex]
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
80 painter.drawText(xpos, ypos, '{0:02X}'.format(b))
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
81 painter.drawText(xposAscii, ypos, asciiChar(b))
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
82 xpos += 2 * chw
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
83 xposAscii += chw
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
84 ypos += chh
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
85 # cursor
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
86 if self.blinkcursor:
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
87 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
88 def keyPressEvent(self, event):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
89 if event.matches(QKeySequence.MoveToNextChar):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
90 self.CursorPosition += 1
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
91 if event.matches(QKeySequence.MoveToPreviousChar):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
92 self.CursorPosition -= 1
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
93 if event.matches(QKeySequence.MoveToNextLine):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
94 self.CursorPosition += 2 * BYTES_PER_LINE
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
95 if event.matches(QKeySequence.MoveToPreviousLine):
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
96 self.CursorPosition -= 2 * BYTES_PER_LINE
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
97 if event.matches(QKeySequence.MoveToNextPage):
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
98 rows = int(self.scrollArea.viewport().height() / self.charHeight)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
99 self.CursorPosition += (rows - 1) * 2 * BYTES_PER_LINE
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
100 if event.matches(QKeySequence.MoveToPreviousPage):
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
101 rows = int(self.scrollArea.viewport().height() / self.charHeight)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
102 self.CursorPosition -= (rows - 1) * 2 * BYTES_PER_LINE
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
103 char = event.text().lower()
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
104 if char and char in '0123456789abcdef':
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
105 i = int(self.CursorPosition / 2)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
106 hb = self.CursorPosition % 2
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
107 v = int(char, 16)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
108 if hb == 0:
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
109 # high half byte
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
110 self.data[i] = (self.data[i] & 0xF) | (v << 4)
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
111 else:
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
112 self.data[i] = (self.data[i] & 0xF0) | v
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
113 self.CursorPosition += 1
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
114 self.scrollArea.ensureVisible(self.cursorX, self.cursorY, self.charWidth, self.charHeight + 2)
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
115 self.update()
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
116 def cursorPositionAt(self, pos):
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
117 """ Calculate cursor position at a certain point """
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
118 if pos.x() > self.xposHex and pos.x() < self.xposAscii:
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
119 x = (pos.x() - self.xposHex) / self.charWidth
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
120 y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
121 return x + y
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
122 return 0
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
123 def mousePressEvent(self, event):
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
124 cpos = self.cursorPositionAt(event.pos())
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
125 self.setCursorPosition(cpos)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
126 def adjust(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
127 self.charHeight = self.fontMetrics().height()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
128 self.charWidth = self.fontMetrics().width('x')
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
129 self.xposAddr = GAP
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
130 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
131 self.xposAscii = self.xposHex + BYTES_PER_LINE * 2 * self.charWidth + GAP
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
132 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
133 self.setMinimumWidth(self.xposEnd)
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
134 sbw = self.scrollArea.verticalScrollBar().width()
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
135 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
136 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
137 self.scrollArea.setMinimumHeight(self.charHeight * 8)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
138 self.update()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
139 def getData(self):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
140 return self.data
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
141 def setData(self, d):
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
142 self.data = d
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
143 self.adjust()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
144 self.setCursorPosition(0)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
145 Data = property(getData, setData)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
146
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
147 class HexEdit(QScrollArea):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
148 def __init__(self):
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
149 super().__init__()
134
1a50d6db24ed Changes on hexedit
Windel Bouwman
parents: 133
diff changeset
150 self.bv = BinViewer(self)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
151 self.setWidget(self.bv)
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
152 self.setWidgetResizable(True)
136
9af544be5d2a Added hexfile edit
Windel Bouwman
parents: 135
diff changeset
153 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
154 self.setFocusPolicy(Qt.NoFocus)
132
205578c96a79 Moved hexview to seperate class
Windel Bouwman
parents:
diff changeset
155
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
156 if __name__ == '__main__':
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
157 app = QApplication(sys.argv)
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
158 he = HexEdit()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
159 he.show()
135
01d88140dc03 Improve hexedit
Windel Bouwman
parents: 134
diff changeset
160 he.bv.Data = bytearray(range(100)) * 8 + b'hjkfd'
133
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
161 app.exec()
a816c683c1c0 Fixes in hexedit
Windel Bouwman
parents: 132
diff changeset
162