Merge pull request #1871 from pijyoi/opengl_init

Allow adding items to GLViewWidget before show
This commit is contained in:
Ogi Moore 2021-07-03 08:10:35 -07:00 committed by GitHub
commit e83b91e9af
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 28 additions and 28 deletions

View File

@ -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) ## Add path to library (just for examples; you do not need this)
import initExample import initExample
from pyqtgraph.Qt import QtCore, QtGui, mkQApp import pyqtgraph as pg
import pyqtgraph.opengl as gl import pyqtgraph.opengl as gl
app = mkQApp("GLViewWidget Example") pg.mkQApp("GLViewWidget Example")
w = gl.GLViewWidget() w = gl.GLViewWidget()
w.opts['distance'] = 20 w.opts['distance'] = 20
w.show() w.show()

View File

@ -1,8 +1,7 @@
from OpenGL.GL import * from OpenGL.GL import *
from OpenGL import GL from OpenGL import GL
from ..Qt import QtGui, QtCore from ..Qt import QtCore
from .. import Transform3D from .. import Transform3D
from ..python2_3 import basestring
GLOptions = { GLOptions = {
@ -42,6 +41,7 @@ class GLGraphicsItem(QtCore.QObject):
self.__children = set() self.__children = set()
self.__transform = Transform3D() self.__transform = Transform3D()
self.__visible = True self.__visible = True
self.__initialized = False
self.setParentItem(parentItem) self.setParentItem(parentItem)
self.setDepthValue(0) self.setDepthValue(0)
self.__glOpts = {} self.__glOpts = {}
@ -91,7 +91,7 @@ class GLGraphicsItem(QtCore.QObject):
""" """
if isinstance(opts, basestring): if isinstance(opts, str):
opts = GLOptions[opts] opts = GLOptions[opts]
self.__glOpts = opts.copy() self.__glOpts = opts.copy()
self.update() self.update()
@ -228,6 +228,12 @@ class GLGraphicsItem(QtCore.QObject):
view, as it may be obscured or outside of the current view area.""" view, as it may be obscured or outside of the current view area."""
return self.__visible return self.__visible
def initialize(self):
self.initializeGL()
self.__initialized = True
def isInitialized(self):
return self.__initialized
def initializeGL(self): def initializeGL(self):
""" """
@ -245,7 +251,7 @@ class GLGraphicsItem(QtCore.QObject):
for k,v in self.__glOpts.items(): for k,v in self.__glOpts.items():
if v is None: if v is None:
continue continue
if isinstance(k, basestring): if isinstance(k, str):
func = getattr(GL, k) func = getattr(GL, k)
func(*v) func(*v)
else: else:

View File

@ -1,4 +1,4 @@
from ..Qt import QtCore, QtGui, QtWidgets, QT_LIB from ..Qt import QtCore, QtGui, QtWidgets
from OpenGL.GL import * from OpenGL.GL import *
import OpenGL.GL.framebufferobjects as glfbo import OpenGL.GL.framebufferobjects as glfbo
import numpy as np import numpy as np
@ -8,7 +8,6 @@ import warnings
from math import cos, sin, tan, radians from math import cos, sin, tan, radians
##Vector = QtGui.QVector3D ##Vector = QtGui.QVector3D
ShareWidget = None
class GLViewWidget(QtWidgets.QOpenGLWidget): class GLViewWidget(QtWidgets.QOpenGLWidget):
@ -99,10 +98,11 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
def addItem(self, item): def addItem(self, item):
self.items.append(item) self.items.append(item)
if hasattr(item, 'initializeGL'):
if self.isValid():
self.makeCurrent() self.makeCurrent()
try: try:
item.initializeGL() item.initialize()
except: except:
self.checkOpenGLVersion('Error while adding item %s to GLViewWidget.' % str(item)) self.checkOpenGLVersion('Error while adding item %s to GLViewWidget.' % str(item))
@ -128,7 +128,12 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
self.update() self.update()
def initializeGL(self): def initializeGL(self):
self.resizeGL(self.width(), self.height()) """
Initialize items that were not initialized during addItem().
"""
for item in self.items:
if not item.isInitialized():
item.initialize()
def setBackgroundColor(self, *args, **kwds): def setBackgroundColor(self, *args, **kwds):
""" """
@ -463,13 +468,9 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
#self.swapBuffers() #self.swapBuffers()
def wheelEvent(self, ev): def wheelEvent(self, ev):
delta = 0 delta = ev.angleDelta().x()
if QT_LIB in ['PyQt4', 'PySide']: if delta == 0:
delta = ev.delta() delta = ev.angleDelta().y()
else:
delta = ev.angleDelta().x()
if delta == 0:
delta = ev.angleDelta().y()
if (ev.modifiers() & QtCore.Qt.KeyboardModifier.ControlModifier): if (ev.modifiers() & QtCore.Qt.KeyboardModifier.ControlModifier):
self.opts['fov'] *= 0.999**delta self.opts['fov'] *= 0.999**delta
else: else:

View File

@ -54,9 +54,6 @@ class GLLinePlotItem(GLGraphicsItem):
#self.vbo.pop(arg, None) #self.vbo.pop(arg, None)
self.update() self.update()
def initializeGL(self):
pass
def paint(self): def paint(self):
if self.pos is None: if self.pos is None:
return return

View File

@ -317,13 +317,9 @@ class GraphicsView(QtGui.QGraphicsView):
super().wheelEvent(ev) super().wheelEvent(ev)
if not self.mouseEnabled: if not self.mouseEnabled:
return return
delta = 0 delta = ev.angleDelta().x()
if QT_LIB in ['PyQt4', 'PySide']: if delta == 0:
delta = ev.delta() delta = ev.angleDelta().y()
else:
delta = ev.angleDelta().x()
if delta == 0:
delta = ev.angleDelta().y()
sc = 1.001 ** delta sc = 1.001 ** delta
#self.scale *= sc #self.scale *= sc