Commit 2e1ba712 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Export also the statechart as png

parent d099b34f
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
Contact: maxime.perrotin@esa.int Contact: maxime.perrotin@esa.int
""" """
import os
import logging import logging
from collections import defaultdict from collections import defaultdict
import re import re
...@@ -352,8 +353,10 @@ def update(scene): ...@@ -352,8 +353,10 @@ def update(scene):
item.position = QtCore.QPointF(item.position - delta) item.position = QtCore.QPointF(item.position - delta)
def render_statechart(scene, graph=None, keep_pos=False): def render_statechart(scene, graph=None, keep_pos=False, dump_gfx=''):
''' Render a graphviz/dot statechart on the QGraphicsScene ''' ''' Render a graphviz/dot statechart on the QGraphicsScene
set a filename to "dump_gfx" parameter to create a PNG of the graph
'''
# Statechart symbols lookup table # Statechart symbols lookup table
lookup = {'point': Point, 'record': Record, 'diamond': Diamond} lookup = {'point': Point, 'record': Record, 'diamond': Diamond}
try: try:
...@@ -373,11 +376,18 @@ def render_statechart(scene, graph=None, keep_pos=False): ...@@ -373,11 +376,18 @@ def render_statechart(scene, graph=None, keep_pos=False):
# Compute all the coordinates (self-modifying function) # Compute all the coordinates (self-modifying function)
# Force the fontsize of the nodes to be 12, as in OpenGEODE # Force the fontsize of the nodes to be 12, as in OpenGEODE
# use -n2 below to keep user-specified node coordinates # use -n2 below to keep user-specified node coordinates
if dump_gfx:
dump_name = 'sc_' + os.path.basename(dump_gfx)
dump_gfx = os.path.dirname(dump_gfx) or '.' + os.sep + dump_name
if dump_gfx.split('.')[-1].lower() != 'png':
dump_gfx += '.png'
graph.layout(prog='neato', args='-Nfontsize=12, -Efontsize=8 ' graph.layout(prog='neato', args='-Nfontsize=12, -Efontsize=8 '
'-Gsplines=curved -Gsep=1 ' '-Gsplines=curved -Gsep=1 '
'-Gstart=random10 -Goverlap=false ' '-Gstart=random10 -Goverlap=false '
'-Nstyle=rounded -Nshape=record -Elen=1 {kp} -Tpng -ocoucou.png' '-Nstyle=rounded -Nshape=record -Elen=1 {kp} {dump}'
.format(kp='-n1' if keep_pos else '')) .format(kp='-n1' if keep_pos else '',
dump=('-Tpng -o' + dump_gfx) if dump_gfx else ''))
# bb is not visible directly - extract it from the low level api: # bb is not visible directly - extract it from the low level api:
bounding_rect = [float(val) for val in bounding_rect = [float(val) for val in
dotgraph.graphviz.agget(graph.handle, 'bb').split(',')] dotgraph.graphviz.agget(graph.handle, 'bb').split(',')]
...@@ -390,8 +400,8 @@ def render_statechart(scene, graph=None, keep_pos=False): ...@@ -390,8 +400,8 @@ def render_statechart(scene, graph=None, keep_pos=False):
#fontname = graph.graph_attr.get('fontname') #fontname = graph.graph_attr.get('fontname')
#fontsize = graph.graph_attr.get('fontsize') #fontsize = graph.graph_attr.get('fontsize')
with open('statechart.dot', 'w') as output: #with open('statechart.dot', 'w') as output:
output.write(graph.to_string()) # output.write(graph.to_string())
nodes = preprocess_nodes(graph, bounding_rect, dot_dpi) nodes = preprocess_nodes(graph, bounding_rect, dot_dpi)
node_symbols = [] node_symbols = []
......
...@@ -947,7 +947,7 @@ class HorizontalSymbol(Symbol, object): ...@@ -947,7 +947,7 @@ class HorizontalSymbol(Symbol, object):
def insert_symbol(self, parent, pos_x, pos_y): def insert_symbol(self, parent, pos_x, pos_y):
''' Insert the symbol in the scene - Align below the parent ''' ''' Insert the symbol in the scene - Align below the parent '''
if not parent: if not parent:
self.position = QPointF(pos_x, pos_y) self.position = QPointF(pos_x or 0, pos_y or 0)
return return
super(HorizontalSymbol, self).insert_symbol(parent, pos_x, pos_y) super(HorizontalSymbol, self).insert_symbol(parent, pos_x, pos_y)
if pos_x is None or pos_y is None: if pos_x is None or pos_y is None:
......
...@@ -2120,6 +2120,18 @@ def export(ast, options): ...@@ -2120,6 +2120,18 @@ def export(ast, options):
LOG.info('Saving {ext} file: {name}.{ext}' LOG.info('Saving {ext} file: {name}.{ext}'
.format(ext=doc_fmt, name=name)) .format(ext=doc_fmt, name=name))
diagram.export_img(name, doc_format=doc_fmt, split=options.split) diagram.export_img(name, doc_format=doc_fmt, split=options.split)
if diagram.context == 'block' and graphviz:
# Also save the statechart viewa of the current scene
LOG.info('Saving statechart sc_{}.png'.format(process.processName))
sc_scene = SDL_Scene(context='statechart')
graph = diagram.sdl_to_statechart()
try:
Statechart.render_statechart(sc_scene, graph,
dump_gfx=process.processName)
sc_scene.refresh()
except (IOError, TypeError) as err:
LOG.debug(str(err))
def cli(options): def cli(options):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment