class FloatOrEmptyValidator(QValidator): def __init__(self, parent): super().__init__(parent) self.dv = QDoubleValidator(parent) self.dv.setLocale(QLocale.c()) def validate(self, s, pos): if len(s) == 0: return (QValidator.Acceptable, s, pos) if "," in s: return (QValidator.Invalid, s, pos) else: return self.dv.validate(s, pos)
class FloatOrEmptyValidator(QValidator): def __init__(self, parent, allow_empty=False, bottom=float("-inf"), top=float("inf"), default_text=""): super().__init__(parent) self.dv = QDoubleValidator(parent) self.allow_empty = allow_empty self.default_text = default_text self.dv.setLocale(QLocale.c()) self.setBottom(bottom) self.setTop(top) def setDefault(self, s): self.default_text = s def setBottom(self, b): self.dv.setBottom(b) def setTop(self, t): self.dv.setTop(t) def fixup(self, p_str): # = only called at editingFinished so an Orange controlled value can still contain # invalid, because they are synchronized and every change # = called before returnPressedHandler try: f = float(p_str) if f > self.dv.top(): return str(self.dv.top()) if f < self.dv.bottom(): return str(self.dv.bottom()) except ValueError: return self.default_text def validate(self, s, pos): if self.allow_empty and len(s) == 0: return (QValidator.Acceptable, s, pos) if "," in s: return (QValidator.Invalid, s, pos) else: return self.dv.validate(s, pos)
class FloatOrEmptyValidator(QValidator): def __init__(self, parent, allow_empty=False, bottom=float("-inf"), top=float("inf"), default_text=""): super().__init__(parent) self.dv = QDoubleValidator(parent) self.allow_empty = allow_empty self.default_text = default_text self.dv.setLocale(QLocale.c()) self.setBottom(bottom) self.setTop(top) def setDefault(self, s): self.default_text = s def setBottom(self, b): self.dv.setBottom(b) def setTop(self, t): self.dv.setTop(t) def fixup(self, p_str): # = only called at editingFinished so an Orange controlled value can still contain # invalid, because they are synchronized and every change # = called before returnPressedHandler try: f = float(p_str) if f > self.dv.top(): return str(self.dv.top()) if f < self.dv.bottom(): return str(self.dv.bottom()) except ValueError: return self.default_text def validate(self, s, pos): if self.allow_empty and len(s) == 0: return (QValidator.Acceptable, s, pos) if "," in s: return (QValidator.Invalid, s, pos) else: return self.dv.validate(s, pos)