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


qt_dotgraph
Author(s): Thibault Kruse
autogenerated on Mon Oct 6 2014 03:57:59