3
$ßL]‚+  ã               @   sp   d Z ddlZddlmZ ddljZejeƒZ	G dd„ de
ƒZG dd„ deƒZG dd	„ d	eƒZG d
d„ deƒZdS )aÜ  
This provides several 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 `waitforbuttonpress`.

`BlockingMouseInput`
    Creates a callable object to retrieve mouse clicks in a blocking way for
    interactive sessions.  Used by `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 `clabel`.
é    N)ÚIntegralc               @   sT   e Zd ZdZf f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   úA/tmp/pip-install-edjban26/matplotlib/matplotlib/blocking_input.pyÚ__init__!   s    zBlockingInput.__init__c             C   sT   | j |ƒ tjdt| jƒƒ | jƒ  t| jƒ| j  ko>dkn  rP| jjj	ƒ  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   5   s    zBlockingInput.post_eventc             C   s(   x| j D ]}| jjj|ƒ qW g | _ dS )zDisconnect all callbacks.N)Ú	callbacksr   r   Zmpl_disconnect)r   Úcbr   r   r   Úcleanup8   s    zBlockingInput.cleanupc             C   s   | j j|ƒ dS )z5For base class, this just appends an event to events.N)r   Úappend)r   r   r   r   r   r
   >   s    zBlockingInput.add_eventé   c             C   s   | j j|ƒ dS )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_eventB   s    zBlockingInput.pop_eventé   c          
      sr   t |tƒstdƒ‚|ˆ _g ˆ _tˆ jjdƒr6ˆ jjƒ  ‡ fdd„ˆ j	D ƒˆ _
zˆ jjj|d� W dˆ jƒ  X ˆ jS )z%Blocking call to retrieve *n* events.zRequires an integer argumentÚmanagerc                s   g | ]}ˆ j jj|ˆ jƒ‘qS r   )r   r   Zmpl_connectr   )Ú.0Úname)r   r   r   ú
<listcomp>X   s   z*BlockingInput.__call__.<locals>.<listcomp>)ÚtimeoutN)Ú
isinstancer   Ú
ValueErrorr   r   Úhasattrr   r   Úshowr   r   Zstart_event_loopr   )r   r   r"   r   )r   r   Ú__call__M   s    



zBlockingInput.__call__Néÿÿÿÿ)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dZdZdZd 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$dd„Zd%dd„Zd&dd„Zd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 like mouse button 3, enter is like mouse button 2
    and all others are like mouse button 1.
    r   é   é   c             C   s&   t j| |dd� || _|| _|| _d S )NÚbutton_press_eventÚkey_press_event)r   r   )r0   r1   )r   r	   Ú
button_addÚ
button_popÚbutton_stop)r   r   Z	mouse_addZ	mouse_popZ
mouse_stopr   r   r   r	   q   s     zBlockingMouseInput.__init__c             C   s@   t | jƒdkrtjdƒ n"| jd jdkr4| jƒ  n| jƒ  dS )zProcess an event.r   zNo events yetr   r1   Nr(   )r   r   r   Úwarningr    Ú	key_eventÚmouse_event)r   r   r   r   r   y   s
    
zBlockingMouseInput.post_eventc             C   sT   | j d }|j}|| jkr&| j|ƒ n*|| jkr<| j|ƒ n|| jkrP| j|ƒ dS )zProcess a mouse click event.r   Nr(   )r   Úbuttonr3   Úmouse_event_popr4   Úmouse_event_stopr2   Úmouse_event_add)r   r   r8   r   r   r   r7   ‚   s    



zBlockingMouseInput.mouse_eventc             C   sX   | j d }|jdkrdS |jjƒ }|dkr6| j|ƒ n|d	krJ| j|ƒ n
| j|ƒ dS )
zV
        Process a key press event, mapping keys to appropriate mouse clicks.
        r   NÚ	backspaceÚdeleteÚescapeÚenterr(   )r<   r=   )r>   r?   )r   ÚkeyÚlowerr9   r:   r;   )r   r   r@   r   r   r   r6   �   s    


zBlockingMouseInput.key_eventc             C   s    |j r| j|ƒ n
tj| ƒ 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   r;   �   s    z"BlockingMouseInput.mouse_event_addc             C   s   t j| ƒ | j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   r:   ª   s    	
z#BlockingMouseInput.mouse_event_stopc             C   s   t j| ƒ | jr| j|ƒ 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   r9   ¹   s    	
z"BlockingMouseInput.mouse_event_popc             C   sv   | j j|j|jfƒ tjdt| j ƒ|j|jƒ | jrrtj	|jg|jgddd�}|j
j|ƒ | jj|ƒ | jjjƒ  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)ÚmarkerÚcolorN)Úclicksr   ZxdataZydatar   r   r   Úshow_clicksÚmlinesZLine2DrB   Zadd_lineÚmarksr   r   Údraw)r   r   Úliner   r   r   rC   Ç   s    
zBlockingMouseInput.add_clickc             C   s2   | j j|ƒ | jr.| jj|ƒjƒ  | jjjƒ  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             C   s   | j ||ƒ tj| |ƒ dS )zx
        Removes a click and the associated event from the list of clicks.

        Defaults to the last click.
        N)rO   r   r   )r   r   r   r   r   r   r   ç   s    zBlockingMouseInput.popNc             C   s>   | j r0x| jD ]}|jƒ  qW g | _| jjjƒ  tj| ƒ dS )z{
        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`, optional
            Not used
        N)rI   rK   rN   r   r   rL   r   r   )r   r   Úmarkr   r   r   r   ð   s    zBlockingMouseInput.cleanupr   Tc             C   s(   || _ g | _g | _tj| ||d� | jS )zV
        Blocking call to retrieve *n* coordinate pairs through mouse clicks.
        )r   r"   )rI   rH   rK   r   r'   )r   r   r"   rI   r   r   r   r'      s
    zBlockingMouseInput.__call__)r   r.   r/   r(   )r(   r(   )r(   )N)r   r   T)r)   r*   r+   r,   r2   r3   r4   r	   r   r7   r6   r;   r:   r9   rC   rO   r   r   r'   r   r   r   r   r-   d   s    
	

	
r-   c               @   sD   e Zd ZdZdd„ Zdd„ Zddd„Zd	d
„ Zdd„ Zddd„Z	dS )ÚBlockingContourLabelerzt
    Callable for retrieving mouse clicks and key presses in a blocking way.

    Used to place contour labels.
    c             C   s   || _ tj| |jjd� d S )N)r   )Úcsr-   r	   ÚaxÚfigure)r   rR   r   r   r   r	     s    zBlockingContourLabeler.__init__c             C   s   | j |ƒ d S )N)Úbutton1)r   r   r   r   r   rC     s    z BlockingContourLabeler.add_clickr   c             C   s   | j |ƒ d S )N)Úbutton3)r   r   r   r   r   r   rO     s    z BlockingContourLabeler.pop_clickc             C   sH   |j | jjkr:| jj|j|j| j| jdd� | jj	j
ƒ  n
tj| ƒ dS )z›
        Process an button-1 event (add a label to a contour).

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        F)Úinline_spacingZ	transformN)rB   rR   rS   Zadd_label_nearÚxÚyÚinlinerW   r   r   rL   r   r   )r   r   r   r   r   rU     s    	zBlockingContourLabeler.button1c             C   s&   | j rn| jjƒ  | 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)rZ   rR   Z	pop_labelrS   rT   r   rL   )r   r   r   r   r   rV   -  s    
zBlockingContourLabeler.button3é   c             C   s"   || _ || _tj| ||dd� d S )NF)r   r"   rI   )rZ   rW   r-   r'   )r   rZ   rW   r   r"   r   r   r   r'   ?  s    
zBlockingContourLabeler.__call__Nr(   )r(   r(   r(   )r[   r(   r(   )
r)   r*   r+   r,   r	   rC   rO   rU   rV   r'   r   r   r   r   rQ     s   
rQ   c               @   s*   e Zd ZdZdd„ Zdd„ Zd
dd„Zd	S )ÚBlockingKeyMouseInputzQ
    Callable for retrieving mouse clicks and key presses in a blocking way.
    c             C   s   t j| |dd� d S )Nr0   r1   )r   r   )r0   r1   )r   r	   )r   r   r   r   r   r	   K  s    zBlockingKeyMouseInput.__init__c             C   s(   | j r| j d jdk| _n
tjdƒ dS )zDetermine if it is a key event.r   r1   zNo events yet.Nr(   )r   r    Ú
keyormouser   r5   )r   r   r   r   r   O  s    z BlockingKeyMouseInput.post_eventr   c             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   )r   r"   )r]   r   r'   )r   r"   r   r   r   r'   V  s    zBlockingKeyMouseInput.__call__N)r   )r)   r*   r+   r,   r	   r   r'   r   r   r   r   r\   F  s   r\   )r,   ÚloggingZnumbersr   Zmatplotlib.linesÚlinesrJ   Ú	getLoggerr)   r   Úobjectr   r-   rQ   r\   r   r   r   r   Ú<module>   s   

F (;