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, tooltip=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 self._label.setFont(GraphItem._LABEL_FONT)
00061 label_rect = self._label.boundingRect()
00062 if label_pos is None:
00063 label_rect.moveCenter(bounding_box.center())
00064 else:
00065 label_rect.moveCenter(label_pos)
00066 self._label.setPos(label_rect.x(), label_rect.y())
00067 self.addToGroup(self._label)
00068 if tooltip is not None:
00069 self.setToolTip(tooltip)
00070
00071 self.set_node_color()
00072
00073 self.setAcceptHoverEvents(True)
00074
00075 self.hovershape = None
00076
00077 def set_hovershape(self, newhovershape):
00078 self.hovershape = newhovershape
00079
00080 def shape(self):
00081 if self.hovershape is not None:
00082 path = QPainterPath()
00083 path.addRect(self.hovershape)
00084 return path
00085 else:
00086 return super(self.__class__, self).shape()
00087
00088 def add_incoming_edge(self, edge):
00089 self._incoming_edges.add(edge)
00090
00091 def add_outgoing_edge(self, edge):
00092 self._outgoing_edges.add(edge)
00093
00094 def set_node_color(self, color=None):
00095 if color is None:
00096 color = self._default_color
00097
00098 self._brush.setColor(color)
00099 self._ellipse_pen.setColor(color)
00100 self._label_pen.setColor(color)
00101
00102 self._graphics_item.setPen(self._ellipse_pen)
00103 self._label.setBrush(self._brush)
00104 self._label.setPen(self._label_pen)
00105
00106 def hoverEnterEvent(self, event):
00107
00108 self.set_node_color(self._COLOR_RED)
00109
00110 if self._highlight_level > 1:
00111 cyclic_edges = self._incoming_edges.intersection(self._outgoing_edges)
00112
00113 incoming_nodes = set()
00114 for incoming_edge in self._incoming_edges.difference(cyclic_edges):
00115 incoming_edge.set_node_color(self._COLOR_BLUE)
00116 if incoming_edge.from_node != self:
00117 incoming_nodes.add(incoming_edge.from_node)
00118
00119 outgoing_nodes = set()
00120 for outgoing_edge in self._outgoing_edges.difference(cyclic_edges):
00121 outgoing_edge.set_node_color(self._COLOR_GREEN)
00122 if outgoing_edge.to_node != self:
00123 outgoing_nodes.add(outgoing_edge.to_node)
00124
00125 for edge in cyclic_edges:
00126 edge.set_node_color(self._COLOR_TEAL)
00127
00128 if self._highlight_level > 2:
00129 cyclic_nodes = incoming_nodes.intersection(outgoing_nodes)
00130
00131 for incoming_node in incoming_nodes.difference(cyclic_nodes):
00132 incoming_node.set_node_color(self._COLOR_BLUE)
00133
00134 for outgoing_node in outgoing_nodes.difference(cyclic_nodes):
00135 outgoing_node.set_node_color(self._COLOR_GREEN)
00136
00137 for node in cyclic_nodes:
00138 node.set_node_color(self._COLOR_TEAL)
00139
00140 def hoverLeaveEvent(self, event):
00141 self.set_node_color()
00142 if self._highlight_level > 1:
00143 for incoming_edge in self._incoming_edges:
00144 incoming_edge.set_node_color()
00145 if self._highlight_level > 2 and incoming_edge.from_node != self:
00146 incoming_edge.from_node.set_node_color()
00147 for outgoing_edge in self._outgoing_edges:
00148 outgoing_edge.set_node_color()
00149 if self._highlight_level > 2 and outgoing_edge.to_node != self:
00150 outgoing_edge.to_node.set_node_color()