view python/hexedit.py @ 132:205578c96a79

Moved hexview to seperate class
author Windel Bouwman
date Sun, 20 Jan 2013 17:45:45 +0100
parents
children a816c683c1c0
line wrap: on
line source


from PyQt4.QtCore import *
from PyQt4.QtGui import *

class HexEdit(QScrollArea):
   def __init__(self):
      super().__init__()

class BinViewer(QWidget):
   """
      The binviewer consists of a hex view, a ascii view
      and perhaps others..
   """
   def __init__(self):
      super().__init__()
      self.setFont(QFont('Courier'))
      self.fontHeight = self.fontMetrics().height()
      self.fontWidth = self.fontMetrics().width('x')
      self.hexfile = hexfile.HexFile()
   def paintEvent(self, event):
      painter = QPainter(self)
      br = QBrush(Qt.lightGray)
      painter.setBrush(br)
      h = self.height()
      addressWidth = self.fontWidth * 8
      painter.drawRect(2, 2, addressWidth, h)
      br2 = QBrush(Qt.yellow)
      painter.setBrush(br2)
      w = self.width()
      byteWidth = self.fontWidth * (16 * 3 - 1)
      painter.drawRect(addressWidth + 4, 2, byteWidth, h)
      asciiWidth = self.fontWidth * 16
      br2 = QBrush(Qt.gray)
      painter.setBrush(br2)
      painter.drawRect(addressWidth + byteWidth + 4, 2, asciiWidth, h)
      rows = int(h / self.fontHeight) + 1
      offset = 0
      for r in range(1, rows):
         bts = self.getBytes(offset + r - 1)
         addr = '{0:08X}'.format(r << 16)
         painter.drawText(2, 2 + self.fontHeight * r, addr)
         x = addressWidth + 4
         for b in bts:
            b = '{0:02X}'.format(b)
            painter.drawText(x, 2 + self.fontHeight * r, b)
            x += 3 * self.fontWidth
         x = addressWidth + byteWidth + 4
         for b in bts:
            b = '{0}'.format(chr(b))
            painter.drawText(x, 2 + self.fontHeight * r, b)
            x += 1 * self.fontWidth
   def getBytes(self, offset):
      if self.hexfile.regions:
         r = self.hexfile.regions[0]
         chunks = [r.data[p:p+16] for p in range(0, len(r.data), 16)]
         if len(chunks) > offset:
            return chunks[offset]
      return bytes()
   def setHexFile(self, hf):
      self.hexfile = hf
      self.update()