Added automatic loading of pulse programmer.

This commit is contained in:
jupfi 2023-07-10 09:10:13 +02:00
parent 4d56beba1c
commit 9eb4999246
2 changed files with 60 additions and 17 deletions

View file

@ -8,7 +8,7 @@ class BaseSpectrometer(Module):
def __init__(self, model, view, controller): def __init__(self, model, view, controller):
super().__init__(model, None, controller) super().__init__(model, None, controller)
# This stops the view from being added to the main window. # This stops the view from being added to the main window.
self._view = None self.view = None
self.settings_view = view(self) self.settings_view = view(self)
def set_active(self): def set_active(self):

View file

@ -7,8 +7,8 @@ from .widget import Ui_Form
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class SpectrometerView(ModuleView): class SpectrometerView(ModuleView):
def __init__(self, module): def __init__(self, module):
"""This class is the view for the spectrometer module. It contains the menu buttons for the different spectrometers. """This class is the view for the spectrometer module. It contains the menu buttons for the different spectrometers.
It also contains the stacked widget that shows the different spectrometer views. It also contains the stacked widget that shows the different spectrometer views.
@ -22,6 +22,8 @@ class SpectrometerView(ModuleView):
self._ui_form.setupUi(self) self._ui_form.setupUi(self)
self._actions = dict() self._actions = dict()
self.blank = QWidget()
def on_active_spectrometer_changed(self, module): def on_active_spectrometer_changed(self, module):
"""This method is called when the active spectrometer is changed. """This method is called when the active spectrometer is changed.
It changes the active view in the stacked widget to the one that was just activated. It changes the active view in the stacked widget to the one that was just activated.
@ -29,38 +31,78 @@ class SpectrometerView(ModuleView):
""" """
self._ui_form.stackedWidgetSettings.setCurrentWidget(module.settings_view) self._ui_form.stackedWidgetSettings.setCurrentWidget(module.settings_view)
def on_spectrometer_widget_changed(self, widget): try:
self._ui_form.stackedWidgetPulseProgrammer.setCurrentWidget(
module.model.pulse_programmer.pulse_programmer_view
)
except AttributeError:
logger.debug(
"No pulse programmer widget to change to for spectrometer %s",
module.model.name,
)
self._ui_form.stackedWidgetPulseProgrammer.setCurrentWidget(self.blank)
def on_spectrometer_widget_changed(self, module):
"""This method is called when a new spectrometer widget is added to the module. """This method is called when a new spectrometer widget is added to the module.
It adds the widget to the stacked widget and sets it as the current widget. It adds the widget to the stacked widget and sets it as the current widget.
:param widget: The widget that was added to the module. :param widget: The widget that was added to the module.
""" """
logger.debug("Adding module widget to stacked widget: %s", widget) logger.debug(
self._ui_form.stackedWidgetSettings.addWidget(widget) "Adding settings widget to stacked widget: %s", module.settings_view
self._ui_form.stackedWidgetSettings.setCurrentWidget(widget) )
self._ui_form.stackedWidgetSettings.addWidget(module.settings_view)
self._ui_form.stackedWidgetSettings.setCurrentWidget(module.settings_view)
try:
logger.debug(
"Adding pulse programmer widget to stacked widget: %s",
module.model.pulse_programmer.pulse_programmer_view,
)
self._ui_form.stackedWidgetPulseProgrammer.addWidget(
module.model.pulse_programmer.pulse_programmer_view
)
self._ui_form.stackedWidgetPulseProgrammer.setCurrentWidget(
module.model.pulse_programmer.pulse_programmer_view
)
except AttributeError as e:
logger.debug(
"No pulse programmer widget to add for spectrometer %s",
module.model.name,
)
# Sets the pulse programmer widget to a blank widget if there is no pulse programmer widget.
self._ui_form.stackedWidgetPulseProgrammer.addWidget(self.blank)
self._ui_form.stackedWidgetPulseProgrammer.setCurrentWidget(self.blank)
def on_spectrometer_added(self, module): def on_spectrometer_added(self, module):
"""This method changes the active spectrometer to the one that was just added. """This method changes the active spectrometer to the one that was just added.
:param module: The BaseSpectrometer module that was just added. :param module: The BaseSpectrometer module that was just added.
""" """
module.change_spectrometer.connect(self.on_menu_button_clicked) module.change_spectrometer.connect(self.on_menu_button_clicked)
self.on_spectrometer_widget_changed(module.settings_view) self.on_spectrometer_widget_changed(module)
def create_menu_entry(self): def create_menu_entry(self):
"""This method creates the menu entry for the spectrometer module. It creates a menu item for each spectrometer that is available. """This method creates the menu entry for the spectrometer module. It creates a menu item for each spectrometer that is available."""
"""
logger.debug("Creating menu entry for spectrometer module") logger.debug("Creating menu entry for spectrometer module")
menu_item = QMenu("Hardware") menu_item = QMenu("Hardware")
logger.debug("Available spectrometer models: %s", self._module.model._available_spectrometers) logger.debug(
"Available spectrometer models: %s",
self._module.model._available_spectrometers,
)
for (
for spectrometer_name, spectrometer_module in self._module.model._available_spectrometers.items(): spectrometer_name,
spectrometer_module,
) in self._module.model._available_spectrometers.items():
logger.debug("Adding module to menu: %s", spectrometer_name) logger.debug("Adding module to menu: %s", spectrometer_name)
self._actions[spectrometer_name] = QAction(spectrometer_module.model.toolbar_name, menu_item) self._actions[spectrometer_name] = QAction(
self._actions[spectrometer_name].triggered.connect(spectrometer_module.set_active) spectrometer_module.model.toolbar_name, menu_item
)
self._actions[spectrometer_name].triggered.connect(
spectrometer_module.set_active
)
# Make it checkable # Make it checkable
self._actions[spectrometer_name].setCheckable(True) self._actions[spectrometer_name].setCheckable(True)
# Get last added action and check it # Get last added action and check it
last_added_action = self._actions[list(self._actions.keys())[-1]] last_added_action = self._actions[list(self._actions.keys())[-1]]
last_added_action.setChecked(True) last_added_action.setChecked(True)
@ -77,5 +119,6 @@ class SpectrometerView(ModuleView):
for action in self._actions.values(): for action in self._actions.values():
action.setChecked(False) action.setChecked(False)
self._actions[spectrometer_name].setChecked(True) self._actions[spectrometer_name].setChecked(True)
self._module.model.active_spectrometer = self._module.model.available_spectrometers[spectrometer_name] self._module.model.active_spectrometer = (
self._module.model.available_spectrometers[spectrometer_name]
)