U
    Pe2@                     @   s   d Z dZddlmZ ddlmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZmZ ddlmZ daG d	d
 d
eZG dd deZdS )a0  
Image
=====

The :class:`Image` widget is used to display an image::

Example in python::

    wimg = Image(source='mylogo.png')

Kv Example::

    Image:
        source: 'mylogo.png'
        size: self.texture_size


Asynchronous Loading
--------------------

To load an image asynchronously (for example from an external webserver), use
the :class:`AsyncImage` subclass::

    aimg = AsyncImage(source='http://mywebsite.com/logo.png')

This can be useful as it prevents your application from waiting until the image
is loaded. If you want to display large images or retrieve them from URL's,
using :class:`AsyncImage` will allow these resources to be retrieved on a
background thread without blocking your application.

Alignment
---------

By default, the image is centered inside the widget bounding box.

Adjustment
----------

To control how the image should be adjusted to fit inside the widget box, you
should use the :attr:`~kivy.uix.image.Image.fit_mode` property. Available
options include:

- ``"scale-down"``: maintains aspect ratio without stretching.
- ``"fill"``: stretches to fill widget, may cause distortion.
- ``"contain"``: maintains aspect ratio and resizes to fit inside widget.
- ``"cover"``: maintains aspect ratio and stretches to fill widget, may clip
image.

For more details, refer to the :attr:`~kivy.uix.image.Image.fit_mode`.


You can also inherit from Image and create your own style. For example, if you
want your image to be greater than the size of your widget, you could do::

    class FullImage(Image):
        pass

And in your kivy language file::

    <-FullImage>:
        canvas:
            Color:
                rgb: (1, 1, 1)
            Rectangle:
                texture: self.texture
                size: self.width + 20, self.height + 20
                pos: self.x - 10, self.y - 10

)Image
AsyncImage    )Widget)r   )resource_find)StringPropertyObjectPropertyListPropertyAliasPropertyBooleanPropertyNumericPropertyColorPropertyOptionProperty)LoggerNc                       s   e Zd ZdZedZedddZeddgZ	dd Z
edZee
d	dd
ZeddddgZedddZedddZedddddgdZedZedZedZedZdd Zeeddd
Z 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' Z$d(d) Z%d*d+ Z&  Z'S ),r   z;Image class, see module documentation for more information.NT)Z	allownoner   c                 C   s   | j r| j jt| j j S dS )Ng      ?)texturewidthfloatheightself r   2/tmp/pip-unpacked-wheel-xzebddm3/kivy/uix/image.pyget_image_ratio}   s    zImage.get_image_ratioF)r   )bindcache   )
deprecated
scale-downfillcontaincover)optionsg      ?c           	      C   s   | j st| jS | j}| j\}}| j j\}}| jdkrn|td| }||kr\||| | gS || | |gS n,| jdkr||gS | jdkr|}n
t||}|| }||kr| jdkr|}n
t||}|| }||gS )Nr   r   r   r   )r   listsizeimage_ratiofit_modemaxmin)	r   ZratiowhZtwthZwidget_ratioiwZihr   r   r   get_norm_image_size  s,    







zImage.get_norm_image_size)r   r"   r#   r$   c                    sV   d | _ d| _| j}| j}|d| |d| |d| j |d| j t jf | d S )Nr   sourcemipmap
keep_ratioallow_stretch)
_coreimage_loopstexture_updatefbind_update_fit_modesuper__init__)r   kwargsupdater3   	__class__r   r   r6   A  s    

zImage.__init__c                 G   sF   | j }| j}|s|r|r$|s$d| _n|s4|r4d| _n|rB|rBd| _d S )Nr   r   r   )r.   r/   r$   )r   argsr.   r/   r   r   r   r4   N  s    zImage._update_fit_modec                 G   s   |  | j d S N)set_texture_from_resourcer,   r   largsr   r   r   r2   [  s    zImage.texture_updatec                 C   s   |s|    d S t|}|s6td|  |    d S | jrL| jj| jd z$t|| j| j	| j
| jd | _}W n0 tk
r   td|  |    | j}Y nX |r|j| jd |j| _d S )NzImage: Not found <%s>
on_texture)r-   
anim_delay	keep_datanocachezImage: Error loading <%s>)_clear_core_imager   r   errorr0   unbind_on_tex_change	CoreImager-   rB   rC   rD   	Exceptionr   r   )r   resourcer,   imager   r   r   r=   ^  s2    zImage.set_texture_from_resourcec                 C   s.   | j d krd S || j _|dk r*| j d d S )Nr   F)r0   rB   
anim_resetr   instancevaluer   r   r   on_anim_delayy  s
    
zImage.on_anim_delayc                 C   s   |r
|j nddg| _d S )Nr   )r"   texture_sizerN   r   r   r   rA     s    zImage.on_texturec                 C   s,   | j r| j j| jd d | _d | _ d| _d S )Nr@   r   )r0   rG   rH   r   r1   r   r   r   r   rE     s
    zImage._clear_core_imagec                 G   sZ   | j j| _| j }| jrV|jt|jjd krV|  jd7  _| j| jkrV|d d| _d S )Nr   Fr   )	r0   r   	anim_loopZ_anim_indexlenZ_imageZtexturesr1   rM   )r   r?   cir   r   r   rH     s    

zImage._on_tex_changec                 C   s   |    | j}d| _|| _dS )aA  Reload image from disk. This facilitates re-loading of
        images from disk in case the image content changes.

        .. versionadded:: 1.3.0

        Usage::

            im = Image(source = '1.jpg')
            # -- do something --
            im.reload()
            # image will be re-loaded from disk

         N)remove_from_cacher,   )r   Z
old_sourcer   r   r   reload  s    zImage.reloadc                 C   s   | j r| j   dS )zBRemove image from cache.

        .. versionadded:: 2.0.0
        N)r0   rW   r   r   r   r   rW     s    zImage.remove_from_cachec                 G   s    | j r|   | jrd| j_d S )NT)rD   rW   r0   Z_nocacher   r;   r   r   r   
on_nocache  s    zImage.on_nocache)(__name__
__module____qualname____doc__r   r,   r   r   r   rR   r   r
   r-   r	   r#   r   colorr/   r.   r   r$   rC   r   rB   rS   rD   r+   Znorm_image_sizer6   r4   r2   r=   rQ   rA   rE   rH   rX   rW   rZ   __classcell__r   r   r9   r   r   [   sF   
 
#

	!
r   c                       s~   e Zd ZdZdZ fddZdd Zdd Zdd
dZdd Z	dd Z
dd Z fddZdd Zdd Z fddZ  ZS )r   a0  Asynchronous Image class. See the module documentation for more
    information.

    .. note::

        The AsyncImage is a specialized form of the Image class. You may
        want to refer to the :mod:`~kivy.loader` documentation and in
        particular, the :class:`~kivy.loader.ProxyImage` for more detail
        on how to handle events around asynchronous image loading.

    .. note::

        AsyncImage currently does not support properties
        :attr:`anim_loop` and :attr:`mipmap` and setting those properties will
        have no effect.
    )on_erroron_loadc                    s<   d | _ d | _tsddlma | d| j t jf | d S )Nr   )Loaderr,   )_found_sourcer0   rc   Zkivy.loaderr3   _load_sourcer5   r6   )r   r7   r9   r   r   r6     s    zAsyncImage.__init__c                 G   s   | j }|s|   d S | |sHt|}|sHtd| j   |   d S || _tj|| j	| j
| jd | _}|j| j| j| jd |j| _d S )NzAsyncImage: Not found <%s>)rD   r-   rB   )rb   ra   rA   )r,   rE   is_urir   r   rF   rd   rc   rL   rD   r-   rB   r0   r   _on_source_load_on_source_errorrH   r   )r   r;   r,   rL   r   r   r   re     s.    
zAsyncImage._load_sourcec                 C   s&   | j j}|sd S |j| _| d d S )Nrb   )r0   rL   r   dispatch)r   rP   rL   r   r   r   rg     s
    zAsyncImage._on_source_loadNc                 C   s   |  d| d S )Nra   )ri   )r   rO   rF   r   r   r   rh     s    zAsyncImage._on_source_errorc                 C   s   d S r<   r   )r   rF   r   r   r   ra     s    zAsyncImage.on_errorc                 G   s   d S r<   r   rY   r   r   r   rb     s    zAsyncImage.on_loadc                 C   s   | ddd }|dkS )Nz://r   r   )httphttpsftpsmb)split)r   filenameprotor   r   r   rf     s    zAsyncImage.is_uric                    s*   | j r| j j| jd t   d | _d S )N)rb   )r0   rG   rg   r5   rE   rd   r   r9   r   r   rE      s    
zAsyncImage._clear_core_imagec                 G   s   | j r| j j| _d S r<   )r0   r   r>   r   r   r   rH     s    zAsyncImage._on_tex_changec                 G   s   d S r<   r   r>   r   r   r   r2   
  s    zAsyncImage.texture_updatec                    s    | j rt| j  t   d S r<   )rd   rc   rW   r5   r   r9   r   r   rW     s    zAsyncImage.remove_from_cache)N)r[   r\   r]   r^   Z
__events__r6   re   rg   rh   ra   rb   rf   rE   rH   r2   rW   r`   r   r   r9   r   r     s   	
r   )r^   __all__Zkivy.uix.widgetr   Zkivy.core.imager   rI   Zkivy.resourcesr   Zkivy.propertiesr   r   r   r	   r
   r   r   r   Zkivy.loggerr   rc   r   r   r   r   r   <module>   s   E(
  ]