Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove usage of Trait() #925

Merged
merged 6 commits into from
Apr 6, 2022
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions enable/abstract_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# Enthought library imports
from traits.api import (
Any, Bool, Event, Float, HasTraits, Instance, List, Property, Trait, Tuple,
Any, Bool, Event, Float, HasTraits, Instance, List, Property, Tuple, Union,
)


Expand Down Expand Up @@ -54,7 +54,7 @@ class AbstractWindow(HasTraits):

# When a component captures the mouse, it can optionally store a
# dispatch order for events (until it releases the mouse).
mouse_owner_dispatch_history = Trait(None, None, List, transient=True)
mouse_owner_dispatch_history = Union(None, List, transient=True)

# A scaling constant applied to any GraphicsContext used for drawing the
# window's component.
Expand Down Expand Up @@ -89,7 +89,7 @@ class AbstractWindow(HasTraits):
_prev_event_handler = Instance(Component, transient=True)

# (dx, dy) integer size of the Window.
_size = Trait(None, Tuple, transient=True)
_size = Union(None, Tuple, transient=True)

# The regions to update upon redraw
_update_region = Any(transient=True)
Expand Down
4 changes: 2 additions & 2 deletions enable/canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
""" Defines the enable Canvas class """

# Enthought library imports
from traits.api import Bool, List, Trait, Tuple
from traits.api import Bool, List, Tuple, Union
from kiva.api import FILL


Expand Down Expand Up @@ -39,7 +39,7 @@ class Canvas(Container):
# of the "region of interest" that it should use when computing its
# notional bounds for clipping and event handling purposes. If this trait
# is None, then the canvas really does behave as if it has no bounds.
view_bounds = Trait(None, None, Tuple)
view_bounds = Union(None, Tuple)

# The (x,y) position of the lower-left corner of the rectangle
# corresponding to the dimensions in self.bounds. Unlike self.position,
Expand Down
6 changes: 3 additions & 3 deletions enable/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# Enthought library imports
from traits.api import (
Any, Bool, Delegate, Enum, Float, Instance, Int, List, Property, Str, Trait
Any, Bool, Delegate, Enum, Float, Instance, Int, List, Property, Str, Union
)
from kiva.api import FILL, STROKE

Expand Down Expand Up @@ -93,7 +93,7 @@ class Component(CoordinateBox, Interactor):
# The ratio of the component's width to its height. This is used by
# the component itself to maintain bounds when the bounds are changed
# independently, and is also used by the layout system.
aspect_ratio = Trait(None, None, Float)
aspect_ratio = Union(None, Float)

# When the component's bounds are set to a (width,height) tuple that does
# not conform to the set aspect ratio, does the component center itself
Expand All @@ -113,7 +113,7 @@ class Component(CoordinateBox, Interactor):
# component specifies, say, a fixed preferred width of 50 and another one
# specifies a fixed preferred width of 100, then the latter component will
# always be twice as wide as the former.
fixed_preferred_size = Trait(None, None, bounds_trait)
fixed_preferred_size = Union(None, bounds_trait)

# ------------------------------------------------------------------------
# Overlays and underlays
Expand Down
6 changes: 3 additions & 3 deletions enable/drawing/drag_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Enthought library imports
from enable.primitives.api import Box
from enable.enable_traits import Pointer
from traits.api import Event, Float, Trait, Tuple
from traits.api import Event, Float, Union, Tuple
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit 🔤


# Application specific imports.

Expand All @@ -43,8 +43,8 @@ class DragBox(Box):
complete = Event

# Constraints on size:
x_bounds = Trait(None, None, Tuple(Float, Float))
y_bounds = Trait(None, None, Tuple(Float, Float))
x_bounds = Union(None, Tuple(Float, Float))
y_bounds = Union(None, Tuple(Float, Float))

# Pointers. ####

Expand Down
70 changes: 54 additions & 16 deletions enable/enable_traits.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

# Enthought library imports
from kiva.trait_defs.api import KivaFont
from traits.api import List, PrefixList, PrefixMap, Range, Trait, TraitFactory
from traits.api import (
BaseFloat, List, Map, PrefixList, PrefixMap, Range, TraitType, Union,
)
from traitsui.api import ImageEnumEditor, EnumEditor

# Try to get the CList trait; for traits 2 backwards compatibility, fall back
Expand Down Expand Up @@ -100,26 +102,23 @@
# -----------------------------------------------------------------------------

# Privates used for specification of line style trait.
__line_style_trait_values = {
_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]),
}
__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
)

# 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 = TraitFactory(__line_style_trait)

LineStyle = Map(
_line_style_trait_values,
default_value='solid',
editor=LineStyleEditor,
)

# -----------------------------------------------------------------------------
# Trait definitions:
Expand All @@ -132,9 +131,6 @@ def __line_style_trait(value="solid", **metadata):
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
# PZW: Make these just floats, or maybe remove them altogether.
ComponentMinSize = Range(0.0, 99999.0)
Expand All @@ -152,8 +148,50 @@ def __line_style_trait(value="solid", **metadata):
border_size_trait = Range(0, 8, editor=border_size_editor)

# Time interval trait:
TimeInterval = Trait(None, None, Range(0.0, 3600.0))
TimeInterval = Union(None, Range(0.0, 3600.0))

# Stretch traits:
Stretch = Range(0.0, 1.0, value=1.0)
NoStretch = Stretch(0.0)

# Scrollbar traits
class ScrollBarRange(TraitType):
""" Trait that holds a (low, high, page_size, line_size) range tuple.
"""

def validate(self, object, name, value):
if isinstance(value, (tuple, list)) and (len(value) == 4):
low, high, page_size, line_size = value
if high < low:
low, high = high, low
elif high == low:
high = low + 1.0
page_size = max(min(page_size, high - low), 0.0)
line_size = max(min(line_size, page_size), 0.0)
return (
float(low),
float(high),
float(page_size),
float(line_size),
)
self.error(object, name, value)

def info(self):
return "a (low,high,page_size,line_size) range tuple"


class ScrollPosition(BaseFloat):
"""A Trait that ensures the position is within the scroll range.
"""

#: the name of the trait holding the range information.
range_name = "range"

def validate(self, object, name, value):
value = super().validate(object, name, value)
try:
low, high, page_size, line_size = getattr(object, self.range_name)
x = max(min(float(value), high - page_size), low)
return x
except Exception:
self.error(object, name, value)
4 changes: 2 additions & 2 deletions enable/interactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

# Enthought library imports
from kiva.api import affine_identity
from traits.api import Any, Bool, HasTraits, List, Property, Str, Trait
from traits.api import Any, Bool, HasTraits, List, Property, Str, Union

# Local relative imports
from enable.colors import ColorTrait
Expand Down Expand Up @@ -62,7 +62,7 @@ class Interactor(HasTraits):
pointer = Pointer

# The "tooltip" to display if a user mouse-overs this interactor
tooltip = Trait(None, None, Str)
tooltip = Union(None, Str)

# The cursor "style" to use
cursor_style = cursor_style_trait
Expand Down
6 changes: 2 additions & 4 deletions enable/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from numpy import array, pi

# Enthought library imports
from traits.api import HasTraits, Bool, Instance, Trait
from traits.api import HasTraits, Bool, Instance, Map
from traitsui.api import EnumEditor
from kiva.api import (
CIRCLE_MARKER, CROSS_MARKER, DIAMOND_MARKER, DOT_MARKER, FILL_STROKE,
Expand Down Expand Up @@ -470,8 +470,6 @@ def get_compiled_path(self, size):
}

#: A mapped trait that allows string naming of marker classes.
MarkerTrait = Trait(
"square", MarkerNameDict, editor=EnumEditor(values=marker_names)
)
MarkerTrait = Map(MarkerNameDict, default_value="square")

marker_trait = MarkerTrait
4 changes: 2 additions & 2 deletions enable/primitives/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# Enthought library imports.
from kiva.api import FILL, FILL_STROKE, STROKE
from traits.api import Any, Event, Float, List, Trait, Bool
from traits.api import Any, Event, Float, List, Bool

# Local imports.
from enable.api import border_size_trait, Component
Expand All @@ -36,7 +36,7 @@ class Line(Component):
line_dash = Any

# The width of the line.
line_width = Trait(1, border_size_trait)
line_width = border_size_trait(1)

# The points that make up this polygon.
points = List # List of Tuples
Expand Down
9 changes: 5 additions & 4 deletions enable/primitives/polygon.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Enthought library imports.
from kiva.api import EOF_FILL_STROKE, FILL, FILL_STROKE, points_in_polygon
from traits.api import (
Any, Event, Float, HasTraits, Instance, List, Property, Trait, Tuple
Any, Event, Float, HasTraits, Instance, List, Map, Property, Tuple
)
from traitsui.api import Group, View

Expand Down Expand Up @@ -42,14 +42,15 @@ class Polygon(Component):
border_dash = Any

# The thickness of the border of this polygon.
border_size = Trait(1, border_size_trait)
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 = Trait(
"winding", {"winding": FILL_STROKE, "oddeven": EOF_FILL_STROKE}
inside_rule = Map(
{"winding": FILL_STROKE, "oddeven": EOF_FILL_STROKE},
default_value="winding",
)

# The points that make up this polygon.
Expand Down
49 changes: 6 additions & 43 deletions enable/qt4/scrollbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,47 +13,10 @@
"""

from pyface.qt import QtCore, QtGui
from traits.api import Any, Bool, Enum, Float, Int, Property, Trait, TraitError
from traits.api import Any, Bool, Enum, Float, Int, Property

from enable.component import Component


def valid_range(object, name, value):
""" Verify that a set of range values for a scrollbar is valid.
"""
try:
if (type(value) in (tuple, list)) and (len(value) == 4):
low, high, page_size, line_size = value
if high < low:
low, high = high, low
elif high == low:
high = low + 1.0
page_size = max(min(page_size, high - low), 0.0)
line_size = max(min(line_size, page_size), 0.0)
return (
float(low),
float(high),
float(page_size),
float(line_size),
)
except Exception:
raise
raise TraitError


valid_range.info = "a (low,high,page_size,line_size) range tuple"


def valid_scroll_position(object, name, value):
""" Verify that a specified scroll bar position is valid.
"""
try:
low, high, page_size, line_size = object.range
x = max(min(float(value), high - page_size), low)
return x
except Exception:
raise
raise TraitError
from enable.enable_traits import ScrollBarRange, ScrollPosition


class QResizableScrollBar(QtGui.QScrollBar):
Expand All @@ -74,17 +37,17 @@ class NativeScrollBar(Component):

# The current position of the scroll bar. This must be within the range
# (self.low, self.high)
scroll_position = Trait(0.0, valid_scroll_position)
scroll_position = ScrollPosition()

# A tuple (low, high, page_size, line_size). Can be accessed using
# convenience properties (see below).
range = Trait((0.0, 100.0, 10.0, 1.0), valid_range)
range = ScrollBarRange((0.0, 100.0, 10.0, 1.0))

# The orientation of the scrollbar
orientation = Trait("horizontal", "vertical")
orientation = Enum("horizontal", "vertical")

# Is y=0 at the top or bottom?
origin = Trait("bottom", "top")
origin = Enum("bottom", "top")

# Determines if the scroll bar should be visible and respond to events
enabled = Bool(True)
Expand Down
12 changes: 4 additions & 8 deletions enable/slider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# Enthought library imports
from kiva.api import STROKE
from traits.api import (
Any, Bool, Enum, Float, Int, Property, Trait, observe,
Any, Bool, Enum, Float, Int, Map, Property, observe,
)
from traitsui.api import EnumEditor

Expand All @@ -21,13 +21,9 @@
from .component import Component
from .markers import CustomMarker, MarkerNameDict, marker_names

slider_marker_names = list(marker_names) + ["rect"]
SliderMarkerTrait = Trait(
"rect",
"rect",
MarkerNameDict,
editor=EnumEditor(values=slider_marker_names),
)
slider_marker_map = {'rect': None}
slider_marker_map.update(MarkerNameDict)
SliderMarkerTrait = Map(slider_marker_map, default_value='rect')


class Slider(Component):
Expand Down
4 changes: 2 additions & 2 deletions enable/text_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

# Enthought library imports
from traits.api import (
Any, Array, Bool, Int, List, Property, Trait, Tuple, observe,
Any, Array, Bool, Int, List, Property, Tuple, observe,
)
from kiva.trait_defs.api import KivaFont

Expand Down Expand Up @@ -87,7 +87,7 @@ class TextGrid(Component):
_cached_cell_coords = Array

# "auto" or a tuple
_cell_size = Trait("auto", Any)
_cell_size = Any("auto")

# ------------------------------------------------------------------------
# Public methods
Expand Down
Loading