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 _qt_pen_styles = {
00040 'dashed': Qt.DashLine,
00041 'dotted': Qt.DotLine,
00042 'solid': Qt.SolidLine,
00043 }
00044
00045 def __init__(self, highlight_level, spline, label_center, label, from_node, to_node, parent=None, penwidth=1, edge_color=None, style='solid'):
00046 super(EdgeItem, self).__init__(highlight_level, parent)
00047
00048 self.from_node = from_node
00049 self.from_node.add_outgoing_edge(self)
00050 self.to_node = to_node
00051 self.to_node.add_incoming_edge(self)
00052
00053 self._default_edge_color = self._COLOR_BLACK
00054 if edge_color is not None:
00055 self._default_edge_color = edge_color
00056
00057 self._default_text_color = self._COLOR_BLACK
00058 self._default_color = self._COLOR_BLACK
00059 self._text_brush = QBrush(self._default_color)
00060 self._shape_brush = QBrush(self._default_color)
00061 if style in ['dashed', 'dotted']:
00062 self._shape_brush = QBrush(Qt.transparent)
00063 self._label_pen = QPen()
00064 self._label_pen.setColor(self._default_text_color)
00065 self._label_pen.setJoinStyle(Qt.RoundJoin)
00066 self._edge_pen = QPen(self._label_pen)
00067 self._edge_pen.setWidth(penwidth)
00068 self._edge_pen.setColor(self._default_edge_color)
00069 self._edge_pen.setStyle(self._qt_pen_styles.get(style, Qt.SolidLine))
00070
00071 self._sibling_edges = set()
00072
00073 self._label = None
00074 if label is not None:
00075 self._label = QGraphicsSimpleTextItem(label)
00076 self._label.setFont(GraphItem._LABEL_FONT)
00077 label_rect = self._label.boundingRect()
00078 label_rect.moveCenter(label_center)
00079 self._label.setPos(label_rect.x(), label_rect.y())
00080 self._label.hoverEnterEvent = self._handle_hoverEnterEvent
00081 self._label.hoverLeaveEvent = self._handle_hoverLeaveEvent
00082 self._label.setAcceptHoverEvents(True)
00083
00084
00085 coordinates = spline.split(' ')
00086
00087 end_point = None
00088 if (coordinates[0].startswith('e,')):
00089 parts = coordinates.pop(0)[2:].split(',')
00090 end_point = QPointF(float(parts[0]), -float(parts[1]))
00091
00092 if (coordinates[0].startswith('s,')):
00093 parts = coordinates.pop(0).split(',')
00094
00095
00096 parts = coordinates.pop(0).split(',')
00097 point = QPointF(float(parts[0]), -float(parts[1]))
00098 path = QPainterPath(point)
00099
00100 while len(coordinates) > 2:
00101
00102 parts = coordinates.pop(0).split(',')
00103 point1 = QPointF(float(parts[0]), -float(parts[1]))
00104 parts = coordinates.pop(0).split(',')
00105 point2 = QPointF(float(parts[0]), -float(parts[1]))
00106 parts = coordinates.pop(0).split(',')
00107 point3 = QPointF(float(parts[0]), -float(parts[1]))
00108 path.cubicTo(point1, point2, point3)
00109
00110 self._arrow = None
00111 if end_point is not None:
00112
00113 self._arrow = QGraphicsPolygonItem()
00114 polygon = QPolygonF()
00115 polygon.append(point3)
00116 offset = QPointF(end_point - point3)
00117 corner1 = QPointF(-offset.y(), offset.x()) * 0.35
00118 corner2 = QPointF(offset.y(), -offset.x()) * 0.35
00119 polygon.append(point3 + corner1)
00120 polygon.append(end_point)
00121 polygon.append(point3 + corner2)
00122 self._arrow.setPolygon(polygon)
00123 self._arrow.hoverEnterEvent = self._handle_hoverEnterEvent
00124 self._arrow.hoverLeaveEvent = self._handle_hoverLeaveEvent
00125 self._arrow.setAcceptHoverEvents(True)
00126
00127 self._path = QGraphicsPathItem()
00128 self._path.setPath(path)
00129 self.addToGroup(self._path)
00130
00131 self.set_node_color()
00132 self.set_label_color()
00133
00134 def add_to_scene(self, scene):
00135 scene.addItem(self)
00136 if self._label is not None:
00137 scene.addItem(self._label)
00138 if self._arrow is not None:
00139 scene.addItem(self._arrow)
00140
00141 def setToolTip(self, tool_tip):
00142 super(EdgeItem, self).setToolTip(tool_tip)
00143 if self._label is not None:
00144 self._label.setToolTip(tool_tip)
00145 if self._arrow is not None:
00146 self._arrow.setToolTip(tool_tip)
00147
00148 def add_sibling_edge(self, edge):
00149 self._sibling_edges.add(edge)
00150
00151 def set_node_color(self, color=None):
00152 if color is None:
00153 self._label_pen.setColor(self._default_text_color)
00154 self._text_brush.setColor(self._default_color)
00155 if self._shape_brush.isOpaque():
00156 self._shape_brush.setColor(self._default_edge_color)
00157 self._edge_pen.setColor(self._default_edge_color)
00158 else:
00159 self._label_pen.setColor(color)
00160 self._text_brush.setColor(color)
00161 if self._shape_brush.isOpaque():
00162 self._shape_brush.setColor(color)
00163 self._edge_pen.setColor(color)
00164
00165 self._path.setPen(self._edge_pen)
00166 if self._arrow is not None:
00167 self._arrow.setBrush(self._shape_brush)
00168 self._arrow.setPen(self._edge_pen)
00169
00170 def set_label_color(self, color=None):
00171 if color is None:
00172 self._label_pen.setColor(self._default_text_color)
00173 else:
00174 self._label_pen.setColor(color)
00175
00176 if self._label is not None:
00177 self._label.setBrush(self._text_brush)
00178 self._label.setPen(self._label_pen)
00179
00180 def _handle_hoverEnterEvent(self, event):
00181
00182 self.set_node_color(self._COLOR_RED)
00183
00184 if self._highlight_level > 1:
00185 if self.from_node != self.to_node:
00186
00187 self.from_node.set_node_color(self._COLOR_BLUE)
00188
00189 self.to_node.set_node_color(self._COLOR_GREEN)
00190 else:
00191
00192 self.from_node.set_node_color(self._COLOR_TEAL)
00193 self.to_node.set_node_color(self._COLOR_TEAL)
00194 if self._highlight_level > 2:
00195
00196 for sibling_edge in self._sibling_edges:
00197 sibling_edge.set_node_color(self._COLOR_ORANGE)
00198
00199 def _handle_hoverLeaveEvent(self, event):
00200 self.set_node_color()
00201 if self._highlight_level > 1:
00202 self.from_node.set_node_color()
00203 self.to_node.set_node_color()
00204 if self._highlight_level > 2:
00205 for sibling_edge in self._sibling_edges:
00206 sibling_edge.set_node_color()