node_item.py
Go to the documentation of this file.
1 #
2 # License: Yujin
3 #
4 ##############################################################################
5 # Description
6 ##############################################################################
7 
8 """
9 .. module:: node_item
10  :platform: Unix
11  :synopsis: Repackaging of the limiting ROS qt_dotgraph.node_item module.
12 
13 Oh my spaghettified magnificence,
14 Bless my noggin with a tickle from your noodly appendages!
15 
16 """
17 
18 ##############################################################################
19 # Imports
20 ##############################################################################
21 
22 from python_qt_binding.QtCore import QPointF, Qt
23 from python_qt_binding.QtGui import QBrush, QPolygonF, QPainterPath, QPen
24 try: # indigo
25  from python_qt_binding.QtGui import QGraphicsEllipseItem, QGraphicsPolygonItem, QGraphicsRectItem, QGraphicsSimpleTextItem
26 except ImportError: # kinetic+ (pyqt5)
27  from python_qt_binding.QtWidgets import QGraphicsEllipseItem, QGraphicsPolygonItem, QGraphicsRectItem, QGraphicsSimpleTextItem
28 
29 from .graph_item import GraphItem
30 
31 ##############################################################################
32 # Classes
33 ##############################################################################
34 
35 
36 class NodeItem(GraphItem):
37 
38  def __init__(self, highlight_level, bounding_box, label, shape, color=None, parent=None, label_pos=None, tooltip=None):
39  super(NodeItem, self).__init__(highlight_level, parent)
40 
41  self._default_color = self._COLOR_BLACK if color is None else color
42  self._brush = QBrush(self._default_color)
43  self._label_pen = QPen()
44  self._label_pen.setColor(self._default_color)
45  self._label_pen.setJoinStyle(Qt.RoundJoin)
46  self._ellipse_pen = QPen(self._label_pen)
47  self._ellipse_pen.setWidth(1)
48 
49  self._incoming_edges = set()
50  self._outgoing_edges = set()
51 
52  if shape == 'box':
53  self._graphics_item = QGraphicsRectItem(bounding_box)
54 
55  # Since we don't have unique GraphicsItems other than Ellipse and Rect,
56  # Using Polygon to draw the following using bounding_box
57 
58  elif shape == 'octagon':
59  rect = bounding_box.getRect()
60  octagon_polygon = QPolygonF([QPointF(rect[0], rect[1] + 3 * rect[3] / 10),
61  QPointF(rect[0], rect[1] + 7 * rect[3] / 10),
62  QPointF(rect[0] + 3 * rect[2] / 10, rect[1] + rect[3]),
63  QPointF(rect[0] + 7 * rect[2] / 10, rect[1] + rect[3]),
64  QPointF(rect[0] + rect[2], rect[1] + 7 * rect[3] / 10),
65  QPointF(rect[0] + rect[2], rect[1] + 3 * rect[3] / 10),
66  QPointF(rect[0] + 7 * rect[2] / 10, rect[1]),
67  QPointF(rect[0] + 3 * rect[2] / 10, rect[1])])
68  self._graphics_item = QGraphicsPolygonItem(octagon_polygon)
69 
70  elif shape == 'doubleoctagon':
71  rect = bounding_box.getRect()
72  inner_fold = 3.0
73 
74  octagon_polygon = QPolygonF([QPointF(rect[0], rect[1] + 3 * rect[3] / 10),
75  QPointF(rect[0], rect[1] + 7 * rect[3] / 10),
76  QPointF(rect[0] + 3 * rect[2] / 10, rect[1] + rect[3]),
77  QPointF(rect[0] + 7 * rect[2] / 10, rect[1] + rect[3]),
78  QPointF(rect[0] + rect[2], rect[1] + 7 * rect[3] / 10),
79  QPointF(rect[0] + rect[2], rect[1] + 3 * rect[3] / 10),
80  QPointF(rect[0] + 7 * rect[2] / 10, rect[1]),
81  QPointF(rect[0] + 3 * rect[2] / 10, rect[1]),
82  # inner
83  QPointF(rect[0], rect[1] + 3 * rect[3] / 10),
84  QPointF(rect[0] + inner_fold, rect[1] + 3 * rect[3] / 10 + inner_fold / 2),
85  QPointF(rect[0] + inner_fold, rect[1] + 7 * rect[3] / 10 - inner_fold / 2),
86  QPointF(rect[0] + 3 * rect[2] / 10, rect[1] + rect[3] - inner_fold),
87  QPointF(rect[0] + 7 * rect[2] / 10, rect[1] + rect[3] - inner_fold),
88  QPointF(rect[0] + rect[2] - inner_fold, rect[1] + 7 * rect[3] / 10 - inner_fold / 2),
89  QPointF(rect[0] + rect[2] - inner_fold, rect[1] + 3 * rect[3] / 10 + inner_fold / 2),
90  QPointF(rect[0] + 7 * rect[2] / 10, rect[1] + inner_fold),
91  QPointF(rect[0] + 3 * rect[2] / 10, rect[1] + inner_fold),
92  QPointF(rect[0] + inner_fold, rect[1] + 3 * rect[3] / 10 + inner_fold / 2)
93  ])
94 
95  self._graphics_item = QGraphicsPolygonItem(octagon_polygon)
96 
97  elif shape == 'note':
98  rect = bounding_box.getRect()
99  note_polygon = QPolygonF([QPointF(rect[0] + 9 * rect[2] / 10, rect[1]),
100  QPointF(rect[0], rect[1]),
101  QPointF(rect[0], rect[1] + rect[3]),
102  QPointF(rect[0] + rect[2], rect[1] + rect[3]),
103  QPointF(rect[0] + rect[2], rect[1] + rect[3] / 5),
104  QPointF(rect[0] + 9 * rect[2] / 10, rect[1] + rect[3] / 5),
105  QPointF(rect[0] + 9 * rect[2] / 10, rect[1]),
106  QPointF(rect[0] + rect[2], rect[1] + rect[3] / 5),
107  QPointF(rect[0] + rect[2], rect[1] + rect[3] / 5)])
108  self._graphics_item = QGraphicsPolygonItem(note_polygon)
109 
110  else:
111  self._graphics_item = QGraphicsEllipseItem(bounding_box)
112  self.addToGroup(self._graphics_item)
113 
114  self._label = QGraphicsSimpleTextItem(label)
115  label_rect = self._label.boundingRect()
116  if label_pos is None:
117  label_rect.moveCenter(bounding_box.center())
118  else:
119  label_rect.moveCenter(label_pos)
120  self._label.setPos(label_rect.x(), label_rect.y())
121  self.addToGroup(self._label)
122  if tooltip is not None:
123  self.setToolTip(tooltip)
124 
125  self.set_node_color()
126 
127  self.setAcceptHoverEvents(True)
128 
129  self.hovershape = None
130 
131  def set_hovershape(self, newhovershape):
132  self.hovershape = newhovershape
133 
134  def shape(self):
135  if self.hovershape is not None:
136  path = QPainterPath()
137  path.addRect(self.hovershape)
138  return path
139  else:
140  return super(self.__class__, self).shape()
141 
142  def add_incoming_edge(self, edge):
143  self._incoming_edges.add(edge)
144 
145  def add_outgoing_edge(self, edge):
146  self._outgoing_edges.add(edge)
147 
148  def set_node_color(self, color=None):
149  if color is None:
150  color = self._default_color
151 
152  self._brush.setColor(color)
153  self._ellipse_pen.setColor(color)
154  self._label_pen.setColor(color)
155 
156  self._graphics_item.setPen(self._ellipse_pen)
157  self._label.setBrush(self._brush)
158  self._label.setPen(self._label_pen)
159 
160  def hoverEnterEvent(self, event):
161  # hovered node item in red
162  self.set_node_color(self._COLOR_RED)
163 
164  if self._highlight_level > 1:
165  cyclic_edges = self._incoming_edges.intersection(self._outgoing_edges)
166  # incoming edges in blue
167  incoming_nodes = set()
168  for incoming_edge in self._incoming_edges.difference(cyclic_edges):
169  incoming_edge.set_node_color(self._COLOR_BLUE)
170  if incoming_edge.from_node != self:
171  incoming_nodes.add(incoming_edge.from_node)
172  # outgoing edges in green
173  outgoing_nodes = set()
174  for outgoing_edge in self._outgoing_edges.difference(cyclic_edges):
175  outgoing_edge.set_node_color(self._COLOR_GREEN)
176  if outgoing_edge.to_node != self:
177  outgoing_nodes.add(outgoing_edge.to_node)
178  # incoming/outgoing edges in teal
179  for edge in cyclic_edges:
180  edge.set_node_color(self._COLOR_TEAL)
181 
182  if self._highlight_level > 2:
183  cyclic_nodes = incoming_nodes.intersection(outgoing_nodes)
184  # incoming nodes in blue
185  for incoming_node in incoming_nodes.difference(cyclic_nodes):
186  incoming_node.set_node_color(self._COLOR_BLUE)
187  # outgoing nodes in green
188  for outgoing_node in outgoing_nodes.difference(cyclic_nodes):
189  outgoing_node.set_node_color(self._COLOR_GREEN)
190  # incoming/outgoing nodes in teal
191  for node in cyclic_nodes:
192  node.set_node_color(self._COLOR_TEAL)
193 
194  def hoverLeaveEvent(self, event):
195  self.set_node_color()
196  if self._highlight_level > 1:
197  for incoming_edge in self._incoming_edges:
198  incoming_edge.set_node_color()
199  if self._highlight_level > 2 and incoming_edge.from_node != self:
200  incoming_edge.from_node.set_node_color()
201  for outgoing_edge in self._outgoing_edges:
202  outgoing_edge.set_node_color()
203  if self._highlight_level > 2 and outgoing_edge.to_node != self:
204  outgoing_edge.to_node.set_node_color()
def __init__(self, highlight_level, bounding_box, label, shape, color=None, parent=None, label_pos=None, tooltip=None)
Definition: node_item.py:38
def set_hovershape(self, newhovershape)
Definition: node_item.py:131


rqt_py_trees
Author(s): Thibault Kruse, Michal Staniaszek, Daniel Stonier, Naveed Usmani
autogenerated on Mon Jun 10 2019 14:55:56