Changed to new DuckWidgets with validators.

This commit is contained in:
jupfi 2024-04-01 20:46:05 +02:00
parent 52d0bca29e
commit 13d9adfb4f
5 changed files with 68 additions and 69 deletions

View file

@ -36,45 +36,47 @@ class MeasurementController(ModuleController):
"""Initialize the controller.""" """Initialize the controller."""
super().__init__(module) super().__init__(module)
@pyqtSlot(str) @pyqtSlot(bool, str)
def set_frequency(self, value: str) -> None: def set_frequency(self, state: bool, value: str) -> None:
"""Set frequency in MHz. """Set frequency in MHz.
Args: Args:
value (str): Frequency in MHz. value (str): Frequency in MHz.
state (bool): State of the input (valid or not).
Raises: Raises:
ValueError: If value cannot be converted to float. ValueError: If value cannot be converted to float.
""" """
# Use validator # Use validator
if ( if state:
self.module.model.validator_measurement_frequency.validate(value, 0) self.module.model.frequency_valid = True
== QValidator.State.Acceptable
):
self.module.model.measurement_frequency = float(value) * 1e6 self.module.model.measurement_frequency = float(value) * 1e6
self.module.nqrduck_signal.emit( self.module.nqrduck_signal.emit(
"set_frequency", str(self.module.model.measurement_frequency) "set_frequency", str(self.module.model.measurement_frequency)
) )
else:
self.module.model.frequency_valid = False
self.toggle_start_button() self.toggle_start_button()
@pyqtSlot(str) @pyqtSlot(bool, str)
def set_averages(self, value: str) -> None: def set_averages(self, state: bool, value: str) -> None:
"""Set number of averages. """Set number of averages.
Args: Args:
value (str): Number of averages. value (str): Number of averages.
state (bool): State of the input (valid or not).
""" """
logger.debug("Setting averages to: " + value) logger.debug("Setting averages to: " + value)
# self.module.nqrduck_signal.emit("set_averages", value) # self.module.nqrduck_signal.emit("set_averages", value)
if ( if state:
self.module.model.validator_averages.validate(value, 0) self.module.model.averages_valid = True
== QValidator.State.Acceptable
):
self.module.model.averages = int(value) self.module.model.averages = int(value)
self.module.nqrduck_signal.emit( self.module.nqrduck_signal.emit(
"set_averages", str(self.module.model.averages) "set_averages", str(self.module.model.averages)
) )
else:
self.module.model.averages_valid = False
self.toggle_start_button() self.toggle_start_button()
@ -105,16 +107,9 @@ class MeasurementController(ModuleController):
def toggle_start_button(self) -> None: def toggle_start_button(self) -> None:
"""Based on wether the Validators for frequency and averages are in an acceptable state, the start button is enabled or disabled.""" """Based on wether the Validators for frequency and averages are in an acceptable state, the start button is enabled or disabled."""
if ( logger.debug(self.module.model.frequency_valid)
self.module.model.validator_measurement_frequency.validate( logger.debug(self.module.model.averages_valid)
self.module.view._ui_form.frequencyEdit.text(), 0 if self.module.model.frequency_valid and self.module.model.averages_valid:
)
== QValidator.State.Acceptable
and self.module.model.validator_averages.validate(
self.module.view._ui_form.averagesEdit.text(), 0
)
== QValidator.State.Acceptable
):
self.module.view._ui_form.buttonStart.setEnabled(True) self.module.view._ui_form.buttonStart.setEnabled(True)
else: else:
self.module.view._ui_form.buttonStart.setEnabled(False) self.module.view._ui_form.buttonStart.setEnabled(False)

View file

@ -55,14 +55,12 @@ class MeasurementModel(ModuleModel):
self.measurements = [] self.measurements = []
self._displayed_measurement = None self._displayed_measurement = None
self.validator_measurement_frequency = DuckFloatValidator(
self, min_value=20.0, max_value=1000.0
)
self.validator_averages = DuckIntValidator(self, min_value=1, max_value=1e6)
self.measurement_frequency = 100.0 # MHz self.measurement_frequency = 100.0 # MHz
self.averages = 1 self.averages = 1
self.frequency_valid = False
self.averages_valid = False
@property @property
def view_mode(self) -> str: def view_mode(self) -> str:
"""View mode of the measurement view. """View mode of the measurement view.
@ -116,6 +114,16 @@ class MeasurementModel(ModuleModel):
self._measurement_frequency = value self._measurement_frequency = value
self.measurement_frequency_changed.emit(value) self.measurement_frequency_changed.emit(value)
@property
def frequency_valid(self) -> bool:
"""Check if the frequency is valid."""
return self._frequency_valid
@frequency_valid.setter
def frequency_valid(self, value: bool):
logger.debug("Frequency valid: " + str(value))
self._frequency_valid = value
@property @property
def averages(self): def averages(self):
"""Number of averages.""" """Number of averages."""
@ -125,3 +133,13 @@ class MeasurementModel(ModuleModel):
def averages(self, value: int): def averages(self, value: int):
self._averages = value self._averages = value
self.averages_changed.emit(value) self.averages_changed.emit(value)
@property
def averages_valid(self) -> bool:
"""Check if the number of averages is valid."""
logger.debug("Averages valid: " + str(self._averages_valid))
return self._averages_valid
@averages_valid.setter
def averages_valid(self, value: bool):
self._averages_valid = value

View file

@ -65,10 +65,10 @@
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QLineEdit" name="averagesEdit"/> <widget class="DuckIntEdit" name="averagesEdit"/>
</item> </item>
<item row="0" column="1"> <item row="0" column="1">
<widget class="QLineEdit" name="frequencyEdit"/> <widget class="DuckFloatEdit" name="frequencyEdit"/>
</item> </item>
<item row="0" column="2"> <item row="0" column="2">
<widget class="QLabel" name="frequencyunitLabel"> <widget class="QLabel" name="frequencyunitLabel">
@ -248,6 +248,16 @@
<header>nqrduck/contrib/mplwidget.h</header> <header>nqrduck/contrib/mplwidget.h</header>
<container>1</container> <container>1</container>
</customwidget> </customwidget>
<customwidget>
<class>DuckFloatEdit</class>
<extends>QLineEdit</extends>
<header>nqrduck/helpers/duckwidgets.h</header>
</customwidget>
<customwidget>
<class>DuckIntEdit</class>
<extends>QLineEdit</extends>
<header>nqrduck/helpers/duckwidgets.h</header>
</customwidget>
</customwidgets> </customwidgets>
<resources/> <resources/>
<connections/> <connections/>

View file

@ -57,28 +57,11 @@ class MeasurementView(ModuleView):
self._ui_form.fftButton.clicked.connect(self.module.controller.change_view_mode) self._ui_form.fftButton.clicked.connect(self.module.controller.change_view_mode)
# Measurement settings controller # Measurement settings controller
self._ui_form.frequencyEdit.textChanged.connect( self._ui_form.frequencyEdit.state_updated.connect(
lambda: self.module.controller.set_frequency( lambda state, text: self.module.controller.set_frequency(state, text)
self._ui_form.frequencyEdit.text()
)
) )
self._ui_form.averagesEdit.textChanged.connect( self._ui_form.averagesEdit.state_updated.connect(
lambda: self.module.controller.set_averages( lambda state, text: self.module.controller.set_averages(state, text)
self._ui_form.averagesEdit.text()
)
)
# Update fields
self._ui_form.frequencyEdit.textChanged.connect(
lambda: self.update_input_widgets(
self._ui_form.frequencyEdit,
self.module.model.validator_measurement_frequency,
)
)
self._ui_form.averagesEdit.textChanged.connect(
lambda: self.update_input_widgets(
self._ui_form.averagesEdit, self.module.model.validator_averages
)
) )
self.module.controller.set_frequency_failure.connect( self.module.controller.set_frequency_failure.connect(
@ -116,6 +99,13 @@ class MeasurementView(ModuleView):
self._ui_form.spLabel.setStyleSheet("font-weight: bold;") self._ui_form.spLabel.setStyleSheet("font-weight: bold;")
# Set Min Max Values for frequency and averages
self._ui_form.frequencyEdit.set_min_value(20.0)
self._ui_form.frequencyEdit.set_max_value(1000.0)
self._ui_form.averagesEdit.set_min_value(1)
self._ui_form.averagesEdit.set_max_value(1e6)
def init_plotter(self) -> None: def init_plotter(self) -> None:
"""Initialize plotter with the according units for time domain.""" """Initialize plotter with the according units for time domain."""
plotter = self._ui_form.plotter plotter = self._ui_form.plotter
@ -223,21 +213,6 @@ class MeasurementView(ModuleView):
if file_name: if file_name:
self.module.controller.load_measurement(file_name) self.module.controller.load_measurement(file_name)
@pyqtSlot()
def update_input_widgets(self, widget, validator) -> None:
"""Update the style of the QLineEdit widget to indicate if the value is valid.
Args:
widget (QLineEdit): The widget to update.
validator (QValidator): The validator to use for the widget.
"""
if validator.validate(widget.text(), 0) == QValidator.State.Acceptable:
widget.setStyleSheet("")
elif validator.validate(widget.text(), 0) == QValidator.State.Intermediate:
widget.setStyleSheet("QLineEdit { background-color: yellow;}")
else:
widget.setStyleSheet("QLineEdit { background-color: red;}")
class MeasurementDialog(QDialog): class MeasurementDialog(QDialog):
"""This Dialog is shown when the measurement is started and therefore blocks the main window. """This Dialog is shown when the measurement is started and therefore blocks the main window.

View file

@ -1,4 +1,4 @@
# Form implementation generated from reading ui file 'Modules/nqrduck-measurement/src/nqrduck_measurement/resources/measurement_widget.ui' # Form implementation generated from reading ui file '../nqrduck-measurement/src/nqrduck_measurement/resources/measurement_widget.ui'
# #
# Created by: PyQt6 UI code generator 6.5.1 # Created by: PyQt6 UI code generator 6.5.1
# #
@ -9,7 +9,7 @@
from PyQt6 import QtCore, QtGui, QtWidgets from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form: class Ui_Form(object):
def setupUi(self, Form): def setupUi(self, Form):
Form.setObjectName("Form") Form.setObjectName("Form")
Form.resize(1920, 1080) Form.resize(1920, 1080)
@ -45,10 +45,10 @@ class Ui_Form:
self.frequencyLabel = QtWidgets.QLabel(parent=Form) self.frequencyLabel = QtWidgets.QLabel(parent=Form)
self.frequencyLabel.setObjectName("frequencyLabel") self.frequencyLabel.setObjectName("frequencyLabel")
self.gridLayout.addWidget(self.frequencyLabel, 0, 0, 1, 1) self.gridLayout.addWidget(self.frequencyLabel, 0, 0, 1, 1)
self.averagesEdit = QtWidgets.QLineEdit(parent=Form) self.averagesEdit = DuckIntEdit(parent=Form)
self.averagesEdit.setObjectName("averagesEdit") self.averagesEdit.setObjectName("averagesEdit")
self.gridLayout.addWidget(self.averagesEdit, 1, 1, 1, 1) self.gridLayout.addWidget(self.averagesEdit, 1, 1, 1, 1)
self.frequencyEdit = QtWidgets.QLineEdit(parent=Form) self.frequencyEdit = DuckFloatEdit(parent=Form)
self.frequencyEdit.setObjectName("frequencyEdit") self.frequencyEdit.setObjectName("frequencyEdit")
self.gridLayout.addWidget(self.frequencyEdit, 0, 1, 1, 1) self.gridLayout.addWidget(self.frequencyEdit, 0, 1, 1, 1)
self.frequencyunitLabel = QtWidgets.QLabel(parent=Form) self.frequencyunitLabel = QtWidgets.QLabel(parent=Form)
@ -146,3 +146,4 @@ class Ui_Form:
self.importButton.setText(_translate("Form", "Import Measurement")) self.importButton.setText(_translate("Form", "Import Measurement"))
self.fftButton.setText(_translate("Form", "FFT")) self.fftButton.setText(_translate("Form", "FFT"))
from nqrduck.contrib.mplwidget import MplWidget from nqrduck.contrib.mplwidget import MplWidget
from nqrduck.helpers.duckwidgets import DuckFloatEdit, DuckIntEdit