a
    «ˆcâG  ã                   @   s|  d Z ddlZddlmZmZmZmZ ddlmZm	Z	 ddlm
Z
 ddlmZmZ ddlmZmZmZmZ ddlm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 dd„ Z e ƒ Z!G dd„ deƒZ"G dd„ deƒZ#G dd„ deƒZ$G dd„ deƒZ%dd„ Z&G dd„ de
ƒZ'G dd„ deƒZ(G dd„ deƒZ)G dd „ d e	ƒZ*G d!d"„ d"e	ƒZ+d*d%d&„Z,d'd(„ Z-e.d)k�rxe-ƒ  dS )+a­  ivtk - Interactive TVTK.

A utility module that makes VTK/TVTK easier to use from the Python
interpreter.  For a standalone application simply run this file.  To
use this under IPython (with --gui=qt) use the `viewer()` helper
function or use the `IVTK` class.  The widget can also make use of the
tvtk pipeline browser.

Here is example usage of the viewer along with tvtk under IPython:

    >>> from tvtk.tools import ivtk
    >>> from tvtk.api import tvtk
    >>> cs = tvtk.ConeSource()
    >>> m = tvtk.PolyDataMapper()
    >>> m.input = cs.output
    >>> a = tvtk.Actor()
    >>> a.mapper = m
    >>> v = ivtk.viewer()
    >>> v.scene.add_actors(a)
    >>> v.scene.reset_zoom()

é    N)Ú
FileDialogÚGUIÚOKÚPythonShell)ÚSplitApplicationWindowÚApplicationWindow)Ú
SplitPanel)ÚSceneÚDecoratedScene)ÚActionÚMenuBarManagerÚMenuManagerÚ	Separator)ÚImageResource)Úresource_path)ÚFloatÚStrÚInstanceÚCallable)Útvtk)ÚPipelineBrowserc                  C   s   t j tƒ dd¡} t| ƒS )NZimagesz	scene.ico)ÚosÚpathÚjoinr   r   )Z	icon_path© r   úR/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/tvtk/tools/ivtk.pyÚmk_scene_icon2   s    r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
ExitActionz Exits the application. c                 C   s   || _ d| _dS )ú Creates a new action. zE&xitN©Ú_windowÚname©ÚselfÚwindowr   r   r   Ú__init__=   s    zExitAction.__init__c                 C   s   | j  ¡  dS ©z Performs the action. N)r    Úclose©r#   r   r   r   ÚperformB   s    zExitAction.performN©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r%   r)   r   r   r   r   r   ;   s   r   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSaveImageActionz%Saves the rendered scene to an image.c                 C   s   || _ d| _d S )NzS&ave Scener   r"   r   r   r   r%   L   s    zSaveImageAction.__init__c                 C   sv   g d¢}g d¢}d}t ||ƒD ]\}}|d |||¡7 }q|d7 }t| jjd|dd�}| ¡ tkrr| jj |j	¡ d	S )
z4Pops up a dialog used to save the scene to an image.)z*.pngz*.jpgz*.tiffz*.bmpz*.psz*.epsz*.pdfz*.texz*.ribz*.wrlz*.ooglz*.vrmlz*.objz*.ivz*.povz*.x3d)ZPNGZJPGZTIFFZBitmapZ
PostScriptZEPSZPDFZTexZRIBZWRLZGeomviewZVRMLZ	WavefrontzOpen InventorZPovrayZX3DÚ z{} ({})|{}|z"Determine by extension (*.*)|(*.*)zsave aszSave scene to image)ÚparentÚactionÚwildcardÚtitleN)
ÚzipÚformatr   r    ÚcontrolÚopenr   ÚsceneÚsaver   )r#   Ú
extensionsZdescriptionsr3   ÚdescriptionÚ	extensionZdlgr   r   r   r)   P   s    þ
ÿzSaveImageAction.performNr*   r   r   r   r   r/   J   s   r/   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSaveToClipboardActionz( Saves rendered scene to the Clipboard. c                 C   s   || _ d| _dS )r   z&CopyNr   r"   r   r   r   r%   l   s    zSaveToClipboardAction.__init__c                 C   s   | j j ¡  dS r&   )r    r9   Zsave_to_clipboardr(   r   r   r   r)   q   s    zSaveToClipboardAction.performNr*   r   r   r   r   r>   j   s   r>   c                   @   s    e Zd ZdZdd„ Zdd„ ZdS )ÚSpecialViewActionz$Sets the scene to a particular view.c                 C   s   || _ || _|| _dS )r   N)r    r!   Úview)r#   r$   r!   r@   r   r   r   r%   {   s    zSpecialViewAction.__init__c                 C   s2   zt | jj| jƒ}|ƒ  W n ty,   Y n0 dS r&   )Úgetattrr    r9   r@   ÚAttributeError)r#   Úmethr   r   r   r)   �   s
    
zSpecialViewAction.performNr*   r   r   r   r   r?   y   s   r?   c                 C   sŒ   t tt| ƒtƒ t| ƒdd�tt| ƒdd�tt| ddƒtƒ t| ddƒt| dd	ƒt| d
dƒt| ddƒt| ddƒt| ddƒt| ddƒdd�
ƒ}|S )z¸Creates a menu bar suitable for all IVTK windows.

    Parameters
    ----------

    - obj : A Pyface application window.

      This is the window which requires the menu items.
    z&File)r!   z&Editz&Reset ZoomZ
reset_zoomz
&IsometricZisometric_viewz&X positiveZx_plus_viewz
X negativeZx_minus_viewz&Y positiveZy_plus_viewz
Y negativeZy_minus_viewz&Z positiveZz_plus_viewz
Z negativeZz_minus_viewz&View)r   r   r/   r   r   r>   r?   )ÚobjÚmenu_bar_managerr   r   r   Úcreate_ivtk_menu‹   s,    ýÿ






÷÷rF   c                       sX   e Zd ZdZedƒZedƒZee	ƒZ
eeƒZ‡ fdd„Zdd„ Zdd	„ Zd
d„ Z‡  ZS )ÚSceneWithBrowserúy Provides an Scene along with an embedded PyCrust Python shell.
    In the shell, 'scene' and 's' are bound to the Scene.g333333Ó?Zverticalc                    s&   | j d ur| j  ¡  tt| ƒ ¡  d S ©N)r9   r'   ÚsuperrG   Údestroyr(   ©Ú	__class__r   r   rK   Ä   s    

zSceneWithBrowser.destroyc                 C   s>   |   |¡ t| jƒ| _|dur&| ¡  | jj|d� | jjjS )ú; Creates the left hand side or top depending on the style. N)r1   )Ú_create_scener   r9   ÚbrowserÚshowZ_uir7   ©r#   r1   r   r   r   Ú_create_lhsÌ   s    
zSceneWithBrowser._create_lhsc                 C   s   |   |¡ d| jj_| jjS )úx Creates the right hand side or bottom depending on the
        style.  's' and 'scene' are bound to the Scene instance.©ç      à?rV   rV   )rO   r9   ÚrendererÚ
backgroundr7   rR   r   r   r   Ú_create_rhsÕ   s    

zSceneWithBrowser._create_rhsc                 C   s   | j du rt|ƒ| _ dS )z, Make sure that the scene has been created. N)r9   r
   rR   r   r   r   rO   ß   s    
zSceneWithBrowser._create_scene)r+   r,   r-   r.   r   Úratior   Ú	directionr   r	   r9   r   rP   rK   rS   rY   rO   Ú__classcell__r   r   rL   r   rG   ±   s   	
rG   c                       s\   e Zd ZdZedƒZedƒZee	ƒZ
eeƒZ‡ fdd„Z‡ fdd„Zdd	„ Zd
d„ Z‡  ZS )ÚIVTKWithCrustrH   çffffffæ?Ú
horizontalc                    s*   t t| ƒjf i |¤Ž d| _t| ƒ| _dS ©z Creates a new window. ú
TVTK SceneN)rJ   r]   r%   r4   rF   rE   ©r#   ZtraitsrL   r   r   r%   û   s    zIVTKWithCrust.__init__c                    s&   | j d ur| j  ¡  tt| ƒ ¡  d S rI   )r9   r'   rJ   r]   r(   rL   r   r   r'     s    

zIVTKWithCrust.closec                 C   s   t |ƒ| _d| jj_| jjS )rN   rU   )r
   r9   rW   rX   r7   rR   r   r   r   rS     s    

zIVTKWithCrust._create_lhsc                 C   s@   t |ƒ| _| j d| j¡ | j d| j¡ | j dt¡ | jjS )rT   r9   Úsr   )r   Úpython_shellÚbindr9   r   r7   rR   r   r   r   rY     s
    
zIVTKWithCrust._create_rhs)r+   r,   r-   r.   r   rZ   r   r[   r   r	   r9   r   rd   r%   r'   rS   rY   r\   r   r   rL   r   r]   è   s   r]   c                       sv   e Zd ZdZedƒZedƒZee	ƒZ
eeƒZeeƒZeeƒZ‡ fdd„Z‡ fdd„ZeeeƒZdd	„ Zd
d„ Z‡  ZS )ÚIVTKWithCrustAndBrowserrH   r^   r_   c                    s*   t t| ƒjf i |¤Ž d| _t| ƒ| _dS r`   )rJ   rf   r%   r4   rF   rE   rb   rL   r   r   r%   >  s    z IVTKWithCrustAndBrowser.__init__c                    s&   | j d ur| j  ¡  tt| ƒ ¡  d S rI   )r9   r'   rJ   rf   r(   rL   r   r   r'   J  s    

zIVTKWithCrustAndBrowser.closec                 C   s&   t |ƒ| _| jj| _| jj| _| jjS )rN   ©rG   Úbrowser_scener9   rP   r7   rR   r   r   r   rS   V  s    


z#IVTKWithCrustAndBrowser._create_lhsc                 C   s`   t |ƒ| _| j d| j¡ | j d| j¡ | j d| j¡ | j d| j¡ | j dt¡ | jjS )rT   r9   rc   rP   Úbr   )r   rd   re   r9   rP   r   r7   rR   r   r   r   rY   ]  s    
z#IVTKWithCrustAndBrowser._create_rhs)r+   r,   r-   r.   r   rZ   r   r[   r   r	   r9   r   rP   rG   rh   r   rd   r%   r'   r   Ú
scene_iconÚiconrS   rY   r\   r   r   rL   r   rf   %  s   

rf   c                       sN   e Zd ZdZeeƒZeeƒZ	‡ fdd„Z
‡ fdd„ZeeeƒZdd„ Z‡  ZS )ÚIVTKú� Provides an Scene along without an embedded Python shell.
    This is useful when scripting from the vanilla Python or IPython
    interpreter.c                    s*   t t| ƒjf i |¤Ž d| _t| ƒ| _dS ©z# Creates a new application window. ra   N)rJ   rl   r%   r4   rF   rE   rb   rL   r   r   r%   |  s    zIVTK.__init__c                    s&   | j d ur| j  ¡  tt| ƒ ¡  d S rI   )r9   r'   rJ   rl   r(   rL   r   r   r'   ‡  s    

z
IVTK.closec                 C   s   |   |¡| _| jjS ©z$ Create the contents of the window. )Ú_scene_factoryr9   r7   rR   r   r   r   Ú_create_contents“  s    zIVTK._create_contents)r+   r,   r-   r.   r   r	   r9   r   r
   rp   r%   r'   r   rj   rk   rq   r\   r   r   rL   r   rl   n  s   

rl   c                       sV   e Zd ZdZeeƒZeeƒZee	ƒZ
‡ fdd„Z‡ fdd„ZeeeƒZdd„ Z‡  ZS )ÚIVTKWithBrowserrm   c                    s*   t t| ƒjf i |¤Ž d| _t| ƒ| _dS rn   )rJ   rr   r%   r4   rF   rE   rb   rL   r   r   r%   ®  s    zIVTKWithBrowser.__init__c                    s&   | j d ur| j  ¡  tt| ƒ ¡  d S rI   )r9   r'   rJ   rr   r(   rL   r   r   r'   ¹  s    

zIVTKWithBrowser.closec                 C   s&   t |ƒ| _| jj| _| jj| _| jjS ro   rg   rR   r   r   r   rq   Å  s    


z IVTKWithBrowser._create_contents)r+   r,   r-   r.   r   r	   r9   r   rP   rG   rh   r%   r'   r   rj   rk   rq   r\   r   r   rL   r   rr   �  s   

rr   TFc                 C   s0   |r
t ƒ }| rtdd�}n
tdd�}| ¡  |S )a«  Creates an IVTK instance, opens the window and returns the
    embedded scene inside it.  This is useful from an IPython/vanilla
    Python shell.  It returns the viewer window instance.

    Parameters
    ----------

    - browser : `bool` (default, True)

      If True, creates an IVTK scene with an embedded PipelineBrowser.
      If False, does not create it.

    - instantiate_gui : `bool` (default: False)

      If True, create an instance of GUI().  This is useful when this
      function is invoked from within an IPython shell.  OTOH, if this
      is called from within a wxPython app (or with ipython --gui=qt)
      you don't want to start another GUI instance.
    )éX  rs   ©Úsize)r   rr   rl   r8   )rP   Zinstantiate_guiÚguiÚvr   r   r   ÚviewerÑ  s    
rx   c                  C   s$   t ƒ } tdd�}| ¡  |  ¡  d S )N)i   rs   rt   )r   rf   r8   Zstart_event_loop)rv   r$   r   r   r   Úmainï  s    
ry   Ú__main__)TF)/r.   Úos.pathr   Z
pyface.apir   r   r   r   r   r   r   Ztvtk.pyface.apir	   r
   Zpyface.action.apir   r   r   r   Zpyface.image_resourcer   Zpyface.resource.apir   Z
traits.apir   r   r   r   Ztvtk.apir   Ztvtk.pipeline.browserr   r   rj   r   r/   r>   r?   rF   rG   r]   rf   rl   rr   rx   ry   r+   r   r   r   r   Ú<module>   s6    &7=I/4


