From 1a29cf7579ab57869055815c0c9fcd62790cb906 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 29 Jun 2021 12:27:04 +0800 Subject: [PATCH 1/6] defer init if we do not have an OpenGL context yet --- pyqtgraph/opengl/GLGraphicsItem.py | 15 ++++++++++----- pyqtgraph/opengl/GLViewWidget.py | 10 +++++++--- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/pyqtgraph/opengl/GLGraphicsItem.py b/pyqtgraph/opengl/GLGraphicsItem.py index a2c2708a..bf9de81a 100644 --- a/pyqtgraph/opengl/GLGraphicsItem.py +++ b/pyqtgraph/opengl/GLGraphicsItem.py @@ -1,8 +1,7 @@ from OpenGL.GL import * from OpenGL import GL -from ..Qt import QtGui, QtCore +from ..Qt import QtCore from .. import Transform3D -from ..python2_3 import basestring GLOptions = { @@ -42,6 +41,7 @@ class GLGraphicsItem(QtCore.QObject): self.__children = set() self.__transform = Transform3D() self.__visible = True + self.__initialized = False self.setParentItem(parentItem) self.setDepthValue(0) self.__glOpts = {} @@ -91,7 +91,7 @@ class GLGraphicsItem(QtCore.QObject): """ - if isinstance(opts, basestring): + if isinstance(opts, str): opts = GLOptions[opts] self.__glOpts = opts.copy() self.update() @@ -228,6 +228,11 @@ class GLGraphicsItem(QtCore.QObject): view, as it may be obscured or outside of the current view area.""" return self.__visible + def setInitialized(self, init=True): + self.__initialized = init + + def initialized(self): + return self.__initialized def initializeGL(self): """ @@ -235,7 +240,7 @@ class GLGraphicsItem(QtCore.QObject): The widget's GL context is made current before this method is called. (So this would be an appropriate time to generate lists, upload textures, etc.) """ - pass + self.setInitialized() def setupGLState(self): """ @@ -245,7 +250,7 @@ class GLGraphicsItem(QtCore.QObject): for k,v in self.__glOpts.items(): if v is None: continue - if isinstance(k, basestring): + if isinstance(k, str): func = getattr(GL, k) func(*v) else: diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 8896bdc0..61a7d445 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -8,7 +8,6 @@ import warnings from math import cos, sin, tan, radians ##Vector = QtGui.QVector3D -ShareWidget = None class GLViewWidget(QtWidgets.QOpenGLWidget): @@ -99,10 +98,12 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): def addItem(self, item): self.items.append(item) - if hasattr(item, 'initializeGL'): + + if self.isValid(): self.makeCurrent() try: item.initializeGL() + item.setInitialized() except: self.checkOpenGLVersion('Error while adding item %s to GLViewWidget.' % str(item)) @@ -128,7 +129,10 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): self.update() def initializeGL(self): - self.resizeGL(self.width(), self.height()) + for item in self.items: + if not item.initialized(): + item.initializeGL() + item.setInitialized() def setBackgroundColor(self, *args, **kwds): """ From b843214f66813fe9e461fa416a609a23e1f7b7a7 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 29 Jun 2021 12:30:46 +0800 Subject: [PATCH 2/6] remove Qt4 mouse wheel handling --- pyqtgraph/opengl/GLViewWidget.py | 12 ++++-------- pyqtgraph/widgets/GraphicsView.py | 10 +++------- 2 files changed, 7 insertions(+), 15 deletions(-) diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 61a7d445..6984059b 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -1,4 +1,4 @@ -from ..Qt import QtCore, QtGui, QtWidgets, QT_LIB +from ..Qt import QtCore, QtGui, QtWidgets from OpenGL.GL import * import OpenGL.GL.framebufferobjects as glfbo import numpy as np @@ -466,13 +466,9 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): #self.swapBuffers() def wheelEvent(self, ev): - delta = 0 - if QT_LIB in ['PyQt4', 'PySide']: - delta = ev.delta() - else: - delta = ev.angleDelta().x() - if delta == 0: - delta = ev.angleDelta().y() + delta = ev.angleDelta().x() + if delta == 0: + delta = ev.angleDelta().y() if (ev.modifiers() & QtCore.Qt.KeyboardModifier.ControlModifier): self.opts['fov'] *= 0.999**delta else: diff --git a/pyqtgraph/widgets/GraphicsView.py b/pyqtgraph/widgets/GraphicsView.py index 036f2f05..ed73cca5 100644 --- a/pyqtgraph/widgets/GraphicsView.py +++ b/pyqtgraph/widgets/GraphicsView.py @@ -317,13 +317,9 @@ class GraphicsView(QtGui.QGraphicsView): super().wheelEvent(ev) if not self.mouseEnabled: return - delta = 0 - if QT_LIB in ['PyQt4', 'PySide']: - delta = ev.delta() - else: - delta = ev.angleDelta().x() - if delta == 0: - delta = ev.angleDelta().y() + delta = ev.angleDelta().x() + if delta == 0: + delta = ev.angleDelta().y() sc = 1.001 ** delta #self.scale *= sc From 96f7ce13256a4d6a1845f9c846db4e4e9b1165ed Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 29 Jun 2021 12:38:46 +0800 Subject: [PATCH 3/6] remove empty initializeGL() --- pyqtgraph/opengl/items/GLLinePlotItem.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/pyqtgraph/opengl/items/GLLinePlotItem.py b/pyqtgraph/opengl/items/GLLinePlotItem.py index 2daf78ba..dcc98cc0 100644 --- a/pyqtgraph/opengl/items/GLLinePlotItem.py +++ b/pyqtgraph/opengl/items/GLLinePlotItem.py @@ -54,9 +54,6 @@ class GLLinePlotItem(GLGraphicsItem): #self.vbo.pop(arg, None) self.update() - def initializeGL(self): - pass - def paint(self): if self.pos is None: return From f9f5d46589c7473ec4edbe9795667c078d33ecac Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Tue, 29 Jun 2021 12:58:57 +0800 Subject: [PATCH 4/6] missing import of pg CI passed because of the way the example gets invoked. --- examples/GLViewWidget.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/GLViewWidget.py b/examples/GLViewWidget.py index 5bc23ba6..910fa8eb 100644 --- a/examples/GLViewWidget.py +++ b/examples/GLViewWidget.py @@ -6,10 +6,10 @@ Very basic 3D graphics example; create a view widget and add a few items. ## Add path to library (just for examples; you do not need this) import initExample -from pyqtgraph.Qt import QtCore, QtGui, mkQApp +import pyqtgraph as pg import pyqtgraph.opengl as gl -app = mkQApp("GLViewWidget Example") +pg.mkQApp("GLViewWidget Example") w = gl.GLViewWidget() w.opts['distance'] = 20 w.show() From e76328ab0eff1c58577bbf62a38c97ffa34b4e02 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Thu, 1 Jul 2021 15:11:55 +0800 Subject: [PATCH 5/6] bug: PyQt6 does not have localPos() this bug must have existed since the removal of the mouse shims from Qt.py. --- pyqtgraph/opengl/GLViewWidget.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 6984059b..4427c24a 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -434,10 +434,11 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): return xDist / self.width() def mousePressEvent(self, ev): - self.mousePos = ev.localPos() + lpos = ev.position() if hasattr(ev, 'position') else ev.localPos() + self.mousePos = lpos def mouseMoveEvent(self, ev): - lpos = ev.localPos() + lpos = ev.position() if hasattr(ev, 'position') else ev.localPos() diff = lpos - self.mousePos self.mousePos = lpos From e9ee11f0100e28681ac8bc919ab6304f01a1adfe Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Sat, 3 Jul 2021 15:53:17 +0800 Subject: [PATCH 6/6] simplify calls needed to initialize items --- pyqtgraph/opengl/GLGraphicsItem.py | 9 +++++---- pyqtgraph/opengl/GLViewWidget.py | 11 ++++++----- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/pyqtgraph/opengl/GLGraphicsItem.py b/pyqtgraph/opengl/GLGraphicsItem.py index bf9de81a..66a21f32 100644 --- a/pyqtgraph/opengl/GLGraphicsItem.py +++ b/pyqtgraph/opengl/GLGraphicsItem.py @@ -228,10 +228,11 @@ class GLGraphicsItem(QtCore.QObject): view, as it may be obscured or outside of the current view area.""" return self.__visible - def setInitialized(self, init=True): - self.__initialized = init + def initialize(self): + self.initializeGL() + self.__initialized = True - def initialized(self): + def isInitialized(self): return self.__initialized def initializeGL(self): @@ -240,7 +241,7 @@ class GLGraphicsItem(QtCore.QObject): The widget's GL context is made current before this method is called. (So this would be an appropriate time to generate lists, upload textures, etc.) """ - self.setInitialized() + pass def setupGLState(self): """ diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 4427c24a..7096b7b4 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -102,8 +102,7 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): if self.isValid(): self.makeCurrent() try: - item.initializeGL() - item.setInitialized() + item.initialize() except: self.checkOpenGLVersion('Error while adding item %s to GLViewWidget.' % str(item)) @@ -129,10 +128,12 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): self.update() def initializeGL(self): + """ + Initialize items that were not initialized during addItem(). + """ for item in self.items: - if not item.initialized(): - item.initializeGL() - item.setInitialized() + if not item.isInitialized(): + item.initialize() def setBackgroundColor(self, *args, **kwds): """