from __future__ import annotations from collections.abc import Callable from typing import Protocol from PySide6.QtCore import QObject from PySide6.QtWidgets import QDialog from bedit_core.models import Component, ComponentID, GraphImplementation from bedit_gui.documents import Document from bedit_gui.models import SimulationDatabase from bedit_gui.views.dialogs.simulation_settings_dialog import SimulationSettingsDialog from bedit_gui.views.main_window import MainWindow class SimulationSettingsDialogLike(Protocol): def exec(self) -> int: ... def database(self) -> SimulationDatabase: ... SimulationSettingsDialogFactory = Callable[[SimulationDatabase, list[tuple[ComponentID, str]], MainWindow], SimulationSettingsDialogLike] class SimulationSettingsController(QObject): def __init__(self, document: Document, window: MainWindow, dialog_factory: SimulationSettingsDialogFactory = SimulationSettingsDialog) -> None: super().__init__(window) self.document = document self.window = window self.dialog_factory = dialog_factory window.ui.actionSimulation_Settings.triggered.connect(self.open_settings) def open_settings(self) -> None: dialog = self.dialog_factory(self.document.simulation_database(), self._components(), self.window) if dialog.exec() == QDialog.DialogCode.Accepted: self.document.change_simulation_database(dialog.database()) def _components(self) -> list[tuple[ComponentID, str]]: components: list[tuple[ComponentID, str]] = [] def collect(items: dict[ComponentID, Component], path: tuple[str, ...] = ()) -> None: for component_id, component in items.items(): component_path = (*path, component.name) components.append((component_id, ".".join(component_path))) if isinstance(component.implementation, GraphImplementation): collect(component.implementation.graph.components, component_path) collect(self.document.model.root) return components