Skip to content

refactor GLViewWidget code into GLViewMixin#2659

Merged
j9ac9k merged 2 commits intopyqtgraph:masterfrom
pijyoi:glviewmixin
May 17, 2023
Merged

refactor GLViewWidget code into GLViewMixin#2659
j9ac9k merged 2 commits intopyqtgraph:masterfrom
pijyoi:glviewmixin

Conversation

@pijyoi
Copy link
Copy Markdown
Contributor

@pijyoi pijyoi commented Mar 23, 2023

Some less-often used features of GLViewWidget no longer work with QOpenGLWidget but do work with QOpenGLWindow. This code factors out the "meat" of GLViewWidget into GLViewMixin so that users can more easily inherit from it to use with QOpenGLWindow.

Sample usage:

import importlib
import pyqtgraph as pg
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QT_LIB, QtWidgets
from pyqtgraph.opengl.GLViewWidget import GLViewMixin
QtOpenGL = importlib.import_module(f'{QT_LIB}.QtOpenGL')    # Qt6
import numpy as np

class GLViewWindow(GLViewMixin, QtOpenGL.QOpenGLWindow):
    def mouseReleaseEvent(self, ev):
        lpos = ev.position() if hasattr(ev, 'position') else ev.localPos()
        region = [lpos.x()-5, lpos.y()-5, 10, 10]
        # itemsAt seems to take in device pixels
        dpr = self.devicePixelRatioF()
        region = tuple([x * dpr for x in region])
        items = self.itemsAt(region)
        for item in items:
            print(item.objectName())

        if items:
            self.makeCurrent()
            ctx = self.context()
            x, y, w, h = region
            region = (x, self.deviceHeight()-(y+h), w, h)
            self.paintGL(region=region)
            ctx.swapBuffers(ctx.surface())

pg.mkQApp()

glv = GLViewWindow()
glv.setCameraParams(elevation=90, azimuth=-90, distance=50)
# X points right, Y points up
glv.show()
container = QtWidgets.QWidget.createWindowContainer(glv)
container.show()

side = 8
names = ['red', 'green', 'blue']
for idx, name in enumerate(names):
    box = np.zeros((side, side, 4), dtype=np.uint8)
    box[..., [idx, 3]] = 255
    img = gl.GLImageItem(box)
    img.setObjectName(name)
    img.translate(-side/2, -side/2, 0)  # center the box
    img.translate((idx-1)*side*2, (idx-1)*side*1, 0)
    glv.addItem(img)

pg.exec()

Non-satisfactory parts

Details
  • docstrings now belong to GLViewMixin.
  • we don't really want to support GLViewMixin. It's a use as-is if you want class.
  • it would break any existing code that sub-classes from the old GLViewWidget, since the __init__ methods have to be called manually

@pijyoi
Copy link
Copy Markdown
Contributor Author

pijyoi commented May 14, 2023

All the following invocations should work.
i.e. parent can be both positional and keyword.

import pyqtgraph as pg
import pyqtgraph.opengl as gl

pg.mkQApp()
gl.GLViewWidget()
gl.GLViewWidget(None)
gl.GLViewWidget(parent=None)
gl.GLViewWidget(rotationMethod='euler')
gl.GLViewWidget(None, rotationMethod='euler')
gl.GLViewWidget(parent=None, rotationMethod='euler')
gl.GLViewWidget(rotationMethod='euler', parent=None)

Although I don't quite understand the utility of specifying the parent on instantiation.
The following doesn't fit the GLViewWidget to its parent's space.

import pyqtgraph as pg
import pyqtgraph.opengl as gl
from pyqtgraph.Qt import QtWidgets

pg.mkQApp()
main = QtWidgets.QWidget()
glw = gl.GLViewWidget(parent=main)
main.show()
pg.exec()

@pijyoi pijyoi marked this pull request as ready for review May 16, 2023 13:54
@j9ac9k
Copy link
Copy Markdown
Member

j9ac9k commented May 17, 2023

Thanks @pijyoi for this PR, sorry for my indecisiveness regarding the documentation blurb. This LGTM, I am curious if it will get adopted much.

@j9ac9k j9ac9k merged commit 7497a67 into pyqtgraph:master May 17, 2023
@pijyoi pijyoi deleted the glviewmixin branch May 17, 2023 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants