"""
Ensures values entered by the user are a multiple of 0.25 and within
the range -/+20.00. The displayed text is also signed -/+, and can
be entered with or without -/+.Unsigned user input is assumed to be
positive.
"""
def __init__(self, min=-20.00, max=20.00, step=0.25, decimals=2, parent=None):
super(PowerSpinBox, self).__init__(parent)
self.setRange(min,max)
self.setSingleStep(step)
self.setDecimals(decimals)
def textFromValue(self, value):
#TODO: Should zero be unsigned?
if value >= 0:
return "+%.2f" % value
else:
return "%.2f" % value
def fixup(self, input):
value = input.toDouble()[0]
if (value % 0.25) > 0:
if value < 0:
value=value*-1
minus=True
else:
minus=False
newValue = float((int(value*100)/25)*25)/100
if minus: newValue=newValue*-1
newInput = "%.2f" % newValue
if not minus: newInput = "+"+newInput
print"Invalid value [%s] replaced with [%s]" % (input, newInput)
input.replace(0, input.length(), newInput)
def validate(self, input, pos ):
newChar = input.at(pos-1)
if pos < 1:
else:
if not newChar.isDigit():
if ((newChar.cell() == '.') and (input.count('.') == 1)) or \
((newChar.cell() == '-') and (pos == 1)) or \
((newChar.cell() == '+') and (pos == 1)):
else:
else:
value = input.toDouble()[0]
if (not (value >= self.minimum() and value <= self.maximum())) or \
(input.length() > input.indexOf('.')+self.decimals()+1):
elif (value % 0.25) > 0:
else:
return (valid, pos)
class PowerSpinBox(QDoubleSpinBox):
"""
Ensures values entered by the user are a multiple of 0.25 and within
the range -/+20.00. The displayed text is also signed -/+, and can
be entered with or without -/+.Unsigned user input is assumed to be
positive.
"""
def __init__(self, min=-20.00, max=20.00, step=0.25, decimals=2, parent=None):
super(PowerSpinBox, self).__init__(parent)
self.setRange(min,max)
self.setSingleStep(step)
self.setDecimals(decimals)
def textFromValue(self, value):
#TODO: Should zero be unsigned?
if value >= 0:
return "+%.2f" % value
else:
return "%.2f" % value
def fixup(self, input):
value = input.toDouble()[0]
if (value % 0.25) > 0:
if value < 0:
value=value*-1
minus=True
else:
minus=False
newValue = float((int(value*100)/25)*25)/100
if minus: newValue=newValue*-1
newInput = "%.2f" % newValue
if not minus: newInput = "+"+newInput
print"Invalid value [%s] replaced with [%s]" % (input, newInput)
QApplication.beep()
input.replace(0, input.length(), newInput)
def validate(self, input, pos ):
valid = QValidator.Invalid
newChar = input.at(pos-1)
if pos < 1:
valid = QValidator.Acceptable
else:
if not newChar.isDigit():
if ((newChar.cell() == '.') and (input.count('.') == 1)) or \
((newChar.cell() == '-') and (pos == 1)) or \
((newChar.cell() == '+') and (pos == 1)):
valid = QValidator.Acceptable
else:
valid = QValidator.Invalid
else:
value = input.toDouble()[0]
if (not (value >= self.minimum() and value <= self.maximum())) or \
(input.length() > input.indexOf('.')+self.decimals()+1):
valid = QValidator.Invalid
elif (value % 0.25) > 0:
valid = QValidator.Intermediate
else:
valid = QValidator.Acceptable
return (valid, pos)
To copy to clipboard, switch view to plain text mode
Bookmarks