comparison python/hexedit.py @ 137:0a540ce31cd5

Added debug toolbar and spaced hexedit
author Windel Bouwman
date Fri, 25 Jan 2013 23:47:34 +0100
parents 9af544be5d2a
children 2ec4d4332b7a
comparison
equal deleted inserted replaced
136:9af544be5d2a 137:0a540ce31cd5
35 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight) 35 self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
36 def setCursorPosition(self, position): 36 def setCursorPosition(self, position):
37 position = clamp(0, int(position), len(self.Data) * 2 - 1) 37 position = clamp(0, int(position), len(self.Data) * 2 - 1)
38 self.cursorPosition = position 38 self.cursorPosition = position
39 x = position % (2 * BYTES_PER_LINE) 39 x = position % (2 * BYTES_PER_LINE)
40 x = x + int(x / 2) # Create a gap between hex values
40 self.cursorX = self.xposHex + x * self.charWidth 41 self.cursorX = self.xposHex + x * self.charWidth
41 self.cursorY = int(position / (2 * BYTES_PER_LINE)) * self.charHeight + 2 42 y = int(position / (2 * BYTES_PER_LINE))
43 self.cursorY = y * self.charHeight + 2
42 self.blinkcursor = True 44 self.blinkcursor = True
43 self.update() 45 self.update()
44 def getCursorPosition(self): 46 def getCursorPosition(self):
45 return self.cursorPosition 47 return self.cursorPosition
46 CursorPosition = property(getCursorPosition, setCursorPosition) 48 CursorPosition = property(getCursorPosition, setCursorPosition)
77 for colIndex in range(BYTES_PER_LINE): 79 for colIndex in range(BYTES_PER_LINE):
78 if index + colIndex < len(self.Data): 80 if index + colIndex < len(self.Data):
79 b = self.Data[index + colIndex] 81 b = self.Data[index + colIndex]
80 painter.drawText(xpos, ypos, '{0:02X}'.format(b)) 82 painter.drawText(xpos, ypos, '{0:02X}'.format(b))
81 painter.drawText(xposAscii, ypos, asciiChar(b)) 83 painter.drawText(xposAscii, ypos, asciiChar(b))
82 xpos += 2 * chw 84 xpos += 3 * chw
83 xposAscii += chw 85 xposAscii += chw
84 ypos += chh 86 ypos += chh
85 # cursor 87 # cursor
86 if self.blinkcursor: 88 if self.blinkcursor:
87 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black) 89 painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black)
109 # high half byte 111 # high half byte
110 self.data[i] = (self.data[i] & 0xF) | (v << 4) 112 self.data[i] = (self.data[i] & 0xF) | (v << 4)
111 else: 113 else:
112 self.data[i] = (self.data[i] & 0xF0) | v 114 self.data[i] = (self.data[i] & 0xF0) | v
113 self.CursorPosition += 1 115 self.CursorPosition += 1
114 self.scrollArea.ensureVisible(self.cursorX, self.cursorY, self.charWidth, self.charHeight + 2) 116 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4)
115 self.update() 117 self.update()
116 def cursorPositionAt(self, pos): 118 def cursorPositionAt(self, pos):
117 """ Calculate cursor position at a certain point """ 119 """ Calculate cursor position at a certain point """
118 if pos.x() > self.xposHex and pos.x() < self.xposAscii: 120 if pos.x() > self.xposHex and pos.x() < self.xposAscii:
119 x = (pos.x() - self.xposHex) / self.charWidth 121 x = (pos.x() - self.xposHex) / self.charWidth
126 def adjust(self): 128 def adjust(self):
127 self.charHeight = self.fontMetrics().height() 129 self.charHeight = self.fontMetrics().height()
128 self.charWidth = self.fontMetrics().width('x') 130 self.charWidth = self.fontMetrics().width('x')
129 self.xposAddr = GAP 131 self.xposAddr = GAP
130 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP 132 self.xposHex = self.xposAddr + 8 * self.charWidth + GAP
131 self.xposAscii = self.xposHex + BYTES_PER_LINE * 2 * self.charWidth + GAP 133 self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP
132 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP 134 self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP
133 self.setMinimumWidth(self.xposEnd) 135 self.setMinimumWidth(self.xposEnd)
134 sbw = self.scrollArea.verticalScrollBar().width() 136 sbw = self.scrollArea.verticalScrollBar().width()
135 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5) 137 self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5)
136 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight) 138 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight)