nqrduck-spectrometer/src/nqrduck_spectrometer/base_spectrometer_view.py
2024-05-27 19:26:58 +02:00

200 lines
7.9 KiB
Python

"""The Base Class for all Spectrometer Views."""
import logging
from PyQt6.QtWidgets import (
QWidget,
QLabel,
QHBoxLayout,
QSizePolicy,
QSpacerItem,
QVBoxLayout,
QPushButton,
QDialog,
)
from nqrduck.module.module_view import ModuleView
from nqrduck.assets.icons import Logos
logger = logging.getLogger(__name__)
class BaseSpectrometerView(ModuleView):
"""The View Class for all Spectrometers."""
def __init__(self, module):
"""Initializes the spectrometer view."""
super().__init__(module)
def load_settings_ui(self) -> None:
"""This method automatically generates a view for the settings of the module.
If there is a widget file that has been generated by Qt Designer, it will be used. Otherwise, a default view will be generated.
"""
from .base_spectrometer_widget import Ui_Form
widget = QWidget()
widget.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Expanding)
self._ui_form = Ui_Form()
self.widget = widget
self._ui_form.setupUi(self)
grid = self._ui_form.gridLayout
self._ui_form.verticalLayout.removeItem(self._ui_form.gridLayout)
# Add name of the spectrometer to the view
label = QLabel(f"{self.module.model.toolbar_name} Settings:")
label.setStyleSheet("font-weight: bold;")
self._ui_form.verticalLayout.setSpacing(5)
self._ui_form.verticalLayout.addWidget(label)
self._ui_form.verticalLayout.addLayout(grid)
for category_count, category in enumerate(self.module.model.settings.keys()):
logger.debug("Adding settings for category: %s", category)
category_layout = QVBoxLayout()
category_label = QLabel(f"{category}:")
category_label.setStyleSheet("font-weight: bold;")
row = category_count // 2
column = category_count % 2
category_layout.addWidget(category_label)
for setting in self.module.model.settings[category]:
logger.debug("Adding setting to settings view: %s", setting.name)
spacer = QSpacerItem(20, 20)
# Create a label for the setting
setting_label = QLabel(setting.name)
setting_label.setMinimumWidth(200)
edit_widget = setting.widget
logger.debug("Setting widget: %s", edit_widget)
# Add a icon that can be used as a tooltip
if setting.description is not None:
logger.debug("Adding tooltip to setting: %s", setting.name)
icon = Logos.QuestionMark_16x16()
icon_label = QLabel()
icon_label.setPixmap(icon.pixmap(icon.availableSizes()[0]))
icon_label.setFixedSize(icon.availableSizes()[0])
icon_label.setToolTip(setting.description)
# Add a horizontal layout for the setting
layout = QHBoxLayout()
# Add the label and the line edit to the layout
layout.addItem(spacer)
layout.addWidget(setting_label)
layout.addWidget(edit_widget)
layout.addStretch(1)
layout.addWidget(icon_label)
# Add the layout to the vertical layout of the widget
category_layout.addLayout(layout)
category_layout.addStretch(1)
self._ui_form.gridLayout.addLayout(category_layout, row, column)
# Push all the settings to the top of the widget
self._ui_form.verticalLayout.addStretch(1)
# Now we add a save and load button to the widget
self.button_layout = QHBoxLayout()
# Default Settings Button
self.default_button = QPushButton("Default Settings")
self.default_button.clicked.connect(self.on_default_button_clicked)
self.button_layout.addWidget(self.default_button)
# Save Button
self.save_button = QPushButton("Save Settings")
self.save_button.setIcon(Logos.Save16x16())
self.save_button.setIconSize(Logos.Save16x16().availableSizes()[0])
self.save_button.clicked.connect(self.on_save_button_clicked)
self.button_layout.addWidget(self.save_button)
# Load Button
self.load_button = QPushButton("Load Settings")
self.load_button.setIcon(Logos.Load16x16())
self.load_button.clicked.connect(self.on_load_button_clicked)
self.button_layout.addWidget(self.load_button)
self.load_button.setIconSize(Logos.Load16x16().availableSizes()[0])
self.button_layout.addStretch(1)
self._ui_form.verticalLayout.addLayout(self.button_layout)
def on_save_button_clicked(self):
"""This method is called when the save button is clicked."""
logger.debug("Save button clicked")
# Open a dialog to save the settings to a file
file_manager = self.FileManager(
extension=self.module.model.SETTING_FILE_EXTENSION, parent=self
)
path = file_manager.saveFileDialog()
if path:
self.module.controller.save_settings(path)
def on_load_button_clicked(self):
"""This method is called when the load button is clicked."""
logger.debug("Load button clicked")
# Open a dialog to load the settings from a file
file_manager = self.FileManager(
extension=self.module.model.SETTING_FILE_EXTENSION, parent=self
)
path = file_manager.loadFileDialog()
self.module.controller.load_settings(path)
if path:
self.module.controller.load_settings(path)
def on_default_button_clicked(self):
"""This method is called when the default button is clicked."""
logger.debug("Default button clicked")
dialog = self.DefaultSettingsDialog(self)
dialog.exec()
class DefaultSettingsDialog(QDialog):
"""Dialog to set or clear the default settings of the spectrometer."""
def __init__(self, parent=None):
"""Initializes the default settings dialog."""
super().__init__(parent)
self.parent = parent
self.setWindowTitle("Default Settings")
self.layout = QVBoxLayout()
# Either we set the current settings as default
self.set_current_button = QPushButton("Set Current Settings as Default")
self.set_current_button.clicked.connect(
self.on_set_current_button_clicked
)
# Or we clear the default settings
self.clear_button = QPushButton("Clear Default Settings")
self.clear_button.clicked.connect(
self.on_clear_button_clicked
)
self.layout.addWidget(self.set_current_button)
self.layout.addWidget(self.clear_button)
self.setLayout(self.layout)
# Ok Button
self.ok_button = QPushButton("Ok")
self.ok_button.clicked.connect(self.accept)
self.layout.addWidget(self.ok_button)
def on_set_current_button_clicked(self):
"""This method is called when the set current button is clicked."""
logger.debug("Set current button clicked")
self.parent.module.model.set_default_settings()
# Show notification that the settings have been set as default
self.parent.module.nqrduck_signal.emit(
"notification", ["Info", "Settings have been set as default."]
)
def on_clear_button_clicked(self):
"""This method is called when the clear button is clicked."""
logger.debug("Clear button clicked")
self.parent.module.model.clear_default_settings()
# Show notification that the default settings have been cleared
self.parent.module.nqrduck_signal.emit(
"notification", ["Info", "Default settings have been cleared."]
)