o
    :ήc                     @   s   d dl Zd dlmZ ddlmZmZ ddgZG dd deZG dd deZ	e
d	kr`dd
lmZ ddlmZ e ZeeZej\ZZe	eZed ed gde d ed gfe_e  dS dS )    N)lines   )CanvasToolBaseToolHandlesLineToolThickLineToolc                       sv   e Zd ZdZ		d fdd	Zedd Zejdd Zd	d
 Zdd Z	dd Z
dd ZdddZedd Z  ZS )r   av  Widget for line selection in a plot.

    Parameters
    ----------
    manager : Viewer or PlotPlugin.
        Skimage viewer or plot plugin object.
    on_move : function
        Function called whenever a control handle is moved.
        This function must accept the end points of line as the only argument.
    on_release : function
        Function called whenever the control handle is released.
    on_enter : function
        Function called whenever the "enter" key is pressed.
    maxdist : float
        Maximum pixel distance allowed when selecting control handle.
    line_props : dict
        Properties for :class:`matplotlib.lines.Line2D`.
    handle_props : dict
        Marker properties for the handles (also see
        :class:`matplotlib.lines.Line2D`).

    Attributes
    ----------
    end_points : 2D array
        End points of line ((x1, y1), (x2, y2)).
    N
   c                    s   t t| j|f|||d| tddddd}	|	|d ur |ni  |	d | _|| _d | _d}
d}t	|
|g| _
tj|
|fd	d
d|	| _| j| j t| j|
||d| _| jd	 | j| jjg| _|d u rodd }|| _| j|  d S )N)on_moveon_enter
on_releaser   g?butt)color	linewidthalphasolid_capstyler   )r   r   FT)visibleanimated)marker_propsc                 S   s<   t | \}}tdt t |d t |d    d S )Nzlength = %0.2f   )np	transposeprintsqrtdiff)ptsxy r   J/tmp/pip-target-vg8gfxp4/lib/python/skimage/viewer/canvastools/linetool.pyr
   >   s    
z#LineTool.__init__.<locals>.on_enter)superr   __init__dictupdater   maxdist
_active_ptr   r   _end_ptsr   Line2D_lineaxadd_liner   _handlesset_visibleartistartistscallback_on_entermanageradd_tool)selfr1   r	   r   r
   r%   
line_propshandle_propskwargspropsr   r   	__class__r   r    r"   $   s2   

zLineTool.__init__c                 C   s   | j tS N)r'   astypeintr3   r   r   r    
end_pointsE   s   zLineTool.end_pointsc                 C   sT   t || _| jt | | jt | | j| j | 	d | 
  d S )NT)r   asarrayr'   r)   set_datar   r,   set_linewidthr   r-   redraw)r3   r   r   r   r    r>   I   s   
c                 C   sN   |j dks| j|sdS | j|j|j\}}|| jk r"|| _dS d | _dS )Nr   FT)	buttonr*   in_axesr,   closestr   r   r%   r&   )r3   eventidxpx_distr   r   r    hit_testT   s   
zLineTool.hit_testc                 C   sH   |  d | jd u r"d| _|j|j}}t||g||gg| _d S d S )NTr   )r-   r&   xdataydatar   arrayr'   )r3   rF   r   r   r   r   r    on_mouse_press_   s   

zLineTool.on_mouse_pressc                 C   s,   |j dkrd S d | _| | j |   d S Nr   )rC   r&   callback_on_releasegeometryrB   r3   rF   r   r   r    on_mouse_releasef   s
   
zLineTool.on_mouse_releasec                 C   sH   |j dks
| jd u rd S | j|sd S | |j|j | | j d S rN   )	rC   r&   r*   rD   r$   rJ   rK   callback_on_moverP   rQ   r   r   r    r	   m   s   zLineTool.on_movec                 C   s,   |d ur||f| j | jd d f< | j | _d S r:   )r'   r&   r>   )r3   r   r   r   r   r    r$   u   s   zLineTool.updatec                 C   s   | j S r:   )r>   r=   r   r   r    rP   z   s   zLineTool.geometry)NNNr   NN)NN)__name__
__module____qualname____doc__r"   propertyr>   setterrI   rM   rR   r	   r$   rP   __classcell__r   r   r8   r    r   	   s     !



c                       sF   e Zd ZdZ		d fdd	Zdd Zdd	 Zd
d Zdd Z  Z	S )r   a<  Widget for line selection in a plot.

    The thickness of the line can be varied using the mouse scroll wheel, or
    with the '+' and '-' keys.

    Parameters
    ----------
    manager : Viewer or PlotPlugin.
        Skimage viewer or plot plugin object.
    on_move : function
        Function called whenever a control handle is moved.
        This function must accept the end points of line as the only argument.
    on_release : function
        Function called whenever the control handle is released.
    on_enter : function
        Function called whenever the "enter" key is pressed.
    on_change : function
        Function called whenever the line thickness is changed.
    maxdist : float
        Maximum pixel distance allowed when selecting control handle.
    line_props : dict
        Properties for :class:`matplotlib.lines.Line2D`.
    handle_props : dict
        Marker properties for the handles (also see
        :class:`matplotlib.lines.Line2D`).

    Attributes
    ----------
    end_points : 2D array
        End points of line ((x1, y1), (x2, y2)).
    Nr   c	           	   	      s8   t t| j|||||||d |d u rdd }|| _d S )N)r	   r
   r   r%   r4   r5   c                  W   s   d S r:   r   )argsr   r   r    	on_change   s   z)ThickLineTool.__init__.<locals>.on_change)r!   r   r"   callback_on_change)	r3   r1   r	   r
   r   r\   r%   r4   r5   r8   r   r    r"      s   
zThickLineTool.__init__c                 C   s:   |j sd S |jdkr|   d S |jdkr|   d S d S )Nupdown)inaxesrC   _thicken_scan_line_shrink_scan_linerQ   r   r   r    	on_scroll   s   

zThickLineTool.on_scrollc                 C   s0   |j dkr|   d S |j dkr|   d S d S )N+-)keyra   rb   rQ   r   r   r    on_key_press   s
   

zThickLineTool.on_key_pressc                 C   s&   |  j d7  _ |   | | j d S rN   r   r$   r]   rP   r=   r   r   r    ra      s   z ThickLineTool._thicken_scan_linec                 C   s4   | j dkr|  j d8  _ |   | | j d S d S rN   rh   r=   r   r   r    rb      s
   
zThickLineTool._shrink_scan_line)NNNNr   NN)
rT   rU   rV   rW   r"   rc   rg   ra   rb   rZ   r   r   r8   r    r      s     __main__)data)ImageViewerr   )numpyr   
matplotlibr   viewer.canvastools.baser   r   __all__r   r   rT    rj   viewerrk   cameraimageshapehw	line_toolr>   showr   r   r   r    <module>   s     vJ
&