11 :synopsis: Repackaging of the limiting ROS qt_dotgraph.dot_to_qt module.
13 Oh my spaghettified magnificence,
14 Bless my noggin with a tickle from your noodly appendages!
29 pyparsing._noncomma =
"".join([c
for c
in pyparsing.printables
if c !=
","])
32 from python_qt_binding.QtCore
import QPointF, QRectF
33 from python_qt_binding.QtGui
import QColor
35 from .edge_item
import EdgeItem
36 from .node_item
import NodeItem
48 value = item.get(name)
52 return value.strip(
'"\n"')
53 except AttributeError:
74 for name
in subgraph.get_attributes().iterkeys():
77 obj_dic = subgraph.__getattribute__(
"obj_dict")
79 if name
not in [
'nodes',
'attributes',
'parent_graph']
and obj_dic[name]
is not None:
82 for key
in obj_dic[
'nodes'][
'graph'][0][
'attributes']:
83 attr[key] =
get_unquoted(obj_dic[
'nodes'][
'graph'][0][
'attributes'], key)
86 bb = subgraph.attr.get(
'bb',
None)
90 bb = bb.strip(
'"').split(
',')
94 bounding_box = QRectF(0, 0, float(bb[2]) - float(bb[0]), float(bb[3]) - float(bb[1]))
95 if 'lp' in subgraph.attr:
96 label_pos = subgraph.attr[
'lp'].strip(
'"').split(
',')
98 label_pos = (float(bb[0]) + (float(bb[2]) - float(bb[0])) / 2, float(bb[1]) + (float(bb[3]) - float(bb[1])) - LABEL_HEIGHT / 2)
99 bounding_box.moveCenter(QPointF(float(bb[0]) + (float(bb[2]) - float(bb[0])) / 2, -float(bb[1]) - (float(bb[3]) - float(bb[1])) / 2))
100 name = subgraph.attr.get(
'label',
'')
101 color = QColor(subgraph.attr[
'color'])
if 'color' in subgraph.attr
else None
102 subgraph_nodeitem =
NodeItem(highlight_level,
107 label_pos=QPointF(float(label_pos[0]), -float(label_pos[1])))
108 bounding_box = QRectF(bounding_box)
113 bounding_box.setHeight(LABEL_HEIGHT)
114 subgraph_nodeitem.set_hovershape(bounding_box)
115 return subgraph_nodeitem
119 returns a pyqt NodeItem object, or None in case of error or invisible style
132 if 'style' in node.attr:
133 if node.attr[
'style'] ==
'invis':
136 color = QColor(node.attr[
'color'])
if 'color' in node.attr
else None
138 if 'label' in node.attr:
139 name = node.attr[
'label']
140 elif 'name' in node.attr:
141 name = node.attr[
'name']
143 print(
"Error, no label defined for node with attr: %s" % node.attr)
147 print(
"Error, label is None for node %s, pygraphviz version may be too old." % node)
152 name = codecs.escape_decode(name)[0].decode(
'utf-8')
155 bb_width = len(name) / 5
156 if 'width' in node.attr:
157 bb_width = node.attr[
'width']
159 if 'width' in node.attr:
160 bb_height = node.attr[
'height']
161 bounding_box = QRectF(0, 0, POINTS_PER_INCH * float(bb_width) - 1.0, POINTS_PER_INCH * float(bb_height) - 1.0)
163 if 'pos' in node.attr:
164 pos = node.attr[
'pos'].split(
',')
165 bounding_box.moveCenter(QPointF(float(pos[0]), -float(pos[1])))
167 node_item =
NodeItem(highlight_level=highlight_level,
168 bounding_box=bounding_box,
170 shape=node.attr.get(
'shape',
'ellipse'),
172 tooltip=node.attr.get(
'tooltip',
None)
179 def addEdgeItem(self, edge, nodes, edges, highlight_level, same_label_siblings=False):
181 adds EdgeItem by data in edge to edges
182 :param same_label_siblings: if true, edges with same label will be considered siblings (collective highlighting)
191 if 'style' in edge.attr:
192 if edge.attr[
'style'] ==
'invis':
194 style = edge.attr.get(
'style',
None)
196 label = edge.attr.get(
'label',
None)
197 label_pos = edge.attr.get(
'lp',
None)
199 if label_pos
is not None:
200 label_pos = label_pos.split(
',')
201 label_center = QPointF(float(label_pos[0]), -float(label_pos[1]))
204 source_node = edge.get_source()
if hasattr(edge,
'get_source')
else edge[0]
205 destination_node = edge.get_destination()
if hasattr(edge,
'get_destination')
else edge[1]
209 if 'pos' in edge.attr:
210 edge_pos = edge.attr[
'pos']
211 if label
is not None:
212 label = label.decode(
'string_escape')
215 if 'colorR' in edge.attr
and 'colorG' in edge.attr
and 'colorB' in edge.attr:
216 r = edge.attr[
'colorR']
217 g = edge.attr[
'colorG']
218 b = edge.attr[
'colorB']
219 color = QColor(float(r), float(g), float(b))
221 edge_item =
EdgeItem(highlight_level=highlight_level,
223 label_center=label_center,
225 from_node=nodes[source_node],
226 to_node=nodes[destination_node],
227 penwidth=int(edge.attr[
'penwidth']),
231 if same_label_siblings:
234 label =
"%s_%s" % (source_node, destination_node)
237 for sibling
in edges[label]:
238 edge_item.add_sibling_edge(sibling)
239 sibling.add_sibling_edge(edge_item)
241 if label
not in edges:
243 edges[label].append(edge_item)
247 takes dotcode, runs layout, and creates qt items based on the dot layout.
248 returns two dicts, one mapping node names to Node_Item, one mapping edge names to lists of Edge_Item
249 :param same_label_siblings: if true, edges with same label will be considered siblings (collective highlighting)
263 graph = pygraphviz.AGraph(
268 graph.layout(prog=
'dot')
271 for subgraph
in graph.subgraphs_iter():
274 if subgraph_nodeitem
is None:
277 subgraph.nodes_iter = subgraph.get_node_list
278 nodes[subgraph.get_name()] = subgraph_nodeitem
279 for node
in subgraph.nodes_iter():
281 if node.get_name()
in (
'graph',
'node',
'empty'):
284 for node
in graph.nodes_iter():
286 if node.get_name()
in (
'graph',
'node',
'empty'):
292 for subgraph
in graph.subgraphs_iter():
293 subgraph.edges_iter = subgraph.get_edge_list
294 for edge
in subgraph.edges_iter():
296 highlight_level=highlight_level,
297 same_label_siblings=same_label_siblings)
299 for edge
in graph.edges_iter():
301 highlight_level=highlight_level,
302 same_label_siblings=same_label_siblings)
308 takes a pydot/pygraphviz graph and creates qt items based on the dot layout.
309 returns two dicts, one mapping node names to Node_Item, one mapping edge names to lists of Edge_Item
310 :param same_label_siblings: if true, edges with same label will be considered siblings (collective highlighting)
321 for subgraph
in graph.subgraphs_iter():
324 if subgraph_nodeitem
is None:
327 subgraph.nodes_iter = subgraph.get_node_list
328 nodes[subgraph.get_name()] = subgraph_nodeitem
329 for node
in subgraph.nodes_iter():
331 if node.get_name()
in (
'graph',
'node',
'empty'):
334 for node
in graph.nodes_iter():
336 if node.get_name()
in (
'graph',
'node',
'empty'):
342 for subgraph
in graph.subgraphs_iter():
343 subgraph.edges_iter = subgraph.get_edge_list
344 for edge
in subgraph.edges_iter():
346 highlight_level=highlight_level,
347 same_label_siblings=same_label_siblings)
349 for edge
in graph.edges_iter():
351 highlight_level=highlight_level,
352 same_label_siblings=same_label_siblings)