From 450626a3bbd6d7e87fd5cde799df7669fd7acb05 Mon Sep 17 00:00:00 2001 From: Luke Campagnola <> Date: Fri, 26 Oct 2012 08:55:53 -0400 Subject: [PATCH] Fixes for PySide compatibility --- examples/__main__.py | 21 +++++++++++++++++++-- examples/exampleLoaderTemplate.ui | 18 ++++++++++++++++++ examples/exampleLoaderTemplate_pyqt.py | 13 ++++++++++++- examples/exampleLoaderTemplate_pyside.py | 13 ++++++++++++- examples/initExample.py | 5 +++++ functions.py | 24 +++++++++++++++++------- graphicsItems/LegendItem.py | 1 + graphicsItems/ScatterPlotItem.py | 4 ++-- 8 files changed, 86 insertions(+), 13 deletions(-) diff --git a/examples/__main__.py b/examples/__main__.py index a0638f5d..3035ddcf 100644 --- a/examples/__main__.py +++ b/examples/__main__.py @@ -87,7 +87,17 @@ class ExampleLoader(QtGui.QMainWindow): self.ui.loadBtn.clicked.connect(self.loadFile) self.ui.exampleTree.currentItemChanged.connect(self.showFile) self.ui.exampleTree.itemDoubleClicked.connect(self.loadFile) + self.ui.pyqtCheck.toggled.connect(self.pyqtToggled) + self.ui.pysideCheck.toggled.connect(self.pysideToggled) + def pyqtToggled(self, b): + if b: + self.ui.pysideCheck.setChecked(False) + + def pysideToggled(self, b): + if b: + self.ui.pyqtCheck.setChecked(False) + def populateTree(self, root, examples): for key, val in examples.items(): @@ -108,12 +118,19 @@ class ExampleLoader(QtGui.QMainWindow): def loadFile(self): fn = self.currentFile() + extra = [] + if self.ui.pyqtCheck.isChecked(): + extra.append('pyqt') + elif self.ui.pysideCheck.isChecked(): + extra.append('pyside') + if fn is None: return if sys.platform.startswith('win'): - os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, '"' + fn + '"') + os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, '"' + fn + '"', *extra) else: - os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn) + + os.spawnl(os.P_NOWAIT, sys.executable, sys.executable, fn, *extra) def showFile(self): diff --git a/examples/exampleLoaderTemplate.ui b/examples/exampleLoaderTemplate.ui index b1941447..1453240c 100644 --- a/examples/exampleLoaderTemplate.ui +++ b/examples/exampleLoaderTemplate.ui @@ -39,6 +39,24 @@ + + + + + + Force PyQt + + + + + + + Force PySide + + + + + diff --git a/examples/exampleLoaderTemplate_pyqt.py b/examples/exampleLoaderTemplate_pyqt.py index 80e95295..26e55a44 100644 --- a/examples/exampleLoaderTemplate_pyqt.py +++ b/examples/exampleLoaderTemplate_pyqt.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file './examples/exampleLoaderTemplate.ui' # -# Created: Sun Sep 9 14:41:31 2012 +# Created: Fri Oct 26 07:53:55 2012 # by: PyQt4 UI code generator 4.9.1 # # WARNING! All changes made in this file will be lost! @@ -35,6 +35,15 @@ class Ui_Form(object): self.exampleTree.headerItem().setText(0, _fromUtf8("1")) self.exampleTree.header().setVisible(False) self.verticalLayout.addWidget(self.exampleTree) + self.horizontalLayout = QtGui.QHBoxLayout() + self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) + self.pyqtCheck = QtGui.QCheckBox(self.layoutWidget) + self.pyqtCheck.setObjectName(_fromUtf8("pyqtCheck")) + self.horizontalLayout.addWidget(self.pyqtCheck) + self.pysideCheck = QtGui.QCheckBox(self.layoutWidget) + self.pysideCheck.setObjectName(_fromUtf8("pysideCheck")) + self.horizontalLayout.addWidget(self.pysideCheck) + self.verticalLayout.addLayout(self.horizontalLayout) self.loadBtn = QtGui.QPushButton(self.layoutWidget) self.loadBtn.setObjectName(_fromUtf8("loadBtn")) self.verticalLayout.addWidget(self.loadBtn) @@ -51,5 +60,7 @@ class Ui_Form(object): def retranslateUi(self, Form): Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + self.pyqtCheck.setText(QtGui.QApplication.translate("Form", "Force PyQt", None, QtGui.QApplication.UnicodeUTF8)) + self.pysideCheck.setText(QtGui.QApplication.translate("Form", "Force PySide", None, QtGui.QApplication.UnicodeUTF8)) self.loadBtn.setText(QtGui.QApplication.translate("Form", "Load Example", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/examples/exampleLoaderTemplate_pyside.py b/examples/exampleLoaderTemplate_pyside.py index fa0ccfd1..a81f7299 100644 --- a/examples/exampleLoaderTemplate_pyside.py +++ b/examples/exampleLoaderTemplate_pyside.py @@ -2,7 +2,7 @@ # Form implementation generated from reading ui file './examples/exampleLoaderTemplate.ui' # -# Created: Sun Sep 9 14:41:31 2012 +# Created: Fri Oct 26 07:53:57 2012 # by: pyside-uic 0.2.13 running on PySide 1.1.0 # # WARNING! All changes made in this file will be lost! @@ -30,6 +30,15 @@ class Ui_Form(object): self.exampleTree.headerItem().setText(0, "1") self.exampleTree.header().setVisible(False) self.verticalLayout.addWidget(self.exampleTree) + self.horizontalLayout = QtGui.QHBoxLayout() + self.horizontalLayout.setObjectName("horizontalLayout") + self.pyqtCheck = QtGui.QCheckBox(self.layoutWidget) + self.pyqtCheck.setObjectName("pyqtCheck") + self.horizontalLayout.addWidget(self.pyqtCheck) + self.pysideCheck = QtGui.QCheckBox(self.layoutWidget) + self.pysideCheck.setObjectName("pysideCheck") + self.horizontalLayout.addWidget(self.pysideCheck) + self.verticalLayout.addLayout(self.horizontalLayout) self.loadBtn = QtGui.QPushButton(self.layoutWidget) self.loadBtn.setObjectName("loadBtn") self.verticalLayout.addWidget(self.loadBtn) @@ -46,5 +55,7 @@ class Ui_Form(object): def retranslateUi(self, Form): Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8)) + self.pyqtCheck.setText(QtGui.QApplication.translate("Form", "Force PyQt", None, QtGui.QApplication.UnicodeUTF8)) + self.pysideCheck.setText(QtGui.QApplication.translate("Form", "Force PySide", None, QtGui.QApplication.UnicodeUTF8)) self.loadBtn.setText(QtGui.QApplication.translate("Form", "Load Example", None, QtGui.QApplication.UnicodeUTF8)) diff --git a/examples/initExample.py b/examples/initExample.py index 040dbeea..1b38363b 100644 --- a/examples/initExample.py +++ b/examples/initExample.py @@ -1,3 +1,8 @@ ## make this version of pyqtgraph importable before any others import sys, os sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..'))) + +if 'pyside' in sys.argv: ## should force example to use PySide instead of PyQt + import PySide +elif 'pyqt' in sys.argv: + import PyQt4 diff --git a/functions.py b/functions.py index a1dea35e..1342b527 100644 --- a/functions.py +++ b/functions.py @@ -22,7 +22,7 @@ SI_PREFIXES_ASCII = 'yzafpnum kMGTPEZY' -from .Qt import QtGui, QtCore +from .Qt import QtGui, QtCore, USE_PYSIDE import numpy as np import decimal, re import ctypes @@ -846,8 +846,12 @@ def makeQImage(imgData, alpha=None, copy=True, transpose=True): if copy is True and copied is False: imgData = imgData.copy() - addr = ctypes.addressof(ctypes.c_char.from_buffer(imgData, 0)) - img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) + if USE_PYSIDE: + ch = ctypes.c_char.from_buffer(imgData, 0) + img = QtGui.QImage(ch, imgData.shape[1], imgData.shape[0], imgFormat) + else: + addr = ctypes.addressof(ctypes.c_char.from_buffer(imgData, 0)) + img = QtGui.QImage(addr, imgData.shape[1], imgData.shape[0], imgFormat) img.data = imgData return img #try: @@ -869,13 +873,19 @@ def imageToArray(img, copy=False, transpose=True): the QImage is collected before the array, there may be trouble). The array will have shape (width, height, (b,g,r,a)). """ - ptr = img.bits() - ptr.setsize(img.byteCount()) fmt = img.format() + ptr = img.bits() + if USE_PYSIDE: + arr = np.frombuffer(ptr, dtype=np.ubyte) + else: + ptr.setsize(img.byteCount()) + arr = np.asarray(ptr) + if fmt == img.Format_RGB32: - arr = np.asarray(ptr).reshape(img.height(), img.width(), 3) + arr = arr.reshape(img.height(), img.width(), 3) elif fmt == img.Format_ARGB32 or fmt == img.Format_ARGB32_Premultiplied: - arr = np.asarray(ptr).reshape(img.height(), img.width(), 4) + arr = arr.reshape(img.height(), img.width(), 4) + if copy: arr = arr.copy() diff --git a/graphicsItems/LegendItem.py b/graphicsItems/LegendItem.py index 9443a8d4..a41201e1 100644 --- a/graphicsItems/LegendItem.py +++ b/graphicsItems/LegendItem.py @@ -28,6 +28,7 @@ class LegendItem(GraphicsWidget): def addItem(self, item, title): """ Add a new entry to the legend. + =========== ======================================================== Arguments item A PlotDataItem from which the line and point style diff --git a/graphicsItems/ScatterPlotItem.py b/graphicsItems/ScatterPlotItem.py index ffb32ef1..6e442a1d 100644 --- a/graphicsItems/ScatterPlotItem.py +++ b/graphicsItems/ScatterPlotItem.py @@ -1,4 +1,4 @@ -from pyqtgraph.Qt import QtGui, QtCore +from pyqtgraph.Qt import QtGui, QtCore, USE_PYSIDE from pyqtgraph.Point import Point import pyqtgraph.functions as fn from .GraphicsItem import GraphicsItem @@ -681,7 +681,7 @@ class ScatterPlotItem(GraphicsObject): p.resetTransform() - if self.opts['useCache']: + if not USE_PYSIDE and self.opts['useCache']: p.drawPixmapFragments(self.fragments, atlas) else: for i in range(len(self.data)):