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."""
super().__init__(module)
@pyqtSlot(str)
def set_frequency(self, value: str) -> None:
@pyqtSlot(bool, str)
def set_frequency(self, state: bool, value: str) -> None:
"""Set frequency in MHz.
Args:
value (str): Frequency in MHz.
state (bool): State of the input (valid or not).
Raises:
ValueError: If value cannot be converted to float.
"""
# Use validator
if (
self.module.model.validator_measurement_frequency.validate(value, 0)
== QValidator.State.Acceptable
):
if state:
self.module.model.frequency_valid = True
self.module.model.measurement_frequency = float(value) * 1e6
self.module.nqrduck_signal.emit(
"set_frequency", str(self.module.model.measurement_frequency)
)
else:
self.module.model.frequency_valid = False
self.toggle_start_button()
@pyqtSlot(str)
def set_averages(self, value: str) -> None:
@pyqtSlot(bool, str)
def set_averages(self, state: bool, value: str) -> None:
"""Set number of averages.
Args:
value (str): Number of averages.
state (bool): State of the input (valid or not).
"""
logger.debug("Setting averages to: " + value)
# self.module.nqrduck_signal.emit("set_averages", value)
if (
self.module.model.validator_averages.validate(value, 0)
== QValidator.State.Acceptable
):
if state:
self.module.model.averages_valid = True
self.module.model.averages = int(value)
self.module.nqrduck_signal.emit(
"set_averages", str(self.module.model.averages)
)
else:
self.module.model.averages_valid = False
self.toggle_start_button()
@ -105,16 +107,9 @@ class MeasurementController(ModuleController):
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."""
if (
self.module.model.validator_measurement_frequency.validate(
self.module.view._ui_form.frequencyEdit.text(), 0
)
== QValidator.State.Acceptable
and self.module.model.validator_averages.validate(
self.module.view._ui_form.averagesEdit.text(), 0
)
== QValidator.State.Acceptable
):
logger.debug(self.module.model.frequency_valid)
logger.debug(self.module.model.averages_valid)
if self.module.model.frequency_valid and self.module.model.averages_valid:
self.module.view._ui_form.buttonStart.setEnabled(True)
else:
self.module.view._ui_form.buttonStart.setEnabled(False)

View file

@ -55,14 +55,12 @@ class MeasurementModel(ModuleModel):
self.measurements = []
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.averages = 1
self.frequency_valid = False
self.averages_valid = False
@property
def view_mode(self) -> str:
"""View mode of the measurement view.
@ -116,6 +114,16 @@ class MeasurementModel(ModuleModel):
self._measurement_frequency = 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
def averages(self):
"""Number of averages."""
@ -125,3 +133,13 @@ class MeasurementModel(ModuleModel):
def averages(self, value: int):
self._averages = 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>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="averagesEdit"/>
<widget class="DuckIntEdit" name="averagesEdit"/>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="frequencyEdit"/>
<widget class="DuckFloatEdit" name="frequencyEdit"/>
</item>
<item row="0" column="2">
<widget class="QLabel" name="frequencyunitLabel">
@ -248,6 +248,16 @@
<header>nqrduck/contrib/mplwidget.h</header>
<container>1</container>
</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>
<resources/>
<connections/>

View file

@ -57,28 +57,11 @@ class MeasurementView(ModuleView):
self._ui_form.fftButton.clicked.connect(self.module.controller.change_view_mode)
# Measurement settings controller
self._ui_form.frequencyEdit.textChanged.connect(
lambda: self.module.controller.set_frequency(
self._ui_form.frequencyEdit.text()
)
self._ui_form.frequencyEdit.state_updated.connect(
lambda state, text: self.module.controller.set_frequency(state, text)
)
self._ui_form.averagesEdit.textChanged.connect(
lambda: self.module.controller.set_averages(
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._ui_form.averagesEdit.state_updated.connect(
lambda state, text: self.module.controller.set_averages(state, text)
)
self.module.controller.set_frequency_failure.connect(
@ -116,6 +99,13 @@ class MeasurementView(ModuleView):
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:
"""Initialize plotter with the according units for time domain."""
plotter = self._ui_form.plotter
@ -223,21 +213,6 @@ class MeasurementView(ModuleView):
if 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):
"""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
#
@ -9,7 +9,7 @@
from PyQt6 import QtCore, QtGui, QtWidgets
class Ui_Form:
class Ui_Form(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(1920, 1080)
@ -45,10 +45,10 @@ class Ui_Form:
self.frequencyLabel = QtWidgets.QLabel(parent=Form)
self.frequencyLabel.setObjectName("frequencyLabel")
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.gridLayout.addWidget(self.averagesEdit, 1, 1, 1, 1)
self.frequencyEdit = QtWidgets.QLineEdit(parent=Form)
self.frequencyEdit = DuckFloatEdit(parent=Form)
self.frequencyEdit.setObjectName("frequencyEdit")
self.gridLayout.addWidget(self.frequencyEdit, 0, 1, 1, 1)
self.frequencyunitLabel = QtWidgets.QLabel(parent=Form)
@ -146,3 +146,4 @@ class Ui_Form:
self.importButton.setText(_translate("Form", "Import Measurement"))
self.fftButton.setText(_translate("Form", "FFT"))
from nqrduck.contrib.mplwidget import MplWidget
from nqrduck.helpers.duckwidgets import DuckFloatEdit, DuckIntEdit