a
    ëÝEb¬4  ã                   @   s^   d dl mZmZmZ d dl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ÚcbookÚwidgetsNc                   @   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   ú[/home/ja/django-apps/lartica_env/lib/python3.9/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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 : `~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&   F   s    zToolManager.figurec                 C   s   |   |¡ d S r   )r$   r%   r   r   r   r&   K   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$   O   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_connectc   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u 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>ª   ó    z/ToolManager.get_tool_keymap.<locals>.<listcomp>)r   Úitems)r   r   Úkeysr   r7   r   Úget_tool_keymap›   s    zToolManager.get_tool_keymapc                 C   s   |   |¡D ]}| j|= q
d S r   )r<   r   )r   r   r5   r   r   r   Ú_remove_keys­   s    zToolManager._remove_keysc              	   C   sv   || j vrt|› d�ƒ‚|  |¡ t|tƒr2|g}|D ]:}|| jv 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   Úkeyr5   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_toolr=   r   r!   r0   r   )r   r   r	   r,   r1   r   r   r   Úremove_toolÇ   s    


zToolManager.remove_toolc                 O   s  |   |¡}|stdt|ƒ ƒ‚|| jv r<t d¡ | j| S |dkrn|tjkrntjdd|j	› d|j
› d�d� || |g|¢R i |¤Ž}|| j|< |jd	ur¨|  ||j¡ t|tjƒrö|jd	u 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 addedÚcursorz3.5zOverriding ToolSetCursor with z˜ was only necessary to provide the .set_cursor() method, which is deprecated since %(since)s and will be removed %(removal)s. Please report this to the z author.)r   N)Ú_get_cls_to_instantiateÚ
ValueErrorr@   r   r   rA   ÚtoolsZSetCursorBaseZwarn_deprecatedr   r   Zdefault_keymaprC   r?   ÚToolToggleBaseÚradio_groupr    Ú
setdefaultÚsetrD   Ú_handle_toggler$   r&   Ú_tool_added_event)r   r   r	   ÚargsÚkwargsZtool_clsZtool_objr   r   r   Úadd_toolà   s6    



ÿúÿ	



zToolManager.add_toolc                 C   s"   d}t || |ƒ}| j ||¡ d S )NZtool_added_event)r   r!   r0   )r   r	   r,   r1   r   r   r   rS   "  s    zToolManager._tool_added_eventc                 C   sž   |j }|du rH|j| jd v r2| jd  |j¡ n| jd  |j¡ dS | j| |jkr^d}n2| j| du 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)rO   r   r    ÚremoveÚaddrH   )r   r	   r   r   r
   rO   rD   r   r   r   rR   '  s"    ýzToolManager._handle_togglec                 C   sZ   t |tƒrF|tƒ v r tƒ | }n&d}t|tƒ tƒ |gdƒ}t||dƒ}t|ƒrR|S d S d S )NÚbackend_toolsé   F)r?   r@   ÚglobalsÚ
__import__ÚlocalsrG   Úcallable)r   Zcallback_classÚmodZcurrent_moduler   r   r   rK   T  s    

ÿz#ToolManager._get_cls_to_instantiatec                 C   s\   |   |¡}|du rdS |du 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)rE   Ú_trigger_toolr   r!   r0   )r   r   r   r   r
   r	   r,   r1   r   r   r   rH   e  s    
zToolManager.trigger_toolc                 C   s8   |   |¡}t|tjƒr&|  ||||¡ | |||¡ dS )zActually trigger a tool.N)rE   r?   rM   rN   rR   Útrigger)r   r   r   r   r
   r	   r   r   r   r`   �  s    
zToolManager._trigger_toolc                 C   sF   |j d u s| j ¡ rd S | j |j d ¡}|d u r4d S | j||d� d S )N)r   )rB   r"   Úlockedr   ÚgetrH   )r   r1   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   rM   •  s    zToolManager.toolsc                 C   sH   t |tjƒr|j| jv r|S || jv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?   rM   ZToolBaser   r   r   rA   )r   r   Úwarnr   r   r   rE   š  s    
zToolManager.get_tool)N)T)N)NNN)NNN)T)r   r   r   r   r   Úpropertyr'   r&   Úsetterr$   r.   r/   r2   r3   r<   r=   rC   rI   rV   rS   rR   rK   rH   r`   r)   rM   rE   r   r   r   r   r   !   s8   





	
B-

	
r   )Z
matplotlibr   r   r   Zmatplotlib.backend_toolsrY   rM   r   r   r   r   r   r   r   r   Ú<module>   s
   	