a
    §ˆc•m  ã                   @   s    d Z ddlmZmZmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZmZmZ ddlmZmZ G dd„ deƒZeeƒG dd„ deƒƒZG d	d
„ d
eƒZdS )zJ Defines the adapter classes associated with the Traits UI TabularEditor.
é    )ÚAnyÚBoolÚDictÚEnumÚEventÚFloatÚHasPrivateTraitsÚ	HasTraitsÚInstanceÚIntÚ	InterfaceÚListÚPropertyÚStrÚUnionÚcached_propertyÚobserveÚprovidesé   )ÚColorÚFontc                   @   s8   e Zd Zeƒ Zeƒ Zeƒ Zeƒ Ze	e
ƒZeƒ Zeƒ ZdS )ÚITabularAdapterN©Ú__name__Ú
__module__Ú__qualname__r   Úrowr   ÚcolumnÚitemÚvaluer   r   Úcolumnsr   ÚacceptsÚis_cacheable© r#   r#   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/tabular_adapter.pyr   '   s   r   c                   @   s<   e Zd Zeƒ Zeƒ Zeƒ Zeƒ Ze	e
ƒZedƒZedƒZdS )ÚAnITabularAdapterTNr   r#   r#   r#   r$   r%   C   s   r%   c                   @   sÈ  e Zd ZdZeƒ Zeƒ ZedƒZ	e
ddd�Ze
ddd�Ze
ddd�Ze
ddd�Ze
ddd�Ze
ddd�ZedddƒZed	ƒZed
ƒZedƒZeƒ ZedƒZeddƒZedƒZeƒ Zeƒ Zeddd�Z eƒ Z!eƒ Z"eƒ Z#eƒ Z$eƒ Z%ee&dd�Z'e(e)ƒZ*eƒ Z+e,ƒ Z-e,ƒ Z.eƒ Z/eƒ Z0eƒ Z1e2ƒ Z3e4dd�Z5edd�Z6edd�Z7e8deƒZ9edd�Z:edd�Z;dd„ Z<dd„ Z=dd„ Z>dd„ Z?dd„ Z@dd„ ZAdd„ ZBdcd d!„ZCddd"d#„ZDded$d%„ZEd&d'„ ZFd(d)„ ZGd*d+„ ZHd,d-„ ZId.d/„ ZJd0d1„ ZKd2d3„ ZLd4d5„ ZMd6d7„ ZNd8d9„ ZOd:d;„ ZPd<d=„ ZQd>d?„ ZRd@dA„ ZSdBdC„ ZTdDdE„ ZUdFdG„ ZVdHdI„ ZWdJdK„ ZXdLdM„ ZYeZdNdO„ ƒZ[eZdPdQ„ ƒZ\dfdRdS„Z]dgdTdU„Z^eZdVdW„ ƒZ_eZdXdY„ ƒZ`eZdZd[„ ƒZadhd\d]„Zbd^d_„ Zcedd`ƒdadb„ ƒZedS )iÚTabularAdapterz`The base class for adapting list items to values that can be edited
    by a TabularEditor.
    Ú NT)ÚupdateÚleftÚcenterÚrightz%séÿÿÿÿFÚafterÚbeforer    )r   zadapters,columnsc                 C   s   | j dddd� dS )z5Clean up the adapter to remove references to objects.N)Úobjectr   r   )Z
trait_setq©Úselfr#   r#   r$   Úcleanupõ   s    zTabularAdapter.cleanupc                 C   s   |   d||d|¡S )zôReturns the alignment style to use for a specified column.

        The possible values that can be returned are: ``'left'``, ``'center'``
        or ``'right'``. All table items share the same alignment for a
        specified column.
        Úget_alignmentr   ©Ú_result_for©r1   r/   Útraitr   r#   r#   r$   r3   û   s    zTabularAdapter.get_alignmentc                 C   s   |   d||d|¡S )a¾  Returns the width to use for a specified column.

        If the value is <= 0, the column will have a *default* width, which is
        the same as specifying a width of 0.1.

        If the value is > 1.0, it is converted to an integer and the result is
        the width of the column in pixels. This is referred to as a
        *fixed width* column.

        If the value is a float such that 0.0 < value <= 1.0, it is treated as
        the *unnormalized fraction of the available space* that is to be
        assigned to the column. What this means requires a little explanation.

        To arrive at the size in pixels of the column at any given time, the
        editor adds together all of the *unnormalized fraction* values
        returned for all columns in the table to arrive at a total value. Each
        *unnormalized fraction* is then divided by the total to create a
        *normalized fraction*. Each column is then assigned an amount of space
        in pixels equal to the maximum of 30 or its *normalized fraction*
        multiplied by the *available space*. The *available space* is defined
        as the actual width of the table minus the width of all *fixed width*
        columns. Note that this calculation is performed each time the table is
        resized in the user interface, thus allowing columns of this type to
        increase or decrease their width dynamically, while leaving *fixed
        width* columns unchanged.
        Ú	get_widthr   r4   r6   r#   r#   r$   r8     s    zTabularAdapter.get_widthc                 C   s   |   d|||d¡S )z¹Returns whether the user can edit a specified row.

        A ``True`` result indicates that the value can be edited, while a
        ``False`` result indicates that it cannot.
        Úget_can_editr   r4   ©r1   r/   r7   r   r#   r#   r$   r9   !  s    zTabularAdapter.get_can_editc                 C   s   |   d|||d¡S )aS  Returns the value to be *dragged* for a specified row.

        A result of ``None`` means that the item cannot be dragged. Note that
        the value returned does not have to be the actual row item. It can be
        any value that you want to drag in its place. In particular, if you
        want the drag target to receive a copy of the row item, you should
        return a copy or clone of the item in its place.

        Also note that if multiple items are being dragged, and this method
        returns ``None`` for any item in the set, no drag operation is
        performed.
        Úget_dragr   r4   r:   r#   r#   r$   r;   )  s    zTabularAdapter.get_dragc                 C   s   |   d|||d|¡S )a@  Returns whether the specified ``value`` can be dropped on the specified row.

        A value of ``True`` means the ``value`` can be dropped; and a value of
        ``False`` indicates that it cannot be dropped.

        The result is used to provide the user positive or negative drag
        feedback while dragging items over the table. ``value`` will always be
        a single value, even if multiple items are being dragged. The editor
        handles multiple drag items by making a separate call to
        :py:meth:`get_can_drop` for each item being dragged.
        Úget_can_dropr   r4   ©r1   r/   r7   r   r   r#   r#   r$   r<   8  s    zTabularAdapter.get_can_dropc                 C   s   |   d|||d|¡S )aÿ  Returns how to handle a specified ``value`` being dropped on a specified row.

        The possible return values are:

        - ``'before'``: Insert the specified ``value`` before the dropped on item.
        - ``'after'``: Insert the specified ``value`` after the dropped on item.

        Note there is no result indicating *do not drop* since you will have
        already indicated that the ``object`` can be dropped by the result
        returned from a previous call to :py:meth:`get_can_drop`.
        Úget_droppedr   r4   r=   r#   r#   r$   r>   F  s    zTabularAdapter.get_droppedr   c                 C   s   |   d||||¡S )a  Returns the font to use for displaying a specified row or cell.

        A result of ``None`` means use the default font; otherwise a toolkit
        font object should be returned. Note that all columns for the specified
        table row will use the font value returned.
        Úget_fontr4   ©r1   r/   r7   r   r   r#   r#   r$   r?   T  s    zTabularAdapter.get_fontc                 C   s   |   d||||¡S )a&  Returns the text color to use for a specified row or cell.

        A result of ``None`` means use the default text color; otherwise a
        toolkit-compatible color should be returned. Note that all columns for
        the specified table row will use the text color value returned.
        Úget_text_colorr4   r@   r#   r#   r$   rA   ]  s    zTabularAdapter.get_text_colorc                 C   s   |   d||||¡S )a@  Returns the background color to use for a specified row or cell.

        A result of ``None`` means use the default background color; otherwise
        a toolkit-compatible color should be returned. Note that all columns
        for the specified table row will use the background color value
        returned.
        Úget_bg_colorr4   r@   r#   r#   r$   rB   f  s    zTabularAdapter.get_bg_colorc                 C   s   |   d||||¡S )aÀ  Returns the image to display for a specified cell.

        A result of ``None`` means no image will be displayed in the specified
        table cell. Otherwise the result should either be the name of the
        image, or an :py:class:`~pyface.image_resource.ImageResource` object
        specifying the image to display.

        A name is allowed in the case where the image is specified in the
        :py:class:`~traitsui.editors.tabular_editor.TabularEditor`
        :py:attr:`~traitsui.editors.tabular_editor.TabularEditor.images` trait.
        In that case, the name should be the same as the string specified in
        the :py:class:`~pyface.image_resource.ImageResource` constructor.
        Ú	get_imager4   r@   r#   r#   r$   rC   p  s    zTabularAdapter.get_imagec                 C   s   |   d||||¡S )aW  Returns the Python formatting string to apply to the specified cell.

        The resulting of formatting with this string will be used as the text
        to display it in the table.

        The return can be any Python string containing exactly one old-style
        Python formatting sequence, such as ``'%.4f'`` or ``'(%5.2f)'``.
        Ú
get_formatr4   r@   r#   r#   r$   rD   €  s    	zTabularAdapter.get_formatc                 C   s   |   d||||¡S )a
  Returns a string containing the text to display for a specified cell.

        If the underlying data representation for a specified item is not a
        string, then it is your responsibility to convert it to one before
        returning it as the result.
        Úget_textr4   r@   r#   r#   r$   rE   ‹  s    zTabularAdapter.get_textc                 C   s   |   d||||¡S )z4Returns the content to display for a specified cell.Úget_contentr4   r@   r#   r#   r$   rF   ”  s    zTabularAdapter.get_contentc                 C   s   |   d|||||¡ dS )aœ  Sets the value for the specified cell.

        This method is called when the user completes an editing operation on a
        table cell.

        The string specified by ``text`` is the value that the user has
        entered in the table cell.  If the underlying data does not store the
        value as text, it is your responsibility to convert ``text`` to the
        correct representation used.
        Úset_textNr4   )r1   r/   r7   r   r   Útextr#   r#   r$   rG   ˜  s    zTabularAdapter.set_textc                 C   s   |   d||||¡S )z®Returns a string containing the tooltip to display for a specified cell.

        You should return the empty string if you do not wish to display a
        tooltip.
        Úget_tooltipr4   r@   r#   r#   r$   rI   ¥  s    zTabularAdapter.get_tooltipc                 C   s   |   d||||¡S )z.Returns the context menu for a specified cell.Úget_menur4   r@   r#   r#   r$   rJ   ­  s    zTabularAdapter.get_menuc                 C   s   |   d||||¡S )z0Returns the context menu for a specified column.Úget_column_menur4   r@   r#   r#   r$   rK   ±  s    zTabularAdapter.get_column_menuc                 C   s$   zt ||ƒ| W S    Y dS 0 dS )a‘  Returns the specified row item.

        The value returned should be the value that exists (or *logically*
        exists) at the specified ``row`` in your data. If your data is not
        really a list or array, then you can just use ``row`` as an integer
        *key* or *token* that can be used to retrieve a corresponding item. The
        value of ``row`` will always be in the range: 0 <= row <
        ``len(object, trait)`` (i.e. the result returned by the adapter
        :py:meth:`len` method).

        The default implementation assumes the trait defined by
        ``object.trait`` is a *sequence* and attempts to return the value at
        index ``row``. If an error occurs, it returns ``None`` instead. This
        definition should work correctly for lists, tuples and arrays, or any
        other object that is indexable, but will have to be overridden for all
        other cases.
        N©Úgetattrr:   r#   r#   r$   Úget_item·  s    zTabularAdapter.get_itemc                 C   s   |du rdS t t||ƒƒS dS )a¯  Returns the number of row items in the specified ``object.trait``.

        The result should be an integer greater than or equal to 0.

        The default implementation assumes the trait defined by
        ``object.trait`` is a *sequence* and attempts to return the result of
        calling ``len(object.trait)``. It will need to be overridden for any
        type of data which for which :py:func:`len` will not work.
        Nr   )ÚlenrM   ©r1   r/   r7   r#   r#   r$   rO   Î  s    zTabularAdapter.lenc                 C   s   | j S )aÑ  Returns a new default value for the specified ``object.trait`` list.

        This method is called when *insert* or *append* operations are allowed
        and the user requests that a new item be added to the table. The result
        should be a new instance of whatever underlying representation is being
        used for table items.

        The default implementation simply returns the value of the adapter's
        :py:attr:`default_value` trait.
        )Údefault_valuerP   r#   r#   r$   Úget_default_valueÞ  s    z TabularAdapter.get_default_valuec                 C   s   t ||ƒ|= dS )a˜  Deletes the specified row item.

        This method is only called if the *delete* operation is specified in
        the :py:class:`~traitsui.editors.tabular_editor.TabularEditor`
        :py:attr:`~traitsui.editors.tabular_editor.TabularEditor.operation`
        trait, and the user requests that the item be deleted from the table.

        The adapter can still choose not to delete the specified item if
        desired, although that may prove confusing to the user.

        The default implementation assumes the trait defined by
        ``object.trait`` is a mutable sequence and attempts to perform a
        ``del object.trait[row]`` operation.
        NrL   r:   r#   r#   r$   Údeleteë  s    zTabularAdapter.deletec                 C   s   |gt ||ƒ||…< dS )aÝ  Inserts ``value`` at the specified ``object.trait[row]`` index.

        The specified ``value`` can be:

        - An item being moved from one location in the data to another.
        - A new item created by a previous call to
          :py:meth:`~TabularAdapter.get_default_value`.
        - An item the adapter previously approved via a call to
          :py:meth:`~TabularAdapter.get_can_drop`.

        The adapter can still choose not to insert the item into the data,
        although that may prove confusing to the user.

        The default implementation assumes the trait defined by
        ``object.trait`` is a mutable sequence and attempts to perform an
        ``object.trait[row:row] = [value]`` operation.
        NrL   r=   r#   r#   r$   Úinsertü  s    zTabularAdapter.insertc                 C   s   || | _ | _| j| S )z@Returns the column id corresponding to a specified column index.)r/   ÚnameÚ
column_map)r1   r/   r7   Úindexr#   r#   r$   Ú
get_column  s    zTabularAdapter.get_columnc                 C   s   | j S ©N)r   r0   r#   r#   r$   Ú	_get_drag  s    zTabularAdapter._get_dragc                 C   s&   | j d dkr| jp| jS | jp$| jS ©Né   r   )r   Zeven_text_color_Údefault_text_colorÚodd_text_colorZdefault_text_color_r0   r#   r#   r$   Ú_get_text_color  s    zTabularAdapter._get_text_colorc                 C   s&   | j d dkr| jp| jS | jp$| jS r[   )r   Zeven_bg_color_Zdefault_bg_color_Úodd_bg_colorr0   r#   r#   r$   Ú_get_bg_color   s    zTabularAdapter._get_bg_colorc                 C   s0   |   | j| j| j| j¡|  | j| j| j| j¡ S rY   )rD   r/   rU   r   r   rF   r0   r#   r#   r$   Ú	_get_text&  s
    ÿþzTabularAdapter._get_textc                 C   sj   t | jtƒr| j| j| j< nJz,| j | j¡j}t| j| j| | j¡ƒ W n   t| j| j|ƒ Y n0 d S rY   )	Ú
isinstanceÚ	column_idÚintr   r   r7   ÚhandlerÚsetattrÚevaluate)r1   r   Ztrait_handlerr#   r#   r$   Ú	_set_text+  s    
ýzTabularAdapter._set_textc                 C   s&   t | jtƒr| j| j S t| j| jƒS rY   )rc   rd   re   r   rM   r0   r#   r#   r$   Ú_get_content:  s    zTabularAdapter._get_contentc                 C   sL   i }t | jƒD ]8\}}t|tƒr0| ||i¡ q| |d |d i¡ q|S )Nr   r   )Ú	enumerater    rc   Ústrr(   )r1   ÚcolsÚir   r#   r#   r$   Ú_get_column_dictB  s    
zTabularAdapter._get_column_dictc                 C   s@   g }t | jƒD ],\}}t|tƒr,| |¡ q| |d ¡ q|S )Nr   ©rk   r    rc   rl   Úappend©r1   Úmaprn   r   r#   r#   r$   Ú_get_column_mapL  s    
zTabularAdapter._get_column_mapc                 C   s
   | j | S )z?Override this method if labels will vary from object to object.)Ú	label_map)r1   ÚsectionÚobjr#   r#   r$   Ú	get_labelW  s    zTabularAdapter.get_labelc                 C   s>   | j d u rd S t|| jd ƒ}|d u r(d S || }t|| j d ƒS rY   )Úrow_label_namerM   rU   )r1   rv   rw   Úrowsr   r#   r#   r$   Úget_row_label[  s    
zTabularAdapter.get_row_labelc                 C   s@   g }t | jƒD ],\}}t|tƒr,| |¡ q| |d ¡ q|S ©Nr   rp   rr   r#   r#   r$   Ú_get_label_mapd  s    
zTabularAdapter._get_label_mapc                 C   sV   | j }g }| jD ]@}g }|jD ]&}t|tƒs4|d }| | |¡¡ q| |¡ q|S r|   )ru   Úadaptersr    rc   rl   rq   rW   )r1   Úlabelsrs   ÚadapterÚindicesÚlabelr#   r#   r$   Ú_get_adapter_column_indiceso  s    


z*TabularAdapter._get_adapter_column_indicesc                 C   sh   | j }g }| jD ]R}i }|jD ]8}d }t|tƒs8|\}}| |¡}|d u rN|}|||< q| |¡ q|S rY   )ru   r~   r    rc   rl   rW   rq   )r1   r   rs   r€   Úmappingr‚   ÚidÚkeyr#   r#   r$   Ú_get_adapter_column_map}  s    




z&TabularAdapter._get_adapter_column_mapc                    sÚ  |ˆ_ |ˆ_|ˆ_|ˆ_ˆj|  ˆ_‰|ˆ_ˆ |||¡ ˆ_}|j	}d|j
||f }	ˆj |	¡}
|
durr|
ƒ S |dd… }|dd… }tˆjƒD ]¤\}‰ |ˆj| v r”|ˆ _|ˆ _|ˆ _ˆj| |  ˆ _‰ˆ jr”dˆ|f ‰ˆ  ˆ¡dur”|dk�r‡ ‡‡‡fdd„}
n‡ ‡‡‡fdd„}
ˆ j�r. �qÊ|
ƒ   S q”|du�ržt|d	ƒ�rž|jD ]F}ˆ d
|j
ˆ|f |¡�pˆˆ d|j
|f |¡}
|
du�rV �qž�qV|
du �rÊˆ dˆ|f |¡�pÈˆ ||¡}
|
ˆj|	< |
ƒ S )z|Returns/Sets the value of the specified *name* attribute for the
        specified *object.trait[row].column* item.
        z%s:%s:%dNé   z%s_%sÚget_c                      s   t ˆ jˆjˆˆjd�ˆƒS ©N)r   r   r   )rM   Ú	trait_setr   r   r#   ©r€   rd   Úget_namer1   r#   r$   Ú<lambda>²  s   ýúz,TabularAdapter._result_for.<locals>.<lambda>c                      s   t ˆ jˆjˆˆjd�ˆˆjƒS rŠ   )rg   r‹   r   r   r   r#   rŒ   r#   r$   rŽ   »  s   ýùÚ__mro__z%s_%s_%s)r/   rU   r   r   rV   rd   r   rN   r   Ú	__class__r   ÚcacheÚgetrk   r~   Úadapter_column_indicesÚadapter_column_mapr!   r7   r"   Úhasattrr�   Ú_get_handler_for)r1   rU   r/   r7   r   r   r   r   Z
item_classr†   rf   ÚprefixZ
trait_namern   Úklassr#   rŒ   r$   r5   ”  sb    
	

þÿý


ÿ
þ
zTabularAdapter._result_forc                    s6   ˆ  ˆ ¡dur2|dkr$‡ ‡fdd„S ‡ ‡fdd„S dS )zWReturns the handler for a specified trait name (or None if not
        found).
        Nr‰   c                      s
   t ˆˆ ƒS rY   rL   r#   ©rU   r1   r#   r$   rŽ   ã  ó    z1TabularAdapter._get_handler_for.<locals>.<lambda>c                      s   t ˆˆ ˆjƒS rY   )rg   r   r#   r™   r#   r$   rŽ   å  rš   )r7   )r1   rU   r—   r#   r™   r$   r–   Ý  s
    zTabularAdapter._get_handler_forzcolumns,adapters.items.+updatec                 C   s   i | _ d| _dS )zXFlushes the cache when the columns or any trait on any adapter
        changes.
        TN)r‘   Úcache_flushed)r1   Úeventr#   r#   r$   Ú_flush_cacheé  s    zTabularAdapter._flush_cache)r   )r   )r   )N)N)N)fr   r   r   Ú__doc__r   r    r   Zcolumn_dictr   rQ   r   r^   Zeven_text_colorr]   r`   Zeven_bg_colorZdefault_bg_colorr   Ú	alignmentr   Úformatr   Úwidthr   Zcan_editZdragZcan_dropZdroppedr   ÚfontZ
text_colorZbg_colorÚimagerH   ÚcontentZtooltipÚmenuZcolumn_menur   r~   r
   r	   r/   rU   r   r   r   rd   r   r   r   r‘   r   r›   rV   ru   r   ry   r“   r”   r2   r3   r8   r9   r;   r<   r>   r?   rA   rB   rC   rD   rE   rF   rG   rI   rJ   rK   rN   rO   rR   rS   rT   rX   rZ   r_   ra   rb   ri   rj   r   ro   rt   rx   r{   r}   rƒ   r‡   r5   r–   r   r�   r#   r#   r#   r$   r&   b   s®   






	
	
	

	
	



	




Ir&   N)rž   Z
traits.apir   r   r   r   r   r   r   r	   r
   r   r   r   r   r   r   r   r   r   Ztoolkit_traitsr   r   r   r%   r&   r#   r#   r#   r$   Ú<module>   s   P