3
P„-])O  ã               @   sÂ  d Z ddlm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	Z	ddl
mZ
 ddlmZ ejeƒZdd	„ Zd
d„ Zdd„ ZG dd„ dejƒZdZx4d/D ],Zeejkršeje ZeedƒrÂejZ[P qšW edk	rØeZneƒ Z[dd„ Ze
jdddddddddddddddddddddddddd0died�Ze
jdeƒZG d d!„ d!e
j ƒZ!d"d#„ Z"e
jdde
j#e$ed�Z%e
jdde
j#e$ed$d%ifd�Z&e
j'd&d%d'�Z(d(a)ed)d*„ ƒZ*d+d,„ Z+G d-d.„ d.e
j,ƒZ-dS )1z%Commonly used code by tvtk objects.

é    )Úprint_functionN)Úcontextmanager)Úapié   )Ú	messengerc              O   s   ddl m} || |ŽS )Nr   )ÚBooleanEditor)Útraitsui.apir   )ÚargsÚkwÚEditor© r   úQ/home/yuhw/wc/wire-cell-python/venv/lib/python3.6/site-packages/tvtk/tvtk_base.pyr      s    r   c              O   s   ddl m} || |ŽS )Nr   )ÚRGBColorEditor)r   r   )r	   r
   r   r   r   r   r      s    r   c              O   s   ddl m} || |ŽS )Nr   )Ú
FileEditor)r   r   )r	   r
   r   r   r   r   r   #   s    r   c               @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )ÚTVTKObjectCachec             O   s6   i | _ tjj| f|ž|Ž tj| ƒfdd„}|| _d S )Nc             S   s(   |ƒ }|d k	r$|j | jƒ |j| j= d S )N)Úteardown_observersÚkeyÚdata)ÚwrÚselfrefÚselfr   r   r   Úremove1   s    z(TVTKObjectCache.__init__.<locals>.remove)Ú_observer_dataÚweakrefÚWeakValueDictionaryÚ__init__ÚrefÚ_remove)r   r	   r
   r   r   r   r   r   -   s    zTVTKObjectCache.__init__c             C   s`   t |dƒr\tj|||ƒ |j|tjƒ}|j}| j}||krN|| j||fƒ n||fg||< dS )a8  Setup the observer for the VTK object's event.

        Parameters
        ----------

        vtk_obj -- The VTK object for which the `event` is
        observed.

        event -- The VTK event to watch.

        method -- The method to be called back when `event` is
        fired on the VTK object.

        ÚAddObserverN)Úhasattrr   Úconnectr   ÚsendÚ__this__r   Úappend)r   Úvtk_objÚeventÚmethodÚob_idr   Úodr   r   r   Úsetup_observers8   s    
zTVTKObjectCache.setup_observersc             C   sz   | j }||krdS x8|| D ],\}}y|j|ƒ W q tk
rF   Y qX qW ytj|ƒ W n tk
rn   Y nX ||= dS )z Given the key of the VTK object (vtk_obj.__this__), this
        removes the observer for the ModifiedEvent and also disconnects
        the messenger.
        N)r   ÚRemoveObserverÚAttributeErrorr   Z
disconnect)r   r   r(   r$   r'   r   r   r   r   V   s    
z"TVTKObjectCache.teardown_observersN)Ú__name__Ú
__module__Ú__qualname__r   r)   r   r   r   r   r   r   ,   s   r   Ú	tvtk_baseútvtk.tvtk_baseÚ_object_cachec             C   s   t j| jƒS )z2Returns the cached TVTK object given a VTK object.)r1   Úgetr"   )r$   r   r   r   Úget_tvtk_object_from_cache…   s    r3   ÚtrueÚtÚyesÚyÚonÚfalseÚfÚnoÚnÚoff)Úeditorc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚTraitRevPrefixMapaÎ  A reverse mapped TraitPrefixMap.  This handler allows for
    something like the following::

      >>> class A(HasTraits):
      ...     a = Trait('ab', TraitRevPrefixMap({'ab':1, 'cd':2}))
      ...
      >>> a = A()
      >>> a.a = 'c'
      >>> print a.a
      'cd'
      >>> a.a = 1
      >>> print a.a
      'ab'

    That is, you can set the trait to the value itself.  If multiple
    keys map to the same value, one of the valid keys will be used.

    c             C   s8   t jj| |ƒ i | _x|jƒ D ]\}}|| j|< qW d S )N)ÚtraitsÚTraitPrefixMapr   Ú_rmapÚitems)r   Úmapr   Úvaluer   r   r   r   «   s    zTraitRevPrefixMap.__init__c             C   sª   yŠ|| j kr| j | }|| jkr‚d }t|ƒ}x4| jjƒ D ]&}||d |… kr8|d k	rZd }P |}q8W |d krx| j|||ƒ || j|< | j| S    | j|||ƒ Y nX d S )N)rB   Ú_mapÚlenrD   ÚkeysÚerror)r   ÚobjectÚnamerE   Úmatchr<   r   r   r   r   Úvalidate±   s$    




zTraitRevPrefixMap.validatec             C   s:   dd„ | j jƒ D ƒ}|jƒ  dj|ƒ}tjj| ƒd | S )Nc             S   s   g | ]}t |ƒ‘qS r   )Úrepr)Ú.0Úxr   r   r   ú
<listcomp>Æ   s    z*TraitRevPrefixMap.info.<locals>.<listcomp>z or )rB   rH   ÚsortÚjoinr@   rA   Úinfo)r   rH   Úmsgr   r   r   rT   Å   s    
zTraitRevPrefixMap.infoN)r,   r-   r.   Ú__doc__r   rM   rT   r   r   r   r   r?   ˜   s   r?   c          	   K   sÚ   t j}| d dkrBt j| t j|ddƒ|ddƒ|ddƒtd�f|ŽS t| d ƒtkr–t jt j|dd| d ƒ|dd| d ƒ|dd| d ƒƒfdti|—ŽS t jt j|dd| d ƒ|dd| d ƒ|dd| d ƒd	d
�f|ŽS d S )Nr   g      ð?g        )r>   r   é   r>   éÿ   é   )Úcolsg      ð¿)r@   ÚRangeÚTraitÚTupler   ÚtypeÚfloat)ÚdefaultÚmetadatar[   r   r   r   Úvtk_color_traitÌ   s&    rb   Útruncate_extTÚproperty)ÚmodifyFc            
   c   s   zda dV  W dda X dS )a[  Disable updating any traits automatically due to changes in VTK.

    Specifically, do not auto-update *any* objects due to the firing of a
    ModifiedEvent from within VTK. This basically can call `update_traits` on
    the object which can in turn fire off other callbacks.

    Use this sparingly when you REALLY need to disable updates.

    TNF)Ú_DISABLE_UPDATEr   r   r   r   Úglobal_disable_updateõ   s    
rg   c             C   s   t | tƒr| jS | S dS )z8Dereferences the VTK object from the object if possible.N)Ú
isinstanceÚTVTKBaseÚ_vtk_obj)Úobjr   r   r   Ú	deref_vtk  s    
rl   c                   s´   e Zd ZdZdZejZejdde	j
ƒ ƒZejZejZejZd‡ fdd„	Zdd„ Zd	d
„ Zdd„ Zdd„ Ze‡ fdd„ƒZdd„ Zdd„ Zddd„Zd dd„Zdd„ Zdd„ Z‡  ZS )!ri   aB  The base class for all TVTK objects.  This class encapsulates
    key functionality common to all the TVTK classes.

    TVTK classes provide a trait wrapped VTK object.  They also
    primitively picklable.  Only the basic state of the object itself
    is pickled.  References to other VTK objects are NOT pickled.

    é
   NTc                s\   d| _ |r|| _n|ƒ | _d| _ tt| ƒjf |Ž d| _ |rD| jƒ  | jƒ  | t| jj< dS )a¯  Object initialization.

        Parameters
        ----------

        - klass: `vtkObjectBase`

          A VTK class to wrap.  If `obj` is passed, its class must be
          the same as `klass` or a subclass of it.

        - obj: `vtkObjectBase` (default: None)

          An optional VTK object.  If passed the passed object is
          wrapped.  This defaults to `None` where a new VTK instance
          of class, `klass` is created.

        - update: `bool` (default: True)

          If True (default), the traits of the class are automatically
          updated based on the state of the wrapped VTK object.  If
          False, no updation is performed.  This is particularly
          useful when the object is being unpickled.

        - traits: `dict`

          A dictionary having the names of the traits as its keys.
          This allows a user to set the traits of the object while
          creating the object.

        r   r   N)	Ú_in_setrj   Úsuperri   r   Úupdate_traitsr)   r1   r"   )r   Úklassrk   Úupdater@   )Ú	__class__r   r   r   >  s     zTVTKBase.__init__c             C   s   dS )z·This is merely a placeholder so that subclasses can
        override this if needed.  This is called by `__setstate__`
        because `traits.HasTrait` is a newstyle class.

        Nr   )Nr   r   )r   r   r   r   Ú__getinitargs__v  s    zTVTKBase.__getinitargs__c             C   s0   | j ƒ  | jjƒ }xdD ]}|j|dƒ qW |S )zRSupport for primitive pickling.  Only the basic state is
        pickled.
        rj   rn   Úreference_countÚglobal_warning_displayÚ__sync_trait__N)rj   rn   ru   rv   rw   )rp   Ú__dict__ÚcopyÚpop)r   ÚdÚir   r   r   Ú__getstate__€  s    
zTVTKBase.__getstate__c             C   s„   | j dkr| j| jƒ Ž  d| _xZ|D ]R}yt| ||| ƒ W q$ tjk
rt } ztddd� t|ƒ W Y dd}~X q$X q$W d| _dS )zRSupport for primitive pickling.  Only the basic state is
        pickled.
        Nr   zWARNING:ú )Úendr   )rj   r   rt   rn   Úsetattrr@   Ú
TraitErrorÚprint)r   Údictr|   rU   r   r   r   Ú__setstate__‹  s    

zTVTKBase.__setstate__c             C   s
   t | jƒS )z�Return a nice string representation of the object.

        This merely returns the result of str on the underlying VTK
        object.
        )Ústrrj   )r   r   r   r   Ú__str__�  s    zTVTKBase.__str__c       	         s¼   t t| ƒjƒ }|jƒ }tjjtjjtƒƒ}tjj	|dƒ}yj| j
jdƒd }tjj	||d ƒ}i }ttt|ƒjƒ |dƒi |ƒ x"|D ]}||kr‚|| |j|< q‚W W n tk
r¶   Y nX |S )a   Returns the ViewElements object associated with the class.

        The returned object can be used to access all the view elements
        associated with the class.

        Overridden here to search through a particular directory for substitute
        views to use for this tvtk object. The view should be declared in a
        file named <class name>_view. We execute this file and replace any
        currently defined view elements with view elements declared in this
        file (that have the same name).

        ÚviewÚ.r   z_view.pyÚexecéÿÿÿÿ)ro   ri   Úclass_trait_view_elementsÚ	filter_byÚosÚpathÚdirnameÚabspathÚ__file__rS   r-   Úsplitr‰   ÚcompileÚopenÚreadÚcontentÚ	Exception)	ÚclsÚview_elementsÚnamesZbaseDirZviewDirÚmodule_nameZview_filenameÚresultrK   )rs   r   r   r‹   ¨  s&    

z"TVTKBase.class_trait_view_elementsc             C   s   t j| jd| jƒ dS )z¤Add an observer for the ModifiedEvent so the traits are kept
        up-to-date with the wrapped VTK object and do it in a way that
        avoids reference cycles.ÚModifiedEventN)r1   r)   rj   rp   )r   r   r   r   r)   Ó  s    zTVTKBase.setup_observersc             C   s   t j| jjƒ dS )z+Remove the observer for the Modified event.N)r1   r   rj   r"   )r   r   r   r   r   Û  s    zTVTKBase.teardown_observersc             C   sÞ   | j s
trdS t| dƒsdS | j| _ | j}tjjƒ }tjjƒ  xˆ| j	D ]~\}}|dkrdt
| ||ƒ qFyt||ƒƒ }W n ttfk
rŒ   Y qFX yt
| ||ƒ W qF tjk
rÂ   || jkr¼n‚ Y qFX qFW tjj|ƒ d| _ dS )aø  Updates all the 'updateable' traits of the object.

        The method works by getting the current value from the wrapped
        VTK object.  `self._updateable_traits_` stores a tuple of
        tuples containing the trait name followed by the name of the
        get method to use on the wrapped VTK object.

        The `obj` and `event` parameters may be ignored and are not
        used in the function.  They exist only for compatibility with
        the VTK observer callback functions.

        NÚ_updateable_traits_rv   r   )rn   rf   r   ÚDOING_UPDATErj   ÚvtkÚ	vtkObjectÚGetGlobalWarningDisplayÚGlobalWarningDisplayOffrž   r€   Úgetattrr+   Ú	TypeErrorr@   r�   Ú_allow_update_failure_ÚSetGlobalWarningDisplay)r   rk   r%   r$   ÚwarnrK   ÚgetterÚvalr   r   r   rp   ß  s0    




zTVTKBase.update_traitsFc             C   sž   | j | jkrdS | j}|  j d7  _ | j|ƒd }z<y||ƒ W n* tk
rj   t|dƒrd||Ž  n‚ Y nX W d|  j d8  _ X |s’| j|ƒ|krš| jƒ  dS )a¢  This is called by the various traits when they change in
        order to update the underlying VTK object.

        Parameters
        ----------

        - method: `method`

          The method to invoke on the VTK object when called.

        - val: `Any`

          Argument to the method.

        - force_update: `bool` (default: False)

          If True, `update_traits` is always called at the end.

        Nr   Ú__len__)rn   rŸ   rj   Ú_wrapped_mtimer¥   r   rp   )r   r&   rª   Úforce_updater$   Úmtimer   r   r   Ú
_do_change  s    

zTVTKBase._do_changec             G   sZ   | j }|  jd7  _| j|ƒd }z||Ž }W d|  jd8  _X | j|ƒ|krV| jƒ  |S )a¸  This method allows us to safely call a VTK method without
        calling `update_traits` during the call.  This method is
        therefore used to wrap any 'Set' call on a VTK object.

        The method returns the output of the vtk_method call.

        Parameters
        ----------

        - vtk_method: `method`

          The method to invoke safely.

        - args: `Any`

          Argument to be passed to the method.

        r   N)rj   rn   r¬   rp   )r   Z
vtk_methodr	   r$   r®   Úretr   r   r   Ú
_wrap_call;  s    zTVTKBase._wrap_callc             C   s"   y|j ƒ S  tk
r   dS X dS )z©A simple wrapper for the mtime so tvtk can be used for
        `vtk.vtkObjectBase` subclasses that neither have an
        `AddObserver` or a `GetMTime` method.
        r   N)ÚGetMTimer+   )r   r$   r   r   r   r¬   Y  s    zTVTKBase._wrapped_mtime)NT)NN)F)r,   r-   r.   rV   rŸ   r@   ÚPythonrn   r\   r    ÚvtkObjectBaserj   r]   r¦   rž   ÚListZ_full_traitnames_list_r   rt   r}   r„   r†   Úclassmethodr‹   r)   r   rp   r¯   r±   r¬   Ú__classcell__r   r   )rs   r   ri     s&   8
+
7
%ri   )r/   r0   rŠ   ).rV   Ú
__future__r   Úsysr   r�   ÚloggingÚ
contextlibr   r    r@   r   Ú r   Ú	getLoggerr,   Úloggerr   r   r   r   r   Z_dummyrK   ÚmodulesÚmodr   r1   r3   r\   Ztrue_bool_traitZfalse_bool_traitrA   r?   rb   ÚStrr…   Zvtk_file_nameZvtk_file_prefixÚDelegateZvtk_property_delegaterf   rg   rl   ÚHasStrictTraitsri   r   r   r   r   Ú<module>   sZ   
	H



	4