Fixed validation of float input
Some checks failed
Build Detektor binarie / build_macos (push) Has been cancelled
Some checks failed
Build Detektor binarie / build_macos (push) Has been cancelled
This commit is contained in:
@@ -71,8 +71,9 @@ class ChannelCalibrationDialog(QDialog):
|
|||||||
|
|
||||||
channel_layout.addWidget(RoundedColorRectangleWidget(ch.color))
|
channel_layout.addWidget(RoundedColorRectangleWidget(ch.color))
|
||||||
|
|
||||||
|
from PyQt6.QtCore import QLocale
|
||||||
float_validator = CustomDoubleValidator()
|
float_validator = CustomDoubleValidator()
|
||||||
# float_validator.setLocale("C")
|
float_validator.setLocale(QLocale(QLocale.Language.C))
|
||||||
|
|
||||||
# Přičíst input
|
# Přičíst input
|
||||||
add_layout = QVBoxLayout()
|
add_layout = QVBoxLayout()
|
||||||
@@ -138,8 +139,15 @@ class ChannelCalibrationDialog(QDialog):
|
|||||||
# get the current channel object of the current (duplicated dataset)
|
# get the current channel object of the current (duplicated dataset)
|
||||||
channel = DetektorContainer().get().get_channel_by_uuid(channel_id)
|
channel = DetektorContainer().get().get_channel_by_uuid(channel_id)
|
||||||
|
|
||||||
offset = float(self.add_input.text())
|
try:
|
||||||
multiple = float(self.multiply_input.text())
|
# validate inputs
|
||||||
|
offset = float(self.add_input.text().replace(',', '.'))
|
||||||
|
multiple = float(self.multiply_input.text().replace(',', '.'))
|
||||||
|
except ValueError:
|
||||||
|
WrongDataDialog()
|
||||||
|
return
|
||||||
|
|
||||||
|
|
||||||
logging.debug(f'Calibrating channel {channel.name} +{offset} x{multiple}')
|
logging.debug(f'Calibrating channel {channel.name} +{offset} x{multiple}')
|
||||||
|
|
||||||
if self._only_region:
|
if self._only_region:
|
||||||
@@ -178,12 +186,37 @@ class MissingChannelDialog(QDialog):
|
|||||||
|
|
||||||
self.setWindowTitle("Vyberte kanál")
|
self.setWindowTitle("Vyberte kanál")
|
||||||
self.setModal(True) # Set the dialog as modal (blocks main window)
|
self.setModal(True) # Set the dialog as modal (blocks main window)
|
||||||
self.resize(300, 150)
|
self.resize(400, 75)
|
||||||
|
|
||||||
# Main layout
|
# Main layout
|
||||||
main_layout = QVBoxLayout()
|
main_layout = QVBoxLayout()
|
||||||
self.setLayout(main_layout)
|
self.setLayout(main_layout)
|
||||||
main_layout.addWidget(QLabel("Vyberte kanál, na kterém chcete provést kalibraci"))
|
label = QLabel("Vyberte kanál, na kterém chcete provést kalibraci")
|
||||||
|
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
main_layout.addWidget(label)
|
||||||
|
|
||||||
|
cancel_button = QPushButton("OK")
|
||||||
|
cancel_button.clicked.connect(self.close)
|
||||||
|
main_layout.addWidget(cancel_button)
|
||||||
|
|
||||||
|
# Show the dialog
|
||||||
|
self.exec()
|
||||||
|
|
||||||
|
|
||||||
|
class WrongDataDialog(QDialog):
|
||||||
|
def __init__(self):
|
||||||
|
super().__init__()
|
||||||
|
|
||||||
|
self.setWindowTitle("Špatná data")
|
||||||
|
self.setModal(True) # Set the dialog as modal (blocks main window)
|
||||||
|
self.resize(400, 75)
|
||||||
|
|
||||||
|
# Main layout
|
||||||
|
main_layout = QVBoxLayout()
|
||||||
|
self.setLayout(main_layout)
|
||||||
|
label = QLabel("Zadaná data nejsou platná čísla.")
|
||||||
|
label.setAlignment(Qt.AlignmentFlag.AlignCenter)
|
||||||
|
main_layout.addWidget(label)
|
||||||
|
|
||||||
cancel_button = QPushButton("OK")
|
cancel_button = QPushButton("OK")
|
||||||
cancel_button.clicked.connect(self.close)
|
cancel_button.clicked.connect(self.close)
|
||||||
@@ -193,6 +226,18 @@ class MissingChannelDialog(QDialog):
|
|||||||
self.exec()
|
self.exec()
|
||||||
|
|
||||||
class CustomDoubleValidator(QDoubleValidator):
|
class CustomDoubleValidator(QDoubleValidator):
|
||||||
|
|
||||||
def validate(self, input_str, pos):
|
def validate(self, input_str, pos):
|
||||||
input_str = input_str.replace(',', '.') # Replace ',' with '.'
|
# Accept both comma and dot as decimal separator for validation
|
||||||
return super().validate(input_str, pos)
|
if ',' in input_str:
|
||||||
|
# Allow comma as intermediate state so user can type it
|
||||||
|
test_str = input_str.replace(',', '.')
|
||||||
|
state, _, _ = super().validate(test_str, pos)
|
||||||
|
if state == QDoubleValidator.State.Acceptable:
|
||||||
|
return (QDoubleValidator.State.Intermediate, input_str, pos)
|
||||||
|
return (QDoubleValidator.State.Intermediate, input_str, pos)
|
||||||
|
return super().validate(input_str, pos)
|
||||||
|
|
||||||
|
def fixup(self, input_str):
|
||||||
|
# Actually replace comma with dot in the QLineEdit
|
||||||
|
return input_str.replace(',', '.')
|
||||||
Reference in New Issue
Block a user