basic openmodelica model emitting

This commit is contained in:
2026-07-20 17:34:18 +02:00
parent 42bf09610b
commit d368b36b8c
26 changed files with 1608 additions and 594 deletions

View File

@@ -12,7 +12,7 @@ from PySide6.QtWidgets import (
QTabWidget,
)
from bedit.core.model import Component, Parameter
from bedit.core.model import Component
from bedit.core.application_log import get_logger
from bedit.core.serializer import DocumentSerializer
from bedit.core.simulation import Simulation
@@ -29,8 +29,10 @@ from bedit.gui.models.library_tree import (
)
from bedit.gui.dialogs.settings import SettingsDialog
from bedit.gui.dialogs.port_options import PortOptionsDialog
from bedit.gui.dialogs.parameter_options import ParameterOptionsDialog
from bedit.gui.dialogs.simulation_settings import SimulationSettingsDialog
from bedit.gui.preferences import application_settings
from bedit.gui.simulation_reload import reload_simulation
from bedit.gui.generated.ui_main_window import Ui_MainWindow
from bedit.gui.application_log import ApplicationLogHandler
@@ -99,6 +101,9 @@ class MainWindow(QMainWindow):
self.ui.graphView.set_model(self.document_controller)
self.ui.graphView.componentOptionsRequested.connect(self.show_component_options)
self.ui.graphView.componentPortOptionsRequested.connect(self.show_component_port_options)
self.ui.graphView.componentParameterOptionsRequested.connect(
self.show_component_parameter_options
)
self.ui.graphView.portOptionsRequested.connect(self.show_port_options)
self.ui.graphView.connectionOptionsRequested.connect(self.show_connection_options)
self.ui.graphView.selectionAvailabilityChanged.connect(
@@ -132,6 +137,7 @@ class MainWindow(QMainWindow):
self.ui.actionNew.triggered.connect(self.new_document)
self.ui.actionOpen.triggered.connect(self.open_document)
self.ui.actionReloadLibraries.triggered.connect(self.reload_libraries)
self.ui.actionReloadSimulation.triggered.connect(self.reload_simulation_code)
self.ui.actionSave.triggered.connect(self.save_document)
self.ui.actionSaveAs.triggered.connect(self.save_document_as)
self.ui.actionClose.triggered.connect(self.close_document)
@@ -191,6 +197,18 @@ class MainWindow(QMainWindow):
"\n".join(self.libraries.load_warnings),
)
@Slot()
def reload_simulation_code(self) -> None:
try:
replacement = reload_simulation(self.simulation)
except Exception as error:
self.log.exception("Could not reload simulation code")
QMessageBox.critical(self, "Could not reload simulation code", str(error))
return
self.simulation = replacement
self.document_controller.simulation = replacement
self.log.info("Reloaded simulation code")
@Slot()
def show_simulation_settings(self) -> None:
component = self.document_controller.active_component
@@ -314,10 +332,7 @@ class MainWindow(QMainWindow):
component.source.get("equations", ""),
component.inputs,
component.outputs,
[
Parameter.from_dict(parameter)
for parameter in component.source.get("parameters", [])
],
component.parameters,
)
def _resolve_source_edits(self) -> bool:
@@ -475,6 +490,7 @@ class MainWindow(QMainWindow):
if scene is not None:
scene.update()
self.ui.graphView.viewport().update()
self.ui.textDefinitionEditor.ui.equationsEdit.reload_highlighting()
def _document_opened_changed(self, opened: bool) -> None:
for action in (self.ui.actionClose, self.ui.actionSave, self.ui.actionSaveAs):
@@ -509,6 +525,7 @@ class MainWindow(QMainWindow):
menu.addSeparator()
options_action = menu.addAction("Component Options…")
ports_action = menu.addAction("Port Options…")
parameters_action = menu.addAction("Parameter Options…")
delete_action = menu.addAction("Delete")
selected = menu.exec(tree_view.viewport().mapToGlobal(position))
if graph_action is not None and selected is graph_action:
@@ -519,6 +536,8 @@ class MainWindow(QMainWindow):
self.show_component_options(component_id)
elif selected is ports_action:
self.show_component_port_options(component_id)
elif selected is parameters_action:
self.show_component_parameter_options(component_id)
elif selected is delete_action:
answer = QMessageBox.question(
self,
@@ -549,7 +568,9 @@ class MainWindow(QMainWindow):
return
menu = QMenu(self)
ports_action = menu.addAction("Port Options…")
if menu.exec(tree.viewport().mapToGlobal(position)) is ports_action:
parameters_action = menu.addAction("Parameter Options…")
selected = menu.exec(tree.viewport().mapToGlobal(position))
if selected is ports_action:
dialog = PortOptionsDialog(component, self)
if dialog.exec() == dialog.DialogCode.Accepted:
old_inputs, old_outputs = deepcopy(component.inputs), deepcopy(component.outputs)
@@ -572,6 +593,32 @@ class MainWindow(QMainWindow):
self.log.error("Could not change library ports: %s", error)
QMessageBox.warning(self, "Cannot change library ports", str(error))
self.library_tree_model.rebuild()
elif selected is parameters_action:
dialog = ParameterOptionsDialog(component, self)
if dialog.exec() == dialog.DialogCode.Accepted:
old_parameters = deepcopy(component.parameters)
component.parameters = dialog.parameters
library = next(
(
library
for library in self.libraries.libraries
if any(item is component for item in library.document.all_components())
),
None,
)
try:
if library is not None:
library.document.validate()
DocumentSerializer.save(
library.document, Path(library.source_path)
)
except (OSError, ValueError) as error:
component.parameters = old_parameters
self.log.error("Could not change library parameters: %s", error)
QMessageBox.warning(
self, "Cannot change library parameters", str(error)
)
self.library_tree_model.rebuild()
@Slot(str)
def show_component_port_options(self, component_id: str) -> None:
@@ -590,6 +637,23 @@ class MainWindow(QMainWindow):
self.log.error("Could not change component ports: %s", error)
QMessageBox.warning(self, "Cannot change ports", str(error))
@Slot(str)
def show_component_parameter_options(self, component_id: str) -> None:
document = self.document_controller.document
component = document.find_component(component_id) if document else None
if component is None:
return
dialog = ParameterOptionsDialog(component, self)
if dialog.exec() != dialog.DialogCode.Accepted:
return
try:
self.document_controller.edit_component_parameters(
component_id, dialog.parameters
)
except ValueError as error:
self.log.error("Could not change component parameters: %s", error)
QMessageBox.warning(self, "Cannot change parameters", str(error))
@Slot(str)
def show_component_options(self, component_id: str) -> None:
document = self.document_controller.document