basic openmodelica model emitting
This commit is contained in:
@@ -252,6 +252,19 @@ class EditComponentPropertiesCommand(QUndoCommand):
|
||||
self.controller._set_component_properties(self.component_id, self.old)
|
||||
|
||||
|
||||
class EditComponentParametersCommand(QUndoCommand):
|
||||
def __init__(self, controller, component_id: str, old: list, new: list) -> None:
|
||||
super().__init__("Edit component parameters")
|
||||
self.controller, self.component_id = controller, component_id
|
||||
self.old, self.new = old, new
|
||||
|
||||
def redo(self) -> None:
|
||||
self.controller._set_component_parameters(self.component_id, self.new)
|
||||
|
||||
def undo(self) -> None:
|
||||
self.controller._set_component_parameters(self.component_id, self.old)
|
||||
|
||||
|
||||
class RenameInterfacePortCommand(QUndoCommand):
|
||||
def __init__(self, controller, owner_id: str, port_id: str, old: str, new: str) -> None:
|
||||
super().__init__("Rename interface port")
|
||||
|
||||
@@ -17,6 +17,7 @@ from bedit.gui.controllers.commands import (
|
||||
EditTextDefinitionCommand,
|
||||
EditComponentAppearanceCommand,
|
||||
EditComponentPropertiesCommand,
|
||||
EditComponentParametersCommand,
|
||||
MoveComponentCommand,
|
||||
MoveInterfacePortCommand,
|
||||
PasteSelectionCommand,
|
||||
@@ -166,7 +167,7 @@ class DocumentController(QObject):
|
||||
name=self._available_component_name(base_name, self.document.roots.values(), number),
|
||||
implementation_kind=kind,
|
||||
icon=Icon(text="Graph" if kind == "graph" else "Text"),
|
||||
source={"equations": "", "parameters": []} if kind == "text" else {},
|
||||
source={"equations": ""} if kind == "text" else {},
|
||||
)
|
||||
self.undo_stack.push(AddComponentCommand(self, None, component))
|
||||
self.activate_component(component.id)
|
||||
@@ -185,7 +186,7 @@ class DocumentController(QObject):
|
||||
name=self._available_component_name(base_name, owner.graph.blocks.values(), number),
|
||||
implementation_kind=kind,
|
||||
icon=Icon(text="Graph" if kind == "graph" else "Text"),
|
||||
source={"equations": "", "parameters": []} if kind == "text" else {},
|
||||
source={"equations": ""} if kind == "text" else {},
|
||||
)
|
||||
self.undo_stack.push(AddComponentCommand(self, owner_id, component))
|
||||
return component.id
|
||||
@@ -628,14 +629,15 @@ class DocumentController(QObject):
|
||||
"inputs": [port.to_dict() for port in component.inputs],
|
||||
"outputs": [port.to_dict() for port in component.outputs],
|
||||
"source": deepcopy(component.source),
|
||||
"parameters": [parameter.to_dict() for parameter in component.parameters],
|
||||
}
|
||||
new = {
|
||||
"inputs": [port.to_dict() for port in inputs],
|
||||
"outputs": [port.to_dict() for port in outputs],
|
||||
"source": {
|
||||
"equations": equations,
|
||||
"parameters": [parameter.to_dict() for parameter in parameters],
|
||||
},
|
||||
"parameters": [parameter.to_dict() for parameter in parameters],
|
||||
}
|
||||
if old != new:
|
||||
candidate = deepcopy(self.document)
|
||||
@@ -643,6 +645,7 @@ class DocumentController(QObject):
|
||||
candidate_component.inputs = deepcopy(inputs)
|
||||
candidate_component.outputs = deepcopy(outputs)
|
||||
candidate_component.source = deepcopy(new["source"])
|
||||
candidate_component.parameters = deepcopy(parameters)
|
||||
candidate.validate()
|
||||
self.undo_stack.push(EditTextDefinitionCommand(self, component.id, old, new))
|
||||
|
||||
@@ -810,6 +813,27 @@ class DocumentController(QObject):
|
||||
if old != new:
|
||||
self.undo_stack.push(EditComponentAppearanceCommand(self, component_id, old, new))
|
||||
|
||||
def edit_component_parameters(
|
||||
self, component_id: str, parameters: list[Parameter]
|
||||
) -> None:
|
||||
component = self.document.find_component(component_id) if self.document else None
|
||||
if component is None:
|
||||
return
|
||||
ids = [parameter.id for parameter in parameters]
|
||||
names = [parameter.name for parameter in parameters]
|
||||
if len(set(ids)) != len(ids):
|
||||
raise ValueError("Parameter IDs must be unique")
|
||||
if len(set(names)) != len(names):
|
||||
raise ValueError("Parameter names must be unique")
|
||||
if any(not name.strip() for name in names):
|
||||
raise ValueError("Every parameter must have a name")
|
||||
old = [parameter.to_dict() for parameter in component.parameters]
|
||||
new = [parameter.to_dict() for parameter in parameters]
|
||||
if old != new:
|
||||
self.undo_stack.push(
|
||||
EditComponentParametersCommand(self, component_id, old, new)
|
||||
)
|
||||
|
||||
def delete_selection(
|
||||
self,
|
||||
block_ids: set[str],
|
||||
@@ -1164,6 +1188,17 @@ class DocumentController(QObject):
|
||||
component.properties = deepcopy(properties)
|
||||
self.componentPropertiesChanged.emit(component_id)
|
||||
|
||||
def _set_component_parameters(self, component_id: str, values: list[dict]) -> None:
|
||||
component = self.document.find_component(component_id) if self.document else None
|
||||
if component is not None:
|
||||
component.parameters = [Parameter.from_dict(item) for item in values]
|
||||
self.documentReset.emit()
|
||||
if (
|
||||
component_id == self.active_component_id
|
||||
and component.implementation_kind == "text"
|
||||
):
|
||||
self.textDefinitionChanged.emit(component_id)
|
||||
|
||||
def _delete_items(
|
||||
self,
|
||||
owner_id: str | None,
|
||||
@@ -1239,6 +1274,9 @@ class DocumentController(QObject):
|
||||
component.inputs = [Port.from_dict(item) for item in values["inputs"]]
|
||||
component.outputs = [Port.from_dict(item) for item in values["outputs"]]
|
||||
component.source = deepcopy(values["source"])
|
||||
component.parameters = [
|
||||
Parameter.from_dict(item) for item in values.get("parameters", [])
|
||||
]
|
||||
self.interfaceChanged.emit()
|
||||
self.documentReset.emit()
|
||||
self.textDefinitionChanged.emit(component_id)
|
||||
|
||||
Reference in New Issue
Block a user