view python/hexviewer.py @ 112:056face59ee7

Added lsusb script
author Windel Bouwman
date Fri, 04 Jan 2013 18:40:05 +0100
parents ad14c7c52589
children d38729d35c4d
line wrap: on
line source

#!/usr/bin/python

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from qtpropertyviewer import QtPropertyViewer
import sys
import hexfile

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()

class BinViewMain(QMainWindow):
   def __init__(self):
      super().__init__()
      self.bv = BinViewer()
      self.setCentralWidget(self.bv)
      mb = self.menuBar()
      fileMenu = mb.addMenu("File")
      
      def addMenuEntry(name, menu, callback, shortcut=None):
         a = QAction(name, self)
         menu.addAction(a)
         a.triggered.connect(callback)
         if shortcut: a.setShortcut(shortcut)
      addMenuEntry("Open", fileMenu, self.openFile, QKeySequence(QKeySequence.Open))
   def openFile(self):
      filename = QFileDialog.getOpenFileName(self, "Open hex file...", "*.hex", "Intel hexfiles (*.hex)")
      if filename:
         h = hexfile.HexFile(filename)
   @pyqtProperty(str)
   def leetValue(self):
      return '1337'

if __name__ == '__main__':
   app = QApplication(sys.argv)
   bv = BinViewMain()
   bv.show()
   bv.bv.setHexFile(hexfile.HexFile('audio.hex'))
   qpv = QtPropertyViewer()
   qpv.propertyModel.InspectedWidget = bv
   qpv.show()
   app.exec_()