forked from M-Labs/artiq
1
0
Fork 0

ScientificSpinBox: fix suffix/prefix

This commit is contained in:
Robert Jördens 2016-08-14 11:28:06 +02:00
parent 5f5975844a
commit 17582047cb
1 changed files with 13 additions and 3 deletions

View File

@ -54,14 +54,24 @@ class ScientificSpinBox(QtWidgets.QDoubleSpinBox):
return t return t
def valueFromText(self, text): def valueFromText(self, text):
return round(float(text), self.decimals()) clean = text
if self.prefix():
clean = clean.split(self.prefix(), 1)[-1]
if self.suffix():
clean = clean.rsplit(self.suffix(), 1)[0]
return round(float(clean), self.decimals())
def validate(self, text, pos): def validate(self, text, pos):
clean = text
if self.prefix():
clean = clean.split(self.prefix(), 1)[-1]
if self.suffix():
clean = clean.rsplit(self.suffix(), 1)[0]
try: try:
float(text) # faster than matching float(clean) # faster than matching
return QtGui.QValidator.Acceptable, text, pos return QtGui.QValidator.Acceptable, text, pos
except ValueError: except ValueError:
if re.fullmatch(_float_intermediate, text): if re.fullmatch(_float_intermediate, clean):
return QtGui.QValidator.Intermediate, text, pos return QtGui.QValidator.Intermediate, text, pos
return QtGui.QValidator.Invalid, text, pos return QtGui.QValidator.Invalid, text, pos