Skip to content

Commit

Permalink
fix: Fix snapping of oblique edges
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Sep 2, 2024
1 parent 81b56cf commit 0492a7a
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
24 changes: 21 additions & 3 deletions capellambse/aird/_edge_factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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)))

Expand Down
5 changes: 4 additions & 1 deletion capellambse/diagram/_diagram.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions capellambse/svg/drawing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0492a7a

Please sign in to comment.