From 35e1fd0e6ce8421fdd48f377023f45e272a69867 Mon Sep 17 00:00:00 2001 From: jupfi Date: Tue, 4 Jul 2023 13:07:43 +0200 Subject: [PATCH] Implemented change of spectrometer. --- src/nqrduck_spectrometer/base_spectrometer.py | 2 +- src/nqrduck_spectrometer/controller.py | 27 +++++++++++++- src/nqrduck_spectrometer/model.py | 19 +++++----- src/nqrduck_spectrometer/view.py | 37 ++++++++++++++++++- 4 files changed, 71 insertions(+), 14 deletions(-) diff --git a/src/nqrduck_spectrometer/base_spectrometer.py b/src/nqrduck_spectrometer/base_spectrometer.py index e0b9a54..821c131 100644 --- a/src/nqrduck_spectrometer/base_spectrometer.py +++ b/src/nqrduck_spectrometer/base_spectrometer.py @@ -7,7 +7,7 @@ class BaseSpectrometer(Module): super().__init__(model, None, controller) # This stops the view from being added to the main window. self._view = None - self._inner_view = view + self._inner_view = view(self) @property def pulse_program(self): diff --git a/src/nqrduck_spectrometer/controller.py b/src/nqrduck_spectrometer/controller.py index ab41020..d69ee76 100644 --- a/src/nqrduck_spectrometer/controller.py +++ b/src/nqrduck_spectrometer/controller.py @@ -1,9 +1,34 @@ +import logging from nqrduck.module.module_controller import ModuleController +from nqrduck.core.main_controller import MainController +from nqrduck_spectrometer.base_spectrometer import BaseSpectrometer +logger = logging.getLogger(__name__) class SpectrometerController(ModuleController): def __init__(self, module): super().__init__(module) def _load_spectrometer_modules(self): - pass \ No newline at end of file + # Get the modules with entry points in the nqrduck group + modules = MainController._get_modules() + logger.debug("Found modules: %s", modules) + + for module_name, module in modules.items(): + # Check if the module instance is a spectrometer by checking if it inherits from BaseSpectrometer + if not issubclass(type(module), BaseSpectrometer): + logger.debug("Module is not a spectrometer: %s ... skipping", module_name) + continue + + # Import the module + logger.debug("Loading spectromter module: %s", module_name) + module.model.widget_changed.connect(self._module.view.on_spectrometer_widget_changed) + logger.debug("Adding spectrometer to spectrometer model: %s", module_name) + self._module.model.add_spectrometers(module_name, module) + + self._module.view.create_menu_entry() + + def on_loading(self): + self._module.model.spectrometer_added.connect(self._module.view.on_spectrometer_added) + self._module.model.active_spectrometer_changed.connect(self._module.view.on_active_spectrometer_changed) + self._load_spectrometer_modules() \ No newline at end of file diff --git a/src/nqrduck_spectrometer/model.py b/src/nqrduck_spectrometer/model.py index bbb78ee..55389af 100644 --- a/src/nqrduck_spectrometer/model.py +++ b/src/nqrduck_spectrometer/model.py @@ -1,17 +1,18 @@ import logging from PyQt5.QtCore import pyqtSignal from nqrduck.module.module_model import ModuleModel +from .base_spectrometer import BaseSpectrometer logger = logging.getLogger(__name__) class SpectrometerModel(ModuleModel): - spectrometer_changed = pyqtSignal() + spectrometer_added = pyqtSignal(BaseSpectrometer) + active_spectrometer_changed = pyqtSignal(BaseSpectrometer) def __init__(self, module) -> None: super().__init__(module) self._active_spectrometer = None - self._available_spectrometers = [] - self._load_available_spectrometers() + self._available_spectrometers = dict() @property def active_spectrometer(self): @@ -20,17 +21,15 @@ class SpectrometerModel(ModuleModel): @active_spectrometer.setter def active_spectrometer(self, value): self._active_spectrometer = value - self.spectrometer_changed.emit() + self.active_spectrometer_changed.emit(value) @property def available_spectrometers(self): return self._available_spectrometers - def _load_available_spectrometers(self): - pass - - - def _load_spectrometer(self, spectrometer_module_name): - pass + def add_spectrometers(self, spectrometer_module_name, module): + self._available_spectrometers [spectrometer_module_name] = module + logger.debug("Added module: %s", spectrometer_module_name) + self.spectrometer_added.emit(module) diff --git a/src/nqrduck_spectrometer/view.py b/src/nqrduck_spectrometer/view.py index c46e6b5..aef5e5d 100644 --- a/src/nqrduck_spectrometer/view.py +++ b/src/nqrduck_spectrometer/view.py @@ -1,7 +1,9 @@ -from PyQt5.QtWidgets import QWidget +import logging +from PyQt5.QtWidgets import QWidget, QToolButton, QToolBar, QAction, QMenu from nqrduck.module.module_view import ModuleView from .widget import Ui_Form +logger = logging.getLogger(__name__) class SpectrometerView(ModuleView): def __init__(self, module): @@ -9,5 +11,36 @@ class SpectrometerView(ModuleView): widget = QWidget() self._ui_form = Ui_Form() - self._ui_form.setupUi(self) self.widget = widget + self._ui_form.setupUi(self) + + def on_active_spectrometer_changed(self, module): + self._ui_form.stackedWidget.setCurrentWidget(module._inner_view) + + def on_spectrometer_widget_changed(self, widget): + logger.debug("Adding module widget to stacked widget: %s", widget) + self._ui_form.stackedWidget.addWidget(widget) + self._ui_form.stackedWidget.setCurrentWidget(widget) + + + def on_spectrometer_added(self, module): + """This method changes the active spectrometer to the one that was just added.""" + self.on_spectrometer_widget_changed(module._inner_view) + + def create_menu_entry(self): + logger.debug("Creating menu entry for spectrometer module") + menu_item = QMenu("Hardware") + logger.debug("Available spectrometer models: %s", self._module.model._available_spectrometers) + + actions = [] + for spectrometer_name, spectrometer_module in self._module.model._available_spectrometers.items(): + logger.debug("Adding module to menu: %s", spectrometer_name) + select_action = QAction(spectrometer_module.model.toolbar_name, menu_item) + select_action.triggered.connect(lambda: self.on_menu_button_clicked(spectrometer_name)) + actions.append(select_action) + + self.add_menubar_item.emit("Hardware", actions) + + def on_menu_button_clicked(self, spectrometer_name): + logger.debug("Active module changed to: %s", spectrometer_name) + self._module.model.active_spectrometer = self._module.model.available_spectrometers[spectrometer_name]