1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
#------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
#------------------------------------------------------------------------------
""" Enaml Standard Library - Fields
This is a library of Enaml components deriving from the builtin Field.
"""
from enaml.validator import RegexValidator, IntValidator, FloatValidator
from enaml.widgets.field import Field
enamldef RegexField(Field):
""" A Field that accepts text validated by a regular expression.
"""
attr regex : str
validator << RegexValidator(regex=regex)
_int_converters = {
2: bin,
8: oct,
10: str,
16: hex
}
enamldef IntField(Field):
""" A field that only accept integer inputs.
"""
attr minimum = None
attr maximum = None
attr base = 10
attr value : int = 0
attr converter << _int_converters.get(base, str)
text << converter(value)
text :: self.value = int(text, base)
validator << IntValidator(base=base, minimum=minimum, maximum=maximum)
enamldef FloatField(Field):
""" A Field that only accept floating point values.
"""
attr minimum = None
attr maximum = None
attr allow_exponent : bool = True
attr value : float = 0.0
attr converter = str
text << converter(value)
text :: self.value = float(text)
validator << FloatValidator(
minimum=minimum, maximum=maximum, allow_exponent=allow_exponent
)
|