B
    ]t\h+                 @   sr   d Z ddlZddlmZ ddlmZ e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               @   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   8lib/python3.7/site-packages/matplotlib/blocking_input.py__init__!   s    zBlockingInput.__init__c             C   sT   |  | tdt| j |   t| j| j  kr@dkrPn n| 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| 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 | 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_eventc             C   s   | 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   fdd j	D  _
z jjj|d W d   X  jS )z%Blocking call to retrieve *n* events.zRequires an integer argumentZmanagerc                s   g | ]} 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   Zshowr   r   Zstart_event_loopr   )r   r   r"   r   )r   r   __call__M   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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   )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d n"| jd jdkr4|   n|   dS )zProcess an event.r   zNo events yetr   r/   N)r   r   r   warningr    	key_eventmouse_event)r   r   r   r   r   y   s
    
zBlockingMouseInput.post_eventc             C   sJ   | j d }|j}|| jkr&| | n || jkr<| | n
| | dS )zProcess a mouse click event.r   N)r   buttonr1   mouse_event_popr2   mouse_event_stopmouse_event_add)r   r   r6   r   r   r   r5      s    


zBlockingMouseInput.mouse_eventc             C   sX   | j d }|jdkrdS |j }|dkr6| | n|dkrJ| | n
| | dS )zV
        Process a key press event, mapping keys to appropriate mouse clicks.
        r   N)Z	backspacedelete)escapeZenter)r   keylowerr7   r8   r9   )r   r   r<   r   r   r   r4      s    


zBlockingMouseInput.key_eventc             C   s    |j r| | n
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   r9      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   r8      s    	
z#BlockingMouseInput.mouse_event_stopc             C   s   t |  | jr| | 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   r7      s    	
z"BlockingMouseInput.mouse_event_popc             C   sv   | j |j|jf tdt| j |j|j | jrrtj	|jg|jgddd}|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)ZmarkerZcolorN)clicksr   ZxdataZydatar   r   r   show_clicksmlinesZLine2Dr>   Zadd_linemarksr   r   draw)r   r   liner   r   r   r?      s    
zBlockingMouseInput.add_clickr   c             C   s2   | j | | jr.| j|  | jj  dS )z
        Remove a click (by default, the last) from the list of clicks.

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        N)rB   r   rC   rE   remover   r   rF   )r   r   r   r   r   r   	pop_click   s    zBlockingMouseInput.pop_clickc             C   s   |  || t| | dS )zx
        Removes a click and the associated event from the list of clicks.

        Defaults to the last click.
        N)rI   r   r   )r   r   r   r   r   r   r      s    zBlockingMouseInput.popNc             C   s>   | j r0x| jD ]}|  qW g | _| jj  t|  dS )z{
        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`, optional
            Not used
        N)rC   rE   rH   r   r   rF   r   r   )r   r   Z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"   )rC   rB   rE   r   r&   )r   r   r"   rC   r   r   r   r&      s
    zBlockingMouseInput.__call__)r   r,   r-   )r   )r   )N)r   r   T)r'   r(   r)   r*   r0   r1   r2   r	   r   r5   r4   r9   r8   r7   r?   rI   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   rK   r   r   r   r	     s    zBlockingContourLabeler.__init__c             C   s   |  | d S )N)button1)r   r   r   r   r   r?     s    z BlockingContourLabeler.add_clickr   c             C   s   |  | d S )N)button3)r   r   r   r   r   r   rI     s    z BlockingContourLabeler.pop_clickc             C   sH   |j | jjkr:| jj|j|j| j| jdd | jj	
  n
t|  dS )z
        Process an button-1 event (add a label to a contour).

        Parameters
        ----------
        event : `~.backend_bases.MouseEvent`
        F)inline_spacingZ	transformN)r>   rK   rL   Zadd_label_nearxyinlinerP   r   r   rF   r   r   )r   r   r   r   r   rN     s    	zBlockingContourLabeler.button1c             C   s&   | j rn| 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)rS   rK   Z	pop_labelrL   rM   r   rF   )r   r   r   r   r   rO   -  s    
zBlockingContourLabeler.button3   c             C   s"   || _ || _tj| ||dd d S )NF)r   r"   rC   )rS   rP   r+   r&   )r   rS   rP   r   r"   r   r   r   r&   ?  s    
zBlockingContourLabeler.__call__N)r   )rT   r   r   )
r'   r(   r)   r*   r	   r?   rI   rN   rO   r&   r   r   r   r   rJ     s   
rJ   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 )N)r.   r/   )r   r   )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d dS )zDetermine if it is a key event.r   r/   zNo events yet.N)r   r    
keyormouser   r3   )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"   )rV   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   rU   F  s   rU   )r*   ZloggingZnumbersr   Zmatplotlib.lineslinesrD   Z	getLoggerr'   r   objectr   r+   rJ   rU   r   r   r   r   <module>   s   
F (;