defer init if we do not have an OpenGL context yet

This commit is contained in:
KIU Shueng Chuan 2021-06-29 12:27:04 +08:00
parent 4a921ddf71
commit 1a29cf7579
2 changed files with 17 additions and 8 deletions

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,11 @@ 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 setInitialized(self, init=True):
self.__initialized = init
def initialized(self):
return self.__initialized
def initializeGL(self): def initializeGL(self):
""" """
@ -235,7 +240,7 @@ class GLGraphicsItem(QtCore.QObject):
The widget's GL context is made current before this method is called. 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.) (So this would be an appropriate time to generate lists, upload textures, etc.)
""" """
pass self.setInitialized()
def setupGLState(self): def setupGLState(self):
""" """
@ -245,7 +250,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

@ -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,12 @@ 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.initializeGL()
item.setInitialized()
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 +129,10 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
self.update() self.update()
def initializeGL(self): 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): def setBackgroundColor(self, *args, **kwds):
""" """