could be like this:
class MainWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)
uic.loadUi(_UI, self) # Load the .ui file
axis = gl.GLAxisItem()
grid = gl.GLGridItem()
grid.scale(100, 100, 100)
self.openGLWidget.addItem(grid)
self.openGLWidget.addItem(axis)
# installEventFilter
self.openGLWidget.installEventFilter(self)
def eventFilter(self, source, event):
if event.type() == QtCore.QEvent.MouseMove:
if event.buttons() == QtCore.Qt.NoButton:
print("Simple mouse motion")
pass
elif event.buttons() == QtCore.Qt.LeftButton:
# print("Left click drag")
pass
elif event.buttons() == QtCore.Qt.RightButton:
coef = 20 # Responsiveness of the pan
xx = ((self.openGLWidget.size().width())/2)/coef
yy = ((self.openGLWidget.size().height())/2)/coef
self.pan((event.x()/coef)-xx, (event.y()/coef)-yy, 0)
self.openGLWidget.updateGL()
# print("Right click drag")
elif event.type() == QtCore.QEvent.MouseButtonPress:
if event.button() == QtCore.Qt.RightButton:
pass
# print("Press!")
return super(MainWindow, self).eventFilter(source, event)
def pan(self, dx, dy, dz, relative='view'):
"""
Moves the center (look-at) position while holding the camera in place.
============== =======================================================
*relative* String that determines the direction of dx,dy,dz.
If "global", then the global coordinate system is used.
If "view", then the z axis is aligned with the view
If "view-upright", then x is in the global xy plane and
points to the right side of the view, y is in the
global xy plane and orthogonal to x, and z points in
the global z direction.
"""
# for backward compatibility:
relative = {True: "view-upright", False: "global"}.get(relative, relative)
if relative == 'global':
self.openGLWidget.opts['center'] += QtGui.QVector3D(dx, dy, dz)
elif relative == 'view-upright':
cPos = self.openGLWidget.cameraPosition()
cVec = self.openGLWidget.opts['center'] - cPos
dist = cVec.length() ## distance from camera to center
xDist = dist * 2. * np.tan(0.5 * self.openGLWidget.opts['fov'] * np.pi / 180.) ## approx. width of view at distance of center point
xScale = xDist / self.width()
zVec = QtGui.QVector3D(0,0,1)
xVec = QtGui.QVector3D.crossProduct(zVec, cVec).normalized()
yVec = QtGui.QVector3D.crossProduct(xVec, zVec).normalized()
self.openGLWidget.opts['center'] = self.openGLWidget.opts['center'] + xVec * xScale * dx + yVec * xScale * dy + zVec * xScale * dz
elif relative == 'view':
# pan in plane of camera
elev = np.radians(self.openGLWidget.opts['elevation'])
azim = np.radians(self.openGLWidget.opts['azimuth'])
fov = np.radians(self.openGLWidget.opts['fov'])
dist = (self.openGLWidget.opts['center'] - self.openGLWidget.cameraPosition()).length()
fov_factor = np.tan(fov / 2) * 2
scale_factor = dist * fov_factor / self.width()
z = scale_factor * np.cos(elev) * dy
x = scale_factor * (np.sin(azim) * dx - np.sin(elev) * np.cos(azim) * dy)
y = scale_factor * (np.cos(azim) * dx + np.sin(elev) * np.sin(azim) * dy)
self.openGLWidget.opts['center'] += QtGui.QVector3D(x, -y, z)
else:
raise ValueError("relative argument must be global, view, or view-upright")
self.update()