Skip to content

Commit 4dd3df0

Browse files
committed
Check FloatEdit with quantity
1 parent 25f69a0 commit 4dd3df0

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

src/silx/gui/widgets/FloatEdit.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,35 @@
3535
class FloatEdit(qt.QLineEdit):
3636
"""Field to edit a float value.
3737
38+
A value can be accessed with :meth:`value` and :meth:`setValue`. Will the
39+
user do not modify the text, the original value is returned.
40+
41+
The widget supports validators from :class:`validators.CustomValidator`, which
42+
not only allow to manage float value.
43+
3844
:param parent: See :class:`QLineEdit`
3945
:param float value: The value to set the QLineEdit to.
4046
"""
41-
def __init__(self, parent=None, value=None):
47+
def __init__(self, parent: qt.QWidget=None, value: object=None):
4248
qt.QLineEdit.__init__(self, parent)
4349
validator = qt.QDoubleValidator(self)
4450
self.setValidator(validator)
4551
self.setAlignment(qt.Qt.AlignRight)
52+
self.__value = None
4653
if value is not None:
4754
self.setValue(value)
4855

49-
def value(self):
56+
def keyPressEvent(self, event: qt.QEvent):
57+
result = super(FloatEdit, self).keyPressEvent(event)
58+
if event.isAccepted():
59+
self.__wasModified = True
60+
return result
61+
62+
def value(self) -> object:
5063
"""Return the QLineEdit current value as a float."""
64+
if not self.isModified():
65+
return self.__value
66+
5167
text = self.text()
5268

5369
validator = self.validator()
@@ -59,11 +75,12 @@ def value(self):
5975
self.setValue(value)
6076
return value
6177

62-
def setValue(self, value):
78+
def setValue(self, value: object):
6379
"""Set the current value of the LineEdit
6480
6581
:param float value: The value to set the QLineEdit to.
6682
"""
83+
self.__value = value
6784
validator = self.validator()
6885
if isinstance(validator, validators.CustomValidator):
6986
text = validator.toText(value)

src/silx/gui/widgets/test/test_floatedit.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,28 @@ def test_none_value(floatEdit):
6262
assert floatEdit.value() is None
6363
floatEdit.setText("")
6464
assert floatEdit.value() is None
65+
66+
67+
def test_original_value(floatEdit):
68+
"""
69+
Check that we can retrieve the original value while it was not edited by the user.
70+
"""
71+
floatEdit.setValue(0.123456789)
72+
assert floatEdit.value() == 0.123456789
73+
floatEdit.setCursorPosition(2)
74+
floatEdit.insert("1")
75+
assert floatEdit.value() == pytest.approx(0.1123456)
76+
77+
78+
def test_quantity_value(floatEdit):
79+
"""
80+
Check that the widget supports quantity validator.
81+
"""
82+
v = validators.DoublePintValidator()
83+
floatEdit.setValidator(v)
84+
85+
floatEdit.setValue((0.12, "mm"))
86+
assert floatEdit.value() == (0.12, "mm")
87+
floatEdit.setCursorPosition(3)
88+
floatEdit.insert("1")
89+
assert floatEdit.value() == (0.112, "mm")

0 commit comments

Comments
 (0)