Launching of simulation application
This commit is contained in:
107
src/bedit_gui/controllers/simulation_file_controller.py
Normal file
107
src/bedit_gui/controllers/simulation_file_controller.py
Normal file
@@ -0,0 +1,107 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from PySide6.QtCore import QObject, Qt
|
||||
from PySide6.QtWidgets import QApplication, QFileDialog, QInputDialog, QMessageBox
|
||||
|
||||
from bedit_gui.services import document_files, simulation_files
|
||||
from bedit_gui.services.simulation_loader import compile_simulation_root, component_choices, load_and_compile_bedit
|
||||
from bedit_gui.simulation_models import CompiledModel, SimulationRoot
|
||||
from bedit_gui.views.simulation_window import SimulationWindow
|
||||
|
||||
|
||||
class SimulationFileController(QObject):
|
||||
def __init__(self, window: SimulationWindow) -> None:
|
||||
super().__init__(window)
|
||||
self.window = window
|
||||
self.root: SimulationRoot | None = None
|
||||
self.compiled_model: CompiledModel | None = None
|
||||
self.path: Path | None = None
|
||||
window.ui.actionNew_Simulation_Run.triggered.connect(self.new)
|
||||
window.ui.actionOpen_Simulation_Run.triggered.connect(self.open_dialog)
|
||||
window.ui.actionSave_Simulation_Run.triggered.connect(self.save)
|
||||
self._update_window()
|
||||
|
||||
def new(self) -> None:
|
||||
self.root = None
|
||||
self.compiled_model = None
|
||||
self.path = None
|
||||
self._update_window()
|
||||
|
||||
def open(self, path: str | Path, *, component: str | None = None, simulation: str | None = None, backed_by_file: bool = True, compiled_model: CompiledModel | None = None) -> None:
|
||||
file_path = Path(path)
|
||||
if file_path.suffix.lower() == ".bes" or simulation_files.is_simulation_json(file_path):
|
||||
self.root = simulation_files.load(file_path)
|
||||
self.compiled_model = compiled_model or self._recompile_with_wait_cursor(self.root)
|
||||
self.path = file_path if backed_by_file else None
|
||||
else:
|
||||
self.root, self.compiled_model = self._compile_with_wait_cursor(file_path, component=component, simulation=simulation)
|
||||
self.path = None
|
||||
self._update_window()
|
||||
|
||||
def open_dialog(self) -> None:
|
||||
filename, _ = QFileDialog.getOpenFileName(self.window, "Open Simulation", "", "Simulation and BEdit files (*.bes *.beb *.json)")
|
||||
if not filename:
|
||||
return
|
||||
try:
|
||||
path = Path(filename)
|
||||
if path.suffix.lower() == ".bes" or simulation_files.is_simulation_json(path):
|
||||
self.open(path)
|
||||
return
|
||||
document = document_files.load(path)
|
||||
database = document.metadata.get("simulation_database") if document.metadata is not None else None
|
||||
entries = [(f"Simulation: {simulation.name}", None, simulation.name) for simulation in getattr(database, "simulations", {}).values()]
|
||||
entries.extend((f"Component: {component_path}", component_path, None) for _component_id, _component, component_path in component_choices(document))
|
||||
if not entries:
|
||||
raise ValueError("the BEdit document contains no components or simulation settings")
|
||||
label, accepted = QInputDialog.getItem(self.window, "Simulation Target", "Compile:", [entry[0] for entry in entries], 0, False)
|
||||
if accepted:
|
||||
_display, component, simulation = next(entry for entry in entries if entry[0] == label)
|
||||
self.open(path, component=component, simulation=simulation)
|
||||
except (OSError, RuntimeError, ValueError) as exc:
|
||||
QMessageBox.critical(self.window, "Could not open simulation", str(exc))
|
||||
|
||||
def save(self) -> None:
|
||||
if self.root is None:
|
||||
return
|
||||
path = self.path
|
||||
if path is None:
|
||||
filename, _ = QFileDialog.getSaveFileName(self.window, "Save Simulation", "simulation.bes", "BEdit simulation (*.bes);;Simulation JSON (*.json)")
|
||||
if not filename:
|
||||
return
|
||||
path = Path(filename)
|
||||
if path.suffix.lower() not in (".bes", ".json"):
|
||||
path = path.with_suffix(".bes")
|
||||
try:
|
||||
simulation_files.save(self.root, path)
|
||||
except (OSError, ValueError) as exc:
|
||||
QMessageBox.critical(self.window, "Could not save simulation", str(exc))
|
||||
return
|
||||
self.path = path
|
||||
self._update_window()
|
||||
|
||||
def _compile_with_wait_cursor(self, path: Path, *, component: str | None, simulation: str | None) -> tuple[SimulationRoot, CompiledModel]:
|
||||
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
|
||||
try:
|
||||
return load_and_compile_bedit(path, component_selector=component, simulation_selector=simulation)
|
||||
finally:
|
||||
QApplication.restoreOverrideCursor()
|
||||
|
||||
def _recompile_with_wait_cursor(self, root: SimulationRoot) -> CompiledModel:
|
||||
QApplication.setOverrideCursor(Qt.CursorShape.WaitCursor)
|
||||
try:
|
||||
return compile_simulation_root(root)
|
||||
finally:
|
||||
QApplication.restoreOverrideCursor()
|
||||
|
||||
def _update_window(self) -> None:
|
||||
if self.root is None:
|
||||
self.window.setWindowTitle("BEdit Simulator")
|
||||
self.window.statusBar().showMessage("No simulation loaded")
|
||||
self.window.ui.actionSave_Simulation_Run.setEnabled(False)
|
||||
return
|
||||
self.window.setWindowTitle(f"{self.root.settings.name} — BEdit Simulator")
|
||||
compiled = self.compiled_model.executable if self.compiled_model is not None else "not compiled"
|
||||
self.window.statusBar().showMessage(f"{self.root.component_path} · {compiled}")
|
||||
self.window.ui.actionSave_Simulation_Run.setEnabled(True)
|
||||
Reference in New Issue
Block a user