3
ÐÆ‘[Ül  ã               @   sî   d Z ddlZddlZddlZddl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mZmZmZ ddlmZmZ ddl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G dd„ deƒZG dd„ deeƒZ dS )zfA VTK interactor scene widget for the PyFace wxPython backend.  See
the class docs for more details.

é    N)Útvtk)Ú	messenger)ÚInstanceÚButtonÚAnyÚBool)ÚViewÚGroupÚItemÚInstanceEditor)ÚWidgetÚGUI)Úpicker)Úlight_manager)Ú
popup_save)Ú	TVTKSceneé   )ÚwxVTKRenderWindowInteractorc               @   s    e Zd ZdZdd„ Zdd„ ZdS )Ú
FullScreenag  Creates a full screen interactor widget.  This will use VTK's
    event loop until the user presses 'q'/'e' on the full screen
    window.  This does not yet support interacting with any widgets on
    the renderered scene.

    This class is really meant to be used for VTK versions earlier
    than 5.1 where there was a bug with reparenting a window.

    c             C   s   || _ |j| _|j| _d S )N)ÚsceneÚrender_windowÚold_rwÚrendererÚren)Úselfr   © r   úZ/home/yuhw/wc/wire-cell-python/venv/lib/python3.6/site-packages/tvtk/pyface/ui/wx/scene.pyÚ__init__7   s    zFullScreen.__init__c             C   sÂ   | j j| jƒ tjddd�}|j| jƒ tjdkr@tt	j
ƒ ƒ|_tjƒ }tj||d�| _| j jrr|j| j jdd� | jjƒ  | jjƒ  | jjƒ  |j| jƒ | j j| jƒ | j jƒ  | jjƒ  d S )NT)Zstereo_capable_windowÚfull_screenÚdarwin)r   Zinteractor_style)Ústereo_typeÚstereo_render)r   Zremove_rendererr   r   ZRenderWindowÚadd_rendererÚsysÚplatformÚtupleÚwxÚGetDisplaySizeÚsizeZInteractorStyleTrackballCameraZRenderWindowInteractorZirenr!   Ú	trait_setr    Ú
initializeÚrenderÚstartÚdisable)r   Zfull_rwÚstyler   r   r   Úrun<   s$    





zFullScreen.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r/   r   r   r   r   r   -   s   	r   c               @   sB   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	d
„Zdd„ Zdd„ Z	dS )Ú
PopupScenezÓPops up a Scene instance with an independent `wx.Frame` in
    order to produce either a standalone window or usually a full
    screen view with *complete* interactivity (including widget
    interaction).
    c             C   s,   d | _ d | _d | _d | _|| _| jj| _d S )N)Úorig_parentÚ	orig_sizeÚorig_posÚframer   Ú_vtk_controlÚvtk_control)r   r   r   r   r   r   m   s    zPopupScene.__init__c             C   s:   | j }|jƒ | _|jƒ | _|jƒ | _tjd dƒ }| _	|S )Nr   éÿÿÿÿ)
r:   Ú	GetParentr5   ÚGetSizer6   ÚGetPositionr7   r&   ÚFramer8   )r   r:   Úfr   r   r   Ú_setup_frameu   s    


zPopupScene._setup_framec             C   sŽ   | j }| j}|j}d|_|j}dd„ |_|jƒ }tjdkr^tjdkr^|j	t
|jƒ ƒƒ |jƒ  |j|ƒ tjƒ jdƒ ||_|jƒ  ||_dS )z0Reparent VTK control to another widget.
        Tc               S   s   d S )Nr   r   r   r   r   Ú<lambda>‡   s    z)PopupScene.reparent_vtk.<locals>.<lambda>r   Ú	__WXMSW__N)r   r:   Údisable_renderÚRenderÚGetRenderWindowr#   r$   r&   ÚPlatformÚSetNextWindowInfoÚstrÚ	GetHandleÚWindowRemapZReparentÚGetAppÚYield)r   Úwidgetr   ÚvcZorig_disable_renderZorig_renderÚrwr   r   r   Úreparent_vtk}   s    

zPopupScene.reparent_vtkNc             C   sH   | j }| jƒ }|dkr&|j|jƒ ƒ n
|j|ƒ |jdƒ | j|ƒ dS )zACreate a popup window of scene and set its default size.
        NT)r:   rA   ÚSetSizer=   ÚShowrQ   )r   r(   rO   r@   r   r   r   Úpopup“   s    

zPopupScene.popupc             C   s*   | j ƒ }|jdƒ | j|ƒ |jdƒ dS )z(Create a popup window of scene.
        TN)rA   rS   rQ   ÚShowFullScreen)r   r@   r   r   r   Ú
fullscreenŸ   s    

zPopupScene.fullscreenc             C   sb   | j }|dkrdS | j}| j| jƒ |j| jƒ |j| jƒ |jdƒ |j	dƒ |j
ƒ  d| _ dS )z6Close the window and reparent the TVTK scene.
        NF)r8   r:   rQ   r5   rR   r6   ÚSetPositionr7   rU   rS   ÚClose)r   r@   rO   r   r   r   Úclose§   s    

zPopupScene.close)N)
r0   r1   r2   r3   r   rA   rQ   rT   rV   rY   r   r   r   r   r4   g   s   
r4   c                   sÆ  e Zd ZdZdZedƒZeej	ƒZe
edd�edd�ddd	�Zee
e
ed
d�edd�edd�edd�edd�edd�edd�edd�edd�eddd�ƒ
e
edddeeeƒd�dd�ƒd d�e
edddd�dd�e
edddd�d d�d!d"gd#�ZeeƒZeZeZdQ‡ fd%d&„	Z‡ fd'd(„Zd)d*„ Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Zd9d:„ Z d;d<„ Z!d=d>„ Z"d?d@„ Z#‡ fdAdB„Z$dCdD„ Z%dEdF„ Z&dGdH„ Z'dIdJ„ Z(dKdL„ Z)dMdN„ Z*dOdP„ Z+‡  Z,S )RÚSceneaæ  A VTK interactor scene widget for pyface and wxPython.

    This widget uses a RenderWindowInteractor and therefore supports
    interaction with VTK widgets.  The widget uses TVTK.  In addition
    to the features that the base TVTKScene provides this widget
    supports:

    - saving the rendered scene to the clipboard.

    - picking data on screen.  Press 'p' or 'P' when the mouse is over
      a point that you need to pick.

    - The widget also uses a light manager to manage the lighting of
      the scene.  Press 'l' or 'L' to activate a GUI configuration
      dialog for the lights.

    - Pressing the left, right, up and down arrow let you rotate the
      camera in those directions.  When shift-arrow is pressed then
      the camera is panned.  Pressing the '+' (or '=')  and '-' keys
      let you zoom in and out.

    - Pressing the 'f' key will set the camera focal point to the
      current point.

    - full screen rendering via the full_screen button on the UI.

    r   zFull Screenr!   )Únamer    TzStereo rendering)Úshow_borderÚlabelÚ
backgroundÚ
foregroundÚparallel_projectionrD   Úoff_screen_renderingZjpeg_qualityZjpeg_progressiveÚmagnificationZanti_aliasing_framesr   F)r[   Ú
show_labelr   Úcustomzobject.stereo)Úview)r[   r.   Úvisible_whenÚeditorrc   )r]   r   )r[   r.   rc   ZLightsZmovie_makerZMovieÚOKÚCancel)ÚbuttonsNc                s$   t t| ƒj|f|Ž tj| ƒ| _dS )z Initializes the object. N)ÚsuperrZ   r   r   ÚPicker)r   ÚparentÚtraits)Ú	__class__r   r   r     s    zScene.__init__c                s,   t t| ƒjƒ }xdD ]}|j|dƒ qW |S )zAllows us to pickle the scene.r9   Ú_fullscreenÚ_interactingN)r9   rp   rq   )rk   rZ   Ú__get_pure_state__Úpop)r   ÚdÚx)ro   r   r   rr   #  s    
zScene.__get_pure_state__c             C   s   | j s| jjƒ  dS )zf Force the scene to be rendered. Nothing is done if the
        `disable_render` trait is set to True.N)rD   r9   rE   )r   r   r   r   r+   /  s    zScene.renderc             C   s
   | j jƒ S )z!Return size of the render window.)r9   r=   )r   r   r   r   Úget_size5  s    zScene.get_sizec             C   s   | j j|ƒ dS )zSet the size of the window.N)r9   rR   )r   r(   r   r   r   Úset_size9  s    zScene.set_sizec             C   s   | j jƒ  dS )zHide the cursor.N)r9   Ú
HideCursor)r   r   r   r   Úhide_cursor=  s    zScene.hide_cursorc             C   s   | j jƒ  dS )zShow the cursor.N)r9   Ú
ShowCursor)r   r   r   r   Úshow_cursorA  s    zScene.show_cursorc             C   sf   t jƒ \}}| j|ƒ tj|tjƒ}tj|ƒ}tjjƒ  tjj	|ƒ tjj
ƒ  tj|ƒ tj|ƒ dS )z-Saves a bitmap of the scene to the clipboard.N)ÚtempfileÚmkstempZsave_bmpr&   ZBitmapZBITMAP_TYPE_BMPZBitmapDataObjectZTheClipboardÚOpenÚSetDatarX   ÚosrY   Úunlink)r   Úhandlerr[   ÚbmpZbmpdor   r   r   Úsave_to_clipboardH  s    




zScene.save_to_clipboardc       	      C   sÖ  |j ƒ }|jƒ }| j}|dk �r^t|ƒ}|dkrP|jdƒ | jƒ  | jdƒ dS |dkrx|jdƒ | jƒ  | jd	ƒ dS |jƒ d ksŽ|tj	kr–| j
ƒ  |jƒ d!kr®|jƒ  dS |jƒ d"krÄ| jdƒ |jƒ d#krÜ|jƒ  dS |jƒ d$k� rö|jƒ  dS |jƒ d%k�r| �r| jjƒ  dS |jƒ d&k�r^| �r^| jjƒ }t|ƒ}t|ƒdk�rZ| j|ƒ dS |jƒ }|tjk�r¸|�rŽ|jd'ƒ | jdƒ n|jdƒ | jdƒ | jƒ  | jdƒ dS |tjk�r
|�rà|jdƒ | jdƒ n|jd(ƒ | jdƒ | jƒ  | jdƒ dS |tjk�rd|�r2|jd)ƒ | jdƒ n|jd*ƒ | jdƒ |jƒ  | jƒ  | jdƒ dS |tjk�r¾|�rŒ|jdƒ | jdƒ n|jdƒ | jdƒ |jƒ  | jƒ  | jdƒ dS | jj|ƒ |jƒ  dS )+zèThis method is overridden to prevent the 's'/'w'/'e'/'q'
        keys from doing the default thing which is generally useless.
        It also handles the 'p' and 'l' keys so the picker and light
        manager are called.
        é   ú-gš™™™™™é?zcamera.zoom(0.8)
render()Nú=ú+g      ô?zcamera.zoom(1.25)
render()ÚqÚeÚwÚrzreset_zoom()Úpr@   ÚlÚsr   é   zcamera.yaw(-5)zcamera.azimuth(5)zrender()zcamera.yaw(5)zcamera.azimuth(-5)zcamera.pitch(-5)zcamera.elevation(-5)z'camera.orthogonalize_view_up()
render()zcamera.pitch(5)zcamera.elevation(5))r‡   rˆ   )r‰   rŠ   )r‹   )rŒ   )r�   )r@   )rŽ   )r�   éûÿÿÿr‘   r‘   r‘   )Ú
GetKeyCodeÚHasModifiersÚcameraÚchrZzoomr+   Ú_record_methodsÚlowerr&   Z
WXK_ESCAPEÚ_disable_fullscreenÚSkipr   Z	configurer9   r<   r   ÚlenÚsaveZ	ShiftDownZWXK_LEFTÚyawÚazimuthZ	WXK_RIGHTZWXK_UPÚpitchÚ	elevationZorthogonalize_view_upZWXK_DOWNÚ	OnKeyDown)	r   ÚeventÚkeycodeÚ	modifiersr”   Úkeyrm   ÚfnameÚshiftr   r   r   r    W  sœ    
























zScene.OnKeyDownc       	      C   s\  |j ƒ }|jƒ }|dk �rDt|ƒ}|jƒ dkr:|jƒ  dS |jƒ dkrÂ|sÂtjdkrb| jj\}}n|j	ƒ }| j
jƒ d	 |jƒ  }| jj||ƒ}|j}|dk	rÂ|| j_| jƒ  | jd
t|ƒ ƒ dS |jƒ dk�r*|�stjdkrî| jj\}}n|j	ƒ }| j
jƒ d	 |jƒ  }| jj||ƒ dS |jƒ  dS |jƒ dk�rD|jƒ  dS | j
j|ƒ |jƒ  dS )a  This method is overridden to prevent the 's'/'w'/'e'/'q'
        keys from doing the default thing which is generally useless.
        It also handles the 'p' and 'l' keys so the picker and light
        manager are called.  The 'f' key sets the camera focus.
        r…   r�   r‹   rŠ   r‰   Nr@   r   r   z camera.focal_point = %r
render()r�   rŽ   )r�   r‹   rŠ   r‰   )r@   )r�   )rŽ   )r’   r“   r•   r—   r™   r#   r$   Ú_interactorZevent_positionÚGetXr9   r=   ÚGetYr   Z
pick_worldZ
coordinater”   Úfocal_pointr+   r–   ÚlistZpickÚOnKeyUp)	r   r¡   r¢   r£   r¤   ru   ÚyÚdataZcoordr   r   r   r¬   ¾  sH    


zScene.OnKeyUpc             C   s¶   | j dkrdS | j j|ƒ t| jjƒdkr.dS tj| ƒ| _| j}|jƒ  t	j
|ƒ}|jdtjƒ tj|d| jƒ |jdtjƒ tj|d| jƒ | j jtjdƒ | j jtj| j jƒ dS )aë  This method is overridden temporarily in order to create
        the light manager.  This is necessary because it makes sense
        to create the light manager only when the widget is realized.
        Only when the widget is realized is the VTK render window
        created and only then are the default lights all setup
        correctly.  This handler is removed on the first Paint event
        and the default paint handler of the
        wxVTKRenderWindowInteractor is used instead.Nr   Ú
StartEventÚEndEvent)r9   ÚOnPaintrš   r   Zlightsr   ZLightManagerÚ_renwinÚupdate_traitsr   Úto_vtkZadd_observerr   ÚsendÚconnectÚ_start_event_callbackÚ_end_event_callbackÚBindr&   Ú	EVT_PAINT)r   r¡   ÚrenwinZvtk_rwr   r   r   r±   ó  s    


zScene.OnPaintc             C   s$   | j dk	r | jj|ƒ | j jƒ  dS )zYOverrides the default OnSize in order to refresh the traits
        of the render window.N)r²   r9   ÚOnSizer³   )r   r¡   r   r   r   r¼     s    
zScene.OnSizec             C   s   d| _ | jj|ƒ dS )z5Overrides the default on button down method.
        TN)rq   r9   ÚOnButtonDown)r   r¡   r   r   r   r½     s    zScene.OnButtonDownc             C   s   d| _ | jj|ƒ d S )NF)rq   r9   Ú
OnButtonUp)r   r¡   r   r   r   r¾   $  s    zScene.OnButtonUpc                s   t t| ƒjƒ  d | _d | _d S )N)rk   rZ   Ú_closed_firedr   r9   )r   )ro   r   r   r¿   ,  s    zScene._closed_firedc             C   sn  t |d| jd� | _}|jtjdƒ |jtj| jƒ |jtjdƒ |jtj| jƒ |jtj	dƒ |jtj	| j
ƒ |jtjdƒ |jtj| jƒ x2tjtjtjfD ]}|j|dƒ |j|| jƒ q W x2tjtjtjfD ]}|j|dƒ |j|| jƒ qÔW |jdƒ |jƒ jƒ  tj|jƒ ƒ }| _|j| j| j| jd� tj ƒ | _!|j"| j!ƒ | j#| _$| j%| j!_%| j&d| j!ƒ | j'j(| j)dƒ | j*| j$_*| j&d| j$ƒ | j+|_+| j&d| jƒ | j,j(| j)dƒ | j,j(| j)dƒ | j,j(| j)d	ƒ | j#j(| j)dƒ d
d„ }tj-dk�r
|||ƒ n2tj.d dk�r4tj.d dk �r4|||ƒ |j/ƒ  |fdd„}d|_0|jtj1|ƒ tj|j2ƒ| _3|S )zA Create the toolkit-specific control that represents the widget. r   )ÚstereoN)Úpoint_smoothingÚline_smoothingÚpolygon_smoothingr^   r`   ra   r!   r    c             S   s2   |j dƒ | j|jƒ ƒ tjƒ jdƒ | jƒ  dS )z3A hack to get the VTK scene properly setup for use.r   TN)rS   rR   r=   r&   rL   rM   rE   )Úwindowrm   r   r   r   Ú_show_parent_hackm  s    
z0Scene._create_control.<locals>._show_parent_hackrC   r   é   r�   c             S   sh   t j|ƒ}|jƒ }|j|d d |d d fƒ |j|ƒ | jd8  _|jdk rd|jt jd ƒ |`d S )Nr   r   )r&   ZGetTopLevelParentr=   rR   Ú_idle_countr¹   ÚEVT_IDLE)r¡   rÄ   r‹   Úszr   r   r   Ú_do_idle‰  s    


z'Scene._create_control.<locals>._do_idler;   )4r   rÀ   r9   r¹   r&   ZEVT_CHARr    Z
EVT_KEY_UPr¬   rº   r±   ZEVT_SIZEr¼   ZEVT_LEFT_DOWNZEVT_RIGHT_DOWNZEVT_MIDDLE_DOWNr½   ZEVT_LEFT_UPZEVT_RIGHT_UPZEVT_MIDDLE_UPr¾   ÚEnableÚGetInteractorStyleÚ SetCurrentStyleToTrackballCamerar   Zto_tvtkrF   r²   r)   rÁ   rÂ   rÃ   ZRendererÚ	_rendererr"   r”   Z_camerar^   Ú
sync_traitr   Úon_trait_changer+   r`   ra   r   rG   ÚVERSIONÚUpdaterÇ   rÈ   Z_Irenr§   )r   rm   rÄ   Zevtr»   rÅ   rÊ   r   r   r   Ú_create_control4  sb    






 

zScene._create_controlc             C   sT   | j jrdS | j}x|r,|jƒ  r,|jƒ }qW |rP|jƒ  tjƒ jdƒ | j	ƒ  dS )zJLift the window to the top. Useful when saving screen to an
        image.NT)
r   ra   r9   Z
IsTopLevelr<   ÚRaiser&   rL   rM   r+   )r   r‹   r   r   r   Ú_liftš  s    zScene._liftc             C   s   | j r
d S d| _d S )NT)rq   Úbusy)r   Úobjr¡   r   r   r   r·   ©  s    zScene._start_event_callbackc             C   s   | j r
d S d| _d S )NF)rq   rÖ   )r   r×   r¡   r   r   r   r¸   ¯  s    zScene._end_event_callbackc             C   s   t j|ƒ d S )N)r   Zset_busy)r   Úvalr   r   r   Ú_busy_changedµ  s    zScene._busy_changedc             C   s¬   | j }t|tƒr |jƒ  d | _ nˆ|d kr¨tjƒ }d}tjdkrDd}n2|jdkrTd}n"|jdkrv|j	dksr|j
dkrvd}|r’t| ƒ}|| _ |jƒ  nt| ƒ}|jƒ  d | _ d S )NFrC   Tr�   r   rÆ   )rp   Ú
isinstancer4   rY   r   ÚVersionr&   rG   Úvtk_major_versionÚvtk_minor_versionÚvtk_build_versionrV   r   r/   )r   ÚfsÚverrT   r@   r   r   r   Ú_full_screen_fired¸  s,    






zScene._full_screen_firedc             C   s"   | j }t|tƒr|jƒ  d | _ d S )N)rp   rÚ   r4   rY   )r   rß   r   r   r   r˜   Ô  s    
zScene._disable_fullscreen)N)-r0   r1   r2   r3   Ú__version__r   r   r   r   rl   r	   r
   Z_stereo_viewr   r   Zdefault_viewr   r9   r   rp   r   rq   r   rr   r+   rv   rw   ry   r{   r„   r    r¬   r±   r¼   r½   r¾   r¿   rÓ   rÕ   r·   r¸   rÙ   rá   r˜   Ú__classcell__r   r   )ro   r   rZ   »   sx   



g5$frZ   )!r3   r#   r€   r|   r&   Útvtk.apir   r   Ú
traits.apir   r   r   r   Útraitsui.apir   r	   r
   r   Ú
pyface.apir   r   Ztvtk.pyfacer   r   Ztvtk.pyface.utilsr   Ztvtk.pyface.tvtk_scener   r   Úobjectr   r4   rZ   r   r   r   r   Ú<module>   s"   :T