edge_item.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 python_qt_binding.QtCore import QPointF, Qt
00032 from python_qt_binding.QtGui import QBrush, QGraphicsPathItem, QGraphicsPolygonItem, QGraphicsSimpleTextItem, QPainterPath, QPen, QPolygonF
00033 
00034 from .graph_item import GraphItem
00035 
00036 
00037 class EdgeItem(GraphItem):
00038 
00039     def __init__(self, highlight_level, spline, label_center, label, from_node, to_node, parent=None):
00040         super(EdgeItem, self).__init__(highlight_level, parent)
00041 
00042         self.from_node = from_node
00043         self.from_node.add_outgoing_edge(self)
00044         self.to_node = to_node
00045         self.to_node.add_incoming_edge(self)
00046 
00047         self._default_color = self._COLOR_BLACK
00048         self._brush = QBrush(self._default_color)
00049         self._label_pen = QPen()
00050         self._label_pen.setColor(self._default_color)
00051         self._label_pen.setJoinStyle(Qt.RoundJoin)
00052         self._edge_pen = QPen(self._label_pen)
00053         self._edge_pen.setWidth(1)
00054 
00055         self._sibling_edges = set()
00056 
00057         self._label = None
00058         if label is not None:
00059             self._label = QGraphicsSimpleTextItem(label)
00060             label_rect = self._label.boundingRect()
00061             label_rect.moveCenter(label_center)
00062             self._label.setPos(label_rect.x(), label_rect.y())
00063             self._label.hoverEnterEvent = self._handle_hoverEnterEvent
00064             self._label.hoverLeaveEvent = self._handle_hoverLeaveEvent
00065             self._label.setAcceptHoverEvents(True)
00066 
00067         # spline specification according to http://www.graphviz.org/doc/info/attrs.html#k:splineType
00068         coordinates = spline.split(' ')
00069         # extract optional end_point
00070         end_point = None
00071         if (coordinates[0].startswith('e,')):
00072             parts = coordinates.pop(0)[2:].split(',')
00073             end_point = QPointF(float(parts[0]), -float(parts[1]))
00074         # extract optional start_point
00075         if (coordinates[0].startswith('s,')):
00076             parts = coordinates.pop(0).split(',')
00077 
00078         # first point
00079         parts = coordinates.pop(0).split(',')
00080         point = QPointF(float(parts[0]), -float(parts[1]))
00081         path = QPainterPath(point)
00082 
00083         while len(coordinates) > 2:
00084             # extract triple of points for a cubic spline
00085             parts = coordinates.pop(0).split(',')
00086             point1 = QPointF(float(parts[0]), -float(parts[1]))
00087             parts = coordinates.pop(0).split(',')
00088             point2 = QPointF(float(parts[0]), -float(parts[1]))
00089             parts = coordinates.pop(0).split(',')
00090             point3 = QPointF(float(parts[0]), -float(parts[1]))
00091             path.cubicTo(point1, point2, point3)
00092 
00093         self._arrow = None
00094         if end_point is not None:
00095             # draw arrow
00096             self._arrow = QGraphicsPolygonItem()
00097             polygon = QPolygonF()
00098             polygon.append(point3)
00099             offset = QPointF(end_point - point3)
00100             corner1 = QPointF(-offset.y(), offset.x()) * 0.35
00101             corner2 = QPointF(offset.y(), -offset.x()) * 0.35
00102             polygon.append(point3 + corner1)
00103             polygon.append(end_point)
00104             polygon.append(point3 + corner2)
00105             self._arrow.setPolygon(polygon)
00106             self._arrow.hoverEnterEvent = self._handle_hoverEnterEvent
00107             self._arrow.hoverLeaveEvent = self._handle_hoverLeaveEvent
00108             self._arrow.setAcceptHoverEvents(True)
00109 
00110         self._path = QGraphicsPathItem()
00111         self._path.setPath(path)
00112         self.addToGroup(self._path)
00113 
00114         self.set_color()
00115 
00116     def add_to_scene(self, scene):
00117         scene.addItem(self)
00118         if self._label is not None:
00119             scene.addItem(self._label)
00120         if self._arrow is not None:
00121             scene.addItem(self._arrow)
00122 
00123     def setToolTip(self, tool_tip):
00124         super(EdgeItem, self).setToolTip(tool_tip)
00125         if self._label is not None:
00126             self._label.setToolTip(tool_tip)
00127         if self._arrow is not None:
00128             self._arrow.setToolTip(tool_tip)
00129 
00130     def add_sibling_edge(self, edge):
00131         self._sibling_edges.add(edge)
00132 
00133     def set_color(self, color=None):
00134         if color is None:
00135             color = self._default_color
00136 
00137         self._brush.setColor(color)
00138         self._edge_pen.setColor(color)
00139         self._label_pen.setColor(color)
00140 
00141         self._path.setPen(self._edge_pen)
00142         if self._arrow is not None:
00143             self._arrow.setBrush(self._brush)
00144             self._arrow.setPen(self._edge_pen)
00145         if self._label is not None:
00146             self._label.setBrush(self._brush)
00147             self._label.setPen(self._label_pen)
00148 
00149     def _handle_hoverEnterEvent(self, event):
00150         # hovered edge item in red
00151         self.set_color(self._COLOR_RED)
00152 
00153         if self._highlight_level > 1:
00154             if self.from_node != self.to_node:
00155                 # from-node in blue
00156                 self.from_node.set_color(self._COLOR_BLUE)
00157                 # to-node in green
00158                 self.to_node.set_color(self._COLOR_GREEN)
00159             else:
00160                 # from-node/in-node in teal
00161                 self.from_node.set_color(self._COLOR_TEAL)
00162                 self.to_node.set_color(self._COLOR_TEAL)
00163         if self._highlight_level > 2:
00164             # sibling edges in orange
00165             for sibling_edge in self._sibling_edges:
00166                 sibling_edge.set_color(self._COLOR_ORANGE)
00167 
00168     def _handle_hoverLeaveEvent(self, event):
00169         self.set_color()
00170         if self._highlight_level > 1:
00171             self.from_node.set_color()
00172             self.to_node.set_color()
00173         if self._highlight_level > 2:
00174             for sibling_edge in self._sibling_edges:
00175                 sibling_edge.set_color()


qt_dotgraph
Author(s): Thibault Kruse
autogenerated on Fri Jan 3 2014 11:44:08