3
7ßL]Ç   ã               @   s
  d Z ddlZddlmZmZmZmZmZ ddlm	Z	 yddl
mZ W n ek
r\   dZY nX ddlZejeƒZi Zi Zddd	„Z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G dd„ deƒZG dd„ deƒZeddddƒZeddddƒZdS )z7 Defines common traits used within the pyface library. é    N)ÚABCHasStrictTraitsÚEnumÚRangeÚ
TraitErrorÚ	TraitType)Úget_resource_path)ÚCALLABLE_AND_ARGS_DEFAULT_VALUEé   é   c       	      C   sÎ   t | tjƒs| S | }| jdƒ}|s6t|ƒ}d| |f }tj|ƒ}|dkrÊ|r¨yddlm} |j	| ƒ}W qÂ t
k
r¤ } z"tjd|  ƒ tj|ƒ d}W Y dd}~X qÂX nddlm} || |gd	�}|t|< |S )
zA Converts a specified value to an ImageResource if possible.
    ú@z%s[%s]Né   )ÚImageLibraryzCan't load image resource '%s'.r   )ÚImageResource)Úsearch_path)Ú
isinstanceÚsixÚstring_typesÚ
startswithr   Úimage_resource_cacheÚgetZimage.imager   Úimage_resourceÚ	ExceptionÚloggerÚerrorÚ	exceptionZpyface.image_resourcer   )	ÚvalueÚlevelÚkeyZis_pyface_imager   Úresultr   Úexcr   © r    ú4/tmp/pip-install-5sebvq2j/pyface/pyface/ui_traits.pyÚconvert_image'   s*    


r"   c             C   s.   t j| ƒ}|dkr*| dk	r*| jƒ  t | < }|S )z: Converts an ImageResource to a bitmap using a cache.
    N)Úimage_bitmap_cacher   Zcreate_bitmap)r   Zbitmapr    r    r!   Úconvert_bitmapF   s    
r$   c                   s:   e Zd ZdZdZdZd
‡ fdd„	Zdd„ Zdd	„ Z‡  Z	S )ÚImagezk Defines a trait whose value must be a ImageResource or a string
        that can be converted to one.
    Nz9an ImageResource or string that can be used to define onec                s   t t| ƒjt|ƒf|Ž dS )a   Creates an Image trait.

        Parameters
        ----------
        value : string or ImageResource
            The default value for the Image, either an ImageResource object,
            or a string from which an ImageResource object can be derived.
        N)Úsuperr%   Ú__init__r"   )Úselfr   Úmetadata)Ú	__class__r    r!   r'   \   s    	zImage.__init__c             C   sB   ddl m} |dkrdS t|dƒ}t||ƒr0|S | j|||ƒ dS )zC Validates that a specified value is valid for this trait.
        r   )ÚIImageResourceNé   )Zpyface.i_image_resourcer+   r"   r   r   )r(   ÚobjectÚnamer   r+   Z	new_valuer    r    r!   Úvalidateg   s    

zImage.validatec             C   s   ddl m} |ƒ S )z6 Returns the default UI editor for the trait.
        r   )ÚImageEditor)Ztraitsui.editors.apir0   )r(   r0   r    r    r!   Úcreate_editoru   s    zImage.create_editor)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Údefault_valueÚ	info_textr'   r/   r1   Ú__classcell__r    r    )r*   r!   r%   Q   s   r%   c                   s   e Zd Z‡ fdd„Z‡  ZS )ÚBaseMBc                sœ   t |ƒ}|dkr†|dkr.|d  } } }}nD|dkrP|d  }}|d  }}n"|dkrf|\}}}}ntd| ƒ‚|j||||dœƒ tt| ƒjf |Ž dS )a{   Map posiitonal arguments to traits.

        If one value is provided it is taken as the value for all sides.
        If two values are provided, then the first argument is used for
        left and right, while the second is used for top and bottom.
        If 4 values are provided, then the arguments are mapped to
        left, right, top, and bottom, respectively.
        r   r   é   r,   z10, 1, 2 or 4 arguments expected, but %d specified)ÚleftÚrightÚtopÚbottomN)Úlenr   Úupdater&   r9   r'   )r(   ÚargsZtraitsÚnr;   r<   r=   r>   )r*   r    r!   r'   ‚   s    	zBaseMB.__init__)r2   r3   r4   r'   r8   r    r    )r*   r!   r9   €   s   r9   c               @   s<   e Zd ZedddƒZedddƒZedddƒZedddƒZdS )ÚMarginé    r   Niàÿÿÿiàÿÿÿiàÿÿÿiàÿÿÿ)r2   r3   r4   r   r=   r>   r;   r<   r    r    r    r!   rC   �   s   rC   c               @   s<   e Zd ZedddƒZedddƒZedddƒZedddƒZdS )ÚBorderr   rD   N)r2   r3   r4   r   r=   r>   r;   r<   r    r    r    r!   rE   ¬   s   rE   c                   s8   e Zd ZdZeZedƒZdZdd„ Z‡ fdd„Z	‡  Z
S )Ú	HasMarginz| Defines a trait whose value must be a Margin object or an integer or
        tuple value that can be converted to one.
    r   zŒa Margin instance, or an integer in the range from -32 to 32 or a tuple with 1, 2 or 4 integers in that range that can be used to define onec             C   sœ   t |tƒr>y| j|ƒ}W qz tk
r:   | j|||ƒ Y qzX n<t |tƒrzy| j|Ž }W n" tk
rx   | j|||ƒ Y nX t || jƒrŠ|S | j|||ƒ dS )zC Validates that a specified value is valid for this trait.
        N)r   ÚintÚklassr   r   Útuple)r(   r-   r.   r   r    r    r!   r/   Ë   s    

zHasMargin.validatec                sz   | j }| j}|dk rrt|tƒr*| j|ƒ}nt|tƒr>| j|Ž }t|| jƒsXtt| ƒjƒ S t	 | _}| jf |j
ƒ f}||fS )z˜ Returns a tuple of the form:
                (default_value_type, default_value)
            which describes the default value for this trait.
        r   )r6   Zdefault_value_typer   rG   rH   rI   r&   rF   Úget_default_valuer   Z	trait_get)r(   ZdvZdvt)r*   r    r!   rJ   Þ   s    



zHasMargin.get_default_value)r2   r3   r4   r5   rC   rH   r6   r7   r/   rJ   r8   r    r    )r*   r!   rF   »   s   rF   c               @   s    e Zd ZdZeZedƒZdZdS )Ú	HasBorderz| Defines a trait whose value must be a Border object or an integer
        or tuple value that can be converted to one.
    r   zŠa Border instance, or an integer in the range from 0 to 32 or a tuple with 1, 2 or 4 integers in that range that can be used to define oneN)r2   r3   r4   r5   rE   rH   r6   r7   r    r    r    r!   rK   ô   s   rK   r;   r<   ZaboveZbelowÚdefaultÚcenter)r
   )r5   ÚloggingZ
traits.apir   r   r   r   r   Ztraits.trait_baser   Ztraits.trait_handlersr   ÚImportErrorr   Ú	getLoggerr2   r   r   r#   r"   r$   r%   r9   rC   rE   rF   rK   ZPositionZ	Alignmentr    r    r    r!   Ú<module>   s*   


/9