-
Notifications
You must be signed in to change notification settings - Fork 55
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
Font trait and parser #1042
Font trait and parser #1042
Changes from 6 commits
8a391a4
715bb70
12bf8e2
bd7f3dd
d1d2977
5cead25
6b81950
70520f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,8 +30,10 @@ | |
from traits.trait_base import get_resource_path | ||
|
||
from pyface.color import Color | ||
from pyface.font import Font | ||
from pyface.i_image import IImage | ||
from pyface.util.color_parser import ColorParseError | ||
from pyface.util.font_parser import simple_parser, FontParseError | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
@@ -144,7 +146,7 @@ def create_editor(self): | |
|
||
|
||
class PyfaceColor(TraitType): | ||
""" A Trait which casts strings and tuples to a PyfaceColor value. | ||
""" A Trait which casts strings and tuples to a Pyface Color value. | ||
""" | ||
|
||
#: The default value should be a tuple (factory, args, kwargs) | ||
|
@@ -186,6 +188,54 @@ def info(self): | |
) | ||
|
||
|
||
# ------------------------------------------------------------------------------- | ||
# Font | ||
# ------------------------------------------------------------------------------- | ||
|
||
|
||
class PyfaceFont(TraitType): | ||
""" A Trait which casts strings to a Pyface Font value. | ||
""" | ||
|
||
#: The default value should be a tuple (factory, args, kwargs) | ||
default_value_type = DefaultValue.callable_and_args | ||
|
||
#: The parser to use when converting text to keyword args. This should | ||
#: accept a string and return a dictionary of Font class trait values (ie. | ||
#: "family", "size", "weight", etc.). | ||
parser = None | ||
|
||
def __init__(self, value=None, *, parser=simple_parser, **metadata): | ||
if parser is not None: | ||
self.parser = parser | ||
if value is not None: | ||
font = self.validate(None, None, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks as though this means that a validation error would be turned into a >>> PyfaceFont(b"times new roman")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/mdickinson/Enthought/ETS/pyface/pyface/ui_traits.py", line 212, in __init__
font = self.validate(None, None, value)
File "/Users/mdickinson/Enthought/ETS/pyface/pyface/ui_traits.py", line 231, in validate
self.error(object, name, value)
File "/Users/mdickinson/.venvs/pyface/lib/python3.9/site-packages/traits/base_trait_handler.py", line 74, in error
raise TraitError(
traits.trait_errors.TraitError: None There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There might be something that's worth looking into in Traits about why the error message isn't good, since calling I'll convert this to a |
||
default_value = ( | ||
Font, | ||
(), | ||
font.trait_get(transient=lambda x: not x), | ||
) | ||
else: | ||
default_value = (Font, (), {}) | ||
super().__init__(default_value, **metadata) | ||
|
||
def validate(self, object, name, value): | ||
if isinstance(value, Font): | ||
return value | ||
if isinstance(value, str): | ||
try: | ||
return Font(**self.parser(value)) | ||
except FontParseError: | ||
self.error(object, name, value) | ||
|
||
self.error(object, name, value) | ||
|
||
def info(self): | ||
return ( | ||
"a Pyface Font, or a string describing a Pyface Font" | ||
) | ||
|
||
|
||
# ------------------------------------------------------------------------------- | ||
# Borders, Margins and Layout | ||
# ------------------------------------------------------------------------------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Need a different method name here.