import Trait, TraitFactory, TraitHandler from .quantity \ import Quantity #------------------------------------------------------------------------------- # 'AQuantity' trait factory: #------------------------------------------------------------------------------- def AQuantity(value=1.0, units='m', family='', **metadata): quantity = Quantity(value, units, family_name=family) return Trait(quantity, QuantityTraitHandler(quantity), **metadata) AQuantity = TraitFactory(AQuantity) #------------------------------------------------------------------------------- # 'QuantityTraitHandler' class: #------------------------------------------------------------------------------- class QuantityTraitHandler(TraitHandler): #--------------------------------------------------------------------------- # Initializes the object: #--------------------------------------------------------------------------- def __init__(self, quantity): self.quantity = quantity
# Define Enable/Kiva specific color traits: #------------------------------------------------------------------------------- def RGBAColorFunc(*args, **metadata): """ Returns a trait whose value must be a GUI toolkit-specific RGBA-based color. Description ----------- For wxPython, the returned trait accepts any of the following values: * A tuple of the form (*r*, *g*, *b*, *a*), in which *r*, *g*, *b*, and *a* represent red, green, blue, and alpha values, respectively, and are floats in the range from 0.0 to 1.0 * An integer whose hexadecimal form is 0x*AARRGGBB*, where *AA* is the alpha (transparency) value, *RR* is the red value, *GG* is the green value, and *BB* is the blue value Default Value ------------- For wxPython, (1.0, 1.0, 1.0, 1.0) (that is, opaque white) """ tmp_trait = Trait( 'white', convert_to_color, rgba_standard_colors, editor = RGBAColorEditor ) return tmp_trait(*args, **metadata) RGBAColorTrait = TraitFactory( RGBAColorFunc ) RGBAColor = RGBAColorTrait
def TraitsUIColor(*args, **metadata): """ Returns a trait whose value must be a GUI toolkit-specific color. This is copied from the deprecated trait that is in traits.api. It adds a deferred dependency on TraitsUI. This trait will be replaced by native Pyface color traits in Pyface 8.0. New code should not use this trait. """ from traitsui.toolkit_traits import ColorTrait return ColorTrait(*args, **metadata) TraitsUIColor = TraitFactory(TraitsUIColor) def TraitsUIFont(*args, **metadata): """ Returns a trait whose value must be a GUI toolkit-specific font. This is copied from the deprecated trait that is in traits.api. It adds a deferred dependency on TraitsUI. This trait will be replaced by native Pyface font traits in Pyface 8.0. New code should not use this trait. """ from traitsui.toolkit_traits import FontTrait return FontTrait(*args, **metadata)
from apptools.naming.api import Binding # ------------------------------------------------------------------------------- # 'NamingInstance' trait factory: # ------------------------------------------------------------------------------- def NamingInstance(klass=None, value="", allow_none=False, **metadata): metadata.setdefault("copy", "deep") return Trait( value, NamingTraitHandler(klass, or_none=allow_none, module=get_module_name()), **metadata) NamingInstance = TraitFactory(NamingInstance) # ------------------------------------------------------------------------------- # 'NamingTraitHandler' class: # ------------------------------------------------------------------------------- class NamingTraitHandler(TraitHandler): # --------------------------------------------------------------------------- # Initializes the object: # --------------------------------------------------------------------------- def __init__(self, aClass, or_none, module): """Initializes the object.""" self.or_none = or_none is not False
This trait accepts either a toolkit-specific font object, or a string containing a font description. The string description can contain: * a font name or family. The following generic names are understood: "default", "decorative", "roman", "script", "swiss", and "modern". * a size, in points. * a style, which is one of: "slant" or "italic" * a weight, which is one of: "light" or "bold" * whether the font is underlined, indicated by the inclusion of "underlined". Where values aren't supplied, the application defaults will be used instead. The default value is the application default font, which is toolkit and platform dependent. The default editor is FontEditor. Parameters ---------- **metadata Trait metadata to be passed through. """ return toolkit().font_trait(*args, **traits) Color = TraitFactory(ColorTrait) RGBColor = TraitFactory(RGBColorTrait) Font = TraitFactory(FontTrait)
# Standard library imports # Major library imports # Enthought library imports from scimath.units.api import UnitArray as UA from scimath.units.traits.ui.unit_editor \ import UnitEditor from traits.api import Instance, TraitFactory # Local Imports def UnitArrayTrait(value=None, units=None, **kwargs): args = () kw = {} if value is not None and units is not None: args = (value, ) kwargs['args'] = args kw = {'units': units} kwargs['kw'] = kw return Instance(UA, editor=UnitEditor, **kwargs) UnitArrayTrait = TraitFactory(UnitArrayTrait)
object.on_trait_change(check_values_items, values_name + '_items') _add_listeners = staticmethod(_add_listeners) #------------------------------------------------------------------------------- # Defines the DEnum property: #------------------------------------------------------------------------------- DEnum = Property(DEnumHelper.get_value, DEnumHelper.set_value, values_name='values', editor=(DEnumHelper.make_editor, { 'trait': None })) DEnum = TraitFactory(DEnum) ########################################################################## # `ShadowProperty` trait type. ########################################################################## class ShadowProperty(TraitType): # Not really necessary but specifies the attribute up front. trait_type = None # Call the notifiers smartly only when the value has really changed. # If this is set to False, the notification will always occur. smart_notify = True def __init__(self, trait_type, smart_notify=True, **metadata):
# Standard library imports # Major library imports # Enthought library imports from scimath.units.api import UnitScalar as US from scimath.units.traits.ui.unit_editor \ import UnitEditor from traits.api import Instance, TraitFactory # Local Imports def UnitScalarTrait(value=None, units=None, **kwargs): args = () kw = {} if value is not None and units is not None: args = (value, ) kwargs['args'] = args kw = {'units': units} kwargs['kw'] = kw return Instance(US, editor=UnitEditor, **kwargs) UnitScalarTrait = TraitFactory(UnitScalarTrait)
def unit_system_trait_factory_function(value=None, editor=None, **metadata): if value is None: value = unit_manager.get_default(), if editor is None: # Delay UI imports until here such that this library can be used without # a UI. from traitsui import EnumEditor editor = EnumEditor(values=dict( (str(us), us) for us in unit_manager.unit_systems), mode='list') return Trait(value, UnitSystem, editor=editor, **metadata) UnitSystemTrait = TraitFactory(unit_system_trait_factory_function) unit_system_trait = UnitSystemTrait class UnitsTraitHandler(TraitHandler, HasTraits): """ TraitHandler for units that validates unit string parsing and, optionally, compatibility with a family. """ # If True, None is allowed as a valid value. allow_none = Bool(True) # If True, attempts to assign values that cannot be parsed raise exceptions. # If False, values that cannot be parsed default to 'dimensionless'. is_strict = Bool(False)
td = object.trait(name).tool result.append( cls(dock_control=dock_control, prefix=td.prefix, image=td.image, tooltip=td.tooltip)) return result return None feature_for = classmethod(feature_for) #------------------------------------------------------------------------------- # Defines a 'Tool' trait for easily adding tools to a class definition: #------------------------------------------------------------------------------- def Tool(prefix='feature', image=ImageResource('tool_feature'), tooltip='Drag and drop this tool on a view', **metadata): return Str(tool=ToolDescription(prefix=prefix, image=image, tooltip=tooltip), **metadata) Tool = TraitFactory(Tool)
'dot': array([2.0, 2.0]), 'long dash': array([9.0, 5.0]) } __line_style_trait_map_keys = list(__line_style_trait_values.keys()) LineStyleEditor = EnumEditor(values=__line_style_trait_map_keys) def __line_style_trait(value='solid', **metadata): return Trait(value, __line_style_trait_values, editor=LineStyleEditor, **metadata) # A mapped trait for use in specification of line style attributes. LineStyle = TraitFactory(__line_style_trait) #------------------------------------------------------------------------------- # Trait definitions: #------------------------------------------------------------------------------- # Font trait: font_trait = KivaFont(default_font_name) # Bounds trait bounds_trait = CList([0.0, 0.0]) # (w,h) coordinate_trait = CList([0.0, 0.0]) # (x,y) #bounds_trait = Trait((0.0, 0.0, 20.0, 20.0), valid_bounds, editor=bounds_editor) # Component minimum size trait
is_strict=False, allow_none=True, units_trait='', editor=None, **metadata): if not allow_none and value is None: raise TraitError("value must not be None") if editor is None: # Delay UI imports until here such that this library can be used without # a UI. from traitsui.api import EnumEditor editor = EnumEditor( values=sorted(unit_manager.unit_families.keys()), mode='list') return Trait( value, FamilyNameTraitHandler( allow_none=allow_none, is_strict=is_strict, units_trait=units_trait), editor=editor, **metadata) # A Trait where the value is a units family name string. # See FamilyNameTraitHandler for details. FamilyNameTrait = TraitFactory(family_name_traits_factory_function) ### EOF
"swiss family Arial' or 'default 12')" ) fh = TraitKivaFont() if KivaFontEditor is not None: KivaFontTrait = Trait( fh.validate(None, None, "modern 12"), fh, editor=KivaFontEditor ) else: KivaFontTrait = Trait(fh.validate(None, None, "modern 12"), fh) def KivaFontFunc(*args, **metadata): """ Returns a trait whose value must be a GUI toolkit-specific font. Description: For wxPython, the returned trait accepts any of the following: * an kiva.fonttools.Font instance * a string describing the font, including one or more of the font family, size, weight, style, and typeface name. Default Value: For wxPython, 'Arial 10' """ return KivaFontTrait(*args, **metadata) KivaFont = TraitFactory(KivaFontFunc)
from os \ import access, R_OK from traits.api \ import TraitHandler, TraitFactory #------------------------------------------------------------------------------- # 'TextFile' trait: #------------------------------------------------------------------------------- def TextFile(default='', **metadata): return Trait(default, trait_text_file, **metedata) TextFile = TraitFactory(TextFile) class TraitTextFile(TraitHandler): is_mapped = True def validate(self, object, name, value): if access(value, R_OK): return value self.error(object, name, self.repr(value)) def mapped_value(self, value): fh = None try: fh = file(value, 'rb')