From 4dd3df016d4c1f9ab30a3b96692185b7daf8964c Mon Sep 17 00:00:00 2001 From: Valentin Valls Date: Mon, 26 Jun 2023 19:18:10 +0200 Subject: [PATCH] Check FloatEdit with quantity --- src/silx/gui/widgets/FloatEdit.py | 23 ++++++++++++++++--- src/silx/gui/widgets/test/test_floatedit.py | 25 +++++++++++++++++++++ 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/silx/gui/widgets/FloatEdit.py b/src/silx/gui/widgets/FloatEdit.py index 217c4fb6d5..0cd23c4feb 100644 --- a/src/silx/gui/widgets/FloatEdit.py +++ b/src/silx/gui/widgets/FloatEdit.py @@ -35,19 +35,35 @@ class FloatEdit(qt.QLineEdit): """Field to edit a float value. + A value can be accessed with :meth:`value` and :meth:`setValue`. Will the + user do not modify the text, the original value is returned. + + The widget supports validators from :class:`validators.CustomValidator`, which + not only allow to manage float value. + :param parent: See :class:`QLineEdit` :param float value: The value to set the QLineEdit to. """ - def __init__(self, parent=None, value=None): + def __init__(self, parent: qt.QWidget=None, value: object=None): qt.QLineEdit.__init__(self, parent) validator = qt.QDoubleValidator(self) self.setValidator(validator) self.setAlignment(qt.Qt.AlignRight) + self.__value = None if value is not None: self.setValue(value) - def value(self): + def keyPressEvent(self, event: qt.QEvent): + result = super(FloatEdit, self).keyPressEvent(event) + if event.isAccepted(): + self.__wasModified = True + return result + + def value(self) -> object: """Return the QLineEdit current value as a float.""" + if not self.isModified(): + return self.__value + text = self.text() validator = self.validator() @@ -59,11 +75,12 @@ def value(self): self.setValue(value) return value - def setValue(self, value): + def setValue(self, value: object): """Set the current value of the LineEdit :param float value: The value to set the QLineEdit to. """ + self.__value = value validator = self.validator() if isinstance(validator, validators.CustomValidator): text = validator.toText(value) diff --git a/src/silx/gui/widgets/test/test_floatedit.py b/src/silx/gui/widgets/test/test_floatedit.py index eb3ed39768..ba16aabdfc 100644 --- a/src/silx/gui/widgets/test/test_floatedit.py +++ b/src/silx/gui/widgets/test/test_floatedit.py @@ -62,3 +62,28 @@ def test_none_value(floatEdit): assert floatEdit.value() is None floatEdit.setText("") assert floatEdit.value() is None + + +def test_original_value(floatEdit): + """ + Check that we can retrieve the original value while it was not edited by the user. + """ + floatEdit.setValue(0.123456789) + assert floatEdit.value() == 0.123456789 + floatEdit.setCursorPosition(2) + floatEdit.insert("1") + assert floatEdit.value() == pytest.approx(0.1123456) + + +def test_quantity_value(floatEdit): + """ + Check that the widget supports quantity validator. + """ + v = validators.DoublePintValidator() + floatEdit.setValidator(v) + + floatEdit.setValue((0.12, "mm")) + assert floatEdit.value() == (0.12, "mm") + floatEdit.setCursorPosition(3) + floatEdit.insert("1") + assert floatEdit.value() == (0.112, "mm")