3
$ßL]32  ã               @   s€   d dl Z d dljZd dljZd dlmZ d dljZ	e j
eƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZG d	d
„ d
eƒZdS )é    N)Úvalidate_stringlistc               @   s   e Zd ZdZddd„ZdS )Ú	ToolEventz)Event for tool manipulation (add/remove).Nc             C   s   || _ || _|| _|| _d S )N)ÚnameÚsenderÚtoolÚdata)Úselfr   r   r   r   © r	   úC/tmp/pip-install-edjban26/matplotlib/matplotlib/backend_managers.pyÚ__init__   s    zToolEvent.__init__)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r	   r	   r	   r
   r      s   r   c               @   s   e Zd ZdZddd„ZdS )ÚToolTriggerEventz/Event to inform that a tool has been triggered.Nc             C   s   t j| ||||ƒ || _d S )N)r   r   Úcanvasevent)r   r   r   r   r   r   r	   r	   r
   r      s    zToolTriggerEvent.__init__)NN)r   r   r   r   r   r	   r	   r	   r
   r      s   r   c               @   s   e Zd ZdZdd„ ZdS )ÚToolManagerMessageEventzs
    Event carrying messages from toolmanager.

    Messages usually get displayed to the user by the toolbar.
    c             C   s   || _ || _|| _d S )N)r   r   Úmessage)r   r   r   r   r	   r	   r
   r   !   s    z ToolManagerMessageEvent.__init__N)r   r   r   r   r   r	   r	   r	   r
   r      s   r   c               @   sâ   e Zd ZdZd/dd„Zedd„ ƒZedd„ ƒZejd	d„ ƒZd0dd„Z	dd„ Z
dd„ Zd1dd„Zedd„ ƒZdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd!d"„ Zd#d$„ Zd2d%d&„Zd3d'd(„Zd)d*„ Zed+d,„ ƒZd4d-d.„ZdS )5ÚToolManagera�  
    Manager for actions triggered by user interactions (key press, toolbar
    clicks, ...) on 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
    Nc             C   sT   t jdƒ d | _i | _i | _i | _tjƒ | _t	j
ƒ | _t	j
ƒ | _d | _| j|ƒ d S )Nz�Treat the new Tool classes introduced in v1.5 as experimental for now, the API will likely change in version 2.1 and perhaps the rcParam as well)Ú_logÚwarningÚ_key_press_handler_idÚ_toolsÚ_keysÚ_toggledÚcbookZCallbackRegistryÚ
_callbacksÚwidgetsZLockDrawÚkeypresslockZmessagelockÚ_figureÚ
set_figure)r   Úfigurer	   r	   r
   r   5   s    



zToolManager.__init__c             C   s   | j s
dS | j jS )z Canvas managed by FigureManager.N)r   Úcanvas)r   r	   r	   r
   r"   H   s    zToolManager.canvasc             C   s   | j S )zFigure that holds the canvas.)r   )r   r	   r	   r
   r!   O   s    zToolManager.figurec             C   s   | j |ƒ d S )N)r    )r   r!   r	   r	   r
   r!   T   s    Tc             C   sR   | j r| jj| j ƒ || _|r0| jjd| jƒ| _ |rNx| jjƒ D ]
}||_q@W dS )z¼
        Bind the given figure to the tools.

        Parameters
        ----------
        figure : `.Figure`
        update_tools : bool
            Force tools to update figure
        Zkey_press_eventN)	r   r"   Zmpl_disconnectr   Zmpl_connectÚ
_key_pressr   Úvaluesr!   )r   r!   Zupdate_toolsr   r	   r	   r
   r    X   s    
zToolManager.set_figurec             C   s   | j j||ƒS )a%  
        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   Úconnect)r   ÚsÚfuncr	   r	   r
   Útoolmanager_connectl   s    zToolManager.toolmanager_connectc             C   s   | j j|ƒS )zã
        Disconnect callback id *cid*.

        Example usage::

            cid = toolmanager.toolmanager_connect('tool_trigger_zoom', onpress)
            #...later
            toolmanager.toolmanager_disconnect(cid)
        )r   Z
disconnect)r   Zcidr	   r	   r
   Útoolmanager_disconnect†   s    
z"ToolManager.toolmanager_disconnectc             C   s.   |dkr| }d}t |||ƒ}| jj||ƒ dS )z!Emit a `ToolManagerMessageEvent`.NZtool_message_event)r   r   Úprocess)r   r   r   r&   Úeventr	   r	   r
   Úmessage_event’   s
    zToolManager.message_eventc             C   s   | j S )zCurrently toggled tools.)r   )r   r	   r	   r
   Úactive_toggle›   s    zToolManager.active_togglec                s   ‡ fdd„| j jƒ D ƒ}|S )zô
        Get the keymap associated with the specified tool.

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

        Returns
        -------
        list : list of keys associated with the Tool
        c                s   g | ]\}}|ˆ kr|‘qS r	   r	   )Ú.0ÚkÚi)r   r	   r
   ú
<listcomp>®   s    z/ToolManager.get_tool_keymap.<locals>.<listcomp>)r   Úitems)r   r   Úkeysr	   )r   r
   Úget_tool_keymap    s    zToolManager.get_tool_keymapc             C   s    x| j |ƒD ]}| j|= qW d S )N)r4   r   )r   r   r/   r	   r	   r
   Ú_remove_keys±   s    zToolManager._remove_keysc             G   sr   || j krtd| ƒ‚| j|ƒ xL|D ]D}x>t|ƒD ]2}|| jkr\tjd|| j| |f ƒ || j|< q4W q&W dS )zÏ
        Set the keymap to associate with the specified tool.

        Parameters
        ----------
        name : string
            Name of the Tool
        keys : keys to associate with the Tool
        z%s not in ToolszKey %s changed from %s to %sN)r   ÚKeyErrorr5   r   r   r   Ú_warn_external)r   r   r3   Úkeyr/   r	   r	   r
   Úupdate_keymapµ   s    



zToolManager.update_keymapc             C   s^   | j |ƒ}|jƒ  t|ddƒr*| j|dƒ | j|ƒ d}t|| |ƒ}| jj||ƒ | j|= dS )z…
        Remove tool named *name*.

        Parameters
        ----------
        name : string
            Name of the Tool
        ÚtoggledFZtoolmanagerZtool_removed_eventN)	Úget_toolÚdestroyÚgetattrÚtrigger_toolr5   r   r   r*   r   )r   r   r   r&   r+   r	   r	   r
   Úremove_toolÌ   s    


zToolManager.remove_toolc             O   sØ   | j |ƒ}|stdt|ƒ ƒ‚|| jkr<tjdƒ | j| S || |f|ž|Ž}|| j|< |jdk	rp| j||jƒ t|t	j
ƒr¾|jdkr˜| jjdtƒ ƒ n| jj|jdƒ |jr¾| j|dddƒ |j| jƒ | j|ƒ |S )a’  
        Add *tool* to `ToolManager`.

        If successful, adds a new event ``tool_trigger_{name}`` where
        ``{name}`` is the *name* of the tool; the 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.
        zImpossible to find class for %sz;A "Tool class" with the same name already exists, not addedN)Ú_get_cls_to_instantiateÚ
ValueErrorÚstrr   r   r7   Zdefault_keymapr9   Ú
isinstanceÚtoolsÚToolToggleBaseÚradio_groupr   Ú
setdefaultÚsetr:   Ú_handle_toggler    r!   Ú_tool_added_event)r   r   r   ÚargsÚkwargsZtool_clsZtool_objr	   r	   r
   Úadd_toolå   s&    







zToolManager.add_toolc             C   s"   d}t || |ƒ}| jj||ƒ d S )NZtool_added_event)r   r   r*   )r   r   r&   r+   r	   r	   r
   rJ     s    zToolManager._tool_added_eventc             C   sž   |j }|dkrH|j| jd kr2| jd j|jƒ n| jd j|jƒ dS | j| |jkr^d}n2| j| dkrt|j}n| j| j| | ||ƒ |j}|| j|< dS )a˜  
        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)rF   r   r   ÚremoveÚaddr>   )r   r   r   r   r   rF   r:   r	   r	   r
   rI   "  s     zToolManager._handle_togglec             C   sZ   t |tƒrF|tƒ kr tƒ | }n&d}t|tƒ tƒ |gdƒ}t||dƒ}t|ƒrR|S d S d S )NÚbackend_toolsé   F)rC   rB   ÚglobalsÚ
__import__Úlocalsr=   Úcallable)r   Zcallback_classÚmodZcurrent_moduler	   r	   r
   r@   O  s    

z#ToolManager._get_cls_to_instantiatec             C   s\   | j |ƒ}|dkrdS |dkr"| }| j||||ƒ d| }t|||||ƒ}| jj||ƒ dS )aˆ  
        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
        Nztool_trigger_%s)r;   Ú_trigger_toolr   r   r*   )r   r   r   r   r   r   r&   r+   r	   r	   r
   r>   `  s    
zToolManager.trigger_toolc             C   s8   | j |ƒ}t|tjƒr&| j||||ƒ |j|||ƒ dS )zActually trigger a tool.N)r;   rC   rD   rE   rI   Ztrigger)r   r   r   r   r   r   r	   r	   r
   rW   |  s    
zToolManager._trigger_toolc             C   sF   |j d ks| jjƒ rd S | jj|j d ƒ}|d kr4d S | j||d� d S )N)r   )r8   r   Úlockedr   Úgetr>   )r   r+   r   r	   r	   r
   r#   ‡  s    zToolManager._key_pressc             C   s   | j S )z,A dict mapping tool name -> controlled tool.)r   )r   r	   r	   r
   rD   �  s    zToolManager.toolsc             C   sF   t |tjƒr|j| jkr|S || jkr<|r8tjd| ƒ dS | j| S )a  
        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.
        z$ToolManager does not control tool %sN)rC   rD   ZToolBaser   r   r   r7   )r   r   Úwarnr	   r	   r
   r;   •  s    
zToolManager.get_tool)N)T)N)NNN)NNN)T)r   r   r   r   r   Úpropertyr"   r!   Úsetterr    r(   r)   r,   r-   r4   r5   r9   r?   rM   rJ   rI   r@   r>   rW   r#   rD   r;   r	   r	   r	   r
   r   '   s.   


	8-

	r   )ÚloggingZmatplotlib.cbookr   Zmatplotlib.widgetsr   Zmatplotlib.rcsetupr   Zmatplotlib.backend_toolsrP   rD   Ú	getLoggerr   r   Úobjectr   r   r   r   r	   r	   r	   r
   Ú<module>   s   



	