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 Qt
00032 from python_qt_binding.QtGui import QBrush, QGraphicsEllipseItem, QGraphicsRectItem, QGraphicsSimpleTextItem, QPainterPath, QPen
00033
00034 from .graph_item import GraphItem
00035
00036
00037 class NodeItem(GraphItem):
00038
00039 def __init__(self, highlight_level, bounding_box, label, shape, color=None, parent=None, label_pos=None):
00040 super(NodeItem, self).__init__(highlight_level, parent)
00041
00042 self._default_color = self._COLOR_BLACK if color is None else color
00043 self._brush = QBrush(self._default_color)
00044 self._label_pen = QPen()
00045 self._label_pen.setColor(self._default_color)
00046 self._label_pen.setJoinStyle(Qt.RoundJoin)
00047 self._ellipse_pen = QPen(self._label_pen)
00048 self._ellipse_pen.setWidth(1)
00049
00050 self._incoming_edges = set()
00051 self._outgoing_edges = set()
00052
00053 if shape == 'box':
00054 self._graphics_item = QGraphicsRectItem(bounding_box)
00055 else:
00056 self._graphics_item = QGraphicsEllipseItem(bounding_box)
00057 self.addToGroup(self._graphics_item)
00058
00059 self._label = QGraphicsSimpleTextItem(label)
00060 label_rect = self._label.boundingRect()
00061 if label_pos is None:
00062 label_rect.moveCenter(bounding_box.center())
00063 else:
00064 label_rect.moveCenter(label_pos)
00065 self._label.setPos(label_rect.x(), label_rect.y())
00066 self.addToGroup(self._label)
00067
00068 self.set_color()
00069
00070 self.setAcceptHoverEvents(True)
00071
00072 self.hovershape = None
00073
00074 def set_hovershape(self, newhovershape):
00075 self.hovershape = newhovershape
00076
00077 def shape(self):
00078 if self.hovershape is not None:
00079 path = QPainterPath()
00080 path.addRect(self.hovershape)
00081 return path
00082 else:
00083 return super(self.__class__, self).shape()
00084
00085 def add_incoming_edge(self, edge):
00086 self._incoming_edges.add(edge)
00087
00088 def add_outgoing_edge(self, edge):
00089 self._outgoing_edges.add(edge)
00090
00091 def set_color(self, color=None):
00092 if color is None:
00093 color = self._default_color
00094
00095 self._brush.setColor(color)
00096 self._ellipse_pen.setColor(color)
00097 self._label_pen.setColor(color)
00098
00099 self._graphics_item.setPen(self._ellipse_pen)
00100 self._label.setBrush(self._brush)
00101 self._label.setPen(self._label_pen)
00102
00103 def hoverEnterEvent(self, event):
00104
00105 self.set_color(self._COLOR_RED)
00106
00107 if self._highlight_level > 1:
00108 cyclic_edges = self._incoming_edges.intersection(self._outgoing_edges)
00109
00110 incoming_nodes = set()
00111 for incoming_edge in self._incoming_edges.difference(cyclic_edges):
00112 incoming_edge.set_color(self._COLOR_BLUE)
00113 if incoming_edge.from_node != self:
00114 incoming_nodes.add(incoming_edge.from_node)
00115
00116 outgoing_nodes = set()
00117 for outgoing_edge in self._outgoing_edges.difference(cyclic_edges):
00118 outgoing_edge.set_color(self._COLOR_GREEN)
00119 if outgoing_edge.to_node != self:
00120 outgoing_nodes.add(outgoing_edge.to_node)
00121
00122 for edge in cyclic_edges:
00123 edge.set_color(self._COLOR_TEAL)
00124
00125 if self._highlight_level > 2:
00126 cyclic_nodes = incoming_nodes.intersection(outgoing_nodes)
00127
00128 for incoming_node in incoming_nodes.difference(cyclic_nodes):
00129 incoming_node.set_color(self._COLOR_BLUE)
00130
00131 for outgoing_node in outgoing_nodes.difference(cyclic_nodes):
00132 outgoing_node.set_color(self._COLOR_GREEN)
00133
00134 for node in cyclic_nodes:
00135 node.set_color(self._COLOR_TEAL)
00136
00137 def hoverLeaveEvent(self, event):
00138 self.set_color()
00139 if self._highlight_level > 1:
00140 for incoming_edge in self._incoming_edges:
00141 incoming_edge.set_color()
00142 if self._highlight_level > 2 and incoming_edge.from_node != self:
00143 incoming_edge.from_node.set_color()
00144 for outgoing_edge in self._outgoing_edges:
00145 outgoing_edge.set_color()
00146 if self._highlight_level > 2 and outgoing_edge.to_node != self:
00147 outgoing_edge.to_node.set_color()