Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
46 changes: 46 additions & 0 deletions enable/examples/demo/enable/editors/font_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# (C) Copyright 2005-2022 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
from traits.api import HasStrictTraits
from traitsui.api import View, Item

from enable.api import Container, TextField, font_trait
from enable.trait_defs.ui.api import KivaFontEditor
from enable.examples._example_support import demo_main

from kiva.api import Font
from kiva.constants import ITALIC, SWISS, WEIGHT_BOLD


size = (500, 200)

sample_text = "Sphinx of black quartz, judge my vow."


class Demo(HasStrictTraits):
""" An example which shows the KivaFontEditor's variations. """

font = font_trait(Font("Times", 24, SWISS, WEIGHT_BOLD, ITALIC))

view = View(
Item('font', editor=KivaFontEditor(), style='simple', label="Simple"),
Item('font', editor=KivaFontEditor(), style='custom', label="Custom"),
Item('font', editor=KivaFontEditor(), style='text', label="Text"),
Item('font', editor=KivaFontEditor(), style='readonly', label="Readonly"),
Item('font', editor=KivaFontEditor(sample_text=sample_text), style='readonly', label="sample text"),
resizable=True,
width=size[0],
height=size[1],
)


if __name__ == "__main__":
# Save demo so that it doesn't get garbage collected when run within
# existing event loop (i.e. from ipython).
demo = demo_main(Demo, size=size)
31 changes: 8 additions & 23 deletions enable/label.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# Enthought library imports
from kiva.api import FILL, STROKE
from kiva.trait_defs.api import KivaFont
from traits.api import Bool, Enum, Float, HasTraits, Int, List, Str
from traits.api import Bool, Enum, Float, HasTraits, Int, List, Str, observe

# Local, relative imports
from .colors import black_color_trait, transparent_color_trait
Expand Down Expand Up @@ -92,11 +92,11 @@ def _calc_line_positions(self, gc):
for line in self.text.split("\n")[::-1]:
if line != "":
(
width,
height,
descent,
leading,
) = gc.get_full_text_extent(line)
width,
height,
) = gc.get_text_extent(line)
if width > max_width:
max_width = width
new_y_pos = (
Expand Down Expand Up @@ -191,25 +191,6 @@ def _draw_mainlayer(self, gc, view_bounds=None, mode="normal"):
with gc:
gc.translate_ctm(*self.position)

# Draw border and fill background
Comment thread
jwiggins marked this conversation as resolved.
width, height = self._bounding_box
if self.bgcolor != "transparent":
gc.set_fill_color(self.bgcolor_)
gc.draw_rect((0, 0, width, height), FILL)
if self.border_width > 0:
gc.set_stroke_color(self.border_color_)
gc.set_line_width(self.border_width)
border_offset = (self.border_width - 1) / 2.0
gc.draw_rect(
(
border_offset,
border_offset,
width - 2 * border_offset,
height - 2 * border_offset,
),
STROKE,
)

gc.set_fill_color(self.color_)
gc.set_stroke_color(self.color_)
gc.set_font(self.font)
Expand Down Expand Up @@ -254,3 +235,7 @@ def _text_changed(self):

def _rotate_angle_changed(self):
self._position_cache_valid = False

@observe('bounds.items')
def _update_bounds(self, event):
self._position_cache_valid = False
180 changes: 180 additions & 0 deletions enable/tests/trait_defs/test_kiva_font_editor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
# (C) Copyright 2005-2022 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" Test the interaction between traitsui and enable's ComponentEditor.
"""
import unittest

from traits.api import HasTraits
from traits.testing.api import UnittestTools
from traitsui.api import Item, View

from kiva.api import Font
from enable.enable_traits import font_trait
from enable.trait_defs.ui.kiva_font_editor import KivaFontEditor
from enable.tests._testing import skip_if_null


ITEM_WIDTH, ITEM_HEIGHT = 700, 200


class KivaFontView(HasTraits):
""" View containing an item with ComponentEditor. """

font = font_trait()

traits_view = View(
Item("font", editor=KivaFontEditor()),
resizable=True,
)


class TestKivaFontEditor(UnittestTools, unittest.TestCase):

@skip_if_null
def test_readonly_default_view(self):
obj = KivaFontView()

ui = obj.edit_traits(
view=View(
Item("font", editor=KivaFontEditor(), style='readonly'),
resizable=True,
)
)
try:
# check initial state
editor = ui.info.font
component = editor.component
self.assertIs(editor.value, obj.font)
self.assertIs(editor.font, obj.font)
self.assertIs(component.font, obj.font)
self.assertEqual(editor.str_value, "10 point")
self.assertEqual(component.text, "10 point")

# check a change
new_font = Font(
face_name="Helvetica",
size=24,
weight=700,
style=2,
)

obj.font = new_font

self.assertIs(editor.value, new_font)
self.assertIs(component.font, new_font)
self.assertEqual(editor.str_value, "24 point Helvetica Bold Italic")
self.assertEqual(component.text, "24 point Helvetica Bold Italic")
finally:
ui.dispose()

@skip_if_null
def test_simple_default_view(self):
obj = KivaFontView()

ui = obj.edit_traits(
view=View(
Item("font", editor=KivaFontEditor()),
resizable=True,
)
)
try:
editor = ui.info.font
component = editor.component
self.assertIs(editor.value, obj.font)
self.assertIs(editor.font, obj.font)
self.assertIs(component.font, obj.font)
self.assertEqual(editor.str_value, "10 point")
self.assertEqual(component.text, "10 point")
finally:
ui.dispose()

@skip_if_null
def test_simple_default_object_change(self):
obj = KivaFontView()

ui = obj.edit_traits(
view=View(
Item("font", editor=KivaFontEditor()),
resizable=True,
)
)
try:
editor = ui.info.font
component = editor.component

new_font = Font(
face_name="Helvetica",
size=24,
weight=700,
style=2,
)
obj.font = new_font

self.assertIs(editor.value, new_font)
self.assertIs(editor.font, new_font)
self.assertIs(component.font, new_font)
self.assertEqual(editor.str_value, "24 point Helvetica Bold Italic")
self.assertEqual(component.text, "24 point Helvetica Bold Italic")
finally:
ui.dispose()

@skip_if_null
def test_simple_default_editor_change(self):
obj = KivaFontView()

ui = obj.edit_traits(
view=View(
Item("font", editor=KivaFontEditor()),
resizable=True,
)
)
try:
editor = ui.info.font
component = editor.component

new_font = Font(
face_name="Helvetica",
size=24,
weight=700,
style=2,
)
editor.update_object(new_font)

self.assertIs(obj.font, new_font)
self.assertIs(editor.value, new_font)
self.assertIs(editor.font, new_font)
self.assertIs(component.font, new_font)
self.assertEqual(editor.str_value, "24 point Helvetica Bold Italic")
self.assertEqual(component.text, "24 point Helvetica Bold Italic")
finally:
ui.dispose()

@skip_if_null
def test_sample_text(self):
# this is a smoke test
obj = KivaFontView()

ui = obj.edit_traits(
view=View(
Item(
"font",
editor=KivaFontEditor(sample_text="sample text"),
style='readonly',
),
resizable=True,
)
)
try:
editor = ui.info.font
component = editor.component
self.assertEqual(editor.str_value, "sample text")
self.assertEqual(component.text, "sample text")
finally:
ui.dispose()
2 changes: 2 additions & 0 deletions enable/trait_defs/ui/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

from .kiva_font_editor import KivaFontEditor
from .rgba_color_editor import RGBAColorEditor
Loading