a
    §ˆc!  ã                   @   sº   d Z ddl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mZmZmZmZ ddlmZ edd	ƒZedd	d
dƒZddd„Zddd„Zdd„ Zdd„ ZG dd„ dejƒZdd„ ZdS )zn Defines helper functions and classes used to define PyQt-based trait
    editors and trait editor factories.
é    N)ÚSystemMetrics)ÚQtCoreÚQtGuiÚis_qt5Úis_pyqt)Úconvert_image)ÚEnumÚCTraitÚBaseTraitHandlerÚ
TraitError)ÚSequenceTypesZ
horizontalZverticalÚtabÚfixedc                 C   s
  | dd… dkr4t |  dd¡ ¡ ƒ}|dur4| ¡ S tj | ¡\}} |  dd¡ ¡ } |rhtj || ¡}n0|du rŠtj tj t	¡d| ¡}ntj || ¡}tj 
|¡}trÖtj |¡}|du rÔt |¡}tj ||¡ n0t ¡ }tj ||¡�s| |¡ tj ||¡ |S )zúConvert an image file name to a cached QPixmap

    Returns 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é   ú@ú Ú_Zimages)r   ÚreplaceÚlowerZcreate_imageÚosÚpathÚsplitÚjoinÚdirnameÚ__file__Úabspathr   r   ZQPixmapCacheÚfindZQPixmapÚinsertÚload)Únamer   ÚimageZ	name_pathÚfilenameÚpm© r#   úV/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/helper.pyÚpixmap_cache,   s,    

r%   c              	   C   s  t ƒ }|j}|j}|  ¡ }|p$| ¡ }|p0| ¡ }|du r@| j}|du rh|  || d || d ¡ dS t|t	j
ƒrÀ| t ¡ ¡}| ¡ }	| ¡ }
| ¡ }| ¡ }| ¡ }| ¡  ¡ | ¡  }n|\}	}
}}d}|	|| d 8 }	|
|| 7 }
|  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   Zscreen_widthZscreen_heightZframeGeometryÚwidthÚheightÚ_parentÚmoveÚ
isinstancer   ZQWidgetZmapToGlobalr   ZQPointÚxÚyÚwindowÚmaxÚmin)r.   r'   r(   ÚparentZsystem_metricsZ	screen_dxZ	screen_dyZfgeomZgposr,   r-   ZcdxZcdyÚpwZfheightr#   r#   r$   Úposition_windowP   s4    $ÿr3   c                 C   s    |   ¡ }|dur| jj|Ž  dS )z6Restores 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  ¡ }|  | ¡ | ¡ | ¡ | ¡ f¡ dS )z3Saves 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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 | ¡ t j ¡  ¡ }| t jjj	¡}t
|tƒrpt|ƒ}| ¡ }| ¡ }||k rX|}||k rd|}t  |¡}n
| |¡}|  |¡ |  d¡ |  tjjj¡ | j |¡ dS )zyInitialise the button.  icon is either the name of an image file or
        one of the QtGui.QStyle.SP_* values.
        TN)r   ÚQPushButtonÚ__init__ZQApplicationÚinstanceÚstyleZpixelMetricÚQStyleZPixelMetricZPM_ButtonIconSizer+   Ústrr%   r'   r(   ZQIconZstandardIconZsetIconZsetFlatZsetFocusPolicyr   ÚQtZFocusPolicyZNoFocusZclickedÚconnect)	ÚselfZiconZslotZstyZico_szr"   Zpm_widthZ	pm_heightZicor#   r#   r$   r:   –   s"    



zIconButton.__init__c                 C   s8   |   ¡  t ¡ }|  |¡ |  ¡  tjjj||j	¡}|S )z¥Reimplement sizeHint to return a recommended button size based on
        the size of the icon.

        Returns
        -------
        size : QtCore.QSize
        )
ZensurePolishedr   ZQStyleOptionButtonZinitStyleOptionr<   ZsizeFromContentsr=   ZContentsTypeZCT_ToolButtonZiconSize)rA   ÚoptionÚsizer#   r#   r$   ÚsizeHintº   s    
ÿzIconButton.sizeHintN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r:   rD   r#   r#   r#   r$   r8   ‘   s   $r8   c                 C   sô   t  |¡}| ¡ }d}g }|  ¡ D ]˜}d}	t  ||¡}
|
 ¡  || |kr¢|
 ¡ }| ¡ s\q¢| t	|ƒ¡ | 
¡ }	|	| ¡  }||	|…  ¡ }| |¡ ||7 }q>|
 ¡  || |kr" q¼q"|rð|| |krð||	d… }| |tj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   Néÿÿÿÿ)r   ZQFontMetricsZlineSpacingÚ
splitlinesZQTextLayoutZbeginLayoutZ
createLineZisValidZsetLineWidthÚintZ	textStartÚ
textLengthÚrstripÚappendZ	endLayoutZ
elidedTextr   r?   ZTextElideModeZ
ElideRight)ÚtextÚfontr'   r(   Zfont_metricsZline_spacingZy_offsetÚlinesZ	paragraphZ
line_startZtext_layoutÚlineZline_endZ	line_textÚ	last_liner#   r#   r$   Úwrap_text_with_elisionÓ   s6    


ÿrT   )N)NNN)rH   Úos.pathr   Z
pyface.apir   Z	pyface.qtr   r   r   r   Zpyface.ui_traitsr   Z
traits.apir   r	   r
   r   Ztraitsui.ui_traitsr   ZOrientationZ	DockStyler%   r3   r6   r7   r9   r8   rT   r#   r#   r#   r$   Ú<module>   s   

$
4B