3
8Ad]\#  ã               @   sÄ   d Z ddlmZ ddlZddlmZmZ ddlm	Z	 ddl
mZmZmZmZ ddlmZ ddlZejdkZed	d
ƒZddd„Zddd„Zdd„ Zdd„ ZG dd„ dejƒZed	d
ddƒZdd„ ZdS )zn Defines helper functions and classes used to define PyQt-based trait
    editors and trait editor factories.
é    )Úabsolute_importN)ÚQtCoreÚQtGui)Úconvert_image)ÚEnumÚCTraitÚBaseTraitHandlerÚ
TraitError)ÚSequenceTypesé   Ú
horizontalZverticalc             C   s
  | dd… dkr4t | jddƒjƒ ƒ}|dk	r4|jƒ S tjj| ƒ\}} | jddƒjƒ } |rhtjj|| ƒ}n0|dkrŠtjjtjjt	ƒd| ƒ}ntjj|| ƒ}tjj
|ƒ}trÖtjj|ƒ}|dkrÔtj|ƒ}tjj||ƒ n0tjƒ }tjj||ƒ�s|j|ƒ tjj||ƒ |S )zÎ Return the QPixmap corresponding to a filename. If the filename does not
        contain a path component, 'path' is used (or if 'path' is not specified,
        the local 'images' directory is used).
    Né   ú@ú Ú_Úimages)r   ÚreplaceÚlowerZcreate_imageÚosÚpathÚsplitÚjoinÚdirnameÚ__file__ÚabspathÚis_qt5r   ZQPixmapCacheÚfindZQPixmapÚinsertÚload)Únamer   ÚimageZ	name_pathÚfilenameÚpm© r#   úo/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/qt4/helper.pyÚpixmap_cache-   s,    

r%   c             C   s*  t jjƒ j| ƒ}|jƒ }|jƒ }| jƒ }|p2|jƒ }|p>|jƒ }|dkrN| j}|dkrv| j|| d || d ƒ dS t	|t j
ƒrÎ|jtjƒ ƒ}|jƒ }	|jƒ }
|jƒ }|jƒ }|jƒ }|jƒ jƒ |jƒ  }n|\}	}
}}d}|	|| d 8 }	|
|| 7 }
| jtdt|	|| ƒƒtdt|
|| ƒƒƒ dS )zŽ Positions a window on the screen with a specified width and height so
        that the window completely fits on the screen if possible.
    Né   r   )r   ÚQApplicationZdesktopZavailableGeometryÚwidthÚheightZframeGeometryZ_parentÚmoveÚ
isinstanceZQWidgetZmapToGlobalr   ZQPointÚxÚyÚwindowÚmaxÚmin)r.   r(   r)   ÚparentZsgeomZ	screen_dxZ	screen_dyZfgeomZgposr,   r-   ZcdxZcdyÚpwZfheightr#   r#   r$   Úposition_windowT   s2    r3   c             C   s    | j ƒ }|dk	r| jj|Ž  dS )z< Restores the user preference items for a specified UI.
    N)Zrestore_prefsÚcontrolZsetGeometry)ÚuiZprefsr#   r#   r$   Úrestore_window‹   s    r6   c             C   s0   | j jƒ }| j|jƒ |jƒ |jƒ |jƒ fƒ dS )z9 Saves the user preference items for a specified UI.
    N)r4   ZgeometryZ
save_prefsr,   r-   r(   r)   )r5   Zgeomr#   r#   r$   Úsave_window—   s    
r7   c               @   s   e Zd ZdZdd„ ZdS )Ú
IconButtonz� The IconButton class is a push button that contains a small image or a
        standard icon provided by the current style.
    c       	      C   s¸   t jj| ƒ t jjƒ jƒ }|jt jjƒ}t	|t
jƒrpt|ƒ}|jƒ }|jƒ }||k rX|}||k rd|}t j|ƒ}n
|j|ƒ}| j|ƒ | j||ƒ | jdƒ | jtjjƒ | jj|ƒ dS )z~ Initialise the button.  icon is either the name of an image file or
            one of the QtGui.QStyle.SP_* values.
        TN)r   ÚQPushButtonÚ__init__r'   ÚinstanceÚstyleZpixelMetricZQStyleZPM_ButtonIconSizer+   ÚsixÚstring_typesr%   r(   r)   ZQIconZstandardIconZsetIconZsetMaximumSizeZsetFlatZsetFocusPolicyr   ÚQtZNoFocusZclickedÚconnect)	ÚselfÚiconZslotZstyZico_szr"   Zpm_widthZ	pm_heightZicor#   r#   r$   r:   ¨   s$    


zIconButton.__init__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r:   r#   r#   r#   r$   r8   £   s   r8   ÚtabÚfixedc             C   sô   t j|ƒ}|jƒ }d}g }x¢| jƒ D ]–}d}	t j||ƒ}
|
jƒ  xb|| |kr¢|
jƒ }|jƒ s`P |j|ƒ |j	ƒ }	|	|j
ƒ  }||	|… jƒ }|j|ƒ ||7 }qBW |
jƒ  || |kr$P q$W |rð|| |krð||	d… }|j|tjj|ƒ|d< |S )aÌ   Wrap paragraphs to fit inside a given size, eliding if too long.

    Parameters
    ----------
    text : unicode
        The text to wrap.
    font : QFont instance
        The font the text will be rendered in.
    width : int
        The width of the box the text will display in.
    height : int
        The height of the box the text will display in.

    Returns
    -------
    lines : list of unicode
        The lines of text to display, split
    r   Nr   éÿÿÿÿ)r   ZQFontMetricsZlineSpacingÚ
splitlinesZQTextLayoutZbeginLayoutZ
createLineZisValidZsetLineWidthZ	textStartZ
textLengthÚrstripÚappendZ	endLayoutZ
elidedTextr   r?   Z
ElideRight)ÚtextZfontr(   r)   Zfont_metricsZline_spacingZy_offsetÚlinesZ	paragraphZ
line_startZtext_layoutÚlineZline_endÚ	line_textÚ	last_liner#   r#   r$   Úwrap_text_with_elision×   s4    


rR   )r   )N)NNN)rF   Ú
__future__r   Úos.pathr   Z	pyface.qtr   r   Zpyface.ui_traitsr   Z
traits.apir   r   r   r	   Ztraitsui.ui_traitsr
   r=   Ú__version_info__r   ZOrientationr%   r3   r6   r7   r9   r8   Z	DockStylerR   r#   r#   r#   r$   Ú<module>   s    


'
7.