Formatting and min max arguments for IntSetting without implementation.

This commit is contained in:
jupfi 2024-03-13 11:57:30 +01:00
parent e314f51ae4
commit 8806e9553c

View file

@ -6,13 +6,15 @@ from PyQt6.QtGui import QValidator, QRegularExpressionValidator
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class Setting(QObject): class Setting(QObject):
"""A setting for the spectrometer is a value that is the same for all events in a pulse sequence. """A setting for the spectrometer is a value that is the same for all events in a pulse sequence.
E.g. the number of averages or the number of points in a spectrum.""" E.g. the number of averages or the number of points in a spectrum."""
settings_changed = pyqtSignal() settings_changed = pyqtSignal()
def __init__(self, name : str, description : str, default = None) -> None: def __init__(self, name: str, description: str, default=None) -> None:
""" Create a new setting. """Create a new setting.
Args: Args:
name (str): The name of the setting. name (str): The name of the setting.
@ -47,23 +49,35 @@ class Setting(QObject):
""" """
widget = QLineEdit(str(self.value)) widget = QLineEdit(str(self.value))
widget.setMinimumWidth(100) widget.setMinimumWidth(100)
widget.editingFinished.connect(lambda x=widget, s=self: s.on_value_changed(x.text())) widget.editingFinished.connect(
lambda x=widget, s=self: s.on_value_changed(x.text())
)
return widget return widget
def update_widget_style(self): def update_widget_style(self):
""" Update the style of the QLineEdit widget to indicate if the value is valid.""" """Update the style of the QLineEdit widget to indicate if the value is valid."""
logger.debug("Updating widget style") if (
if self.validator.validate(self.widget.text(), 0)[0] == QValidator.State.Acceptable: self.validator.validate(self.widget.text(), 0)[0]
== QValidator.State.Acceptable
):
self.widget.setStyleSheet("QLineEdit { background-color: white; }") self.widget.setStyleSheet("QLineEdit { background-color: white; }")
elif self.validator.validate(self.widget.text(), 0)[0] == QValidator.State.Intermediate: elif (
self.validator.validate(self.widget.text(), 0)[0]
== QValidator.State.Intermediate
):
self.widget.setStyleSheet("QLineEdit { background-color: yellow; }") self.widget.setStyleSheet("QLineEdit { background-color: yellow; }")
else: else:
self.widget.setStyleSheet("QLineEdit { background-color: red; }") self.widget.setStyleSheet("QLineEdit { background-color: red; }")
class FloatSetting(Setting): class FloatSetting(Setting):
""" A setting that is a Float. """ """A setting that is a Float."""
DEFAULT_LENGTH = 100 DEFAULT_LENGTH = 100
def __init__(self, name : str, default : float, description : str, validator : QValidator = None) -> None:
def __init__(
self, name: str, default: float, description: str, validator: QValidator = None
) -> None:
super().__init__(name, description, default) super().__init__(name, description, default)
# If a validator is given, set it for the QLineEdit widget # If a validator is given, set it for the QLineEdit widget
@ -97,9 +111,19 @@ class FloatSetting(Setting):
self._value = float(value) self._value = float(value)
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, validator : QValidator = None) -> None:
def __init__(
self,
name: str,
default: int,
description: str,
validator: QValidator = None,
min_value=None,
max_value=None,
) -> None:
super().__init__(name, description, default) super().__init__(name, description, default)
# If a validator is given, set it for the QLineEdit widget # If a validator is given, set it for the QLineEdit widget
@ -126,10 +150,11 @@ class IntSetting(Setting):
raise ValueError("Value must be an int") raise ValueError("Value must be an int")
self.settings_changed.emit() self.settings_changed.emit()
class BooleanSetting(Setting):
""" A setting that is a Boolean."""
def __init__(self, name : str, default : bool, description : str) -> None: class BooleanSetting(Setting):
"""A setting that is a Boolean."""
def __init__(self, name: str, default: bool, description: str) -> None:
super().__init__(name, description, default) super().__init__(name, description, default)
# Overrides the default widget # Overrides the default widget
@ -147,7 +172,6 @@ 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): def get_widget(self):
"""Return a widget for the setting. """Return a widget for the setting.
This returns a QCheckBox widget. This returns a QCheckBox widget.
@ -157,12 +181,18 @@ class BooleanSetting(Setting):
""" """
widget = QCheckBox() widget = QCheckBox()
widget.setChecked(self.value) widget.setChecked(self.value)
widget.stateChanged.connect(lambda x=widget, s=self: s.on_value_changed(bool(x))) widget.stateChanged.connect(
lambda x=widget, s=self: s.on_value_changed(bool(x))
)
return widget return widget
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:
super().__init__(name, description, default) super().__init__(name, description, default)
# Check if default is in options # Check if default is in options
if default not in options: if default not in options:
@ -201,12 +231,16 @@ class SelectionSetting(Setting):
widget = QComboBox() widget = QComboBox()
widget.addItems(self.options) widget.addItems(self.options)
widget.setCurrentText(self.value) widget.setCurrentText(self.value)
widget.currentTextChanged.connect(lambda x=widget, s=self: s.on_value_changed(x)) widget.currentTextChanged.connect(
lambda x=widget, s=self: s.on_value_changed(x)
)
return widget 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:
super().__init__(name, description) super().__init__(name, description)
self.value = default self.value = default
@ -223,9 +257,11 @@ class IPSetting(Setting):
raise ValueError("Value must be a valid IP address") raise ValueError("Value must be a valid IP address")
self.settings_changed.emit() self.settings_changed.emit()
class StringSetting(Setting): class StringSetting(Setting):
""" A setting that is a string.""" """A setting that is a string."""
def __init__(self, name : str, default : str, description : str) -> None:
def __init__(self, name: str, default: str, description: str) -> None:
super().__init__(name, description, default) super().__init__(name, description, default)
@property @property