diff python/hexedit.py @ 140:104037b292cc

Added ui file for hexeditor
author Windel Bouwman
date Sun, 27 Jan 2013 12:16:09 +0100
parents 2ec4d4332b7a
children 982ddb5f786d
line wrap: on
line diff
--- a/python/hexedit.py	Sat Jan 26 19:44:36 2013 +0100
+++ b/python/hexedit.py	Sun Jan 27 12:16:09 2013 +0100
@@ -1,8 +1,9 @@
 import sys
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
+from PyQt4 import uic
 
-BYTES_PER_LINE, GAP = 8, 12
+BYTES_PER_LINE, GAP = 16, 12
 
 def clamp(minimum, x, maximum):
    return max(minimum, min(x, maximum))
@@ -18,7 +19,7 @@
    def __init__(self, scrollArea):
       super().__init__(scrollArea)
       self.scrollArea = scrollArea
-      self.setFont(QFont('Courier', 18))
+      self.setFont(QFont('Courier', 16))
       self.setFocusPolicy(Qt.StrongFocus)
       self.blinkcursor = False
       self.cursorX = self.cursorY = 0
@@ -70,12 +71,18 @@
       painter.setPen(Qt.black)
       ypos = yposStart
       for index in range(firstIndex, lastIndex, BYTES_PER_LINE):
+         painter.setPen(Qt.black)
          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]
+               bo = self.originalData[index + colIndex]
+               if b == bo:
+                  painter.setPen(Qt.black)
+               else:
+                  painter.setPen(Qt.red)
                painter.drawText(xpos, ypos, '{0:02X}'.format(b))
                painter.drawText(xposAscii, ypos, asciiChar(b))
                xpos += 3 * chw
@@ -136,7 +143,8 @@
       self.scrollArea.setMinimumHeight(self.charHeight * 8)
       self.update()
    def setData(self, d):
-      self.data = d
+      self.data = bytearray(d)
+      self.originalData = bytearray(d)
       self.adjust()
       self.setCursorPosition(0)
    Data = property(lambda self: self.data, setData)
@@ -150,10 +158,42 @@
       self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
       self.setFocusPolicy(Qt.NoFocus)
 
+class HexEditor(QMainWindow):
+   def __init__(self):
+      super().__init__()
+      uic.loadUi('hexeditor.ui', baseinstance=self)
+      self.he = HexEdit()
+      self.setCentralWidget(self.he)
+      self.actionOpen.triggered.connect(self.doOpen)
+      self.actionSave.triggered.connect(self.doSave)
+      self.actionSaveAs.triggered.connect(self.doSaveAs)
+      self.fileName = None
+      self.updateControls()
+   def updateControls(self):
+      s = True if self.fileName else False
+      self.actionSave.setEnabled(s)
+      self.actionSaveAs.setEnabled(s)
+   def doOpen(self):
+      filename = QFileDialog.getOpenFileName(self)
+      if filename:
+         with open(filename, 'rb') as f:
+            self.he.bv.Data = f.read()
+         self.fileName = filename
+      self.updateControls()
+   def doSave(self):
+      self.updateControls()
+   def doSaveAs(self):
+      filename = QFileDialog.getSaveFileName(self)
+      if filename:
+         with open(filename, 'wb') as f:
+            f.write(self.he.bv.Data)
+         self.fileName = filename
+      self.updateControls()
+
 if __name__ == '__main__':
    app = QApplication(sys.argv)
-   he = HexEdit()
+   he = HexEditor()
    he.show()
-   he.bv.Data = bytearray(range(100)) * 8 + b'x'
+   #he.bv.Data = bytearray(range(100)) * 8 + b'x'
    app.exec()