changeset 139:2ec4d4332b7a

Improve hexedit
author Windel Bouwman
date Sat, 26 Jan 2013 19:44:36 +0100
parents 14e739ed03ab
children 104037b292cc
files python/hexedit.py
diffstat 1 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/python/hexedit.py	Sat Jan 26 10:04:32 2013 +0100
+++ b/python/hexedit.py	Sat Jan 26 19:44:36 2013 +0100
@@ -2,8 +2,7 @@
 from PyQt4.QtCore import *
 from PyQt4.QtGui import *
 
-BYTES_PER_LINE = 8
-GAP = 12
+BYTES_PER_LINE, GAP = 8, 12
 
 def clamp(minimum, x, maximum):
    return max(minimum, min(x, maximum))
@@ -46,12 +45,10 @@
    def getCursorPosition(self):
       return self.cursorPosition
    CursorPosition = property(getCursorPosition, setCursorPosition)
-   def getOffset(self):
-      return self.offset
    def setOffset(self, off):
       self.offset = off
       self.update()
-   Offset = property(getOffset, setOffset)
+   Offset = property(lambda self: self.offset, setOffset)
    def paintEvent(self, event):
       # Helper variables:
       er = event.rect()
@@ -115,16 +112,14 @@
          self.CursorPosition += 1
       self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4)
       self.update()
-   def cursorPositionAt(self, pos):
+   def setCursorPositionAt(self, pos):
       """ Calculate cursor position at a certain point """
       if pos.x() > self.xposHex and pos.x() < self.xposAscii:
-         x = (pos.x() - self.xposHex) / self.charWidth
+         x = round((2 * (pos.x() - self.xposHex)) / (self.charWidth * 3))
          y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE
-         return x + y
-      return 0
+         self.setCursorPosition(x + y)
    def mousePressEvent(self, event):
-      cpos = self.cursorPositionAt(event.pos())
-      self.setCursorPosition(cpos)
+      self.setCursorPositionAt(event.pos())
    def adjust(self):
       self.charHeight = self.fontMetrics().height()
       self.charWidth = self.fontMetrics().width('x')
@@ -135,16 +130,16 @@
       self.setMinimumWidth(self.xposEnd)
       sbw = self.scrollArea.verticalScrollBar().width()
       self.scrollArea.setMinimumWidth(self.xposEnd + sbw + 5)
-      self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + 1) * self.charHeight)
+      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 getData(self):
-      return self.data
    def setData(self, d):
       self.data = d
       self.adjust()
       self.setCursorPosition(0)
-   Data = property(getData, setData)
+   Data = property(lambda self: self.data, setData)
 
 class HexEdit(QScrollArea):
    def __init__(self):
@@ -159,6 +154,6 @@
    app = QApplication(sys.argv)
    he = HexEdit()
    he.show()
-   he.bv.Data = bytearray(range(100)) * 8 + b'hjkfd'
+   he.bv.Data = bytearray(range(100)) * 8 + b'x'
    app.exec()