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

move example helper modules into examples #805

Merged
merged 6 commits into from
Jun 21, 2021
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
76 changes: 16 additions & 60 deletions enable/example_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,65 +11,21 @@
Example Application Support
===========================

This module provides a simple Pyface application that can be used by examples
in places where a DemoFrame is insufficient.
This module is meant for internal use only and it is not meant for use in
library code. Importing from this module is deprecated and it will be removed
in Enable 6.0. We highly recommend that you update your code and vendorize what
is necessary.

"""

from pyface.api import ApplicationWindow, GUI


class DemoApplication(ApplicationWindow):
""" Simple Pyface application displaying a component.

This application has the same interface as the DemoFrames from the
example_support module, but the window is embedded in a full Pyface
application. This means that subclasses have the opportunity of
adding Menus, Toolbars, and other similar features to the demo, where
needed.

"""

def _create_contents(self, parent):
self.enable_win = self._create_window()
return self.enable_win.control

def _create_window(self):
"Subclasses should override this method and return an enable.Window"
raise NotImplementedError()

@classmethod
def demo_main(cls, **traits):
""" Create the demo application and start the mainloop, if needed

This should be called with appropriate arguments which will be passed
to the class' constructor.

"""
# get the Pyface GUI
gui = GUI()

# create the application's main window
window = cls(**traits)
window.open()

# start the application
# if there isn't already a running mainloop, this will block
gui.start_event_loop()

# if there is already a running mainloop (eg. in an IPython session),
# return a reference to the window so that our caller can hold on to it
return window


def demo_main(cls, **traits):
""" Create the demo application and start the mainloop, if needed.

This is a simple wrapper around `cls.demo_main` for compatibility with the
`DemoFrame` implementation.

This should be called with appropriate arguments which will be passed to
the class' constructor.

"""
cls.demo_main(**traits)
import warnings

from enable.examples._example_application import DemoApplication, demo_main

warnings.warn(
"This module is meant for internal use only and it is not meant for use in"
" library code. Importing from this module is deprecated and it will be"
" removed in Enable 6.0. We highly recommend that you update your code and"
" vendorize what is necessary.",
DeprecationWarning,
stacklevel=2
)
49 changes: 17 additions & 32 deletions enable/example_canvas.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,21 @@
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!
""" A suitable replacement for the old Canvas class in Kiva.
"""

from enable.api import Component, Window


class _DummyComponent(Component):
def __init__(self, draw_func, *args, **kwargs):
super(_DummyComponent, self).__init__(*args, **kwargs)
self._draw_func = draw_func

def __del__(self):
self._draw_func = None

def draw(self, gc, **kwargs):
""" Call our wrapped draw function.
"""
self._draw_func(gc)


class Canvas(Window):
def __init__(self):
# Create a component that wraps our do_draw method
self.component = _DummyComponent(self.do_draw)

# call our base class
super(Window, self).__init__(None)

def do_draw(self, gc):
""" Method to be implemented by subclasses to actually perform various
GC drawing commands before the GC is blitted into the screen.
"""
pass
This module is meant for internal use only and it is not meant for use in
library code. Importing from this module is deprecated and it will be removed
in Enable 6.0. We highly recommend that you update your code and vendorize what
is necessary.
"""
import warnings

from enable.examples._example_canvas import Canvas

warnings.warn(
"This module is meant for internal use only and it is not meant for use in"
" library code. Importing from this module is deprecated and it will be"
" removed in Enable 6.0. We highly recommend that you update your code and"
" vendorize what is necessary.",
DeprecationWarning,
stacklevel=2
)
62 changes: 16 additions & 46 deletions enable/example_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,50 +8,20 @@
#
# Thanks for using Enthought open source!
"""
Support class that wraps up the boilerplate toolkit calls that virtually all
demo programs have to use.
This module is meant for internal use only and it is not meant for use in
library code. Importing from this module is deprecated and it will be removed
in Enable 6.0. We highly recommend that you update your code and vendorize what
is necessary.
"""

from traits.api import HasTraits, Instance
from traits.etsconfig.api import ETSConfig
from traitsui.api import Item, View

from enable.api import Component, ComponentEditor

# FIXME - it should be enough to do the following import, but because of the
# PyQt/traits problem (see below) we can't because it would drag in traits too
# early. Until it is fixed we just assume wx if we can import it.
# Force the selection of a valid toolkit.
# import enable.toolkit
if not ETSConfig.toolkit:
for toolkit, toolkit_module in (("wx", "wx"), ("qt4", "PyQt4")):
try:
exec("import " + toolkit_module)
ETSConfig.toolkit = toolkit
break
except ImportError:
pass
else:
raise RuntimeError("Can't load wx or qt4 backend for Chaco.")


class DemoFrame(HasTraits):

component = Instance(Component)

traits_view = View(
Item("component", editor=ComponentEditor(), show_label=False),
resizable=True,
)

def _component_default(self):
return self._create_component()

def _create_component(self):
""" Create and return a component which is typically a
container with nested components """
raise NotImplementedError


def demo_main(demo_class, size=(640, 480), title="Enable Example"):
demo_class().configure_traits()
import warnings

from enable.examples._example_support import DemoFrame, demo_main

warnings.warn(
"This module is meant for internal use only and it is not meant for use in"
" library code. Importing from this module is deprecated and it will be"
" removed in Enable 6.0. We highly recommend that you update your code and"
" vendorize what is necessary.",
DeprecationWarning,
stacklevel=2
)
75 changes: 75 additions & 0 deletions enable/examples/_example_application.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# (C) Copyright 2005-2021 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!
"""
Example Application Support
===========================

This module provides a simple Pyface application that can be used by examples
in places where a DemoFrame is insufficient.

"""

from pyface.api import ApplicationWindow, GUI


class DemoApplication(ApplicationWindow):
""" Simple Pyface application displaying a component.

This application has the same interface as the DemoFrames from the
example_support module, but the window is embedded in a full Pyface
application. This means that subclasses have the opportunity of
adding Menus, Toolbars, and other similar features to the demo, where
needed.

"""

def _create_contents(self, parent):
self.enable_win = self._create_window()
return self.enable_win.control

def _create_window(self):
"Subclasses should override this method and return an enable.Window"
raise NotImplementedError()

@classmethod
def demo_main(cls, **traits):
""" Create the demo application and start the mainloop, if needed

This should be called with appropriate arguments which will be passed
to the class' constructor.

"""
# get the Pyface GUI
gui = GUI()

# create the application's main window
window = cls(**traits)
window.open()

# start the application
# if there isn't already a running mainloop, this will block
gui.start_event_loop()

# if there is already a running mainloop (eg. in an IPython session),
# return a reference to the window so that our caller can hold on to it
return window


def demo_main(cls, **traits):
""" Create the demo application and start the mainloop, if needed.

This is a simple wrapper around `cls.demo_main` for compatibility with the
`DemoFrame` implementation.

This should be called with appropriate arguments which will be passed to
the class' constructor.

"""
cls.demo_main(**traits)
42 changes: 42 additions & 0 deletions enable/examples/_example_canvas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# (C) Copyright 2005-2021 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!
""" A suitable replacement for the old Canvas class in Kiva.
"""

from enable.api import Component, Window


class _DummyComponent(Component):
def __init__(self, draw_func, *args, **kwargs):
super(_DummyComponent, self).__init__(*args, **kwargs)
self._draw_func = draw_func

def __del__(self):
self._draw_func = None

def draw(self, gc, **kwargs):
""" Call our wrapped draw function.
"""
self._draw_func(gc)


class Canvas(Window):
def __init__(self):
# Create a component that wraps our do_draw method
self.component = _DummyComponent(self.do_draw)

# call our base class
super(Window, self).__init__(None)

def do_draw(self, gc):
""" Method to be implemented by subclasses to actually perform various
GC drawing commands before the GC is blitted into the screen.
"""
pass
41 changes: 41 additions & 0 deletions enable/examples/_example_support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# (C) Copyright 2005-2021 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!
"""
Support class that wraps up the boilerplate toolkit calls that virtually all
demo programs have to use.
"""

from traits.api import HasTraits, Instance
from traits.etsconfig.api import ETSConfig
from traitsui.api import Item, View

from enable.api import Component, ComponentEditor


class DemoFrame(HasTraits):

component = Instance(Component)

traits_view = View(
Item("component", editor=ComponentEditor(), show_label=False),
resizable=True,
)

def _component_default(self):
return self._create_component()

def _create_component(self):
""" Create and return a component which is typically a
container with nested components """
raise NotImplementedError


def demo_main(demo_class, size=(640, 480), title="Enable Example"):
demo_class().configure_traits()
2 changes: 1 addition & 1 deletion enable/examples/demo/enable/basic_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
This demonstrates the most basic drawing capabilities using Enable. A new
component is created and added to a container.
"""
from enable.example_support import DemoFrame, demo_main
from enable.examples._example_support import DemoFrame, demo_main
from enable.api import Component, Container


Expand Down
2 changes: 1 addition & 1 deletion enable/examples/demo/enable/basic_move.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from traits.api import Float

from enable.api import Component, Pointer, Container
from enable.example_support import DemoFrame, demo_main
from enable.examples._example_support import DemoFrame, demo_main


class Box(Component):
Expand Down
2 changes: 1 addition & 1 deletion enable/examples/demo/enable/brush_draw.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"""
from traits.api import Instance

from enable.example_support import DemoFrame, demo_main
from enable.examples._example_support import DemoFrame, demo_main
from enable.api import (
Brush, ColorStop, Component, Container, Gradient, RadialGradientBrush
)
Expand Down
2 changes: 1 addition & 1 deletion enable/examples/demo/enable/canvas_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#
# Thanks for using Enthought open source!
from enable.api import Canvas, Viewport
from enable.example_support import demo_main, DemoFrame
from enable.examples._example_support import demo_main, DemoFrame
from enable.tools.api import ViewportPanTool


Expand Down
Loading