a
    §ˆc&:  ã                   @   sŠ   d Z ddlmZmZmZmZmZmZ ddlm	Z	m
Z
mZ ddlmZ ddlZe e¡ZG dd„ de
ƒZG dd	„ d	eƒZG d
d„ deƒZdS )aý  
Provides a framework that assembles Traits UI Views at run time,
when the view is requested, rather than at the time a class is written.

This capability is particularly useful when the object being 'viewed' with a
Traits UI is part of a plug-in application -- such as Envisage.  In general,
this capability allows:

*  The GUI for an object can be extendable by contributions
   other than from the original code writer.
*  The view can be dynamic in that the elements it is composed
   of can change each time it is requested.
*  Registration of a handler can be associated with the view contributions.

Either the original object writer, or a contributor, can use this framework to
declare one or more dynamic views that are composed of sub-elements that only
need to exist at the time the view is requested.

Users of this framework create a dynamic view by registering a DynamicView
declaration.  That declaration includes a name that forms the basis for the
metadata attributes that are used to identify and order the desired view
sub-elements into the view's composition.  In addition, the declaration
includes any data to be passed into the constructor of the dynamic view and
the id that should be used to persist the user's customization of the view.

Additionally, this framework allows sub-elements themselves to also be
dynamically composed of further sub-elements.

For example, a dynamic view could be composed of two sub-elements:

1. The first is a dynamically composed HFlow, which represents a toolbar
   that can be extended through contributions of toolbar buttons.
2. The second could be a dynamic tabset where each page is also a
   contribution.

Programmers include dynamic sub-elements within their dynamic views by
contributing a DynamicViewSubElement into that view.  When the framework comes
across this contribution while building the view, it replaces that
DynamicViewSubElement with a fully initialized Traits ViewSubElement composed
in a manner similar to how the elements of the View itself were composed.

Each contribution to a dynamic view or sub-element must be an instance of a
Traits ViewSubElement and must have associated metadata like the following for
each dynamic view or sub-element it will display in:

_<dynamic name>_order : A float value.
    The framework uses only ViewSubElements with this metadata
    instantiated when building the dynamic view or sub-element with
    the specified name.  The elements are sorted by ascending
    order of this value using the standard list sort function.
_<dynamic name>_priority : A float value.
    The framework resolves any overloading of an order value by
    picking the first element encountered that has the highest
    priority value. The other elements with the same view order are
    not displayed at all.

In addition, dynamic view contributions can also provide a 'handler', which
behaves like a normal Traits Handler.  That is, it can contain methods that
are called when model values change and can access  the Traits UIInfo
object representing the actual UI instances.  To provide a handler, append the
following metadata to your view sub-element:

_<dynamic_name>_handler : A HasTraits instance.
    The framework will connect listeners to call the handler methods as
    part of the handler for the dynamic view.


é    )ÚAnyÚBoolÚDictÚ	HasTraitsÚInstanceÚStr)ÚViewÚViewSubElementÚViewElement)ÚDelegatingHandlerNc                   @   s"   e Zd ZdZeƒ Zeƒ Zeƒ Z	dS )ÚDynamicViewSubElementz;
    Declares a dynamic sub-element of a dynamic view.
    N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úkeywordsr   Úklassr   Úname© r   r   úd/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/extras/has_dynamic_views.pyr   `   s   r   c                   @   s*   e Zd ZdZeƒ Zeƒ Zeƒ Ze	dƒZ
dS )ÚDynamicViewz"
    Declares a dynamic view.
    FN)r   r   r   r   r   Úidr   r   r   r   Úuse_as_defaultr   r   r   r   r   }   s
   r   c                       sh   e Zd ZdZeeeeƒƒZd‡ fdd„	Z	dd„ Z
dd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )ÚHasDynamicViewsz–
    Provides of a framework that builds Traits UI Views at run time,
    when the view is requested, rather than at the time a class is written.
    Nc                    sŠ   d}t |tƒsp|du rT|du s*t|ƒdk rT| j ¡ D ]\}}|jr4|  |¡} qpq4n|du rp|| jv rp|  |¡}|du r†tƒ  ||¡}|S )z†Gets or sets a ViewElement associated with an object's class.

        Extended here to build dynamic views and sub-elements.
        Né   )	Ú
isinstancer
   ÚlenÚ_dynamic_view_registryÚitemsr   Ú_compose_dynamic_viewÚsuperÚ
trait_view)Úselfr   Zview_elementÚresultZdnameÚdeclaration©Ú	__class__r   r   r!   ´   s    

ÿ
zHasDynamicViews.trait_viewc                 C   s   || j |j< dS )z]A convenience method to add a new dynamic view declaration to this
        instance.
        N)r   r   )r"   r$   r   r   r   Údeclare_dynamic_viewÝ   s    z$HasDynamicViews.declare_dynamic_viewc                 C   s"   t  d|j|¡ |j|i |j¤ŽS )zžReturns the fully composed ViewSubElement from the sub-element
        contributions to the dynamic sub-element identified by the
        definition.
        z5	Building dynamic sub-element [%s] with elements [%s])ÚloggerÚdebugr   r   r   )r"   Ú
definitionÚsub_elementsr   r   r   Ú_build_dynamic_sub_elementå   s    ýz*HasDynamicViews._build_dynamic_sub_elementc                 C   s(   t  d|j|¡ t||j|dœ|j¤ŽS )a  Returns a Traits View representing the specified dynamic view
        composed out of the provided view sub-elements.

        Implemented as a separate method to allow implementors to override
        the way in which the instantiated view is configured.
        z.	Building dynamic view [%s] with elements [%s])r   Úhandler)r(   r)   r   r   r   r   )r"   r$   r+   r-   r   r   r   Ú_build_dynamic_viewò   s    ýüú
öz#HasDynamicViews._build_dynamic_viewc                 C   s(   t  d|j| ¡ |  |j¡}|  ||¡S )zAReturns a dynamic UI element composed from its contributed parts.z1Composing dynamic sub-element named [%s] for [%s])r(   r)   r   Ú_get_dynamic_elementsr,   )r"   r*   Úelementsr   r   r   Ú_compose_dynamic_sub_element  s    ýz,HasDynamicViews._compose_dynamic_sub_elementc                 C   sZ   t  d|| ¡ | j| }|  |j¡}d}|  |j|¡}t|ƒdkrLt|d�}|  |||¡S )z;Returns a dynamic view composed from its contributed parts.z$Composing dynamic view [%s] for [%s]Nr   )Zsub_handlers)	r(   r)   r   r/   r   Ú_get_dynamic_handlersr   r   r.   )r"   r   r$   r0   r-   Úhandlersr   r   r   r     s    

z%HasDynamicViews._compose_dynamic_viewc                    sú   |  dd¡}d| ‰d| }‡fdd„ˆjtd�D ƒ}‡fdd„|D ƒ}i ‰ |D ]J}t|ˆƒ}t||ƒpjd	}ˆ  ||¡}||urPt||ƒ}	|	|k rP|ˆ |< qPtˆ ƒ}
‡ fd
d„|
D ƒ}tt|ƒƒD ]2}t|| t	ƒrÂ| 
|¡}ˆ |¡}| ||¡ qÂ|S )z™Returns a list of the current elements meant to go into the
        composition of a dynamic view or subelement with the specified
        name.
        ú Ú_z	_%s_orderz_%s_priorityc                    s   g | ]}ˆ   |¡‘qS r   )r!   )Ú.0Úg)r"   r   r   Ú
<listcomp><  s   z9HasDynamicViews._get_dynamic_elements.<locals>.<listcomp>)r   c                    s(   g | ] }t |ˆ ƒrt|ˆ ƒd ur|‘qS ©N©ÚhasattrÚgetattr©r6   Úe)Úorder_trait_namer   r   r8   ?  s   
ýr   c                    s   g | ]}ˆ | ‘qS r   r   )r6   Úorder)Úfilteredr   r   r8   U  ó    )ÚreplaceZtrait_viewsr	   r<   Ú
setdefaultÚsortedÚranger   r   r   Úpopr1   Úinsert)r"   r   Zpriority_trait_nameZall_elementsr0   r>   r@   ÚpriorityÚcurrentZcurrent_priorityZorderingÚiZcomposedr   )rA   r?   r"   r   r/   .  s4    

ÿ
þ





z%HasDynamicViews._get_dynamic_elementsc                    s6   |  dd¡}d| ‰ ‡ fdd„|D ƒ}t d|¡ |S )zŒReturn a list of the handlers associated with the current elements
        meant to go into the dynamic view of the specified name.
        r4   r5   z_%s_handlerc                    s.   g | ]&}t |ˆ ƒrt|ˆ ƒd urt|ˆ ƒ‘qS r9   r:   r=   ©Zhandler_namer   r   r8   l  s   
ýz9HasDynamicViews._get_dynamic_handlers.<locals>.<listcomp>z	Found sub-handlers: %s)rC   r(   r)   )r"   r   r0   r3   r   rL   r   r2   c  s    
þz%HasDynamicViews._get_dynamic_handlers)NN)r   r   r   r   r   r   r   r   r   r!   r'   r,   r.   r1   r   r/   r2   Ú__classcell__r   r   r%   r   r   ž   s   )5r   )r   Z
traits.apir   r   r   r   r   r   Ztraitsui.apir   r	   r
   Ztraitsui.delegating_handlerr   ÚloggingÚ	getLoggerr   r(   r   r   r   r   r   r   r   Ú<module>   s   F 
!