3
ƽh#6                 @   sz   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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	   A/tmp/pip-build-7iwl8md4/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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j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 jd d | _i | _i | _i | _tj | _t	j
 | _t	j
 | _d | _| j| d S )NzTreat 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, 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 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 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 )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| j |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| | j| 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rtj	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.3zPassing 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   warn_deprecatedr   _warn_external)r   r   keyr6   r/   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 : 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   | 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 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j|| d S )NZtool_added_event)r   r   r*   )r   r   r&   r+   r	   r	   r
   rL   *  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 : `.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)rH   r   r   removeaddrB   )r   r   r   r   r   rH   r>   r	   r	   r
   rK   /  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__localsrA   callable)r   Zcallback_classmodZcurrent_moduler	   r	   r
   rD   \  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 : 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
   rB   m  s    
zToolManager.trigger_toolc             C   s8   | j |}t|tjr&| j|||| |j||| dS )zActually trigger a tool.N)r?   r8   rF   rG   rK   Ztrigger)r   r   r   r   r   r   r	   r	   r
   rX     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   )r<   r   lockedr   getrB   )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
   rF     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 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   rF   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=   rC   rN   rL   rK   rD   rB   rX   r#   rF   r?   r	   r	   r	   r
   r   '   s.   


	8-

	r   )loggingZmatplotlib.cbookr   Zmatplotlib.widgetsr   Zmatplotlib.rcsetupr   Zmatplotlib.backend_toolsrQ   rF   	getLoggerr   r   r   r   r   r   r	   r	   r	   r
   <module>   s   



	