From 0492a7a466d510c2809bbb47f4756b3f9c7eb3d3 Mon Sep 17 00:00:00 2001 From: huyenngn Date: Mon, 2 Sep 2024 19:30:03 +0200 Subject: [PATCH] fix: Fix snapping of oblique edges --- capellambse/aird/_edge_factories.py | 24 +++++++++++++++++++++--- capellambse/diagram/_diagram.py | 5 ++++- capellambse/svg/drawing.py | 2 ++ 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/capellambse/aird/_edge_factories.py b/capellambse/aird/_edge_factories.py index 1ce5b6ff..624c5596 100644 --- a/capellambse/aird/_edge_factories.py +++ b/capellambse/aird/_edge_factories.py @@ -62,13 +62,14 @@ def generic_factory(seb: C.SemanticElementBuilder) -> diagram.Edge: edge.styleoverrides = _styling.apply_style_overrides( seb.target_diagram.styleclass, f"Edge.{seb.styleclass}", ostyle ) # type: ignore[assignment] - edge.labels.extend(_construct_labels(edge, seb)) if isinstance(targetport, diagram.Box): snaptarget(edge, -1, -2, targetport, not edge.hidden, routingstyle) if isinstance(sourceport, diagram.Box): snaptarget(edge, 0, 1, sourceport, not edge.hidden, routingstyle) + edge.labels.extend(_construct_labels(edge, seb)) + sourceport.add_context(seb.data_element.attrib["element"]) if targetport is not None: targetport.add_context(seb.data_element.attrib["element"]) @@ -310,9 +311,18 @@ def snap_oblique( target: diagram.DiagramElement, ) -> None: """Snap ``points``' end to ``target`` in a straight line.""" - points[i] = target.vector_snap( - points[i], source=points[next_i], style=diagram.RoutingStyle.OBLIQUE + source = points[next_i] + new_point = target.vector_snap( + points[i], style=diagram.RoutingStyle.OBLIQUE, source=source ) + delta = (points[i] - source).angleto(new_point - source) + if (math.ceil(delta) != 0) and (math.floor(delta) != 0): + new_point = target.vector_snap( + source, + style=diagram.RoutingStyle.OBLIQUE, + source=points[next_i - i + next_i], + ) + points[i] = new_point def snap_manhattan( @@ -388,6 +398,14 @@ def _construct_labels( int(layout.attrib.get("height", "0")), ) + if melodyobj.tag == "ownedFeatures" and len(melodyobj) == 2: + start_obj, end_obj = melodyobj + start, end = start_obj.get("value", "1"), end_obj.get("value", "1") + if (start != "1") or (end != "1"): + if start_obj.tag != "ownedMinCard": + end, start = start, end + labeltext = f"[{start}..{end}] {labeltext}" + # Rotate the position vector into place label_pos = label_pos.rotatedby(travel_direction.angleto((1, 0))) diff --git a/capellambse/diagram/_diagram.py b/capellambse/diagram/_diagram.py index c0f38307..8f516068 100644 --- a/capellambse/diagram/_diagram.py +++ b/capellambse/diagram/_diagram.py @@ -272,7 +272,7 @@ def vector_snap( source = diagram.Vector2D(*source) if style is RoutingStyle.OBLIQUE: - return self.__vector_snap_oblique(source) + return self.__vector_snap_oblique(point) if style is RoutingStyle.MANHATTAN: return self.__vector_snap_manhattan(point, point - source) if style is RoutingStyle.TREE: @@ -282,6 +282,9 @@ def vector_snap( def __vector_snap_oblique( self, source: diagram.Vector2D ) -> diagram.Vector2D: + if source == self.center: + return source + angle = self.size.angleto(source - self.center) alpha = 2 * self.size.angleto((1, 0)) assert alpha >= 0 diff --git a/capellambse/svg/drawing.py b/capellambse/svg/drawing.py index dedbf285..0a903d6a 100644 --- a/capellambse/svg/drawing.py +++ b/capellambse/svg/drawing.py @@ -286,6 +286,8 @@ def _draw_feature_text( def _draw_label(self, builder: LabelBuilder) -> LinesData | None: """Draw label text on given object and return the label's group.""" + if not builder.labels: + return None builder.icon &= diagram.has_icon(builder.class_ or "") text = self._make_text(builder) lines = None