Skip to content

Commit

Permalink
Add a really simple traitsui view for playing with Kiva.
Browse files Browse the repository at this point in the history
  • Loading branch information
corranwebster committed Oct 26, 2016
1 parent 168e222 commit 0d632cf
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions examples/kiva_explorer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
"""
Kiva Explorer
=============
Interactive editor for exploring Kiva drawing commands.
"""

from traits.api import Any, Code, DelegatesTo, Instance, Str
from traitsui.api import HSplit, ModelView, UItem, VGroup, View
from enable.api import Component, ComponentEditor


default_script = """# Write your code here.
# The graphics context is available as gc.
from math import pi
from kiva import constants
from kiva.fonttools import Font
with gc:
gc.set_fill_color((1.0, 1.0, 0.0, 1.0))
gc.arc(200, 200, 100, 0, 2*pi)
gc.fill_path()
with gc:
gc.set_font(Font('Times New Roman', size=24))
gc.translate_ctm(200, 200)
for i in range(0, 12):
gc.set_fill_color((i/12.0, 0.0, 1.0-(i/12.0), 0.75))
gc.rotate_ctm(2*pi/12.0)
gc.show_text_at_point("Hello World", 20, 0)
gc.set_stroke_color((0.0, 0.0, 1.0, 1.0))
gc.set_line_width(7)
gc.set_line_join(constants.JOIN_ROUND)
gc.set_line_cap(constants.CAP_ROUND)
gc.rect(100, 400, 50, 50)
gc.stroke_path()
"""


class ScriptedComponent(Component):

#: kiva drawing code for mainlayer
draw_script = Code(default_script)

#: any errors which occur
error = Str

#: compiled code
_draw_code = Any

def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
with gc:
try:
self.error = ''
exec self._draw_code in {}, {'gc': gc}
except Exception as exc:
self.error = str(exc)

def _compile_script(self):
try:
self.error = ''
return compile(self.draw_script, "<script>", "exec")
except SyntaxError as exc:
self.error = str(exc)
return None

def _draw_script_changed(self):
code = self._compile_script()
if code is not None:
self._draw_code = code
self.request_redraw()

def __draw_code_default(self):
code = self._compile_script()
if code is None:
code = compile("", "<script>", "exec")
return code


class ScriptedComponentView(ModelView):

model = Instance(ScriptedComponent, ())

script = DelegatesTo('model', 'draw_script')

error = DelegatesTo('model')

view = View(
HSplit(
VGroup(
UItem('script'),
UItem('error', visible_when="error != ''", style='readonly',
height=100)
),
UItem('model', editor=ComponentEditor()),
),
resizable=True
)

if __name__ == '__main__':
view = ScriptedComponentView()
view.configure_traits()

0 comments on commit 0d632cf

Please sign in to comment.