Added widget creation to settings object.

This commit is contained in:
jupfi 2024-03-11 18:09:29 +01:00
parent cb5df0a9af
commit a39b4a1bc7
2 changed files with 41 additions and 19 deletions

View file

@ -53,24 +53,7 @@ class BaseSpectrometerView(ModuleView):
setting_label = QLabel(setting.name) setting_label = QLabel(setting.name)
setting_label.setMinimumWidth(200) setting_label.setMinimumWidth(200)
# Depending on the setting type we add different widgets to the view edit_widget = setting.get_widget()
if isinstance(setting, FloatSetting) or isinstance(setting, IntSetting) or isinstance(setting, StringSetting):
edit_widget = QLineEdit(str(setting.value))
edit_widget.setMinimumWidth(100)
# Connect the editingFinished signal to the on_value_changed slot of the setting
edit_widget.editingFinished.connect(lambda x=edit_widget, s=setting: s.on_value_changed(x.text()))
elif isinstance(setting, BooleanSetting):
edit_widget = QCheckBox()
edit_widget.setChecked(setting.value)
edit_widget.stateChanged.connect(lambda x=edit_widget, s=setting: s.on_value_changed(x))
elif isinstance(setting, SelectionSetting):
edit_widget = QComboBox()
edit_widget.addItems(setting.options)
edit_widget.setCurrentText(setting.value)
edit_widget.currentTextChanged.connect(lambda x=edit_widget, s=setting: s.on_value_changed(x))
# Add a icon that can be used as a tooltip # Add a icon that can be used as a tooltip
if setting.description is not None: if setting.description is not None:

View file

@ -1,6 +1,7 @@
import logging import logging
import ipaddress import ipaddress
from PyQt6.QtCore import QObject, pyqtSignal, pyqtSlot from PyQt6.QtCore import QObject, pyqtSignal, pyqtSlot
from PyQt6.QtWidgets import QLineEdit, QComboBox, QCheckBox
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -23,6 +24,20 @@ class Setting(QObject):
def get_setting(self): def get_setting(self):
return float(self.value) return float(self.value)
def get_widget(self):
"""Return a widget for the setting.
The default widget is simply a QLineEdit.
This method can be overwritten by subclasses to return a different widget.
Returns:
QLineEdit: A QLineEdit widget that can be used to change the setting.
"""
widget = QLineEdit(str(self.value))
widget.setMinimumWidth(100)
widget.editingFinished.connect(lambda x=widget, s=self: s.on_value_changed(x.text()))
return widget
class FloatSetting(Setting): class FloatSetting(Setting):
""" A setting that is a Float. """ """ A setting that is a Float. """
def __init__(self, name : str, default : float, description : str) -> None: def __init__(self, name : str, default : float, description : str) -> None:
@ -41,7 +56,6 @@ class FloatSetting(Setting):
raise ValueError("Value must be a float") raise ValueError("Value must be a float")
self.settings_changed.emit() self.settings_changed.emit()
class IntSetting(Setting): class IntSetting(Setting):
""" A setting that is an Integer.""" """ A setting that is an Integer."""
def __init__(self, name : str, default : int, description : str) -> None: def __init__(self, name : str, default : int, description : str) -> None:
@ -79,6 +93,18 @@ class BooleanSetting(Setting):
raise ValueError("Value must be a bool") raise ValueError("Value must be a bool")
self.settings_changed.emit() self.settings_changed.emit()
def get_widget(self):
"""Return a widget for the setting.
This returns a QCheckBox widget.
Returns:
QCheckBox: A QCheckBox widget that can be used to change the setting.
"""
widget = QCheckBox()
widget.setChecked(self.value)
widget.stateChanged.connect(lambda x=widget, s=self: s.on_value_changed(x.text()))
class SelectionSetting(Setting): class SelectionSetting(Setting):
""" A setting that is a selection from a list of options.""" """ A setting that is a selection from a list of options."""
def __init__(self, name : str, options : list, default : str, description : str) -> None: def __init__(self, name : str, options : list, default : str, description : str) -> None:
@ -102,6 +128,19 @@ class SelectionSetting(Setting):
raise ValueError("Value must be one of the options") raise ValueError("Value must be one of the options")
self.settings_changed.emit() self.settings_changed.emit()
def get_widget(self):
"""Return a widget for the setting.
This returns a QComboBox widget.
Returns:
QComboBox: A QComboBox widget that can be used to change the setting.
"""
widget = QComboBox()
widget.addItems(self.options)
widget.setCurrentText(self.value)
widget.currentTextChanged.connect(lambda x=widget, s=self: s.on_value_changed(x.text()))
return widget
class IPSetting(Setting): class IPSetting(Setting):
""" A setting that is an IP address.""" """ A setting that is an IP address."""
def __init__(self, name : str, default : str, description : str) -> None: def __init__(self, name : str, default : str, description : str) -> None: