σ
 m[c           @` sΩ   d  Z  d d l m Z m Z m Z m Z d d l Z d d l Z d d l j	 Z	 d d l
 j Z d d l m Z d d l j Z d e f d     YZ d e f d     YZ d	 e f d
     YZ d e f d     YZ d S(   u   
`ToolManager`
    Class that makes the bridge between user interaction (key press,
    toolbar clicks, ..) and the actions in response to the user inputs.
i    (   t   absolute_importt   divisiont   print_functiont   unicode_literalsN(   t   validate_stringlistt	   ToolEventc           B` s   e  Z d  Z d d  Z RS(   u(   Event for tool manipulation (add/remove)c         C` s(   | |  _  | |  _ | |  _ | |  _ d  S(   N(   t   namet   sendert   toolt   data(   t   selfR   R   R   R	   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   __init__   s    			N(   t   __name__t
   __module__t   __doc__t   NoneR   (    (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR      s   t   ToolTriggerEventc           B` s   e  Z d  Z d d d  Z RS(   u/   Event to inform  that a tool has been triggeredc         C` s&   t  j |  | | | |  | |  _ d  S(   N(   R   R   t   canvasevent(   R
   R   R   R   R   R	   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR      s    N(   R   R   R   R   R   (    (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR      s   t   ToolManagerMessageEventc           B` s   e  Z d  Z d   Z RS(   uq   
    Event carrying messages from toolmanager

    Messages usually get displayed to the user by the toolbar
    c         C` s   | |  _  | |  _ | |  _ d  S(   N(   R   R   t   message(   R
   R   R   R   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR   (   s    		(   R   R   R   R   (    (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR   "   s   t   ToolManagerc           B` s  e  Z d  Z d d  Z e d    Z e d    Z e j d    Z e	 d  Z
 d   Z d   Z d d  Z e d	    Z d
   Z d   Z d   Z d   Z d   Z d   Z d   Z d   Z d d d d  Z d d d d  Z d   Z e d    Z e	 d  Z RS(   uY  
    Helper class that groups all the user interactions for a Figure

    Attributes
    ----------
    figure: `Figure`
    keypresslock: `widgets.LockDraw`
        `LockDraw` object to know if the `canvas` key_press_event is locked
    messagelock: `widgets.LockDraw`
        `LockDraw` object to know if the message is available to write
    c         C` s   t  j d d d  d  |  _ i  |  _ i  |  _ i  |  _ t j   |  _	 t
 j   |  _ t
 j   |  _ d  |  _ |  j |  d  S(   Nu1   Treat the new Tool classes introduced in v1.5 as u4   experimental for now, the API will likely change in u+   version 2.1 and perhaps the rcParam as well(   t   warningst   warnR   t   _key_press_handler_idt   _toolst   _keyst   _toggledt   cbookt   CallbackRegistryt
   _callbackst   widgetst   LockDrawt   keypresslockt   messagelockt   _figuret
   set_figure(   R
   t   figure(    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR   ;   s    					c         C` s   |  j  s d S|  j  j S(   u   Canvas managed by FigureManagerN(   R"   R   t   canvas(   R
   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR%   N   s    	c         C` s   |  j  S(   u   Figure that holds the canvas(   R"   (   R
   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR$   U   s    c         C` s   |  j  |  d  S(   N(   R#   (   R
   R$   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR$   Z   s    c         C` s|   |  j  r |  j j |  j   n  | |  _ | rL |  j j d |  j  |  _  n  | rx x# |  j j   D] } | | _ qb Wn  d S(   uΐ   
        Sets the figure to interact with the tools

        Parameters
        ==========
        figure: `Figure`
        update_tools: bool
            Force tools to update figure
        u   key_press_eventN(	   R   R%   t   mpl_disconnectR"   t   mpl_connectt
   _key_pressR   t   valuesR$   (   R
   R$   t   update_toolsR   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR#   ^   s    
			c         C` s   |  j  j | |  S(   u%  
        Connect event with string *s* to *func*.

        Parameters
        ----------
        s : String
            Name of the event

            The following events are recognized

            - 'tool_message_event'
            - 'tool_removed_event'
            - 'tool_added_event'

            For every tool added a new event is created

            - 'tool_trigger_TOOLNAME`
              Where TOOLNAME is the id of the tool.

        func : function
            Function to be called with signature
            def func(event)
        (   R   t   connect(   R
   t   st   func(    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   toolmanager_connectr   s    c         C` s   |  j  j |  S(   u  
        Disconnect callback id *cid*

        Example usage::

            cid = toolmanager.toolmanager_connect('tool_trigger_zoom',
                                                  on_press)
            #...later
            toolmanager.toolmanager_disconnect(cid)
        (   R   t
   disconnect(   R
   t   cid(    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   toolmanager_disconnect   s    c         C` sD   | d k r |  } n  d } t | | |  } |  j j | |  d S(   u!    Emit a `ToolManagerMessageEvent`u   tool_message_eventN(   R   R   R   t   process(   R
   R   R   R,   t   event(    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   message_event   s
    	c         C` s   |  j  S(   u   Currently toggled tools(   R   (   R
   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   active_toggle’   s    c         C` s;   g  t  j |  j  D] \ } } | | k r | ^ q } | S(   uσ   
        Get the keymap associated with the specified tool

        Parameters
        ----------
        name : string
            Name of the Tool

        Returns
        -------
        list : list of keys associated with the Tool
        (   t   sixt	   iteritemsR   (   R
   R   t   kt   it   keys(    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   get_tool_keymap¨   s    7c         C` s(   x! |  j  |  D] } |  j | =q Wd  S(   N(   R;   R   (   R
   R   R8   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   _remove_keysΉ   s    c         G` s   | |  j  k r" t d |   n  |  j |  xe | D]] } xT t |  D]F } | |  j k r t j d | |  j | | f  n  | |  j | <qI Wq6 Wd S(   uΞ   
        Set the keymap to associate with the specified tool

        Parameters
        ----------
        name : string
            Name of the Tool
        keys : keys to associate with the Tool
        u   %s not in Toolsu   Key %s changed from %s to %sN(   R   t   KeyErrorR<   R   R   R   R   (   R
   R   R:   t   keyR8   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   update_keymap½   s    	c         C` s   |  j  |  } | j   t | d t  r> |  j | d  n  |  j |  d } t | |  |  } |  j j | |  |  j	 | =d S(   u   
        Remove tool from `ToolManager`

        Parameters
        ----------
        name : string
            Name of the Tool
        u   toggledu   toolmanageru   tool_removed_eventN(
   t   get_toolt   destroyt   getattrt   Falset   trigger_toolR<   R   R   R2   R   (   R
   R   R   R,   R3   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   remove_toolΤ   s    

c         O` s2  |  j  |  } | s. t d t |    n  | |  j k rU t j d  |  j | S| |  | | |  } | |  j | <| j d k	 r |  j | | j  n  t	 | t
 j  r| j d k rΦ |  j j d t    n |  j j | j d  | j r|  j | d d d  qn  | j |  j  |  j |  | S(   u  
        Add *tool* to `ToolManager`

        If successful adds a new event `tool_trigger_name` where **name** is
        the **name** of the tool, this event is fired everytime
        the tool is triggered.

        Parameters
        ----------
        name : str
            Name of the tool, treated as the ID, has to be unique
        tool : class_like, i.e. str or type
            Reference to find the class of the Tool to added.

        Notes
        -----
        args and kwargs get passed directly to the tools constructor.

        See Also
        --------
        matplotlib.backend_tools.ToolBase : The base class for tools.
        u   Impossible to find class for %su;   A "Tool class" with the same name already exists, not addedN(   t   _get_cls_to_instantiatet
   ValueErrort   strR   R   R   t   default_keymapR   R?   t
   isinstancet   toolst   ToolToggleBaset   radio_groupR   t
   setdefaultt   sett   toggledt   _handle_toggleR#   R$   t   _tool_added_event(   R
   R   R   t   argst   kwargst   tool_clst   tool_obj(    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   add_toolν   s&    	c         C` s/   d } t  | |  |  } |  j j | |  d  S(   Nu   tool_added_event(   R   R   R2   (   R
   R   R,   R3   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyRR   %  s    c         C` sΥ   | j  } | d k r` | j |  j d k rE |  j d j | j  n |  j d j | j  d S|  j | | j k r d } nE |  j | d k r | j } n& |  j |  j | |  | |  | j } | |  j | <d S(   u  
        Toggle tools, need to untoggle prior to using other Toggle tool
        Called from trigger_tool

        Parameters
        ----------
        tool: Tool object
        sender: object
            Object that wishes to trigger the tool
        canvasevent : Event
            Original Canvas event or None
        data : Object
            Extra data to pass to the tool when triggering
        N(   RM   R   R   R   t   removet   addRD   (   R
   R   R   R   R	   RM   RP   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyRQ   *  s     			c         C` s   t  | t j  rm | t   k r1 t   | } qm d } t | t   t   | g d  } t | | t  } n  t |  r} | Sd  Sd  S(   Nu   backend_toolsi   (
   RJ   R6   t   string_typest   globalst
   __import__t   localsRB   RC   t   callableR   (   R
   t   callback_classt   modt   current_module(    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyRF   W  s    c         C` s   |  j  |  } | d k r d S| d k r4 |  } n  |  j | | | |  d | } t | | | | |  } |  j j | |  d S(   u  
        Trigger a tool and emit the tool_trigger_[name] event

        Parameters
        ----------
        name : string
            Name of the tool
        sender: object
            Object that wishes to trigger the tool
        canvasevent : Event
            Original Canvas event or None
        data : Object
            Extra data to pass to the tool when triggering
        Nu   tool_trigger_%s(   R@   R   t   _trigger_toolR   R   R2   (   R
   R   R   R   R	   R   R,   R3   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyRD   h  s    	
c         C` sQ   |  j  |  } t | t j  r: |  j | | | |  n  | j | | |  d S(   uP   
        Trigger on a tool

        Method to actually trigger the tool
        N(   R@   RJ   RK   RL   RQ   t   trigger(   R
   R   R   R   R	   R   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyRb     s    c         C` sa   | j  d  k s |  j j   r" d  S|  j j | j  d   } | d  k rJ d  S|  j | d | d  S(   NR   (   R>   R   R    t   lockedR   t   getRD   (   R
   R3   R   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR(     s    c         C` s   |  j  S(   u,   Return the tools controlled by `ToolManager`(   R   (   R
   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyRK     s    c         C` s`   t  | t j  r( | j |  j k r( | S| |  j k rU | rQ t j d |  n  d S|  j | S(   u  
        Return the tool object, also accepts the actual tool for convenience

        Parameters
        ----------
        name : str, ToolBase
            Name of the tool, or the tool itself
        warn : bool, optional
            If this method should give warnings.
        u$   ToolManager does not control tool %sN(   RJ   RK   t   ToolBaseR   R   R   R   R   (   R
   R   R   (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR@   £  s    $N(   R   R   R   R   R   t   propertyR%   R$   t   settert   TrueR#   R.   R1   R4   R5   R;   R<   R?   RE   RW   RR   RQ   RF   RD   Rb   R(   RK   R@   (    (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyR   .   s0   								8		-			(   R   t
   __future__R    R   R   R   R6   R   t   matplotlib.cbookR   t   matplotlib.widgetsR   t   matplotlib.rcsetupR   t   matplotlib.backend_toolst   backend_toolsRK   t   objectR   R   R   R   (    (    (    s:   lib/python2.7/site-packages/matplotlib/backend_managers.pyt   <module>   s   "	