o
    :ήc                     @   s  d dl ZddlmZmZmZmZ ddlmZ d dl	Z
d dlmZ d dl	mZ d dlmZ er:d	e
  vr:ed
 g dZdadd Zd#ddZd#dd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d Zd$ddZd%dd Zd!d" ZdS )&    N   )	QtWidgetshas_qtFigureManagerQTFigureCanvasQTAgg   )warn)Figure)_pylab_helpers)LinearSegmentedColormapaggzNRecommended matplotlib backend is `Agg` for full skimage.viewer functionality.)	
init_qtappstart_qtappRequiredAttrfigimageLinearColormapClearColormapFigureCanvasnew_plotupdate_axes_imagec                   C   s    t j atdu rt g atS )zgInitialize QAppliction.

    The QApplication needs to be initialized before creating any QWidgets
    N)r   QApplicationinstanceQApp r   r   @/tmp/pip-target-vg8gfxp4/lib/python/skimage/viewer/utils/core.pyr      s   

r   c                 C   s"   | du rt  } t| dr| jS dS )z%Return True if event loop is running.N_in_event_loopF)r   hasattrr   appr   r   r   is_event_loop_running"   s
   
r   c                 C   s8   | du rt  } t| sd| _|   d| _dS d| _dS )zStart Qt mainloopNTF)r   r   r   exec_r   r   r   r   r   ,   s   

r   c                   @   s0   e Zd ZdZe Zd	ddZdd Zdd ZdS )
r   z.A class attribute that must be set before use.Nc                 C   s   || j | d f< d S N	instances)selfinit_valr   r   r   __init__=      zRequiredAttr.__init__c                 C   s"   | j | |f }|d u rtd|S )NzRequired attribute not set)r#   AttributeError)r$   objobjtypevaluer   r   r   __get__@   s   zRequiredAttr.__get__c                 C   s   || j | |f< d S r!   r"   )r$   r)   r+   r   r   r   __set__F   r'   zRequiredAttr.__set__r!   )	__name__
__module____qualname____doc__dictr#   r&   r,   r-   r   r   r   r   r   8   s    
r   c                   @   s   e Zd ZdZdd ZdS )r   aS  LinearSegmentedColormap in which color varies smoothly.

    This class is a simplification of LinearSegmentedColormap, which doesn't
    support jumps in color intensities.

    Parameters
    ----------
    name : str
        Name of colormap.

    segmented_data : dict
        Dictionary of 'red', 'green', 'blue', and (optionally) 'alpha' values.
        Each color key contains a list of `x`, `y` tuples. `x` must increase
        monotonically from 0 to 1 and corresponds to input values for a
        mappable object (e.g. an image). `y` corresponds to the color
        intensity.

    c                 K   s,   dd |  D }tj| ||fi | d S )Nc                 S   s    i | ]\}}|d d |D qS )c                 S   s   g | ]	\}}|||fqS r   r   ).0xyr   r   r   
<listcomp>^   s    z6LinearColormap.__init__.<locals>.<dictcomp>.<listcomp>r   )r3   keyr+   r   r   r   
<dictcomp>^   s    z+LinearColormap.__init__.<locals>.<dictcomp>)itemsr   r&   )r$   namesegmented_datakwargsr   r   r   r&   ]   s   zLinearColormap.__init__Nr.   r/   r0   r1   r&   r   r   r   r   r   J   s    r   c                   @   s   e Zd ZdZdddZdS )r   z7Color map that varies linearly from alpha = 0 to 1
       clear_colorc                 C   sV   |\}}}d|fd|fgd|fd|fgd|fd|fgdd|fgd}t | || d S )N        g      ?)r@   r@   )bluegreenredalpha)r   r&   )r$   rgb	max_alphar:   rgbcg_speqr   r   r   r&   f   s   

zClearColormap.__init__N)r>   r?   r=   r   r   r   r   r   c   s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )r   zCanvas for displaying images.c                 K   s8   || _ t| | j  t| tjjtjj t|  d S r!   )figr   r&   setSizePolicyr   QSizePolicy	ExpandingupdateGeometry)r$   figurer<   r   r   r   r&   q   s   zFigureCanvas.__init__c                 C   s   t | | |   d S r!   )r   resizeEventresize_event)r$   eventr   r   r   rQ   y   s   zFigureCanvas.resizeEventN)r.   r/   r0   r1   r&   rQ   r   r   r   r   r   o   s    r   c                  O   sR   t jj }|rt|d nd}|dt}|| i |}t|}t||}|j	S )zReturn a new figure canvas.r>   FigureClass)
r
   Gcffigskeysmaxpopr	   r   r   canvas)argsr<   allnumsnumrT   rP   rZ   fig_managerr   r   r   
new_canvas   s   
r_   c                 K   sB   |du ri }t di |}||  |j}|jdi |}||fS )a  Return new figure and axes.

    Parameters
    ----------
    parent : QtWidget
        Qt widget that displays the plot objects. If None, you must manually
        call ``canvas.setParent`` and pass the parent widget.
    subplot_kw : dict
        Keyword arguments passed ``matplotlib.figure.Figure.add_subplot``.
    fig_kw : dict
        Keyword arguments passed ``matplotlib.figure.Figure``.
    N)r>   r>   r>   r   )r_   	setParentrP   add_subplot)parent
subplot_kwfig_kwrZ   rK   axr   r   r   r      s   
r   r>   c           
      K   s   |dur|nt jd }|dd |dd t| j\}}}tj||ftd| | }t||d\}}	|j	d	d	d
d
d |	
  |	j| fi | |	jj  ||	fS )a/  Return figure and axes with figure tightly surrounding image.

    Unlike pyplot.figimage, this actually plots onto an axes object, which
    fills the figure. Plotting the image onto an axes allows for subsequent
    overlays of axes artists.

    Parameters
    ----------
    image : array
        image to plot
    scale : float
        If scale is 1, the figure and axes have the same dimension as the
        image.  Smaller values of `scale` will shrink the figure.
    dpi : int
        Dots per inch for figure. If None, use the default rcParam.
    Nz
figure.dpiinterpolationnearestcmapgray)dtype)figsizedpir   r>   )leftbottomrighttop)mplrcParams
setdefaultnp
atleast_3dshapearrayfloatr   subplots_adjustset_axis_offimshowrP   rZ   draw)
imagescalerl   r<   hwdrk   rK   re   r   r   r   r      s   r   c                 C   s2   |  | |jdd \}}| d||df dS )a  Update the image displayed by an image plot.

    This sets the image plot's array and updates its shape appropriately

    Parameters
    ----------
    image_axes : `matplotlib.image.AxesImage`
        Image axes to update.
    image : array
        Image array.
    Nr   r   )	set_arrayrv   
set_extent)
image_axesr}   r   r   r   r   r   r      s   
r   r!   )NN)r>   N) numpyrt   qtr   r   r   r   _shared.utilsr   
matplotlibrq   matplotlib.figurer	   r
   matplotlib.colorsr   get_backendlower__all__r   r   r   r   objectr   r   r   r   r_   r   r   r   r   r   r   r   <module>   s,    




!