Merge pull request #1871 from pijyoi/opengl_init
Allow adding items to GLViewWidget before show
This commit is contained in:
commit
e83b91e9af
@ -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()
|
||||
|
@ -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,12 @@ class GLGraphicsItem(QtCore.QObject):
|
||||
view, as it may be obscured or outside of the current view area."""
|
||||
return self.__visible
|
||||
|
||||
def initialize(self):
|
||||
self.initializeGL()
|
||||
self.__initialized = True
|
||||
|
||||
def isInitialized(self):
|
||||
return self.__initialized
|
||||
|
||||
def initializeGL(self):
|
||||
"""
|
||||
@ -245,7 +251,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:
|
||||
|
@ -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
|
||||
@ -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,11 @@ 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.initialize()
|
||||
except:
|
||||
self.checkOpenGLVersion('Error while adding item %s to GLViewWidget.' % str(item))
|
||||
|
||||
@ -128,7 +128,12 @@ class GLViewWidget(QtWidgets.QOpenGLWidget):
|
||||
self.update()
|
||||
|
||||
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):
|
||||
"""
|
||||
@ -463,13 +468,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:
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user