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):
super().__init__(model, None, controller)
# This stops the view from being added to the main window.
self._view = None
self.view = None
self.settings_view = view(self)
def set_active(self):

View file

@ -7,8 +7,8 @@ from .widget import Ui_Form
logger = logging.getLogger(__name__)
class SpectrometerView(ModuleView):
class SpectrometerView(ModuleView):
def __init__(self, module):
"""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.
@ -22,6 +22,8 @@ class SpectrometerView(ModuleView):
self._ui_form.setupUi(self)
self._actions = dict()
self.blank = QWidget()
def on_active_spectrometer_changed(self, module):
"""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.
@ -29,35 +31,75 @@ class SpectrometerView(ModuleView):
"""
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.
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.
"""
logger.debug("Adding module widget to stacked widget: %s", widget)
self._ui_form.stackedWidgetSettings.addWidget(widget)
self._ui_form.stackedWidgetSettings.setCurrentWidget(widget)
logger.debug(
"Adding settings widget to stacked widget: %s", module.settings_view
)
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):
"""This method changes the active spectrometer to the one that was just added.
:param module: The BaseSpectrometer module that was just added.
"""
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):
"""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")
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 spectrometer_name, spectrometer_module in self._module.model._available_spectrometers.items():
for (
spectrometer_name,
spectrometer_module,
) in self._module.model._available_spectrometers.items():
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].triggered.connect(spectrometer_module.set_active)
self._actions[spectrometer_name] = QAction(
spectrometer_module.model.toolbar_name, menu_item
)
self._actions[spectrometer_name].triggered.connect(
spectrometer_module.set_active
)
# Make it checkable
self._actions[spectrometer_name].setCheckable(True)
@ -77,5 +119,6 @@ class SpectrometerView(ModuleView):
for action in self._actions.values():
action.setChecked(False)
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]
)