3
8ßL]ü(  ã               @   sx   d Z ddlmZ ddlmZmZmZ ddlmZ ddl	m
Z
 ddlmZ ddlmZ G d	d
„ d
eƒZG dd„ deƒZdS )z( Traits UI 'display only' image editor.
é    )Úabsolute_import)ÚQFrameÚQPainterÚQPalette)ÚImageResource)Úconvert_bitmap)ÚImageEditoré   )ÚEditorc                   s’   e Zd ZdZd‡ fdd„	Zdd„ Z‡ f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 ) Ú
QImageViewz  A custom QFrame that will paint a QPixmap as an image. The
    api is similar to QLabel, but with a few more options to control
    how the image scales.

    Nc                s>   t t| ƒj|ƒ d| _d| _d| _d| _d| _| jt	j
ƒ dS )z© Initialize a QImageView.

        Parameters
        ----------
        parent : QWidget or None, optional
            The parent widget of this image viewer.

        NF)Úsuperr   Ú__init__Ú_pixmapÚ_scaled_contentsÚ_allow_upscalingÚ_preserve_aspect_ratioÚ_allow_clippingZsetBackgroundRoler   ZWindow)ÚselfÚparent)Ú	__class__© ú?/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/image_editor.pyr   5   s    	zQImageView.__init__c             C   s‚  | j }|dkrdS |jƒ }|jƒ }|jƒ }|dks:|dkr>dS | jƒ jƒ }| jƒ jƒ }| jsštdt|d |d  ƒƒ}tdt|d |d  ƒƒ}	|}
|}n¾| j�r
t|ƒ| }t|ƒ| }||kræ| j	rÎ|}n
t
||ƒ}t|| ƒ}
n"| j	rò|}
n
t
||ƒ}
t|
| ƒ}n&| j	�r|}|}
nt
||ƒ}t
||ƒ}
t|d |
d  ƒ}t|d |d  ƒ}	t| ƒ}|jtjƒ |j||	|
||ƒ dS )zp A custom paint event handler which draws the image according
        to the current size constraints.

        Nr   g       @)r   ÚsizeÚwidthÚheightr   ÚmaxÚintr   Úfloatr   Úminr   ZsetRenderHintZSmoothPixmapTransformZ
drawPixmap)r   ÚeventÚpixmapZpm_sizeZpm_widthZ	pm_heightr   r   Zpaint_xZpaint_yZpaint_widthZpaint_heightZpm_ratioZratioZpainterr   r   r   Ú
paintEventJ   sJ    



zQImageView.paintEventc                s$   | j }|dk	r|jƒ S tt| ƒjƒ S )za Returns a appropriate size hint for the image based on the
        underlying QPixmap.

        N)r   r   r   r   ÚsizeHint)r   r    )r   r   r   r"   ‰   s    zQImageView.sizeHintc                s4   | j }|dk	r&| j r&| j r&|jƒ S tt| ƒjƒ S )zi Returns a appropriate minimum size hint for the image based on the
        underlying QPixmap.

        N)r   r   r   r   r   r   r"   )r   r    )r   r   r   ÚminimumSizeHint“   s    zQImageView.minimumSizeHintc             C   s   | j S )z< Returns the underlying pixmap for the image view.

        )r   )r   r   r   r   r    �   s    zQImageView.pixmapc             C   s   || _ | jƒ  dS )z· Set the pixmap to use as the image in the widget.

        Parameters
        ----------
        pixamp : QPixmap
            The QPixmap to use as the image in the widget.

        N)r   Úupdate)r   r    r   r   r   Ú	setPixmap£   s    	zQImageView.setPixmapc             C   s   | j S )zR Returns whether or not the contents scale with the widget
        size.

        )r   )r   r   r   r   ÚscaledContents¯   s    zQImageView.scaledContentsc             C   s   || _ | jƒ  dS )aP   Set whether the contents scale with the widget size.

        Parameters
        ----------
        scaled : bool
            If True, the image will be scaled to fit the widget size,
            subject to the other sizing constraints in place. If False,
            the image will not scale and will be clipped as required.

        N)r   r$   )r   Zscaledr   r   r   ÚsetScaledContents¶   s    zQImageView.setScaledContentsc             C   s   | j S )z` Returns whether or not the image can be scaled greater than
        its natural size.

        )r   )r   r   r   r   ÚallowUpscalingÄ   s    zQImageView.allowUpscalingc             C   s   || _ | jƒ  dS )a˜   Set whether or not to allow the image to be scaled beyond
        its natural size.

        Parameters
        ----------
        allow : bool
            If True, then the image may be scaled larger than its
            natural if it is scaled to fit. If False, the image will
            never be scaled larger than its natural size. In either
            case, the image may be scaled smaller.

        N)r   r$   )r   Úallowr   r   r   ÚsetAllowUpscalingË   s    zQImageView.setAllowUpscalingc             C   s   | j S )zf Returns whether or not the aspect ratio of the image is
        maintained during a resize.

        )r   )r   r   r   r   ÚpreserveAspectRatioÛ   s    zQImageView.preserveAspectRatioc             C   s   || _ | jƒ  dS )a&   Set whether or not to preserve the image aspect ratio.

        Parameters
        ----------
        preserve : bool
            If True then the aspect ratio of the image will be preserved
            if it is scaled to fit. Otherwise, the aspect ratio will be
            ignored.

        N)r   r$   )r   Zpreserver   r   r   ÚsetPreserveAspectRatioâ   s    z!QImageView.setPreserveAspectRatioc             C   s   | j S )zJ Returns whether or not the image should be clipped in the view.

        )r   )r   r   r   r   ÚallowClippingð   s    zQImageView.allowClippingc             C   s   || _ | jƒ  dS )zú Set whether or not the image should be clipped in the view.

        Parameters
        ----------
        allow : bool
            If True then clipping will be allowed.  Otherwise the minimum
            size hint will be the image size.

        N)r   r$   )r   r)   r   r   r   ÚsetAllowClippingö   s    
zQImageView.setAllowClipping)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r!   r"   r#   r    r%   r&   r'   r(   r*   r+   r,   r-   r.   Ú__classcell__r   r   )r   r   r   .   s   ?

r   c               @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú_ImageEditorz, Traits UI 'display only' image editor.
    c             C   sz   | j j}|dkr| j}tƒ | _| jjt|ƒƒ | jj| j jƒ | jj	| j j
ƒ | jj| j jƒ | jj| j jƒ | jƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        N)ÚfactoryÚimageÚvaluer   Úcontrolr%   r   r'   Úscaler*   Úallow_upscalingr,   Úpreserve_aspect_ratior.   Úallow_clippingZset_tooltip)r   r   r6   r   r   r   Úinit  s    z_ImageEditor.initc             C   sp   | j jdkr,| j}t|tƒr,| jjt|ƒƒ | jj| j j	ƒ | jj
| j jƒ | jj| j jƒ | jj| j jƒ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        N)r5   r6   r7   Ú
isinstancer   r8   r%   r   r'   r9   r*   r:   r,   r;   r.   r<   )r   r7   r   r   r   Úupdate_editor&  s    
z_ImageEditor.update_editorN)r/   r0   r1   r2   r=   r?   r   r   r   r   r4     s   r4   N)r2   Ú
__future__r   Zpyface.qt.QtGuir   r   r   Zpyface.image_resourcer   Zpyface.ui_traitsr   Ztraitsui.editors.image_editorr   Úeditorr
   r   r4   r   r   r   r   Ú<module>   s   	 [