changeset 140:104037b292cc

Added ui file for hexeditor
author Windel Bouwman
date Sun, 27 Jan 2013 12:16:09 +0100
parents 2ec4d4332b7a
children cdddae282d1a
files python/hexedit.py python/hexeditor.ui
diffstat 2 files changed, 123 insertions(+), 5 deletions(-) [+]
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()
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/python/hexeditor.ui	Sun Jan 27 12:16:09 2013 +0100
@@ -0,0 +1,78 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>617</width>
+    <height>503</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>HexEditor</string>
+  </property>
+  <widget class="QWidget" name="centralwidget"/>
+  <widget class="QMenuBar" name="menubar">
+   <property name="geometry">
+    <rect>
+     <x>0</x>
+     <y>0</y>
+     <width>617</width>
+     <height>25</height>
+    </rect>
+   </property>
+   <widget class="QMenu" name="menuFile">
+    <property name="title">
+     <string>File</string>
+    </property>
+    <addaction name="actionOpen"/>
+    <addaction name="actionSave"/>
+    <addaction name="actionSaveAs"/>
+   </widget>
+   <addaction name="menuFile"/>
+  </widget>
+  <widget class="QStatusBar" name="statusbar"/>
+  <widget class="QToolBar" name="toolBar">
+   <property name="windowTitle">
+    <string>toolBar</string>
+   </property>
+   <attribute name="toolBarArea">
+    <enum>TopToolBarArea</enum>
+   </attribute>
+   <attribute name="toolBarBreak">
+    <bool>false</bool>
+   </attribute>
+   <addaction name="actionOpen"/>
+   <addaction name="actionSave"/>
+   <addaction name="actionSaveAs"/>
+  </widget>
+  <action name="actionOpen">
+   <property name="text">
+    <string>Open</string>
+   </property>
+   <property name="toolTip">
+    <string>Opens a file for editing</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+O</string>
+   </property>
+  </action>
+  <action name="actionSave">
+   <property name="text">
+    <string>Save</string>
+   </property>
+   <property name="shortcut">
+    <string>Ctrl+S</string>
+   </property>
+  </action>
+  <action name="actionSaveAs">
+   <property name="text">
+    <string>Save As</string>
+   </property>
+  </action>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>