Mercurial > lcfOS
view python/hexedit.py @ 139:2ec4d4332b7a
Improve hexedit
author | Windel Bouwman |
---|---|
date | Sat, 26 Jan 2013 19:44:36 +0100 |
parents | 0a540ce31cd5 |
children | 104037b292cc |
line wrap: on
line source
import sys from PyQt4.QtCore import * from PyQt4.QtGui import * BYTES_PER_LINE, GAP = 8, 12 def clamp(minimum, x, maximum): return max(minimum, min(x, maximum)) def asciiChar(v): if v < 0x20 or v > 0x7e: return '.' else: return chr(v) class BinViewer(QWidget): """ The view has an address, hex byte and ascii column """ def __init__(self, scrollArea): super().__init__(scrollArea) self.scrollArea = scrollArea self.setFont(QFont('Courier', 18)) self.setFocusPolicy(Qt.StrongFocus) self.blinkcursor = False self.cursorX = self.cursorY = 0 self.scrollArea = scrollArea self.Data = bytearray() self.Offset = 0 t = QTimer(self) t.timeout.connect(self.updateCursor) t.setInterval(500) t.start() def updateCursor(self): self.blinkcursor = not self.blinkcursor self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight) def setCursorPosition(self, position): position = clamp(0, int(position), len(self.Data) * 2 - 1) self.cursorPosition = position x = position % (2 * BYTES_PER_LINE) x = x + int(x / 2) # Create a gap between hex values self.cursorX = self.xposHex + x * self.charWidth y = int(position / (2 * BYTES_PER_LINE)) self.cursorY = y * self.charHeight + 2 self.blinkcursor = True self.update() def getCursorPosition(self): return self.cursorPosition CursorPosition = property(getCursorPosition, setCursorPosition) def setOffset(self, off): self.offset = off self.update() Offset = property(lambda self: self.offset, setOffset) def paintEvent(self, event): # Helper variables: er = event.rect() chw, chh = self.charWidth, self.charHeight painter = QPainter(self) # Background: painter.fillRect(er, self.palette().color(QPalette.Base)) painter.fillRect(QRect(self.xposAddr, er.top(), 8 * chw, er.bottom() + 1), Qt.gray) painter.setPen(Qt.gray) x = self.xposAscii - (GAP / 2) painter.drawLine(x, er.top(), x, er.bottom()) x = self.xposEnd - (GAP / 2) painter.drawLine(x, er.top(), x, er.bottom()) # first and last index firstIndex = max((int(er.top() / chh) - chh) * BYTES_PER_LINE, 0) lastIndex = max((int(er.bottom() / chh) + chh) * BYTES_PER_LINE, 0) yposStart = int(firstIndex / BYTES_PER_LINE) * chh + chh # Draw contents: painter.setPen(Qt.black) ypos = yposStart for index in range(firstIndex, lastIndex, BYTES_PER_LINE): painter.drawText(self.xposAddr, ypos, '{0:08X}'.format(index + self.Offset)) xpos = self.xposHex xposAscii = self.xposAscii for colIndex in range(BYTES_PER_LINE): if index + colIndex < len(self.Data): b = self.Data[index + colIndex] painter.drawText(xpos, ypos, '{0:02X}'.format(b)) painter.drawText(xposAscii, ypos, asciiChar(b)) xpos += 3 * chw xposAscii += chw ypos += chh # cursor if self.blinkcursor: painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black) def keyPressEvent(self, event): if event.matches(QKeySequence.MoveToNextChar): self.CursorPosition += 1 if event.matches(QKeySequence.MoveToPreviousChar): self.CursorPosition -= 1 if event.matches(QKeySequence.MoveToNextLine): self.CursorPosition += 2 * BYTES_PER_LINE if event.matches(QKeySequence.MoveToPreviousLine): self.CursorPosition -= 2 * BYTES_PER_LINE if event.matches(QKeySequence.MoveToNextPage): rows = int(self.scrollArea.viewport().height() / self.charHeight) self.CursorPosition += (rows - 1) * 2 * BYTES_PER_LINE if event.matches(QKeySequence.MoveToPreviousPage): rows = int(self.scrollArea.viewport().height() / self.charHeight) self.CursorPosition -= (rows - 1) * 2 * BYTES_PER_LINE char = event.text().lower() if char and char in '0123456789abcdef': i = int(self.CursorPosition / 2) hb = self.CursorPosition % 2 v = int(char, 16) if hb == 0: # high half byte self.data[i] = (self.data[i] & 0xF) | (v << 4) else: self.data[i] = (self.data[i] & 0xF0) | v self.CursorPosition += 1 self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4) self.update() def setCursorPositionAt(self, pos): """ Calculate cursor position at a certain point """ if pos.x() > self.xposHex and pos.x() < self.xposAscii: x = round((2 * (pos.x() - self.xposHex)) / (self.charWidth * 3)) y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE self.setCursorPosition(x + y) def mousePressEvent(self, event): self.setCursorPositionAt(event.pos()) def adjust(self): self.charHeight = self.fontMetrics().height() self.charWidth = self.fontMetrics().width('x') self.xposAddr = GAP self.xposHex = self.xposAddr + 8 * self.charWidth + GAP self.xposAscii = self.xposHex + (BYTES_PER_LINE * 3 - 1) * self.charWidth + GAP self.xposEnd = self.xposAscii + self.charWidth * BYTES_PER_LINE + GAP self.setMinimumWidth(self.xposEnd) sbw = self.scrollArea.verticalScrollBar().width() self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5) r = len(self.Data) % BYTES_PER_LINE r = 1 if r > 0 else 0 self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + r) * self.charHeight + 4) self.scrollArea.setMinimumHeight(self.charHeight * 8) self.update() def setData(self, d): self.data = d self.adjust() self.setCursorPosition(0) Data = property(lambda self: self.data, setData) class HexEdit(QScrollArea): def __init__(self): super().__init__() self.bv = BinViewer(self) self.setWidget(self.bv) self.setWidgetResizable(True) self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn) self.setFocusPolicy(Qt.NoFocus) if __name__ == '__main__': app = QApplication(sys.argv) he = HexEdit() he.show() he.bv.Data = bytearray(range(100)) * 8 + b'x' app.exec()