From b0eee8aeb3b4575fbaf2e4c5bc1261b4f31e65f0 Mon Sep 17 00:00:00 2001 From: jupfi Date: Thu, 6 Jul 2023 16:40:35 +0200 Subject: [PATCH] Implemented dispatcher --- src/nqrduck_spectrometer/base_spectrometer.py | 10 ++--- .../base_spectrometer_controller.py | 11 +++++ src/nqrduck_spectrometer/controller.py | 45 ++++++++++++++++--- src/nqrduck_spectrometer/model.py | 5 +++ 4 files changed, 59 insertions(+), 12 deletions(-) create mode 100644 src/nqrduck_spectrometer/base_spectrometer_controller.py diff --git a/src/nqrduck_spectrometer/base_spectrometer.py b/src/nqrduck_spectrometer/base_spectrometer.py index 6d87e2e..7567916 100644 --- a/src/nqrduck_spectrometer/base_spectrometer.py +++ b/src/nqrduck_spectrometer/base_spectrometer.py @@ -13,13 +13,11 @@ class BaseSpectrometer(Module): @property def pulse_program(self): - """Pulse program of the spectrometer.""" - raise NotImplementedError - - def start_measurement(self): - """Starts the measurement.""" + """Pulse program of the spectrometer. + """ raise NotImplementedError def set_active(self): - """Sets the spectrometer as the active spectrometer.""" + """Sets the spectrometer as the active spectrometer. + """ self.change_spectrometer.emit(self._model.name) diff --git a/src/nqrduck_spectrometer/base_spectrometer_controller.py b/src/nqrduck_spectrometer/base_spectrometer_controller.py new file mode 100644 index 0000000..c628345 --- /dev/null +++ b/src/nqrduck_spectrometer/base_spectrometer_controller.py @@ -0,0 +1,11 @@ +from nqrduck.module.module_controller import ModuleController + +class BaseSpectrometerController(ModuleController): + + def __init__(self, module): + super().__init__(module) + + def start_measurement(self): + """Starts the measurement. + """ + raise NotImplementedError \ No newline at end of file diff --git a/src/nqrduck_spectrometer/controller.py b/src/nqrduck_spectrometer/controller.py index d69ee76..ff99659 100644 --- a/src/nqrduck_spectrometer/controller.py +++ b/src/nqrduck_spectrometer/controller.py @@ -1,15 +1,23 @@ import logging +from PyQt5.QtCore import pyqtSlot 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): + """This class is the controller for the spectrometer module.""" + def __init__(self, module): + """This method initializes the controller. + :param module: The module that this controller belongs to. + """ super().__init__(module) def _load_spectrometer_modules(self): + """This method loads the spectrometer modules and adds them to the spectrometer model.""" # Get the modules with entry points in the nqrduck group modules = MainController._get_modules() logger.debug("Found modules: %s", modules) @@ -17,18 +25,43 @@ class SpectrometerController(ModuleController): 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) + 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) + 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 process_signals(self, key: str, value: str): + if key == "start_measurement": + self.on_measurement_start() + 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 + """This method is called when the module is loaded. + It connects the signals from the spectrometer model to the view. + """ + 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() + + def on_measurement_start(self): + """This method is called when a measurement is started. + It calls the on_measurement_start method of the active spectrometer. + """ + logger.debug( + "Measurement started with spectrometer: %s", + self._module.model.active_spectrometer, + ) + self._module.model.active_spectrometer.controller.start_measurement() diff --git a/src/nqrduck_spectrometer/model.py b/src/nqrduck_spectrometer/model.py index 55389af..2d19999 100644 --- a/src/nqrduck_spectrometer/model.py +++ b/src/nqrduck_spectrometer/model.py @@ -16,6 +16,8 @@ class SpectrometerModel(ModuleModel): @property def active_spectrometer(self): + """The currently active spectrometer. This is the one that is currently being used. + """ return self._active_spectrometer @active_spectrometer.setter @@ -25,11 +27,14 @@ class SpectrometerModel(ModuleModel): @property def available_spectrometers(self): + """A dictionary of all available spectrometers. The key is the name of the spectrometer and the value is the module. + """ return self._available_spectrometers 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) + self.active_spectrometer = module