interactive_graphics_view.py
Go to the documentation of this file.
00001 #
00002 # License: BSD
00003 #   https://raw.github.com/robotics-in-concert/rocon_qt_gui/license/LICENSE
00004 #
00005 ##############################################################################
00006 # Imports
00007 ##############################################################################
00008 
00009 from __future__ import division
00010 
00011 from python_qt_binding.QtCore import QPointF, QRectF, Qt
00012 from python_qt_binding.QtGui import QGraphicsView, QTransform
00013 
00014 ##############################################################################
00015 # Class
00016 ##############################################################################
00017 
00018 
00019 class InteractiveGraphicsView(QGraphicsView):
00020 
00021     def __init__(self, parent=None):
00022         super(InteractiveGraphicsView, self).__init__(parent)
00023         self.setObjectName('InteractiveGraphicsView')
00024 
00025         self._last_pan_point = None
00026         self._last_scene_center = None
00027 
00028     def mousePressEvent(self, mouse_event):
00029         self._last_pan_point = mouse_event.pos()
00030         self._last_scene_center = self._map_to_scene_f(QRectF(self.frameRect()).center())
00031         self.setCursor(Qt.ClosedHandCursor)
00032 
00033     def mouseReleaseEvent(self, mouse_event):
00034         self.setCursor(Qt.OpenHandCursor)
00035         self._last_pan_point = None
00036 
00037     def mouseMoveEvent(self, mouse_event):
00038         if self._last_pan_point is not None:
00039             delta_scene = self.mapToScene(mouse_event.pos()) - self.mapToScene(self._last_pan_point)
00040             if not delta_scene.isNull():
00041                 self.centerOn(self._last_scene_center - delta_scene)
00042                 self._last_scene_center -= delta_scene
00043             self._last_pan_point = mouse_event.pos()
00044         QGraphicsView.mouseMoveEvent(self, mouse_event)
00045 
00046     def wheelEvent(self, wheel_event):
00047         if wheel_event.modifiers() == Qt.NoModifier:
00048             num_degrees = wheel_event.delta() / 8.0
00049             num_steps = num_degrees / 15.0
00050             mouse_before_scale_in_scene = self.mapToScene(wheel_event.pos())
00051 
00052             scale_factor = 1.2 * num_steps
00053             if num_steps < 0:
00054                 scale_factor = -1.0 / scale_factor
00055             scaling = QTransform(scale_factor, 0, 0, scale_factor, 0, 0)
00056             self.setTransform(self.transform() * scaling)
00057 
00058             mouse_after_scale_in_scene = self.mapToScene(wheel_event.pos())
00059             center_in_scene = self.mapToScene(self.frameRect().center())
00060             self.centerOn(center_in_scene + mouse_before_scale_in_scene - mouse_after_scale_in_scene)
00061 
00062             wheel_event.accept()
00063         else:
00064             QGraphicsView.wheelEvent(self, wheel_event)
00065 
00066     def _map_to_scene_f(self, pointf):
00067         point = pointf.toPoint()
00068         if pointf.x() == point.x() and pointf.y() == point.y():
00069             # map integer coordinates
00070             return self.mapToScene(point)
00071         elif pointf.x() == point.x():
00072             # map integer x and decimal y coordinates
00073             pointA = self.mapToScene((pointf + QPointF(0, -0.5)).toPoint())
00074             pointB = self.mapToScene((pointf + QPointF(0, 0.5)).toPoint())
00075             return (pointA + pointB) / 2.0
00076         elif pointf.y() == point.y():
00077             # map decimal x  and integer y and coordinates
00078             pointA = self.mapToScene((pointf + QPointF(-0.5, 0)).toPoint())
00079             pointB = self.mapToScene((pointf + QPointF(0.5, 0)).toPoint())
00080             return (pointA + pointB) / 2.0
00081         else:
00082             # map decimal coordinates
00083             pointA = self.mapToScene((pointf + QPointF(-0.5, -0.5)).toPoint())
00084             pointB = self.mapToScene((pointf + QPointF(-0.5, 0.5)).toPoint())
00085             pointC = self.mapToScene((pointf + QPointF(0.5, -0.5)).toPoint())
00086             pointD = self.mapToScene((pointf + QPointF(0.5, 0.5)).toPoint())
00087             return (pointA + pointB + pointC + pointD) / 4.0


rocon_gateway_graph
Author(s): Daniel Stonier
autogenerated on Fri Feb 12 2016 02:50:08