Commit 451071c0 authored by Maxime Perrotin's avatar Maxime Perrotin
Browse files

Fix edge label positionning in statecharts

parent 30da64c1
......@@ -20,9 +20,9 @@ import logging
from PySide.QtCore import Qt, QPointF, QLineF, Slot
from PySide.QtGui import QGraphicsPathItem, QPainterPath, QGraphicsItem, QPen,\
QPainter, QFont, QGraphicsTextItem, QColor, \
QFontMetrics
from PySide.QtGui import(QGraphicsPathItem, QPainterPath, QGraphicsItem, QPen,
QPainter, QFont, QGraphicsTextItem, QColor,
QFontMetrics, QTextBlockFormat, QTextCursor)
import ogParser
from TextInteraction import EditableText
......@@ -521,17 +521,28 @@ class Edge(Connection):
try:
# Add the transition label, if any (none for the START edge)
font = QFont('arial', pointSize=8)
width = QFontMetrics(font).width(
self.edge.get('label', 0))
metrics = QFontMetrics(font)
label = self.edge.get('label', '')
lines = label.split('\n')
width = metrics.width(max(lines)) # longest line
height = metrics.height() * len(lines)
# lp is the position of the center of the text
pos = self.mapFromScene(*self.edge['lp'])
#path.addText(pos.x() - width/2, pos.y(),
# font, self.edge['label'])
if not self.text_label:
self.text_label = QGraphicsTextItem(
self.edge.get('label', ''), parent=self)
self.text_label.setX(pos.x() - width / 2)
self.text_label.setY(pos.y())
self.text_label.setY(pos.y() - height / 2)
self.text_label.setFont(font)
# Make horizontal center alignment, as dot does
self.text_label.setTextWidth(self.text_label.boundingRect().width())
fmt = QTextBlockFormat()
fmt.setAlignment(Qt.AlignHCenter)
cursor = self.text_label.textCursor()
cursor.select(QTextCursor.Document)
cursor.mergeBlockFormat(fmt)
cursor.clearSelection()
self.text_label.setTextCursor(cursor)
self.text_label.show()
except KeyError:
# no label
......
......@@ -203,6 +203,7 @@ def preprocess_edges(my_graph, nodes, bounding_rect, dpi):
pass
else:
# translate the label position from dot-coordinates to Qt
# Note: lp is the position of the CENTER of the label
new_edge['lp'][0] *= RENDER_DPI['X'] / dpi
new_edge['lp'][1] = (
bb_height - new_edge['lp'][1]) * (RENDER_DPI['Y'] / dpi)
......@@ -372,9 +373,10 @@ def render_statechart(scene, graph=None, keep_pos=False):
# Compute all the coordinates (self-modifying function)
# Force the fontsize of the nodes to be 12, as in OpenGEODE
# use -n2 below to keep user-specified node coordinates
graph.layout(prog='neato', args='-Nfontsize=12, -Gsplines=curved -Gsep=1 '
'-Gstart=random10 -Goverlap=false '
'-Nstyle=rounded -Nshape=record -Elen=1.5 {kp}'
graph.layout(prog='neato', args='-Nfontsize=12, -Efontsize=8 '
'-Gsplines=curved -Gsep=1 '
'-Gstart=random10 -Goverlap=false '
'-Nstyle=rounded -Nshape=record -Elen=1 {kp} -Tpng -ocoucou.png'
.format(kp='-n1' if keep_pos else ''))
# bb is not visible directly - extract it from the low level api:
bounding_rect = [float(val) for val in
......@@ -388,7 +390,8 @@ def render_statechart(scene, graph=None, keep_pos=False):
#fontname = graph.graph_attr.get('fontname')
#fontsize = graph.graph_attr.get('fontsize')
#print 'AFTER PROCESSING: ', graph.to_string()
with open('statechart.dot', 'w') as output:
output.write(graph.to_string())
nodes = preprocess_nodes(graph, bounding_rect, dot_dpi)
node_symbols = []
......@@ -504,7 +507,8 @@ def create_dot_graph(root_ast, basic=False):
for target, labels in target_states.viewitems():
# Basic mode
graph.add_edge(source, target, label=',\n'.join(labels))
#print graph.to_string()
# with open('statechart.dot', 'w') as output:
# output.write(graph.to_string())
return graph
......
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