a
    «ˆcd  ã                   @   sÎ   d Z ddlZddl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 dd	lmZ d
dlmZ eƒ Zeje_ej dd¡Zdd„ ZG dd„ deƒZeƒ ZejZejZejZej Z!dS )z)
Central registry for figures with mlab.
é    N)Ú	HasTraitsÚInstance)Úpreference_manager)Úregistry©ÚEngine©ÚOffScreenEngine©Ú
NullEngine)Úprocess_ui_eventsé   )ÚPreferencesMirrorZETS_TOOLKITÚ c                  C   s\   ddl m}  ddlm} ddlm} | ƒ  |jdkrX|jsX|jdv rXtdkrXd}t	|ƒ‚d	S )
zf Check if either we are in test mode, or if there is a
        suitable traits backend installed.
    r   )Útoolkit)Ú	ETSConfig)ÚoptionsÚtest)Únullr   r   zˆCould not import backend for traitsui.  Make sure you
        have a suitable UI toolkit like PyQt/PySide or wxPython
        installed.N)
Ztraitsui.toolkitr   Ztraits.etsconfig.apir   Zmayavi.tools.engine_managerr   ÚbackendÚ	offscreenÚenv_toolkitÚImportError)r   r   r   Úmsg© r   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/mayavi/tools/engine_manager.pyÚcheck_backend   s    ÿÿr   c                   @   sJ   e Zd ZdZeeƒZdd„ Zdd„ Zdd„ Z	dd	„ Z
d
d„ Zddd„ZdS )ÚEngineManageraF   Central registry for figures with mlab.

        This is a container for a list of engines having declared
        themselves as usable by mlab.

        This object is meant to be a thin wrapper on top of the different
        Engine classes, making sure that mlab knows how to start an
        engine and get a figure.
    c                 C   sö   | j }|dur(|jr"|tj ¡ vr(d| _ | j dur@t| j fƒ}ntƒ }| ttj ¡ ƒ¡ tjdkrtdd„ |D ƒ}nXtjdkrŽdd„ |D ƒ}n>tj	r¤dd„ |D ƒ}n(tjdkr¾d	d„ |D ƒ}nd
d„ |D ƒ}t
|ƒdkrà|  ¡ S |d | _ |d S dS )z: Returns an engine in agreement with the options.
        NÚenvisagec                 S   s   g | ]}|j jd kr|‘qS )ÚEnvisageEngine©Ú	__class__Ú__name__©Ú.0Úer   r   r   Ú
<listcomp>N   s   ÿz,EngineManager.get_engine.<locals>.<listcomp>r   c                 S   s   g | ]}|j jd kr|‘qS r
   r    r#   r   r   r   r&   Q   s   ÿc                 S   s   g | ]}|j jd kr|‘qS r   r    r#   r   r   r   r&   T   s   ÿÚautoc                 S   s   g | ]}|j jd vr|‘qS ))r   r	   r    r#   r   r   r   r&   W   s   ÿc                 S   s   g | ]}|j jd kr|‘qS r   r    r#   r   r   r   r&   [   s   ÿr   éÿÿÿÿ)Úcurrent_engineÚrunningr   ÚenginesÚvaluesÚlistÚextendr   r   r   ÚlenÚ
new_engine)ÚselfÚcer+   Zsuitabler   r   r   Ú
get_engine=   s*    




zEngineManager.get_enginec                 C   s˜   | j }|dur(|jr"|tj ¡ vr(d| _ | j dur@t| j fƒ}ntƒ }| ttj ¡ ƒ¡ d}|D ]}|jjdkrb|} qŽqbt	dd�}| 
¡  || _ |S )zPReturn a suitable null engine and make that the current
        engine.
        Nr   úNull Mlab Engine©Úname)r)   r*   r   r+   r,   r-   r.   r!   r"   r   Ústart)r1   r2   r+   Úenginer%   r   r   r   Úget_null_engined   s"    

zEngineManager.get_null_enginec                 C   s(   |j stjddd� || _t |¡ dS )z Sets the mlab engine.
        zEngine is not runningé   )Ú
stacklevelN)r*   ÚwarningsÚwarnr)   r   Zregister_engine)r1   r8   r   r   r   Ú
set_engine   s    zEngineManager.set_enginec                 C   sž   t ƒ  tjdkrJddlm} |dd�}| ¡  tƒ  |jjj	}| 
t¡}nJtjdkrhtdd�}| ¡  n,tjr‚td	d�}| ¡  ntd
d�}| ¡  || _|S )zU Creates a new engine, envisage or not depending on the
            options.
        r   r   )ÚMayaviF)Zstart_gui_event_loopr   r4   r5   zMlab offscreen EnginezMlab Engine)r   r   r   Zmayavi.plugins.appr?   Úmainr   ZapplicationZ	workbenchZactive_windowZget_servicer   r   r7   r   r	   r)   )r1   r?   ÚmZwindowr8   r   r   r   r0   ‡   s$    








zEngineManager.new_enginec                 C   s.   t j ¡ D ]}||jv r
|  S q
tdƒ‚dS )z@ Find the engine corresponding to a given mayavi scene.
        z'Figure not attached to a mayavi engine.N)r   r+   r,   ZscenesÚ	TypeError)r1   Zfigr8   r   r   r   Úfind_figure_engine    s    

z EngineManager.find_figure_engineNTc                 C   s�   |du r|   ¡ }|jjdksŒtjdkr(nd|rhddlm} |j}||d�}|du rVd}n|j}|j	|d�S ddl
m} |j}||d�}| ¡ S dS )	z§ Show a dialog with the mayavi pipeline. This dialog allows to
            edit graphically the properties of the different objects on
            the scenes.
        Nr   r   r   )ÚEngineRichView)r8   )Úscene)Ú
EngineView)r3   r!   r"   r   r   Zmayavi.core.ui.engine_rich_viewrD   Zcurrent_scenerE   Zscene_editing_viewZmayavi.core.ui.engine_viewrF   Zedit_traits)r1   r8   Z	rich_viewrD   ÚfigureÚviewrE   rF   r   r   r   Úshow_engine©   s$    ÿ

zEngineManager.show_engine)NT)r"   Ú
__module__Ú__qualname__Ú__doc__r   r   r)   r3   r9   r>   r0   rC   rI   r   r   r   r   r   0   s   
'	r   )"rL   Úosr<   Z
traits.apir   r   Zmayavi.preferences.apir   Zmayavi.core.registryr   Zmayavi.core.enginer   Zmayavi.core.off_screen_enginer	   Zmayavi.core.null_enginer   Zmayavi.core.commonr   Zpreferences_mirrorr   r   ZmlabZpreferencesÚenvironÚgetr   r   r   Zengine_managerr3   r9   r>   rI   Zshow_pipeliner   r   r   r   Ú<module>   s*    