Skip to content

Commit

Permalink
Check FloatEdit with quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
vallsv committed Jul 20, 2023
1 parent 25f69a0 commit 4dd3df0
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
23 changes: 20 additions & 3 deletions src/silx/gui/widgets/FloatEdit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
Expand Down
25 changes: 25 additions & 0 deletions src/silx/gui/widgets/test/test_floatedit.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit 4dd3df0

Please sign in to comment.