B
    Mô`#6  ã               @   s€   d dl Z d dlmZ d dlmZ d dlmZ d dlmZ	 e  
e¡ZG dd„ dƒZG dd„ deƒZG dd„ dƒZG d	d
„ d
ƒ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	   ú?/tmp/pip-unpacked-wheel-0c1wfn4v/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  | ||||¡ || _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d1dd„Zedd„ ƒZedd„ ƒZejd	d„ ƒZd2dd„Z	dd„ Z
dd„ Zd3dd„Zedd„ ƒZdd„ Zdd„ Ze dd¡dd„ ƒZdd„ Zdd „ Zd!d"„ Zd#d$„ Zd%d&„ Zd4d'd(„Zd5d)d*„Zd+d,„ Zed-d.„ ƒZd6d/d0„ZdS )7ÚToolManageraž  
    Manager for actions triggered by user interactions (key press, toolbar
    clicks, ...) on a Figure.

    Attributes
    ----------
    figure : `.Figure`
    keypresslock : `~matplotlib.widgets.LockDraw`
        `.LockDraw` object to know if the `canvas` key_press_event is locked.
    messagelock : `~matplotlib.widgets.LockDraw`
        `.LockDraw` object to know if the message is available to write.
    Nc             C   sT   t  d¡ d | _i | _i | _i | _t ¡ | _t	 
¡ | _t	 
¡ | _d | _|  |¡ 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   |   |¡ d S )N)r    )r   r!   r	   r	   r
   r!   T   s    Tc             C   sR   | j r| j | j ¡ || _|r0| j d| j¡| _ |rNx| j ¡ D ]
}||_q@W dS )zÌ
        Bind the given figure to the tools.

        Parameters
        ----------
        figure : `.Figure`
        update_tools : bool, default: True
            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  ||¡S )aÕ  
        Connect event with string *s* to *func*.

        Parameters
        ----------
        s : str
            The 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 : callable
            Callback function for the toolmanager event with signature::

                def func(event: ToolEvent) -> Any

        Returns
        -------
        cid
            The callback id for the connection. This can be used in
            `.toolmanager_disconnect`.
        )r   Úconnect)r   ÚsÚfuncr	   r	   r
   Útoolmanager_connectl   s    zToolManager.toolmanager_connectc             C   s   | 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 ||¡ 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  ¡ D ƒ}|S )a  
        Return the keymap associated with the specified tool.

        Parameters
        ----------
        name : str
            Name of the Tool.

        Returns
        -------
        list of str
            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|   |¡D ]}| j|= qW d S )N)r4   r   )r   r   r/   r	   r	   r
   Ú_remove_keys¶   s    zToolManager._remove_keysz3.3Úargsc             G   s²   || j krtd| ƒ‚|  |¡ xŒ|f|•D ]~}t|tƒr^t|ƒ|gkr^tjddd� t|ƒ}t|tƒrn|g}x:|D ]2}|| jkrœt 	d|| j| |f ¡ || j|< qtW q,W dS )zí
        Set the keymap to associate with the specified tool.

        Parameters
        ----------
        name : str
            Name of the Tool.
        keys : str or list of str
            Keys to associate with the tool.
        z%s not in Toolsz3.3z¨Passing a list of keys as a single comma-separated string is deprecated since %(since)s and support will be removed %(removal)s; pass keys as a list of strings instead.)r   zKey %s changed from %s to %sN)
r   ÚKeyErrorr5   Ú
isinstanceÚstrr   r   Zwarn_deprecatedr   Ú_warn_external)r   r   Úkeyr6   r/   r	   r	   r
   Úupdate_keymapº   s    





zToolManager.update_keymapc             C   s^   |   |¡}| ¡  t|ddƒr*|  |d¡ |  |¡ d}t|| |ƒ}| j ||¡ | j|= dS )zƒ
        Remove tool named *name*.

        Parameters
        ----------
        name : str
            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Ø   |   |¡}|stdt|ƒ ƒ‚|| jkr<t d¡ | j| S || |f|ž|Ž}|| j|< |jdk	rp|  ||j¡ t|t	j
ƒr¾|jdkr˜| j dtƒ ¡ n| j |jd¡ |jr¾|  |ddd¡ | | 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 every time 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Ú
ValueErrorr9   r   r   r:   Zdefault_keymapr<   r8   ÚtoolsÚToolToggleBaseÚradio_groupr   Ú
setdefaultÚsetr=   Ú_handle_toggler    r!   Ú_tool_added_event)r   r   r   r6   ÚkwargsZtool_clsZtool_objr	   r	   r
   Úadd_toolò   s&    







zToolManager.add_toolc             C   s"   d}t || |ƒ}| j ||¡ d S )NZtool_added_event)r   r   r*   )r   r   r&   r+   r	   r	   r
   rK   *  s    zToolManager._tool_added_eventc             C   sž   |j }|dkrH|j| jd kr2| jd  |j¡ n| jd  |j¡ dS | j| |jkr^d}n2| j| dkrt|j}n|  | j| | ||¡ |j}|| j|< dS )a›  
        Toggle tools, need to untoggle prior to using other Toggle tool.
        Called from trigger_tool.

        Parameters
        ----------
        tool : `.ToolBase`
        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)rG   r   r   ÚremoveÚaddrA   )r   r   r   r   r   rG   r=   r	   r	   r
   rJ   /  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)r8   r9   ÚglobalsÚ
__import__Úlocalsr@   Úcallable)r   Zcallback_classÚmodZcurrent_moduler	   r	   r
   rC   \  s    

z#ToolManager._get_cls_to_instantiatec             C   s\   |   |¡}|dkrdS |dkr"| }|  ||||¡ d| }t|||||ƒ}| j ||¡ dS )a‰  
        Trigger a tool and emit the ``tool_trigger_{name}`` event.

        Parameters
        ----------
        name : str
            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
   rA   m  s    
zToolManager.trigger_toolc             C   s8   |   |¡}t|tjƒr&|  ||||¡ | |||¡ dS )zActually trigger a tool.N)r>   r8   rE   rF   rJ   Ztrigger)r   r   r   r   r   r   r	   r	   r
   rW   ‰  s    
zToolManager._trigger_toolc             C   sF   |j d ks| j ¡ rd S | j |j d ¡}|d kr4d S | j||d� d S )N)r   )r;   r   Úlockedr   ÚgetrA   )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
   rE   �  s    zToolManager.toolsc             C   sF   t |tjƒr|j| jkr|S || jkr<|r8t d| ¡ dS | j| S )aû  
        Return the tool object with the given name.

        For convenience, this passes tool objects through.

        Parameters
        ----------
        name : str or `.ToolBase`
            Name of the tool, or the tool itself.
        warn : bool, default: True
            Whether a warning should be emitted it no tool with the given name
            exists.

        Returns
        -------
        `.ToolBase` or None
            The tool or None if no tool with the given name exists.
        z$ToolManager does not control tool %sN)r8   rE   ZToolBaser   r   r   r:   )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   r   Z_delete_parameterr<   rB   rM   rK   rJ   rC   rA   rW   r#   rE   r>   r	   r	   r	   r
   r   '   s.   


	8-

	r   )ÚloggingZmatplotlib.cbookr   Zmatplotlib.widgetsr   Zmatplotlib.rcsetupr   Zmatplotlib.backend_toolsrP   rE   Ú	getLoggerr   r   r   r   r   r   r	   r	   r	   r
   Ú<module>   s   
	