a
    §ˆcû  ã                   @   sp   d Z ddlmZ ddlmZmZmZmZ eddƒZeddddƒZ	dd	„ Z
d
d„ Zefdd„Zdd„ ZeefZdS )za Defines various helper functions that are useful for creating Traits-based
    user interfaces.
é    )Ú
itemgetter)ÚBaseTraitHandlerÚCTraitÚEnumÚ
TraitErrorZ
horizontalZverticalÚtabÚfixedc                 C   sb   |   dd¡} | dd…  ¡ | dd…  } d}d}| D ](}| ¡ rL|rL|d7 }| ¡ }||7 }q4|S )z5Returns a "user-friendly" name for a specified trait.Ú_ú Né   Ú r   )ÚreplaceÚupperÚisupperÚislower)ÚnameÚresultZ
last_lowerÚc© r   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/helper.pyÚuser_name_for   s    
r   c                    sb   t t| ƒƒ‰ ˆ  tˆ ƒd d d ¡‰ d ‡ fdd„tdtˆ ƒdƒD ƒ¡ ¡ }| dkrZ|S d| S )zFormats a specified value as an integer string with embedded commas.
    For example: commatize( 12345 ) returns "12,345".
    é   é   ú,c                    s   g | ]}ˆ ||d  … ‘qS )r   r   )Ú.0Úi©Úsr   r   Ú
<listcomp>3   ó    zcommatize.<locals>.<listcomp>r   ú-)ÚstrÚabsÚrjustÚlenÚjoinÚrangeÚlstrip)Úvaluer   r   r   r   Ú	commatize-   s    (r)   c           	         s6  t | tƒrn‡fdd„|  ¡ D ƒ}t|ƒdkrö|jtdƒd� |d d  d¡d ‰ ˆ dkrö‡ fdd„|D ƒ}nˆt | tƒsä| }t |tƒrŒ|j	}t |t
ƒsžtdƒ‚|jrÎ‡fd	d„|j ¡ D ƒ}|jtdƒd� qö‡fd
d„|jD ƒ}n‡fdd„| D ƒ}dd„ |D ƒ}i }i }|D ]\}}|||< |||< �q|||fS )z<Recomputes the mappings for a new set of enumeration values.c                    s   g | ]\}}ˆ |ƒ|f‘qS r   r   ©r   ÚnÚv©Ústrfuncr   r   r   >   r   z'enum_values_changed.<locals>.<listcomp>r   ©Úkeyú:r   c                    s    g | ]\}}|ˆ d … |f‘qS )Nr   r*   )Úcolr   r   r   C   r   z$Invalid value for 'values' specifiedc                    s   g | ]}ˆ |ƒ|f‘qS r   r   )r   r+   r-   r   r   r   K   r   c                    s   g | ]}ˆ |ƒ|f‘qS r   r   ©r   r,   r-   r   r   r   N   r   c                    s   g | ]}ˆ |ƒ|f‘qS r   r   r3   r-   r   r   r   P   r   c                 S   s   g | ]}|d  ‘qS )r   r   )r   Úxr   r   r   r   R   r   )Ú
isinstanceÚdictÚitemsr$   Úsortr   ÚfindÚSequenceTypesr   Úhandlerr   r   Z	is_mappedÚmapÚkeysÚvalues)	r>   r.   Údatar;   ÚnamesÚmappingZinverse_mappingr   r(   r   )r2   r.   r   Úenum_values_changed:   s2    



rB   c                 C   s0  g }g }|du rdgt |ƒ }t||ƒD ]R\}}|dkrR| d¡ | t|ƒ¡ q(|dkrf| d¡ n
| |¡ | |¡ q(|dur®t|ƒD ] \}}	|	durŒ|	||< d||< qŒt|ƒ}
t|ƒ| k �r,|
dk�r,tt|ƒtddƒd�D ]F\}}t|ƒ}
t||
 |  ƒ}t||| ƒ||< d||< | || 8 } qä|S )	a4  Distribute column space amongst columns based on requested space.

    Widths requests can be specified as one of the following:

    - a value greater than 1.0 is treated as a fixed width with no flexibility
      (ie. a minimum width as specified and a weight of 0.0)

    - a value between 0.0 and 1.0 is treaded as a flexible width column with
      the specified width as a weight and a minimum width provided by the
      min_widths entry.

    - a value less than or equal to 0.0 is treated as a flexible width column
      with a weight of 0.1 and a minimum width provided by the min_widths
      parameter.

    If user widths are supplied then any non-None values override the
    requested widths, and are treated as having a flexibility of 0.

    Space is distributed by evaluating each column from smallest weight to
    largest and seeing if the weighted proportion of the remaining space is
    more than the minimum, and if so replacing the width with the weighted
    width.  The column is then removed from the available width and the
    total weight and the analysis continues.

    Parameters
    ----------
    available_space : int
        The available horizontal space.
    requested : list of numbers
        The requested width or weight for each column.
    min_widths : None or list of ints
        The minimum width for each flexible column
    user_widths : None or list of int or None
        Any widths specified by the user resizing the columns manually.

    Returns
    -------
    widths : list of ints
        The assigned width for each column
    Né   g      ð?g        r   gš™™™™™¹?r   r/   )	r$   ÚzipÚappendÚintÚ	enumerateÚsumÚsortedr   Úmax)Zavailable_spaceÚ	requestedZ
min_widthsZuser_widthsZwidthsÚweightsÚrequestZ	min_widthr   Z
user_widthZtotal_weightZweightZ	stretchedr   r   r   Úcompute_column_widths\   s4    )


rN   N)Ú__doc__Úoperatorr   Z
traits.apir   r   r   r   ZOrientationZ	DockStyler   r)   r!   rB   rN   ÚtupleÚlistr:   r   r   r   r   Ú<module>   s   
"U