From 394b0dd75c7f94e8aad2fcb9b3da757fb9e57a89 Mon Sep 17 00:00:00 2001 From: KIU Shueng Chuan Date: Mon, 19 Jul 2021 06:57:07 +0800 Subject: [PATCH] implement set/get for cameraParams --- examples/GLPainterItem.py | 5 ++--- pyqtgraph/opengl/GLViewWidget.py | 31 +++++++++++++++---------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/examples/GLPainterItem.py b/examples/GLPainterItem.py index 683e4dc5..ae54b66a 100644 --- a/examples/GLPainterItem.py +++ b/examples/GLPainterItem.py @@ -48,10 +48,9 @@ class GLPainterItem(pg.opengl.GLGraphicsItem.GLGraphicsItem): painter.drawText(rect, af.AlignBottom | af.AlignLeft, 'BL') painter.drawText(rect, af.AlignBottom | af.AlignRight, 'BR') - opts = self.view().getCameraPosition() - opts['fov'] = self.view().fov() + opts = self.view().cameraParams() lines = [] - center = opts['pos'] + center = opts['center'] lines.append(f"center : ({center.x():.1f}, {center.y():.1f}, {center.z():.1f})") for key in ['distance', 'fov', 'elevation', 'azimuth']: lines.append(f"{key} : {opts[key]:.1f}") diff --git a/pyqtgraph/opengl/GLViewWidget.py b/pyqtgraph/opengl/GLViewWidget.py index 97aa1fda..3afa7388 100644 --- a/pyqtgraph/opengl/GLViewWidget.py +++ b/pyqtgraph/opengl/GLViewWidget.py @@ -308,22 +308,6 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): self.update() - def getCameraPosition(self): - opts = { 'pos': self.opts['center'], - 'distance' : self.opts['distance'] } - if self.opts['rotationMethod'] == "quaternion": - opts['rotation'] = self.opts['rotation'] - else: - opts['elevation'] = self.opts['elevation'] - opts['azimuth'] = self.opts['azimuth'] - return opts - - def setFov(self, fov): - self.opts['fov'] = fov - - def fov(self): - return self.opts['fov'] - def cameraPosition(self): """Return current position of camera based on center, dist, elevation, and azimuth""" center = self.opts['center'] @@ -341,6 +325,21 @@ class GLViewWidget(QtWidgets.QOpenGLWidget): ) return pos + def setCameraParams(self, **kwds): + valid_keys = {'center', 'rotation', 'distance', 'fov', 'elevation', 'azimuth'} + if not set(kwds).issubset(valid_keys): + raise ValueError(f'valid keywords are {valid_keys}') + + self.setCameraPosition(pos=kwds.get('center'), distance=kwds.get('distance'), + elevation=kwds.get('elevation'), azimuth=kwds.get('azimuth'), + rotation=kwds.get('rotation')) + if 'fov' in kwds: + self.opts['fov'] = kwds['fov'] + + def cameraParams(self): + valid_keys = {'center', 'rotation', 'distance', 'fov', 'elevation', 'azimuth'} + return { k : self.opts[k] for k in valid_keys } + def orbit(self, azim, elev): """Orbits the camera around the center position. *azim* and *elev* are given in degrees.""" if self.opts['rotationMethod'] == 'quaternion':