<filename>enaml/mpl_canvas.py
#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Typed, ForwardTyped, Bool, observe, set_default, Value, List, Enum

from enaml.core.declarative import d_

from .control import Control, ProxyControl


#: Delay the import of matplotlib until needed. This removes the hard
#: dependecy on matplotlib for the rest of the Enaml code base.
def Figure():
    from matplotlib.figure import Figure
    return Figure


class ProxyMPLCanvas(ProxyControl):
    """ The abstract definition of a proxy MPLCanvas object.

    """
    #: A reference to the MPLCanvas declaration.
    declaration = ForwardTyped(lambda: MPLCanvas)

    def set_figure(self, figure):
        raise NotImplementedError

    def set_toolbar_visible(self, visible):
        raise NotImplementedError

    def set_toolbar_location(self, location):
        raise NotImplementedError

    def set_event_actions(self, actions):
        raise NotImplementedError

    def draw(self):
        raise NotImplementedError


class MPLCanvas(Control):
    """ A control which can be used to embded a matplotlib figure.

    """
    #: The matplotlib figure to display in the widget.
    figure = d_(ForwardTyped(Figure))

    #: Whether or not the matplotlib figure toolbar is visible.
    toolbar_visible = d_(Bool(False))
    toolbar_location = d_(Enum('top', 'bottom'))
    event_actions = d_(List(Value()))

    #: Matplotlib figures expand freely in height and width by default.
    hug_width = set_default('ignore')
    hug_height = set_default('ignore')

    #: A reference to the ProxyMPLCanvas object.
    proxy = Typed(ProxyMPLCanvas)

    def draw(self):
        """ Request draw on the Figure """
        if self.proxy_is_active:
            self.proxy.draw()

    #--------------------------------------------------------------------------
    # Observers
    #--------------------------------------------------------------------------
    @observe('figure', 'toolbar_visible', 'toolbar_location', 'event_actions')
    def _update_proxy(self, change):
        """ An observer which sends state change to the proxy.

        """
        # The superclass handler implementation is sufficient.
        super(MPLCanvas, self)._update_proxy(change)


<filename>enaml/mpl_canvas.py
#------------------------------------------------------------------------------
# Copyright (c) 2013, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#------------------------------------------------------------------------------
from atom.api import Typed, ForwardTyped, Bool, observe, set_default, Value, List, Enum

from enaml.core.declarative import d_

from .control import Control, ProxyControl


#: Delay the import of matplotlib until needed. This removes the hard
#: dependecy on matplotlib for the rest of the Enaml code base.
def Figure():
    from matplotlib.figure import Figure
    return Figure


class ProxyMPLCanvas(ProxyControl):
    """ The abstract definition of a proxy MPLCanvas object.

    """
    #: A reference to the MPLCanvas declaration.
    declaration = ForwardTyped(lambda: MPLCanvas)

    def set_figure(self, figure):
        raise NotImplementedError

    def set_toolbar_visible(self, visible):
        raise NotImplementedError

    def set_toolbar_location(self, location):
        raise NotImplementedError

    def set_event_actions(self, actions):
        raise NotImplementedError

    def draw(self):
        raise NotImplementedError


class MPLCanvas(Control):
    """ A control which can be used to embded a matplotlib figure.

    """
    #: The matplotlib figure to display in the widget.
    figure = d_(ForwardTyped(Figure))

    #: Whether or not the matplotlib figure toolbar is visible.
    toolbar_visible = d_(Bool(False))
    toolbar_location = d_(Enum('top', 'bottom'))
    event_actions = d_(List(Value()))

    #: Matplotlib figures expand freely in height and width by default.
    hug_width = set_default('ignore')
    hug_height = set_default('ignore')

    #: A reference to the ProxyMPLCanvas object.
    proxy = Typed(ProxyMPLCanvas)

    def draw(self):
        """ Request draw on the Figure """
        if self.proxy_is_active:
            self.proxy.draw()

    #--------------------------------------------------------------------------
    # Observers
    #--------------------------------------------------------------------------
    @observe('figure', 'toolbar_visible', 'toolbar_location', 'event_actions')
    def _update_proxy(self, change):
        """ An observer which sends state change to the proxy.

        """
        # The superclass handler implementation is sufficient.
        super(MPLCanvas, self)._update_proxy(change)


#------------------------------------------------------------------------------ # Copyright (c) 2013, Nucleic Development Team. # # Distributed under the terms of the Modified BSD License. # # The full license is in the file COPYING.txt, distributed with this software. #------------------------------------------------------------------------------ #: Delay the import of matplotlib until needed. This removes the hard #: dependecy on matplotlib for the rest of the Enaml code base. The abstract definition of a proxy MPLCanvas object. #: A reference to the MPLCanvas declaration. A control which can be used to embded a matplotlib figure. #: The matplotlib figure to display in the widget. #: Whether or not the matplotlib figure toolbar is visible. #: Matplotlib figures expand freely in height and width by default. #: A reference to the ProxyMPLCanvas object. Request draw on the Figure #-------------------------------------------------------------------------- # Observers #-------------------------------------------------------------------------- An observer which sends state change to the proxy. # The superclass handler implementation is sufficient.