U
    Âmœdø.  ã                   @   sV   d dl mZmZmZmZ G dd„ dƒZG dd„ deƒZG dd„ dƒZG dd	„ d	ƒZd
S )é    )Ú_apiÚbackend_toolsÚcbookÚwidgetsc                   @   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   úT/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/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‡ fdd„	Z‡  ZS )ÚToolTriggerEventz/Event to inform that a tool has been triggered.Nc                    s   t ƒ  ||||¡ || _d S r   )Úsuperr   Úcanvasevent)r   r   r	   r
   r   r   ©Ú	__class__r   r   r      s    zToolTriggerEvent.__init__)NN)r   r   r   r   r   Ú__classcell__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 r   )r   r	   Úmessage)r   r   r	   r   r   r   r   r      s    z ToolManagerMessageEvent.__init__Nr   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d*dd„Z	dd„ Z
dd„ Zd+d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!d"„Zd#d$„ Zed%d&„ ƒZd-d'd(„ZdS ).Ú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   sJ   d | _ i | _i | _i | _t ¡ | _t ¡ | _	t ¡ | _
d | _|  |¡ d S r   )Ú_key_press_handler_idÚ_toolsÚ_keysÚ_toggledr   ZCallbackRegistryÚ
_callbacksr   ZLockDrawÚkeypresslockZmessagelockÚ_figureÚ
set_figure©r   Úfigurer   r   r   r   .   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(   >   s    zToolManager.canvasc                 C   s   | j S )zFigure that holds the canvas.)r$   r)   r   r   r   r'   E   s    zToolManager.figurec                 C   s   |   |¡ d S r   )r%   r&   r   r   r   r'   J   s    Tc                 C   sN   | j r| j | j ¡ || _|r0| j d| j¡| _ |rJ| j ¡ D ]
}||_q>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%   N   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_connectb   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   r8   r   Úget_tool_keymapš   s    zToolManager.get_tool_keymapc                 C   s   |   |¡D ]}| j|= q
d S r   )r<   r    )r   r   r6   r   r   r   Ú_remove_keys¬   s    zToolManager._remove_keysc              	   C   sv   || j krt|›d�ƒ‚|  |¡ t|tƒr2|g}|D ]:}|| jkrft d|› d| j| › d|› �¡ || j|< q6dS )zì
        Set the keymap to associate with the specified tool.

        Parameters
        ----------
        name : str
            Name of the Tool.
        key : str or list of str
            Keys to associate with the tool.
        z not in ToolszKey z changed from z to N)r   ÚKeyErrorr=   Ú
isinstanceÚstrr    r   Úwarn_external)r   r   Úkeyr6   r   r   r   Úupdate_keymap°   s    



ÿzToolManager.update_keymapc                 C   sx   |   |¡}tjtjj|ddd�}|dk	r.|ƒ  t|ddƒrF|  |d¡ |  |¡ t	d| |ƒ}| j
 |j|¡ | j|= dS )zƒ
        Remove tool named *name*.

        Parameters
        ----------
        name : str
            Name of the tool.
        z3.6Ztool_removed_event)ZsinceÚalternativeNÚtoggledFZtoolmanager)Úget_toolr   Zdeprecate_method_overrider   ÚToolBaseÚdestroyÚgetattrÚtrigger_toolr=   r   r"   r1   r   r   )r   r   r
   rH   r2   r   r   r   Úremove_toolÆ   s    

  þ
zToolManager.remove_toolc                 O   sð   t  t| jƒ|¡}|s&tdt|ƒ ƒ‚|| jkrDt d¡ | j| S || |f|ž|Ž}|| j|< |j	dk	rx|  
||j	¡ t|t jƒrÄ|jdkr | j dtƒ ¡ n| j |jd¡ |jrÄ|  |dd¡ | | j¡ td| |ƒ}| 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 : type
            Class of the tool to be added.  A subclass will be used
            instead if one was registered for the current canvas class.
        *args, **kwargs
            Passed to the *tool*'s 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 addedNZtool_added_event)r   Z_find_tool_classÚtyper(   Ú
ValueErrorr@   r   r   rA   Zdefault_keymaprC   r?   ÚToolToggleBaseÚradio_groupr!   Ú
setdefaultÚsetrE   Ú_handle_toggler%   r'   r   r"   r1   r   )r   r   r
   ÚargsÚkwargsZtool_clsZtool_objr2   r   r   r   Úadd_toolâ   s(    





zToolManager.add_toolc                 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 )aO  
        Toggle tools, need to untoggle prior to using other Toggle tool.
        Called from trigger_tool.

        Parameters
        ----------
        tool : `.ToolBase`
        canvasevent : Event
            Original Canvas event or None.
        data : object
            Extra data to pass to the tool when triggering.
        N)rO   r   r!   ÚremoveÚaddrJ   )r   r
   r   r   rO   rE   r   r   r   rR     s"    ýzToolManager._handle_togglec                 C   st   |   |¡}|dkrdS |dkr"| }t|tjƒ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)	rF   r?   r   rN   rR   Útriggerr   r"   r1   )r   r   r	   r   r   r
   r-   r2   r   r   r   rJ   F  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   )rB   r#   Úlockedr    ÚgetrJ   )r   r2   r   r   r   r   r*   e  s    zToolManager._key_pressc                 C   s   | j S )z,A dict mapping tool name -> controlled tool.)r   r)   r   r   r   Útoolsn  s    zToolManager.toolsc                 C   sH   t |tjƒr|j| jkr|S || jkr>|r:t 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 N)r?   r   rG   r   r   r   rA   )r   r   Úwarnr   r   r   rF   s  s    
ÿ
ÿzToolManager.get_tool)N)T)N)NNN)T)r   r   r   r   r   Úpropertyr(   r'   Úsetterr%   r/   r0   r3   r4   r<   r=   rC   rK   rU   rR   rJ   r*   r[   rF   r   r   r   r   r       s2   





	
9+
	
r   N)	Z
matplotlibr   r   r   r   r   r   r   r   r   r   r   r   Ú<module>   s   	