Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 from __future__ import division
00032
00033 from python_qt_binding.QtCore import QPointF, QRectF, Qt
00034 from python_qt_binding.QtGui import QTransform
00035 from python_qt_binding.QtWidgets import QGraphicsView
00036
00037
00038 class InteractiveGraphicsView(QGraphicsView):
00039
00040 def __init__(self, parent=None):
00041 super(InteractiveGraphicsView, self).__init__(parent)
00042 self.setObjectName('InteractiveGraphicsView')
00043
00044 self._last_pan_point = None
00045 self._last_scene_center = None
00046
00047 def mousePressEvent(self, mouse_event):
00048 self._last_pan_point = mouse_event.pos()
00049 self._last_scene_center = self._map_to_scene_f(QRectF(self.frameRect()).center())
00050 self.setCursor(Qt.ClosedHandCursor)
00051
00052 def mouseReleaseEvent(self, mouse_event):
00053 self.setCursor(Qt.OpenHandCursor)
00054 self._last_pan_point = None
00055
00056 def mouseMoveEvent(self, mouse_event):
00057 if self._last_pan_point is not None:
00058 delta_scene = self.mapToScene(mouse_event.pos()) - self.mapToScene(self._last_pan_point)
00059 if not delta_scene.isNull():
00060 self.centerOn(self._last_scene_center - delta_scene)
00061 self._last_scene_center -= delta_scene
00062 self._last_pan_point = mouse_event.pos()
00063 QGraphicsView.mouseMoveEvent(self, mouse_event)
00064
00065 def wheelEvent(self, wheel_event):
00066 if wheel_event.modifiers() == Qt.NoModifier:
00067 try:
00068 delta = wheel_event.angleDelta().y()
00069 except AttributeError:
00070 delta = wheel_event.delta()
00071 delta = max(min(delta, 480), -480)
00072 mouse_before_scale_in_scene = self.mapToScene(wheel_event.pos())
00073
00074 scale_factor = 1 + (0.2 * (delta / 120.0))
00075 scaling = QTransform(scale_factor, 0, 0, scale_factor, 0, 0)
00076 self.setTransform(self.transform() * scaling)
00077
00078 mouse_after_scale_in_scene = self.mapToScene(wheel_event.pos())
00079 center_in_scene = self.mapToScene(self.frameRect().center())
00080 self.centerOn(
00081 center_in_scene + mouse_before_scale_in_scene - mouse_after_scale_in_scene)
00082
00083 wheel_event.accept()
00084 else:
00085 QGraphicsView.wheelEvent(self, wheel_event)
00086
00087 def _map_to_scene_f(self, pointf):
00088 point = pointf.toPoint()
00089 if pointf.x() == point.x() and pointf.y() == point.y():
00090
00091 return self.mapToScene(point)
00092 elif pointf.x() == point.x():
00093
00094 pointA = self.mapToScene((pointf + QPointF(0, -0.5)).toPoint())
00095 pointB = self.mapToScene((pointf + QPointF(0, 0.5)).toPoint())
00096 return (pointA + pointB) / 2.0
00097 elif pointf.y() == point.y():
00098
00099 pointA = self.mapToScene((pointf + QPointF(-0.5, 0)).toPoint())
00100 pointB = self.mapToScene((pointf + QPointF(0.5, 0)).toPoint())
00101 return (pointA + pointB) / 2.0
00102 else:
00103
00104 pointA = self.mapToScene((pointf + QPointF(-0.5, -0.5)).toPoint())
00105 pointB = self.mapToScene((pointf + QPointF(-0.5, 0.5)).toPoint())
00106 pointC = self.mapToScene((pointf + QPointF(0.5, -0.5)).toPoint())
00107 pointD = self.mapToScene((pointf + QPointF(0.5, 0.5)).toPoint())
00108 return (pointA + pointB + pointC + pointD) / 4.0