# -*- coding: utf-8 -*- """ This module contains the class which inherits from QtWidgets.QGraphicsView in order to add some custom functionality to a graphics view. Particularly, navigation controls for navigating the viewport. """ from artv2.third_party.Qt import QtWidgets, QtGui, QtCore class ARTv2GraphicsView(QtWidgets.QGraphicsView): # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def __init__(self, scene, parent=None): super(ARTv2GraphicsView, self).__init__(parent) self.scene = scene self._init_interface() self.setScene(self.scene) self.zoom_in_factor = 1.25 self.current_zoom = 10 self.zoom_step = 1 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _init_interface(self): """ Set some flags for the graphics view. """ self.setRenderHints(QtGui.QPainter.Antialiasing | QtGui.QPainter.HighQualityAntialiasing | QtGui.QPainter.TextAntialiasing | QtGui.QPainter.SmoothPixmapTransform) self.setViewportUpdateMode(QtWidgets.QGraphicsView.FullViewportUpdate) self.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff) self.setTransformationAnchor(QtWidgets.QGraphicsView.AnchorUnderMouse) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def mousePressEvent(self, event): """ Override mouse press event for dragging around viewport with the MMB :param event: :return: """ if event.button() == QtCore.Qt.MiddleButton: self._middle_mouse_button_press(event) elif event.button() == QtCore.Qt.LeftButton: self._left_mouse_button_press(event) elif event.button() == QtCore.Qt.RightButton: self._right_mouse_button_press(event) else: super(ARTv2GraphicsView, self).mousePressEvent(event) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def mouseReleaseEvent(self, event): """ Override mouse press event for dragging around viewport with the MMB :param event: :return: """ if event.button() == QtCore.Qt.MiddleButton: self._middle_mouse_button_release(event) elif event.button() == QtCore.Qt.LeftButton: self._left_mouse_button_release(event) elif event.button() == QtCore.Qt.RightButton: self._right_mouse_button_release(event) else: super(ARTv2GraphicsView, self).mouseReleaseEvent(event) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def wheelEvent(self, event): """ OVerride mouse scroll event for zooming in and out. :param event: :return: """ zoom_out_factor = 1 / self.zoom_in_factor # if you are scrolling in (up) on the MMB if event.angleDelta().y() > 0: zoom_factor = self.zoom_in_factor self.current_zoom += self.zoom_step else: zoom_factor = zoom_out_factor self.current_zoom -= self.zoom_step self.scale(zoom_factor, zoom_factor) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _middle_mouse_button_press(self, event): """ By default, setting drag mode wanted to use the LMB, so this will take in a MMB event, and make the MMB do the same thing as the LMB :param event: :return: """ release_event = QtGui.QMouseEvent(QtCore.QEvent.MouseButtonRelease, event.localPos(), event.screenPos(), QtCore.Qt.LeftButton, QtCore.Qt.NoButton, event.modifiers()) super(ARTv2GraphicsView, self).mouseReleaseEvent(release_event) self.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag) fake_event = QtGui.QMouseEvent(event.type(), event.localPos(), event.screenPos(), QtCore.Qt.LeftButton, event.buttons() | QtCore.Qt.LeftButton, event.modifiers()) super(ARTv2GraphicsView, self).mousePressEvent(fake_event) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _left_mouse_button_press(self, event): return super(ARTv2GraphicsView, self).mousePressEvent(event) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _right_mouse_button_press(self, event): return super(ARTv2GraphicsView, self).mousePressEvent(event) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _middle_mouse_button_release(self, event): fake_event = QtGui.QMouseEvent(event.type(), event.localPos(), event.screenPos(), QtCore.Qt.LeftButton, event.buttons() | QtCore.Qt.LeftButton, event.modifiers()) super(ARTv2GraphicsView, self).mouseReleaseEvent(fake_event) self.setDragMode(QtWidgets.QGraphicsView.NoDrag) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _left_mouse_button_release(self, event): return super(ARTv2GraphicsView, self).mouseReleaseEvent(event) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # def _right_mouse_button_release(self, event): return super(ARTv2GraphicsView, self).mouseReleaseEvent(event)