class BasePerson(HasTraits): married = Map({"yes": 1, "yeah": 1, "no": 0, "nah": 0, None: 2}) default_calls = Int(0) def _married_default(self): self.default_calls += 1 return None
class Preferences(HasTraits): """ Example class with a Map that records changes to that map. """ # Changes to primary trait of the mapped trait pair primary_changes = List() # Changes to the shadow trait of the mapped trait pair shadow_changes = List() color = Map({"red": 4, "green": 2, "yellow": 6}, default_value="yellow") @on_trait_change("color") def _record_primary_trait_change(self, obj, name, old, new): change = obj, name, old, new self.primary_changes.append(change) @on_trait_change("color_") def _record_shadow_trait_change(self, obj, name, old, new): change = obj, name, old, new self.shadow_changes.append(change)
"diamond", "dot", "pixel", ) #: Mapping of marker string names to classes. MarkerNameDict = { "square": SquareMarker, "circle": CircleMarker, "triangle": TriangleMarker, "inverted_triangle": Inverted_TriangleMarker, "left_triangle": LeftTriangleMarker, "right_triangle": RightTriangleMarker, "pentagon": PentagonMarker, "hexagon": Hexagon1Marker, "hexagon2": Hexagon2Marker, "plus": PlusMarker, "cross": CrossMarker, "star": StarMarker, "cross_plus": CrossPlusMarker, "diamond": DiamondMarker, "dot": DotMarker, "pixel": PixelMarker, "custom": CustomMarker, } #: A mapped trait that allows string naming of marker classes. MarkerTrait = Map(MarkerNameDict, default_value="square") marker_trait = MarkerTrait
Float, Int, Map, Property, observe, ) from traitsui.api import EnumEditor # Local, relative imports from .colors import ColorTrait from .component import Component from .markers import CustomMarker, MarkerNameDict, marker_names slider_marker_map = {'rect': None} slider_marker_map.update(MarkerNameDict) SliderMarkerTrait = Map(slider_marker_map, default_value='rect') class Slider(Component): """ A horizontal or vertical slider bar """ # ------------------------------------------------------------------------ # Model traits # ------------------------------------------------------------------------ min = Float() max = Float() value = Float()
class Person(HasTraits): married = Map({'yes': 1, 'no': 0}, default_value="yes") married_2 = Map([], default_value="yes") # E: arg-type
# Privates used for specification of line style trait. _line_style_trait_values = { "solid": None, "dot dash": array([3.0, 5.0, 9.0, 5.0]), "dash": array([6.0, 6.0]), "dot": array([2.0, 2.0]), "long dash": array([9.0, 5.0]), } # An editor preset for line styles. LineStyleEditor = EnumEditor(values=list(_line_style_trait_values)) # A mapped trait for use in specification of line style attributes. LineStyle = Map( _line_style_trait_values, default_value='solid', editor=LineStyleEditor, ) # ----------------------------------------------------------------------------- # 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) # Component minimum size trait
#: Font variants. Currently only small caps variants are exposed in Qt, and #: nothing in Wx. In the future this could include things like swashes, #: numeric variants, and so on, as exposed in the toolkit. VARIANTS = ['small-caps'] #: Additional markings on or around the glyphs of the font that are not part #: of the glyphs themselves. Currently Qt and Wx support underline and #: strikethrough, and Qt supports overline. In the future overlines and other #: decorations may be supported, as exposed in the toolkit. DECORATIONS = ['underline', 'strikethrough', 'overline'] #: A trait for font families. FontFamily = CList(Str, ['default']) #: A trait for font weights. FontWeight = Map(WEIGHTS, default_value='normal') #: A trait for font styles. FontStyle = Enum(STYLES) #: A trait for font variant properties. FontVariants = CSet(Enum(VARIANTS)) #: A trait for font decorator properties. FontDecorations = CSet(Enum(DECORATIONS)) class FontStretch(BaseCFloat): """ Trait type for font stretches. The is a CFloat trait which holds floating point values between 50 and 200,
class Person(HasTraits): married = Map(mapping)
class Person(HasTraits): married = Map({"yes": 1, "yeah": 1, "no": 0, "nah": 0})
class Person(HasTraits): married = Map({"yes": 1, "yeah": 1, "no": 0, "nah": 0}, default_value="yes")
class BasePerson(HasTraits): married = Map({"yes": 1, "yeah": 1, "no": 0, "nah": 0, None: 2}, default_value=None)
def test_empty_map(self): with self.assertRaises(ValueError): Map({})
from pyface.qt.QtGui import QLineEdit from .field import Field ECHO_TO_QT_ECHO_MODE = { "normal": QLineEdit.EchoMode.Normal, "password": QLineEdit.EchoMode.Password, "none": QLineEdit.EchoMode.NoEcho, "when_editing": QLineEdit.EchoMode.PasswordEchoOnEdit, } QT_ECHO_MODE_TO_ECHO = { value: key for key, value in ECHO_TO_QT_ECHO_MODE.items() } # mapped trait for Qt line edit echo modes Echo = Map(ECHO_TO_QT_ECHO_MODE, default_value="normal") @provides(ITextField) class TextField(MTextField, Field): """ The Qt-specific implementation of the text field class """ #: Display typed text, or one of several hidden "password" modes. echo = Echo # ------------------------------------------------------------------------ # IWidget interface # ------------------------------------------------------------------------ def _create_control(self, parent): """ Create the toolkit-specific control that represents the widget. """
class Polygon(Component): """ A filled polygon component. """ # ------------------------------------------------------------------------- # Trait definitions. # ------------------------------------------------------------------------- # The background color of this polygon. background_color = ColorTrait("white") # The color of the border of this polygon. border_color = ColorTrait("black") # The dash pattern to use for this polygon. border_dash = Any # The thickness of the border of this polygon. border_size = border_size_trait(1) # Event fired when the polygon is "complete". complete = Event # The rule to use to determine the inside of the polygon. inside_rule = Map( { "winding": FILL_STROKE, "oddeven": EOF_FILL_STROKE }, default_value="winding", ) # The points that make up this polygon. model = Instance(PolygonModel, ()) # Convenience property to access the model's points. points = Property # The color of each vertex. vertex_color = ColorTrait("black") # The size of each vertex. vertex_size = Float(3.0) traits_view = View( Group("<component>", id="component"), Group("<links>", id="links"), Group( "background_color", "_", "border_color", "_", "border_size", id="Box", style="custom", ), ) colorchip_map = {"color": "color", "alt_color": "border_color"} # ------------------------------------------------------------------------- # Traits property accessors # ------------------------------------------------------------------------- def _get_points(self): return self.model.points # ------------------------------------------------------------------------- # 'Polygon' interface # ------------------------------------------------------------------------- def reset(self): "Reset the polygon to the initial state" self.model.reset() self.event_state = "normal" # ------------------------------------------------------------------------- # 'Component' interface # ------------------------------------------------------------------------- def _draw_mainlayer(self, gc, view_bounds=None, mode="normal"): "Draw the component in the specified graphics context" self._draw_closed(gc) # ------------------------------------------------------------------------- # Protected interface # ------------------------------------------------------------------------- def _is_in(self, point): """ Test if the point (an x, y tuple) is within this polygonal region. To perform the test, we use the winding number inclusion algorithm, referenced in the comp.graphics.algorithms FAQ (http://www.faqs.org/faqs/graphics/algorithms-faq/) and described in detail here: http://softsurfer.com/Archive/algorithm_0103/algorithm_0103.htm """ point_array = array((point, )) vertices = array(self.model.points) winding = self.inside_rule == "winding" result = points_in_polygon(point_array, vertices, winding) return result[0] # ------------------------------------------------------------------------- # Private interface # ------------------------------------------------------------------------- def _draw_closed(self, gc): "Draw this polygon as a closed polygon" if len(self.model.points) > 2: # Set the drawing parameters. gc.set_fill_color(self.background_color_) gc.set_stroke_color(self.border_color_) gc.set_line_width(self.border_size) gc.set_line_dash(self.border_dash) # Draw the path. gc.begin_path() gc.move_to( self.model.points[0][0] - self.x, self.model.points[0][1] + self.y, ) offset_points = [(x - self.x, y + self.y) for x, y in self.model.points] gc.lines(offset_points) gc.close_path() gc.draw_path(self.inside_rule_) # Draw the vertices. self._draw_vertices(gc) def _draw_open(self, gc): "Draw this polygon as an open polygon" if len(self.model.points) > 2: # Set the drawing parameters. gc.set_fill_color(self.background_color_) gc.set_stroke_color(self.border_color_) gc.set_line_width(self.border_size) gc.set_line_dash(self.border_dash) # Draw the path. gc.begin_path() gc.move_to( self.model.points[0][0] - self.x, self.model.points[0][1] + self.y, ) offset_points = [(x - self.x, y + self.y) for x, y in self.model.points] gc.lines(offset_points) gc.draw_path(self.inside_rule_) # Draw the vertices. self._draw_vertices(gc) def _draw_vertices(self, gc): "Draw the vertices of the polygon." gc.set_fill_color(self.vertex_color_) gc.set_line_dash(None) offset = self.vertex_size / 2.0 offset_points = [(x + self.x, y + self.y) for x, y in self.model.points] if hasattr(gc, "draw_path_at_points"): path = gc.get_empty_path() path.rect(-offset, -offset, self.vertex_size, self.vertex_size) gc.draw_path_at_points(offset_points, path, FILL_STROKE) else: for x, y in offset_points: gc.draw_rect( ( x - offset, y - offset, self.vertex_size, self.vertex_size, ), FILL, )