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, 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
00074 coordinates = spline.split(' ')
00075
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
00081 if (coordinates[0].startswith('s,')):
00082 parts = coordinates.pop(0).split(',')
00083
00084
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
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
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
00167 self.set_node_color(self._COLOR_RED)
00168
00169 if self._highlight_level > 1:
00170 if self.from_node != self.to_node:
00171
00172 self.from_node.set_node_color(self._COLOR_BLUE)
00173
00174 self.to_node.set_node_color(self._COLOR_GREEN)
00175 else:
00176
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
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()