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 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
00068 coordinates = spline.split(' ')
00069
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
00075 if (coordinates[0].startswith('s,')):
00076 parts = coordinates.pop(0).split(',')
00077
00078
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
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
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
00151 self.set_color(self._COLOR_RED)
00152
00153 if self._highlight_level > 1:
00154 if self.from_node != self.to_node:
00155
00156 self.from_node.set_color(self._COLOR_BLUE)
00157
00158 self.to_node.set_color(self._COLOR_GREEN)
00159 else:
00160
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
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()