o
    :a+                     @   s   d Z 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jdedd eeZG d	d
 d
ZG dd deZG dd deZG dd deZdS )a  
Classes used for blocking interaction with figure windows:

`BlockingInput`
    Creates a callable object to retrieve events in a blocking way for
    interactive sessions.  Base class of the other classes listed here.

`BlockingKeyMouseInput`
    Creates a callable object to retrieve key or mouse clicks in a blocking
    way for interactive sessions.  Used by `~.Figure.waitforbuttonpress`.

`BlockingMouseInput`
    Creates a callable object to retrieve mouse clicks in a blocking way for
    interactive sessions.  Used by `~.Figure.ginput`.

`BlockingContourLabeler`
    Creates a callable object to retrieve mouse clicks in a blocking way that
    will then be used to place labels on a `.ContourSet`.  Used by
    `~.Axes.clabel`.
    N)Integral)_api)MouseButtonz3.5module)nameZobj_typec                   @   sR   e Zd ZdZdddZdd Zdd Zd	d
 Zdd ZdddZ	e	Z
dddZdS )BlockingInputz1Callable for retrieving events in a blocking way. c                 C   s   || _ || _d S Nfig
eventslist)selfr   r   r   r   ;/usr/lib/python3/dist-packages/matplotlib/blocking_input.py__init__$   s   
zBlockingInput.__init__c                 C   sZ   |  | tdt| j |   t| j| j  kr dkr+n dS | jj	  dS dS )zY
        Event handler; will be passed to the current figure to retrieve events.
        zEvent %ir   N)
	add_event_loginfolenevents
post_eventnr   canvasstop_event_loopr   eventr   r   r   on_event(   s   
"zBlockingInput.on_eventc                 C   s   dS )z-For baseclass, do nothing but collect events.Nr   r   r   r   r   r   8   s    zBlockingInput.post_eventc                 C   s$   | j D ]	}| jj| qg | _ dS )zDisconnect all callbacks.N)	callbacksr   r   Zmpl_disconnect)r   cbr   r   r   cleanup;   s   

zBlockingInput.cleanupc                 C      | j | dS )z5For base class, this just appends an event to events.N)r   appendr   r   r   r   r   A   s   zBlockingInput.add_eventc                 C   r    )z
        Remove an event from the event list -- by default, the last.

        Note that this does not check that there are events, much like the
        normal pop method.  If no events exist, this will throw an exception.
        N)r   pop)r   indexr   r   r   	pop_eventE   s   zBlockingInput.pop_event      c                    sp   t jt|d | _g  _ jjjr j   fdd j	D  _
z jjj|d W     jS    w )z%Blocking call to retrieve *n* events.)r   c                    s   g | ]} j j| jqS r   )r   r   Zmpl_connectr   ).0r   r   r   r   
<listcomp>Z   s    z*BlockingInput.__call__.<locals>.<listcomp>)timeout)r   Zcheck_isinstancer   r   r   r   r   ZmanagerZshowr   r   Zstart_event_loopr   )r   r   r*   r   r   r   __call__P   s   



zBlockingInput.__call__N)r   r"   )r&   r'   )__name__
__module____qualname____doc__r   r   r   r   r   r%   r#   r+   r   r   r   r   r   !   s    

	r   c                       s   e Zd ZdZejZejZej	Z
ejejej	f f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dZd fdd	Zd  fdd	Zd! fdd	Z  ZS )"BlockingMouseInputa  
    Callable for retrieving mouse clicks in a blocking way.

    This class will also retrieve keypresses and map them to mouse clicks:
    delete and backspace are a right click, enter is like a middle click,
    and all others are like a left click.
    c                    s&   t  j|dd || _|| _|| _d S N)Zbutton_press_eventkey_press_eventr
   )superr   
button_add
button_popbutton_stop)r   r   Z	mouse_addZ	mouse_popZ
mouse_stop	__class__r   r   r   s   s   
zBlockingMouseInput.__init__c                 C   sD   t | jdkrtd dS | jd jdkr|   dS |   dS )zProcess an event.r   zNo events yetr"   r3   N)r   r   r   warningr   	key_eventmouse_eventr   r   r   r   r   }   s
   zBlockingMouseInput.post_eventc                 C   s\   | j d }|j}|| jkr| | dS || jkr | | dS || jkr,| | dS dS )zProcess a mouse click event.r"   N)r   buttonr6   mouse_event_popr7   mouse_event_stopr5   mouse_event_add)r   r   r=   r   r   r   r<      s   



zBlockingMouseInput.mouse_eventc                 C   sV   | j d }|jdu rdS |jdv r| | dS |jdv r$| | dS | | dS )zV
        Process a key press event, mapping keys to appropriate mouse clicks.
        r"   N)Z	backspacedelete)escapeZenter)r   keyr>   r?   r@   r   r   r   r   r;      s   



zBlockingMouseInput.key_eventc                 C   s"   |j r
| | dS t|  dS )z
        Process an button-1 event (add a click if inside axes).

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        N)inaxes	add_clickr   r#   r   r   r   r   r@      s   z"BlockingMouseInput.mouse_event_addc                 C   s   t |  | jj  dS )z
        Process an button-2 event (end blocking input).

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        N)r   r#   r   r   r   r   r   r   r   r?      s   
	z#BlockingMouseInput.mouse_event_stopc                 C   s"   t |  | jr| | dS dS )z
        Process an button-3 event (remove the last click).

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        N)r   r#   r   r   r   r   r   r>      s   
	z"BlockingMouseInput.mouse_event_popc                 C   sz   | j |j|jf tdt| j |j|j | jr;tj	|jg|jgddd}|j
| | j| | jj  dS dS )z
        Add the coordinates of an event to the list of clicks.

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        zinput %i: %f, %f+r)ZmarkerZcolorN)clicksr!   ZxdataZydatar   r   r   show_clicksmlinesZLine2DrD   Zadd_linemarksr   r   draw)r   r   liner   r   r   rE      s   zBlockingMouseInput.add_clickr"   c                 C   s6   | j | | jr| j|  | jj  dS dS )z
        Remove a click (by default, the last) from the list of clicks.

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        N)rH   r#   rI   rK   remover   r   rL   r   r   r$   r   r   r   	pop_click   s
   zBlockingMouseInput.pop_clickc                    s   |  || t | dS )zw
        Remove a click and the associated event from the list of clicks.

        Defaults to the last click.
        N)rP   r4   r#   rO   r8   r   r   r#      s   zBlockingMouseInput.popNc                    s:   | j r| jD ]}|  qg | _| jj  t   dS )z{
        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`, optional
            Not used
        N)rI   rK   rN   r   r   rL   r4   r   )r   r   Zmarkr8   r   r   r      s   

zBlockingMouseInput.cleanupr&   r'   Tc                    s(   || _ g | _g | _t j||d | jS )zV
        Blocking call to retrieve *n* coordinate pairs through mouse clicks.
        r   r*   )rI   rH   rK   r4   r+   )r   r   r*   rI   r8   r   r   r+     s
   zBlockingMouseInput.__call__r,   r	   )r&   r'   T)r-   r.   r/   r0   r   ZLEFTr5   ZRIGHTr6   ZMIDDLEr7   r   r   r<   r;   r@   r?   r>   rE   rP   r#   r   r+   __classcell__r   r   r8   r   r1   f   s(    
	
	r1   c                       sP   e Zd ZdZ fddZdd ZdddZd	d
 Zdd Zd fdd	Z	  Z
S )BlockingContourLabelerzt
    Callable for retrieving mouse clicks and key presses in a blocking way.

    Used to place contour labels.
    c                    s   || _ t j|jjd d S )N)r   )csr4   r   Zaxesfigure)r   rT   r8   r   r   r     s   zBlockingContourLabeler.__init__c                 C      |  | d S r	   )button1r   r   r   r   rE        z BlockingContourLabeler.add_clickr"   c                 C   rV   r	   )button3rO   r   r   r   rP     rX   z BlockingContourLabeler.pop_clickc                 C   sJ   |j | jjkr| jj|j|j| j| jdd | jj	
  dS t|  dS )z
        Process an button-1 event (add a label to a contour).

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        F)inline_spacingZ	transformN)rD   rT   axZadd_label_nearxyinlinerZ   r   r   rL   r   r#   r   r   r   r   rW     s   	zBlockingContourLabeler.button1c                 C   s(   | j rdS | j  | jjjj  dS )a  
        Process an button-3 event (remove a label if not in inline mode).

        Unfortunately, if one is doing inline labels, then there is currently
        no way to fix the broken contour - once humpty-dumpty is broken, he
        can't be put back together.  In inline mode, this does nothing.

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        N)r^   rT   Z	pop_labelr[   rU   r   rL   r   r   r   r   rY   0  s   
zBlockingContourLabeler.button3   c                    s"   || _ || _t j||dd d S )NF)r   r*   rI   )r^   rZ   r4   r+   )r   r^   rZ   r   r*   r8   r   r   r+   B  s   zBlockingContourLabeler.__call__r,   )r_   r"   r"   )r-   r.   r/   r0   r   rE   rP   rW   rY   r+   rR   r   r   r8   r   rS     s    
rS   c                       s6   e Zd ZdZ fddZdd Zd	 fdd	Z  ZS )
BlockingKeyMouseInputzQ
    Callable for retrieving mouse clicks and key presses in a blocking way.
    c                    s   t  j|dd d S r2   )r4   r   )r   r   r8   r   r   r   M  s   
zBlockingKeyMouseInput.__init__c                 C   s*   | j r| j d jdk| _dS td dS )zDetermine if it is a key event.r"   r3   zNo events yet.N)r   r   
keyormouser   r:   r   r   r   r   r   Q  s   z BlockingKeyMouseInput.post_eventr'   c                    s   d| _ t jd|d | j S )z
        Blocking call to retrieve a single mouse click or key press.

        Returns ``True`` if key press, ``False`` if mouse click, or ``None`` if
        timed out.
        Nr&   rQ   )ra   r4   r+   )r   r*   r8   r   r   r+   X  s   zBlockingKeyMouseInput.__call__)r'   )r-   r.   r/   r0   r   r   r+   rR   r   r   r8   r   r`   H  s
    r`   )r0   ZloggingZnumbersr   Z
matplotlibr   Zmatplotlib.backend_basesr   Zmatplotlib.lineslinesrJ   Zwarn_deprecatedr-   Z	getLoggerr   r   r1   rS   r`   r   r   r   r   <module>   s    
E ):