a
    «ˆcÃ(  ã                   @   s�   d Z ddlZddl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mZ ddlmZ ddlmZ ddlmZ ddlmZ G d	d
„ d
eƒZdS )zYA component that provides a selection of implicit widgets
to be used by various modules.
é    N)	ÚInstanceÚTraitÚBoolÚTraitMapÚEnumÚDictÚStrÚIntÚAny)ÚViewÚGroupÚItem)Útvtk)Ú	set_state)Ú	Component)ÚDataSetHelperc                       s\  e Zd ZdZedddddd�Zeejdd	�Z	e
d
edddœƒdd�Zeejdd�ZedƒZedƒZedƒZeZeeeejdd�ƒZeeeejdd�ƒZeeedd�edddd�ddd�dd�Z‡ fdd„Z‡ fdd„Zdd„ Z d d!„ Z!d"d#„ Z"d$d%„ Z#d&d'„ Z$d(d)„ Z%d<d*d+„Z&d,d-„ Z'd.d/„ Z(d0d1„ Z)d2d3„ Z*‡ fd4d5„Z+d6d7„ Z,d8d9„ Z-d:d;„ Z.‡  Z/S )=ÚImplicitWidgetsr   ÚBoxÚSphereÚPlaneÚImplicitPlanezthe implicit widget to use)ÚdescT)Úrecordúsemi-interactiveZInteractionEventZEndInteractionEvent)Úinteractiver   z)speed at which the data should be updatedF)Ú
allow_noneéÿÿÿÿÚwidget_mode)ÚnameÚwidgetZcustom)r   ÚstyleÚ	resizablezWidget Source)ÚlabelZshow_labels)r!   c                    s.   t t| ƒjf i |¤Ž d|vr*|  | j¡ d S )Nr   )Úsuperr   Ú__init__Ú_widget_mode_changedr   )ÚselfZtraits©Ú	__class__© úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/components/implicit_widgets.pyr$   G   s    zImplicitWidgets.__init__c                    sT   t t| ƒ ¡ }dD ]}| |d ¡ qt ¡ }| jd }| |¡ t 	|j
¡|d< |S )N)Ú_firstÚ_busyÚ_observer_idr   Úimplicit_functionr   Úmatrix)r#   r   Ú__get_pure_state__Úpopr   Ú	TransformÚ_widget_dictZget_transformÚpickleÚdumpsr/   )r&   ÚdÚattrÚtfmÚwr'   r)   r*   r0   S   s    

z"ImplicitWidgets.__get_pure_state__c                 C   sÒ   |  d¡}t| |dgdgd� t| |dgd� t ¡ }| t |¡¡ | jd }| |¡ | j	}t
| jƒdkr†|  || jd jd ¡ | ¡  | j}|dkr¾|j| }|j|_|j|_| ¡  |  ¡  |g| _d S )	Nr/   r   Ú*)ÚfirstÚignore)r<   r   r   r   )r1   r   r   r2   Z
set_matrixr4   Úloadsr3   Zset_transformr   ÚlenÚinputsÚconfigure_inputÚoutputsÚupdate_traitsr   ÚoriginÚnormalZupdate_placementÚupdate_implicit_functionÚwidgets)r&   ÚstateÚmatr8   r9   ÚmodeÚwdr)   r)   r*   Ú__set_pure_state___   s&    



z"ImplicitWidgets.__set_pure_state__c                 C   s   | j g| _dS )zMOverride this method so that it *creates* the tvtk
        pipeline.
        N)r   rF   ©r&   r)   r)   r*   Úsetup_pipeline€   s    zImplicitWidgets.setup_pipelinec                 C   s�   t | jƒdkrdS | jd jd }| j}|  ||¡ t| jd jd ƒ}| ¡ | _| jrj| 	| j¡ d| _| j|gkr€|g| _nd| _
d| _dS )zêOverride this method so that it *updates* the tvtk pipeline
        when data upstream is known to have changed.

        This method is invoked (automatically) when any of the inputs
        sends a `pipeline_changed` event.
        r   NFT)r>   r?   rA   r   r@   r   Z
get_boundsÚ_boundsr+   Úplace_widgetÚdata_changedZpipeline_changed)r&   Úinpr9   Zdshr)   r)   r*   Úupdate_pipeline‡   s    

zImplicitWidgets.update_pipelinec                 C   s
   d| _ dS )zÌOverride this method so that it flushes the vtk pipeline if
        that is necessary.

        This method is invoked (automatically) when any of the inputs
        sends a `data_changed` event.
        TN)rP   rL   r)   r)   r*   Úupdate_data£   s    zImplicitWidgets.update_datac                 C   s.   dddddœ}t | j|| j ƒ}|| jƒ dS )z;Update the implicit_function from the widget data.
        Z
get_sphereZ
get_planesZ	get_plane)r   r   r   r   N)Úgetattrr   r   r.   )r&   ÚdispatchÚmethodr)   r)   r*   rE   ¯   s
    ÿz(ImplicitWidgets.update_implicit_functionc                 C   s€   t | jƒdkrB|  || jd jd ¡ | jr:| | j¡ n| ¡  | j| j | _|d urf| j	|dd� | j	|dd� |g| _
d S )Nr   T©ÚremoveF)r>   r?   r@   rA   rN   rO   Ú_implicit_function_dictr   r.   Ú_connectrF   )r&   ÚoldÚvaluer)   r)   r*   Ú_widget_changedº   s    zImplicitWidgets._widget_changedc                 C   s    |r| j dkr| | j ¡ n| | j| j¡| _ t|tjƒsFt|tjƒr||j	| j
d|d� |j	| j
d|d� |j	| j
d|d� |j	| j|d� |j	| j|d� dS )zsWire up event handlers or tear them down given a widget
        `value`.  If `remove` is True, then tear them down.r   Znormal_to_x_axisrW   Znormal_to_y_axisZnormal_to_z_axisN)r-   Úremove_observerÚadd_observerÚupdate_mode_Ú_on_interaction_eventÚ
isinstancer   ÚPlaneWidgetÚImplicitPlaneWidgetÚon_trait_changeÚ_on_alignment_setÚ_on_widget_trait_changedÚrender)r&   r\   rX   r)   r)   r*   rZ   È   s*    ÿ
ÿÿÿÿÿzImplicitWidgets._connectc                 C   s   |   ¡  d S ©N)rE   )r&   ÚobjÚeventr)   r)   r*   ra   Ý   s    z%ImplicitWidgets._on_interaction_eventc                 C   sD   | j }|d ur@| | j¡ | | j| j¡| _| | j¡ |  ¡  d S ri   )r   r^   r-   r_   r`   ra   re   rh   )r&   r[   Únewr9   r)   r)   r*   Ú_update_mode_changedà   s    ÿz$ImplicitWidgets._update_mode_changedc                 C   s>   | j s:| jdkr:d| _ | j| j | _|  ¡  |  ¡  d| _ d S )Nznon-interactiveTF)r,   Úupdate_moderY   r   r.   rE   rh   rL   r)   r)   r*   rg   ê   s    z(ImplicitWidgets._on_widget_trait_changedc                 C   s&   | j }| | j¡ | ¡  |  ¡  dS )zHEvent handler when the widget's normal is reset (if
        applicable).N)r   rO   rN   rB   rh   ©r&   r9   r)   r)   r*   rf   ò   s    z!ImplicitWidgets._on_alignment_setc                    s$   t t| ƒ ||¡ |  d |j¡ d S ri   )r#   r   Ú_scene_changedZ_foreground_changed_for_sceneZ
foreground)r&   r[   rl   r'   r)   r*   rp   ú   s    zImplicitWidgets._scene_changedc                 C   s   | j | j | _dS )z[This method is invoked (automatically) when the `source`
        trait is changed.
        N)r3   r   r   )r&   r\   r)   r)   r*   r%   þ   s    z$ImplicitWidgets._widget_mode_changedc                 C   s4   t jdd�t jdd�t jdd�t jddd�dœ}|S )úDefault value for source dict.gÍÌÌÌÌÌì?)Úplace_factorF)rr   Z
draw_plane©r   r   r   r   )r   Z	BoxWidgetZSphereWidgetrc   rd   ro   r)   r)   r*   Z__widget_dict_default  s    


ÿüz%ImplicitWidgets.__widget_dict_defaultc                 C   s"   t  ¡ t  ¡ t  ¡ t  ¡ dœ}|S )rq   rs   )r   ZPlanesr   r   )r&   Úipr)   r)   r*   Z __implicit_function_dict_default  s    ýz0ImplicitWidgets.__implicit_function_dict_default)F)0Ú__name__Ú
__module__Ú__qualname__Ú__version__r   r   r   r   ZThreeDWidgetr   r   r   rn   ZImplicitFunctionr.   r   r+   r,   r	   r-   r
   rN   r   r   r3   rY   r   r   r   Úviewr$   r0   rK   rM   rR   rS   rE   r]   rZ   ra   rm   rg   rf   rp   r%   Z%_ImplicitWidgets__widget_dict_defaultZ0_ImplicitWidgets__implicit_function_dict_defaultÚ__classcell__r)   r)   r'   r*   r      s\   
ÿÿý
ÿ
ÿÿþý!


r   )Ú__doc__r4   Z
traits.apir   r   r   r   r   r   r   r	   r
   Ztraitsui.apir   r   r   Ztvtk.apir   Z"apptools.persistence.state_picklerr   Zmayavi.core.componentr   Zmayavi.core.utilsr   r   r)   r)   r)   r*   Ú<module>   s   ,