diff python/ide/hexedit.py @ 333:dcae6574c974

Increment to qt5
author Windel Bouwman
date Sun, 09 Feb 2014 15:27:57 +0100
parents 7b38782ed496
children 11c5a8a70c02
line wrap: on
line diff
--- a/python/ide/hexedit.py	Fri Feb 07 12:51:55 2014 +0100
+++ b/python/ide/hexedit.py	Sun Feb 09 15:27:57 2014 +0100
@@ -2,9 +2,8 @@
 
 import sys
 import os
-from PyQt4.QtCore import *
-from PyQt4.QtGui import *
-from PyQt4 import uic
+from qtwrapper import QtGui, QtCore, QtWidgets, Qt
+
 
 BYTES_PER_LINE, GAP = 8, 12
 
@@ -17,25 +16,27 @@
    else:
       return chr(v)
 
-class BinViewer(QWidget):
+class BinViewer(QtWidgets.QWidget):
    """ The view has an address, hex byte and ascii column """
    def __init__(self, scrollArea):
       super().__init__(scrollArea)
       self.scrollArea = scrollArea
-      self.setFont(QFont('Courier', 16))
+      self.setFont(QtGui.QFont('Courier', 16))
       self.setFocusPolicy(Qt.StrongFocus)
       self.blinkcursor = False
       self.cursorX = self.cursorY = 0
       self.scrollArea = scrollArea
       self.Data = bytearray()
       self.Offset = 0
-      t = QTimer(self)
+      t = QtCore.QTimer(self)
       t.timeout.connect(self.updateCursor)
       t.setInterval(500)
       t.start()
+
    def updateCursor(self):
       self.blinkcursor = not self.blinkcursor
       self.update(self.cursorX, self.cursorY, self.charWidth, self.charHeight)
+
    def setCursorPosition(self, position):
       position = clamp(0, int(position), len(self.Data) * 2 - 1)
       self.cursorPosition = position
@@ -46,21 +47,23 @@
       self.cursorY = y * self.charHeight + 2
       self.blinkcursor = True
       self.update()
+
    def getCursorPosition(self):
       return self.cursorPosition
    CursorPosition = property(getCursorPosition, setCursorPosition)
    def setOffset(self, off):
       self.offset = off
       self.update()
+
    Offset = property(lambda self: self.offset, setOffset)
    def paintEvent(self, event):
       # Helper variables:
       er = event.rect()
       chw, chh = self.charWidth, self.charHeight
-      painter = QPainter(self)
+      painter = QtGui.QPainter(self)
       # Background:
-      painter.fillRect(er, self.palette().color(QPalette.Base))
-      painter.fillRect(QRect(self.xposAddr, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
+      painter.fillRect(er, self.palette().color(QtGui.QPalette.Base))
+      painter.fillRect(QtCore.QRect(self.xposAddr, er.top(), 8 * chw, er.bottom() + 1), Qt.gray)
       painter.setPen(Qt.gray)
       x = self.xposAscii - (GAP / 2)
       painter.drawLine(x, er.top(), x, er.bottom())
@@ -94,6 +97,7 @@
       # cursor
       if self.blinkcursor:
          painter.fillRect(self.cursorX, self.cursorY + chh - 2, chw, 2, Qt.black)
+
    def keyPressEvent(self, event):
       if event.matches(QKeySequence.MoveToNextChar):
          self.CursorPosition += 1
@@ -122,14 +126,17 @@
          self.CursorPosition += 1
       self.scrollArea.ensureVisible(self.cursorX, self.cursorY + self.charHeight / 2, 4, self.charHeight / 2 + 4)
       self.update()
+
    def setCursorPositionAt(self, pos):
       """ Calculate cursor position at a certain point """
       if pos.x() > self.xposHex and pos.x() < self.xposAscii:
          x = round((2 * (pos.x() - self.xposHex)) / (self.charWidth * 3))
          y = int(pos.y() / self.charHeight) * 2 * BYTES_PER_LINE
          self.setCursorPosition(x + y)
+
    def mousePressEvent(self, event):
       self.setCursorPositionAt(event.pos())
+
    def adjust(self):
       self.charHeight = self.fontMetrics().height()
       self.charWidth = self.fontMetrics().width('x')
@@ -146,9 +153,11 @@
       self.setMinimumHeight((int(len(self.Data) / BYTES_PER_LINE) + r) * self.charHeight + 4)
       self.scrollArea.setMinimumHeight(self.charHeight * 8)
       self.update()
+
    def showEvent(self, e):
       self.adjust()
       super().showEvent(e)
+
    def setData(self, d):
       self.data = bytearray(d)
       self.originalData = bytearray(d)
@@ -156,7 +165,8 @@
       self.setCursorPosition(0)
    Data = property(lambda self: self.data, setData)
 
-class HexEdit(QScrollArea):
+
+class HexEdit(QtWidgets.QScrollArea):
    def __init__(self):
       super().__init__()
       self.bv = BinViewer(self)
@@ -165,7 +175,8 @@
       self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOn)
       self.setFocusPolicy(Qt.NoFocus)
 
-class HexEditor(QMainWindow):
+
+class HexEditor(QtWidgets.QMainWindow):
    def __init__(self):
       super().__init__()
       basedir = os.path.dirname(__file__)
@@ -198,10 +209,10 @@
          self.fileName = filename
       self.updateControls()
 
+
 if __name__ == '__main__':
    app = QApplication(sys.argv)
    he = HexEditor()
    he.show()
    #he.bv.Data = bytearray(range(100)) * 8 + b'x'
    app.exec()
-