Saving sim results
This commit is contained in:
@@ -963,6 +963,68 @@ class DocumentController(QObject):
|
||||
self.undo_stack.push(PasteSelectionCommand(self, owner.id, blocks, connections))
|
||||
return list(blocks)
|
||||
|
||||
def paste_components_to(
|
||||
self,
|
||||
owner_id: str | None,
|
||||
source_components: list[Component],
|
||||
source_connections: list[Connection] | None = None,
|
||||
) -> list[str]:
|
||||
"""Clone components into the document root or a graph at origin."""
|
||||
|
||||
if self.document is None:
|
||||
raise ValueError("Open or create a document before pasting components")
|
||||
if owner_id is None:
|
||||
siblings = self.document.roots.values()
|
||||
else:
|
||||
owner = self.document.find_component(owner_id)
|
||||
if owner is None or owner.implementation_kind != "graph":
|
||||
raise ValueError("Components can only be pasted into a graph")
|
||||
siblings = owner.graph.blocks.values()
|
||||
|
||||
pairs = [(source, clone_component(source)) for source in source_components]
|
||||
if not pairs:
|
||||
return []
|
||||
id_map = {source.id: clone.id for source, clone in pairs}
|
||||
used = list(siblings)
|
||||
minimum_x = min(source.x for source, _clone in pairs)
|
||||
minimum_y = min(source.y for source, _clone in pairs)
|
||||
blocks: dict[str, Component] = {}
|
||||
for source, clone in pairs:
|
||||
clone.name = self._available_component_name(source.name, used, 0)
|
||||
if owner_id is not None:
|
||||
clone.x = source.x - minimum_x
|
||||
clone.y = source.y - minimum_y
|
||||
blocks[clone.id] = clone
|
||||
used.append(clone)
|
||||
|
||||
connections: dict[str, Connection] = {}
|
||||
if owner_id is not None:
|
||||
for source in source_connections or []:
|
||||
if source.source.block not in id_map or source.target.block not in id_map:
|
||||
continue
|
||||
properties = deepcopy(source.properties)
|
||||
for point in properties.get("waypoints", []):
|
||||
if isinstance(point, dict):
|
||||
point["x"] = float(point.get("x", 0)) - minimum_x
|
||||
point["y"] = float(point.get("y", 0)) - minimum_y
|
||||
connection = Connection(
|
||||
id=str(uuid4()),
|
||||
source=Endpoint(
|
||||
block=id_map[source.source.block], port=source.source.port
|
||||
),
|
||||
target=Endpoint(
|
||||
block=id_map[source.target.block], port=source.target.port
|
||||
),
|
||||
name=source.name,
|
||||
properties=properties,
|
||||
)
|
||||
connections[connection.id] = connection
|
||||
|
||||
self.undo_stack.push(
|
||||
PasteSelectionCommand(self, owner_id, blocks, connections)
|
||||
)
|
||||
return list(blocks)
|
||||
|
||||
def _graph_for(self, owner_id: str):
|
||||
if self.document is None:
|
||||
raise ValueError("There is no open document")
|
||||
|
||||
Reference in New Issue
Block a user