comparison python/hexedit.py @ 134:1a50d6db24ed

Changes on hexedit
author Windel Bouwman
date Mon, 21 Jan 2013 20:24:40 +0100
parents a816c683c1c0
children 01d88140dc03
comparison
equal deleted inserted replaced
133:a816c683c1c0 134:1a50d6db24ed
5 BYTES_PER_LINE = 8 5 BYTES_PER_LINE = 8
6 GAP = 16 6 GAP = 16
7 7
8 class BinViewer(QWidget): 8 class BinViewer(QWidget):
9 """ The view has an address, hex byte and ascii column """ 9 """ The view has an address, hex byte and ascii column """
10 def __init__(self): 10 def __init__(self, scrollArea):
11 super().__init__() 11 super().__init__()
12 self.setFont(QFont('Courier', 10)) 12 self.setFont(QFont('Courier', 10))
13 self.setFocusPolicy(Qt.StrongFocus) 13 self.setFocusPolicy(Qt.StrongFocus)
14 self.blinkcursor = False 14 self.blinkcursor = False
15 self.cursorX = 0
16 self.cursorY = 0
17 self.scrollArea = scrollArea
15 self.Data = bytearray() 18 self.Data = bytearray()
16 t = QTimer(self) 19 t = QTimer(self)
17 t.timeout.connect(self.updateCursor) 20 t.timeout.connect(self.updateCursor)
18 t.setInterval(500) 21 t.setInterval(500)
19 t.start() 22 t.start()
20 self.cursorX = 0
21 self.cursorY = 0
22 def updateCursor(self): 23 def updateCursor(self):
23 self.blinkcursor = not self.blinkcursor 24 self.blinkcursor = not self.blinkcursor
24 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight) 25 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
25 def setCursorPosition(self, position): 26 def setCursorPosition(self, position):
26 if position > len(self.Data) * 2 - 1: 27 if position > len(self.Data) * 2 - 1:
49 lastIndex = 0 50 lastIndex = 0
50 yposStart = int(firstIndex / BYTES_PER_LINE) * self.charHeight + self.charHeight 51 yposStart = int(firstIndex / BYTES_PER_LINE) * self.charHeight + self.charHeight
51 52
52 ypos = yposStart 53 ypos = yposStart
53 for index in range(firstIndex, lastIndex, BYTES_PER_LINE): 54 for index in range(firstIndex, lastIndex, BYTES_PER_LINE):
55 painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index))
56
54 xpos = self.xposHex 57 xpos = self.xposHex
55 for colIndex in range(BYTES_PER_LINE): 58 for colIndex in range(BYTES_PER_LINE):
56 if index + colIndex < len(self.Data): 59 if index + colIndex < len(self.Data):
57 b = '{0:02X}'.format(self.Data[index + colIndex]) 60 b = '{0:02X}'.format(self.Data[index + colIndex])
58 painter.drawText(xpos, ypos, b) 61 painter.drawText(xpos, ypos, b)
62 # cursor 65 # cursor
63 if self.blinkcursor: 66 if self.blinkcursor:
64 painter.fillRect(self.cursorX, self.cursorY + self.charHeight - 2, self.charWidth, 2, Qt.blue) 67 painter.fillRect(self.cursorX, self.cursorY + self.charHeight - 2, self.charWidth, 2, Qt.blue)
65 def keyPressEvent(self, event): 68 def keyPressEvent(self, event):
66 if event.matches(QKeySequence.MoveToNextChar): 69 if event.matches(QKeySequence.MoveToNextChar):
67 self.setCursorPosition(self.cursorPosition + 1) 70 self.setCursorPosition(self.cursorPosition + 1)
68 if event.matches(QKeySequence.MoveToPreviousChar): 71 if event.matches(QKeySequence.MoveToPreviousChar):
69 self.setCursorPosition(self.cursorPosition - 1) 72 self.setCursorPosition(self.cursorPosition - 1)
70 if event.matches(QKeySequence.MoveToNextLine): 73 if event.matches(QKeySequence.MoveToNextLine):
71 self.setCursorPosition(self.cursorPosition + 2 * BYTES_PER_LINE) 74 self.setCursorPosition(self.cursorPosition + 2 * BYTES_PER_LINE)
72 if event.matches(QKeySequence.MoveToPreviousLine): 75 if event.matches(QKeySequence.MoveToPreviousLine):
73 self.setCursorPosition(self.cursorPosition - 2 * BYTES_PER_LINE) 76 self.setCursorPosition(self.cursorPosition - 2 * BYTES_PER_LINE)
74 77 self.ensureVisible()
78 self.update()
79 def ensureVisible(self):
80 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight, 3, self.charHeight / 2 + 2)
75 def adjust(self): 81 def adjust(self):
76 self.charHeight = self.fontMetrics().height() 82 self.charHeight = self.fontMetrics().height()
77 self.charWidth = self.fontMetrics().width('x') 83 self.charWidth = self.fontMetrics().width('x')
78 self.xposAddr = 2 84 self.xposAddr = 2
79 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP 85 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
96 102
97 class HexEdit(QScrollArea): 103 class HexEdit(QScrollArea):
98 def __init__(self): 104 def __init__(self):
99 super().__init__() 105 super().__init__()
100 self.setWidgetResizable(True) 106 self.setWidgetResizable(True)
101 self.bv = BinViewer() 107 self.bv = BinViewer(self)
102 self.setWidget(self.bv) 108 self.setWidget(self.bv)
103 self.setFocusPolicy(Qt.NoFocus) 109 self.setFocusPolicy(Qt.NoFocus)
104 110
105 if __name__ == '__main__': 111 if __name__ == '__main__':
106 app = QApplication(sys.argv) 112 app = QApplication(sys.argv)