Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/plantuml_gui/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
)
from .merge import get_index_merge
from .note import delete_note, edit_note, get_note_line, get_note_text, note_toggle
from .participant import add_participant
from .puml_encoder import plantuml_decode, plantuml_encode
from .render import _create_png_from_uml, _create_svg_from_uml
from .title import (
Expand Down Expand Up @@ -161,6 +162,15 @@ def renderpng():
)


@plantuml.route("/addParticipant", methods=["POST"])
def addparticipant():
data = request.get_json()
puml = data["plantuml"]
svg = data["svg"]
cx = data["cx"]
return add_participant(puml, svg, cx)


@plantuml.route("/editText", methods=["POST"])
def edittext():
data = request.get_json()
Expand Down
76 changes: 76 additions & 0 deletions src/plantuml_gui/participant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# SPDX-License-Identifier: MIT
#
# MIT License
#
# Copyright (c) 2025 Ericsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

import re
from typing import List

from .sequence_classes import Diagram, Participant


def add_participant(puml: str, svg: str, cx: int) -> str:
"""Add a participant at the correct position in the puml code."""
diagram = Diagram.from_svg(svg)
index_of_closest_participant, closest_participant_cx = find_closest_participant(
diagram.participants, cx
)

lines = puml.splitlines()

if index_of_closest_participant == -1:
lines.insert(1, "participant participant1")
return "\n".join(lines)

count = 0
index = -1
participant_numbers = []
for i, line in enumerate(lines):
if line.startswith("participant"):
match = re.search(r"participant participant(\d+)", line)

if match:
participant_numbers.append(int(match.group(1)))

if count == index_of_closest_participant:
index = i if cx < closest_participant_cx else i + 1
count += 1

next_participant_number = max(participant_numbers, default=0) + 1
lines.insert(index, f"participant participant{next_participant_number}")
return "\n".join(lines)


def find_closest_participant(participants: List[Participant], target_cx: int) -> tuple:
"""Find the index of the participant with the closest cx value to the target_cx, and return their cx."""
closest_index = -1
min_distance = float("inf")
closest_cx = None

for index, participant in enumerate(participants):
distance = abs(participant.cx - target_cx)
if distance < min_distance:
min_distance = distance
closest_index = index
closest_cx = participant.cx

return closest_index, closest_cx
78 changes: 78 additions & 0 deletions src/plantuml_gui/sequence_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# SPDX-License-Identifier: MIT
#
# MIT License
#
# Copyright (c) 2025 Ericsson
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


from dataclasses import dataclass, field
from typing import Dict, List

from pyquery import PyQuery as Pq # pragma: no cover


@dataclass
class Participant:
cx: float
cy: float

def __eq__(self, other):
return isinstance(other, Participant) and self.x == other.x

@classmethod
def from_svg(cls, svgtext: str):
svg = Pq(svgtext)
rect = svg("rect")
x = float(rect.attr("x"))
y = float(rect.attr("y"))
width = float(rect.attr("width"))
height = float(rect.attr("height"))

cx = x + width / 2
cy = y + height / 2

return cls(cx, cy)


@dataclass
class Diagram:
participants: List[Participant] = field(default_factory=list)

@classmethod
def from_svg(cls, svgtext: str):
svg = Pq(svgtext)
diagram = cls()

diagram._parse_participants(svg)

return diagram

def _parse_participants(self, svg):
"""Extract unique participants based on `cx` value."""
unique_participants: Dict[int, Participant] = {}

for rect in svg("rect").items():
participant = Participant.from_svg(rect)

if participant.cx not in unique_participants:
unique_participants[participant.cx] = participant

self.participants.extend(unique_participants.values())
Loading