interactive_graphics_view.py
Go to the documentation of this file.
00001 # Copyright (c) 2011, Dirk Thomas, TU Darmstadt
00002 # All rights reserved.
00003 #
00004 # Redistribution and use in source and binary forms, with or without
00005 # modification, are permitted provided that the following conditions
00006 # are met:
00007 #
00008 #   * Redistributions of source code must retain the above copyright
00009 #     notice, this list of conditions and the following disclaimer.
00010 #   * Redistributions in binary form must reproduce the above
00011 #     copyright notice, this list of conditions and the following
00012 #     disclaimer in the documentation and/or other materials provided
00013 #     with the distribution.
00014 #   * Neither the name of the TU Darmstadt nor the names of its
00015 #     contributors may be used to endorse or promote products derived
00016 #     from this software without specific prior written permission.
00017 #
00018 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00025 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00028 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00029 # POSSIBILITY OF SUCH DAMAGE.
00030 
00031 from __future__ import division
00032 
00033 from python_qt_binding.QtCore import QPointF, QRectF, Qt
00034 from python_qt_binding.QtGui import QGraphicsView, QTransform
00035 
00036 
00037 class InteractiveGraphicsView(QGraphicsView):
00038 
00039     def __init__(self, parent=None):
00040         super(InteractiveGraphicsView, self).__init__(parent)
00041         self.setObjectName('InteractiveGraphicsView')
00042 
00043         self._last_pan_point = None
00044         self._last_scene_center = None
00045 
00046     def mousePressEvent(self, mouse_event):
00047         self._last_pan_point = mouse_event.pos()
00048         self._last_scene_center = self._map_to_scene_f(QRectF(self.frameRect()).center())
00049         self.setCursor(Qt.ClosedHandCursor)
00050 
00051     def mouseReleaseEvent(self, mouse_event):
00052         self.setCursor(Qt.OpenHandCursor)
00053         self._last_pan_point = None
00054 
00055     def mouseMoveEvent(self, mouse_event):
00056         if self._last_pan_point is not None:
00057             delta_scene = self.mapToScene(mouse_event.pos()) - self.mapToScene(self._last_pan_point)
00058             if not delta_scene.isNull():
00059                 self.centerOn(self._last_scene_center - delta_scene)
00060                 self._last_scene_center -= delta_scene
00061             self._last_pan_point = mouse_event.pos()
00062         QGraphicsView.mouseMoveEvent(self, mouse_event)
00063 
00064     def wheelEvent(self, wheel_event):
00065         if wheel_event.modifiers() == Qt.NoModifier:
00066             num_degrees = wheel_event.delta() / 8.0
00067             num_steps = num_degrees / 15.0
00068             mouse_before_scale_in_scene = self.mapToScene(wheel_event.pos())
00069 
00070             scale_factor = 1.2 * num_steps
00071             if num_steps < 0:
00072                 scale_factor = -1.0 / scale_factor
00073             scaling = QTransform(scale_factor, 0, 0, scale_factor, 0, 0)
00074             self.setTransform(self.transform() * scaling)
00075 
00076             mouse_after_scale_in_scene = self.mapToScene(wheel_event.pos())
00077             center_in_scene = self.mapToScene(self.frameRect().center())
00078             self.centerOn(center_in_scene + mouse_before_scale_in_scene - mouse_after_scale_in_scene)
00079 
00080             wheel_event.accept()
00081         else:
00082             QGraphicsView.wheelEvent(self, wheel_event)
00083 
00084     def _map_to_scene_f(self, pointf):
00085         point = pointf.toPoint()
00086         if pointf.x() == point.x() and pointf.y() == point.y():
00087             # map integer coordinates
00088             return self.mapToScene(point)
00089         elif pointf.x() == point.x():
00090             # map integer x and decimal y coordinates
00091             pointA = self.mapToScene((pointf + QPointF(0, -0.5)).toPoint())
00092             pointB = self.mapToScene((pointf + QPointF(0, 0.5)).toPoint())
00093             return (pointA + pointB) / 2.0
00094         elif pointf.y() == point.y():
00095             # map decimal x  and integer y and coordinates
00096             pointA = self.mapToScene((pointf + QPointF(-0.5, 0)).toPoint())
00097             pointB = self.mapToScene((pointf + QPointF(0.5, 0)).toPoint())
00098             return (pointA + pointB) / 2.0
00099         else:
00100             # map decimal coordinates
00101             pointA = self.mapToScene((pointf + QPointF(-0.5, -0.5)).toPoint())
00102             pointB = self.mapToScene((pointf + QPointF(-0.5, 0.5)).toPoint())
00103             pointC = self.mapToScene((pointf + QPointF(0.5, -0.5)).toPoint())
00104             pointD = self.mapToScene((pointf + QPointF(0.5, 0.5)).toPoint())
00105             return (pointA + pointB + pointC + pointD) / 4.0


rqt_graph
Author(s): Dirk Thomas
autogenerated on Fri Jan 3 2014 11:54:23