Skip to content

Commit

Permalink
Merge pull request #94 from enthought/fix-examples
Browse files Browse the repository at this point in the history
Fix examples on Qt and interactive session
  • Loading branch information
rkern committed Mar 7, 2013
2 parents 6a190d6 + 3332268 commit b0e4464
Show file tree
Hide file tree
Showing 25 changed files with 252 additions and 314 deletions.
14 changes: 8 additions & 6 deletions examples/enable/basic_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
This demonstrates the most basic drawing capabilities using Enable. A new
component is created and added to a container.
"""
from __future__ import with_statement

from enable.example_support import DemoFrame, demo_main
from enable.api import Component, Container, Window


class Box(Component):

resizable = ""
Expand All @@ -19,14 +18,17 @@ def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):
gc.rect(x, y, dx, dy)
gc.fill_path()


class MyFrame(DemoFrame):

def _create_window(self):
box = Box(bounds=[100.0, 100.0], position=[50.0, 50.0])
container = Container(bounds=[500,500])
container = Container(bounds=[500, 500])
container.add(box)
return Window(self, -1, component=container)

if __name__ == "__main__":
demo_main(MyFrame)

# EOF
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(MyFrame)
19 changes: 10 additions & 9 deletions examples/enable/basic_move.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
"""
This allows a simple component to be moved around the screen.
"""

from __future__ import with_statement

from enable.example_support import DemoFrame, demo_main

from traits.api import Float, Tuple
from enable.api import Component, Container, Pointer, Window
from traits.api import Float
from enable.api import Component, Pointer, Window


class Box(Component):
"""
Expand Down Expand Up @@ -37,7 +35,8 @@ def _draw_mainlayer(self, gc, view_bounds=None, mode="default"):

# draw line around outer box
gc.set_stroke_color((0,0,0,1))
gc.rect(self.outer_x, self.outer_y, self.outer_width, self.outer_height)
gc.rect(self.outer_x, self.outer_y, self.outer_width,
self.outer_height)
gc.stroke_path()

return
Expand Down Expand Up @@ -73,13 +72,15 @@ def moving_mouse_leave(self, event):
event.handled = True
return


class MyFrame(DemoFrame):

def _create_window(self):
box = Box(bounds=[100,100], position=[50,50], padding=15)
return Window(self, -1, component=box)

if __name__ == "__main__":
demo_main(MyFrame, title="Click and drag to move the box")

# EOF
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(MyFrame, title="Click and drag to move the box")
15 changes: 8 additions & 7 deletions examples/enable/canvas_demo.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@


from enable.api import Canvas, Viewport, Window
from enable.tools.api import ViewportPanTool
from enable.primitives.api import Box
from enable.example_support import demo_main, DemoFrame


class MyFrame(DemoFrame):

def _create_window(self):

canvas = Canvas(bgcolor="lightsteelblue", draw_axes=True)
from basic_move import Box
box = Box(color="red", bounds=[50,50], resizable="")
box.position= [75,75]
box = Box(color="red", bounds=[50, 50], resizable="")
box.position= [75, 75]
canvas.add(box)


viewport = Viewport(component=canvas)
viewport.view_position = [0,0]
viewport.view_position = [0, 0]
viewport.tools.append(ViewportPanTool(viewport))

return Window(self, -1, component=viewport)


if __name__ == "__main__":
demo_main(MyFrame, title="Canvas example")
# Save demo so that it doesn't get garbage collected when run within
# existing event loop (i.e. from ipython).
demo = demo_main(MyFrame, title="Canvas example")
9 changes: 5 additions & 4 deletions examples/enable/compass_example.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from enable.api import OverlayContainer, Compass, Window
from enable.example_support import demo_main, DemoFrame


class MyFrame(DemoFrame):

def _create_window(self):
Expand All @@ -17,7 +17,8 @@ def _create_window(self):
def _arrow_printer(self):
print "Clicked:", self.compass.clicked

if __name__ == "__main__":
demo_main(MyFrame, title="Slider example")


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(MyFrame, title="Slider example")
11 changes: 7 additions & 4 deletions examples/enable/component_demo.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
from __future__ import with_statement

"""
Basic demo of drawing within an Enable component.
"""
from enable.api import Component, ComponentEditor
from traits.api import HasTraits, Instance
from traitsui.api import Item, View


class MyComponent(Component):
def draw(self, gc, **kwargs):
w,h = gc.width(), gc.height()
gc.clear()

# Draw a rounded rect just inside the bounds
gc.set_line_width(2.0)
gc.set_stroke_color((0.0, 0.0, 0.0, 1.0))
Expand All @@ -35,10 +37,11 @@ def draw(self, gc, **kwargs):
def normal_key_pressed(self, event):
print "key pressed: ", event.character


class Demo(HasTraits):
canvas = Instance(Component)

traits_view = View(Item('canvas', editor=ComponentEditor(bgcolor="lightgray"),
traits_view = View(Item('canvas', editor=ComponentEditor(),
show_label=False, width=200, height=200),
resizable=True, title="Component Example")

Expand Down
91 changes: 34 additions & 57 deletions examples/enable/container_demo.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
from __future__ import with_statement

from traits.etsconfig.api import ETSConfig
ETSConfig.toolkit = 'wx'
from traits.api import Enum, Float, Int, Str, Tuple

import wx
from enable.api import ColorTrait, Window
from chaco.api import *
from chaco.tools.api import DragTool
from kiva.fonttools import Font
from kiva.trait_defs.kiva_font_trait import KivaFont
from traits.api import Enum, Float, Int, Str, Tuple
from enable.api import ColorTrait, Window
from enable.tools.api import DragTool
from enable.example_support import DemoFrame, demo_main

from chaco.api import PlotComponent, AbstractOverlay, OverlayPlotContainer


class Region(PlotComponent, DragTool):

Expand All @@ -24,7 +21,7 @@ def __init__(self, color=None, **kw):
if color:
self.color = color
if not kw.has_key("bounds"):
self.bounds = [100,100]
self.bounds = [100, 100]

def _draw_plot(self, gc, view_bounds=None, mode="normal"):
with gc:
Expand Down Expand Up @@ -58,11 +55,11 @@ def overlay(self, component, gc, view_bounds=None, mode="normal"):
with gc:
gc.set_font(self.font)
twidth, theight = gc.get_text_extent(self.text)[2:]
tx = component.x + (component.width - twidth)/2.0
ty = component.y + (component.height - theight)/2.0
tx = component.x + (component.width - twidth) / 2.0
ty = component.y + (component.height - theight) / 2.0

# Draw a small, light rectangle representing this overlay
gc.set_fill_color((1.0,1.0,1.0,self.alpha))
gc.set_fill_color((1.0, 1.0, 1.0, self.alpha))
gc.rect(tx-self.margin, ty-self.margin,
twidth+2*self.margin, theight+2*self.margin)
gc.fill_path()
Expand All @@ -71,53 +68,33 @@ def overlay(self, component, gc, view_bounds=None, mode="normal"):
gc.show_text(self.text)


rect1 = Region("orchid", position=[50,50])
rect2 = Region("cornflowerblue", position=[200,50])
rect1.overlays.append(Overlay("One", component=rect1))
rect2.overlays.append(Overlay("Two", component=rect2))
container1 = OverlayPlotContainer(bounds=[400,400], resizable="")
container1.add(rect1, rect2)
container1.bgcolor = (0.60, 0.98, 0.60, 0.5) #"palegreen"

rect3 = Region("purple", position=[50,50])
rect4 = Region("teal", position=[200,50])
rect3.overlays.append(Overlay("Three", component=rect3))
rect4.overlays.append(Overlay("Four", component=rect4))
container2 = OverlayPlotContainer(bounds=[400,400], resizable="")
container2.add(rect3, rect4)
container2.bgcolor = "navajowhite"
container2.position = [200, 200]

top_container = OverlayPlotContainer()
top_container.add(container1, container2)
class PlotFrame(DemoFrame):

#rect1.unified_draw = True
#rect2.unified_draw = True
def _create_window(self):
rect1 = Region("orchid", position=[50, 50])
rect2 = Region("cornflowerblue", position=[200, 50])
rect1.overlays.append(Overlay("One", component=rect1))
rect2.overlays.append(Overlay("Two", component=rect2))
container1 = OverlayPlotContainer(bounds=[400, 400], resizable="")
container1.add(rect1, rect2)
container1.bgcolor = (0.60, 0.98, 0.60, 0.5) #"palegreen"

class PlotFrame(wx.Frame):
def __init__(self, *args, **kw):
wx.Frame.__init__( *(self,) + args, **kw )
rect3 = Region("purple", position=[50, 50])
rect4 = Region("teal", position=[200, 50])
rect3.overlays.append(Overlay("Three", component=rect3))
rect4.overlays.append(Overlay("Four", component=rect4))
container2 = OverlayPlotContainer(bounds=[400, 400], resizable="")
container2.add(rect3, rect4)
container2.bgcolor = "navajowhite"
container2.position = [200, 200]

# Create the Enable Window object, and store a reference to it.
# (This will be handy later.) The Window requires a WX parent object
# as its first argument, so we just pass 'self'.
self.plot_window = Window(self, component=top_container)
top_container = OverlayPlotContainer()
top_container.add(container1, container2)

# We'll create a default sizer to put our plot_window in.
sizer = wx.BoxSizer(wx.HORIZONTAL)
return Window(self, -1, component=top_container)

# Since Window is an Enable object, we need to get its corresponding
# WX control. This is stored in its ".control" attribute.
sizer.Add(self.plot_window.control, 1, wx.EXPAND)

# More WX boilerplate.
self.SetSizer(sizer)
self.SetAutoLayout(True)
self.Show(True)
return

if __name__ == "__main__":
app = wx.PySimpleApp()
frame = PlotFrame(None, size=(600,600))
app.MainLoop()

# Save demo so that it doesn't get garbage collected when run within
# existing event loop (i.e. from ipython).
demo = demo_main(PlotFrame, size=(600, 600))
Loading

0 comments on commit b0e4464

Please sign in to comment.