edge_item.py
Go to the documentation of this file.
1 #
2 # License: Yujin
3 #
4 ##############################################################################
5 # Description
6 ##############################################################################
7 
8 """
9 .. module:: edge_item
10  :platform: Unix
11  :synopsis: Repackaging of the limiting ROS qt_dotgraph.edge_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, QPainterPath, QPen, QPolygonF
24 try: # indigo
25  from python_qt_binding.QtGui import QGraphicsPathItem, QGraphicsPolygonItem, QGraphicsSimpleTextItem
26 except ImportError: # kinetic+ (pyqt5)
27  from python_qt_binding.QtWidgets import QGraphicsPathItem, QGraphicsPolygonItem, QGraphicsSimpleTextItem
28 
29 from .graph_item import GraphItem
30 
31 ##############################################################################
32 # Classes
33 ##############################################################################
34 
35 
36 class EdgeItem(GraphItem):
37 
38  _qt_pen_styles = {
39  'dashed': Qt.DashLine,
40  'dotted': Qt.DotLine,
41  'solid': Qt.SolidLine,
42  }
43 
44  def __init__(self, highlight_level, spline, label_center, label, from_node, to_node, parent=None, penwidth=1, edge_color=None, style='solid'):
45  super(EdgeItem, self).__init__(highlight_level, parent)
46 
47  self.from_node = from_node
48  self.from_node.add_outgoing_edge(self)
49  self.to_node = to_node
50  self.to_node.add_incoming_edge(self)
51 
52  self._default_edge_color = self._COLOR_BLACK
53  self._default_shape_color = self._COLOR_BLACK
54  if edge_color is not None:
55  self._default_edge_color = edge_color
56  self._default_shape_color = edge_color
57 
58  self._default_text_color = self._COLOR_BLACK
59  self._default_color = self._COLOR_BLACK
60  self._text_brush = QBrush(self._default_color)
61  self._shape_brush = QBrush(self._default_color)
62  if style in ['dashed', 'dotted']:
63  self._shape_brush = QBrush(Qt.transparent)
64  self._label_pen = QPen()
65  self._label_pen.setColor(self._default_text_color)
66  self._label_pen.setJoinStyle(Qt.RoundJoin)
67  self._edge_pen = QPen(self._label_pen)
68  self._edge_pen.setWidth(penwidth)
69  self._edge_pen.setColor(self._default_edge_color)
70  self._edge_pen.setStyle(self._qt_pen_styles.get(style, Qt.SolidLine))
71  self._sibling_edges = set()
72 
73  self._label = None
74  if label is not None:
75  self._label = QGraphicsSimpleTextItem(label)
76  label_rect = self._label.boundingRect()
77  label_rect.moveCenter(label_center)
78  self._label.setPos(label_rect.x(), label_rect.y())
79  self._label.hoverEnterEvent = self._handle_hoverEnterEvent
80  self._label.hoverLeaveEvent = self._handle_hoverLeaveEvent
81  self._label.setAcceptHoverEvents(True)
82 
83  # spline specification according to http://www.graphviz.org/doc/info/attrs.html#k:splineType
84  coordinates = spline.split(' ')
85  # extract optional end_point
86  end_point = None
87  if (coordinates[0].startswith('e,')):
88  parts = coordinates.pop(0)[2:].split(',')
89  end_point = QPointF(float(parts[0]), -float(parts[1]))
90  # extract optional start_point
91  if (coordinates[0].startswith('s,')):
92  parts = coordinates.pop(0).split(',')
93 
94  # first point
95  parts = coordinates.pop(0).split(',')
96  point = QPointF(float(parts[0]), -float(parts[1]))
97  path = QPainterPath(point)
98 
99  while len(coordinates) > 2:
100  # extract triple of points for a cubic spline
101  parts = coordinates.pop(0).split(',')
102  point1 = QPointF(float(parts[0]), -float(parts[1]))
103  parts = coordinates.pop(0).split(',')
104  point2 = QPointF(float(parts[0]), -float(parts[1]))
105  parts = coordinates.pop(0).split(',')
106  point3 = QPointF(float(parts[0]), -float(parts[1]))
107  path.cubicTo(point1, point2, point3)
108 
109  self._arrow = None
110  if end_point is not None:
111  # draw arrow
112  self._arrow = QGraphicsPolygonItem()
113  polygon = QPolygonF()
114  polygon.append(point3)
115  offset = QPointF(end_point - point3)
116  corner1 = QPointF(-offset.y(), offset.x()) * 0.35
117  corner2 = QPointF(offset.y(), -offset.x()) * 0.35
118  polygon.append(point3 + corner1)
119  polygon.append(end_point)
120  polygon.append(point3 + corner2)
121  self._arrow.setPolygon(polygon)
122  self._arrow.hoverEnterEvent = self._handle_hoverEnterEvent
123  self._arrow.hoverLeaveEvent = self._handle_hoverLeaveEvent
124  self._arrow.setAcceptHoverEvents(True)
125 
126  self._path = QGraphicsPathItem()
127  self._path.setPath(path)
128  self.addToGroup(self._path)
129 
130  self.set_node_color()
131  self.set_label_color()
132 
133  def add_to_scene(self, scene):
134  scene.addItem(self)
135  if self._label is not None:
136  scene.addItem(self._label)
137  if self._arrow is not None:
138  scene.addItem(self._arrow)
139 
140  def setToolTip(self, tool_tip):
141  super(EdgeItem, self).setToolTip(tool_tip)
142  if self._label is not None:
143  self._label.setToolTip(tool_tip)
144  if self._arrow is not None:
145  self._arrow.setToolTip(tool_tip)
146 
147  def add_sibling_edge(self, edge):
148  self._sibling_edges.add(edge)
149 
150  def set_node_color(self, color=None):
151  if color is None:
152  self._label_pen.setColor(self._default_text_color)
153  self._text_brush.setColor(self._default_color)
154  if self._shape_brush.isOpaque():
155  self._shape_brush.setColor(self._default_shape_color)
156  self._edge_pen.setColor(self._default_edge_color)
157  else:
158  self._label_pen.setColor(color)
159  self._text_brush.setColor(color)
160  if self._shape_brush.isOpaque():
161  self._shape_brush.setColor(color)
162  self._edge_pen.setColor(color)
163 
164  self._path.setPen(self._edge_pen)
165  if self._arrow is not None:
166  self._arrow.setBrush(self._shape_brush)
167  self._arrow.setPen(self._edge_pen)
168 
169  def set_label_color(self, color=None):
170  if color is None:
171  self._label_pen.setColor(self._default_text_color)
172  else:
173  self._label_pen.setColor(color)
174 
175  if self._label is not None:
176  self._label.setBrush(self._text_brush)
177  self._label.setPen(self._label_pen)
178 
179  def _handle_hoverEnterEvent(self, event):
180  # hovered edge item in red
181  self.set_node_color(self._COLOR_RED)
182 
183  if self._highlight_level > 1:
184  if self.from_node != self.to_node:
185  # from-node in blue
186  self.from_node.set_node_color(self._COLOR_BLUE)
187  # to-node in green
188  self.to_node.set_node_color(self._COLOR_GREEN)
189  else:
190  # from-node/in-node in teal
191  self.from_node.set_node_color(self._COLOR_TEAL)
192  self.to_node.set_node_color(self._COLOR_TEAL)
193  if self._highlight_level > 2:
194  # sibling edges in orange
195  for sibling_edge in self._sibling_edges:
196  sibling_edge.set_node_color(self._COLOR_ORANGE)
197 
198  def _handle_hoverLeaveEvent(self, event):
199  self.set_node_color()
200  if self._highlight_level > 1:
201  self.from_node.set_node_color()
202  self.to_node.set_node_color()
203  if self._highlight_level > 2:
204  for sibling_edge in self._sibling_edges:
205  sibling_edge.set_node_color()
def __init__(self, highlight_level, spline, label_center, label, from_node, to_node, parent=None, penwidth=1, edge_color=None, style='solid')
Definition: edge_item.py:44


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