3
8ßL]Ñb  ã               @   s¬   d Z ddlmZ ddlmZmZmZmZmZm	Z	m
Z
mZmZmZ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 dd	lmZ dd
lZeeƒZG dd„ deƒZd
S )z‚ Defines the abstract Editor class, which represents an editing control for
    an object trait in a Traits-based user interface.
é    )Úabsolute_import)ÚAnyÚBoolÚHasPrivateTraitsÚ	HasTraitsÚInstanceÚPropertyÚReadOnlyÚStrÚTraitÚ
TraitErrorÚTraitListEventÚ	UndefinedÚcached_property)Únot_noneé   )ÚEditorFactory)ÚContextValue)ÚUndoItem)ÚItemNc               @   sR  e Zd ZdZedƒZedƒZeeƒZ	e
ZeZeZeZe
Zeef ƒZeZeZedƒZedƒZedƒZeZedƒZeZeZeZedƒZ dd„ Z!dd	„ Z"d
d„ Z#dd„ Z$dd„ Z%e&dd„ ƒZ'e&dd„ ƒZ(dd„ Z)dd„ Z*dd„ Z+dd„ Z,d8dd„Z-dd „ Z.d!d"„ Z/d#d$„ Z0d%d&„ Z1d'd(„ Z2d)d*„ Z3d+d,„ Z4d-d.„ Z5d/d0„ Z6d9d2d3„Z7d4d5„ Z8d6d7„ Z9dS ):ÚEditorza Represents an editing control for an object trait in a Traits-based
        user interface.
    ztraitsui.ui.UIÚobjectTFc             K   sz   t j| f|Ž yt| j| jƒ| _W n@ tk
rb   | jj| jƒ}|jdksT| jdkr\t	| _n‚ Y nX | j
| jjddƒ dS )z( Initializes the editor object.
        ÚeventZspringÚinvalidÚfromN)r   Ú__init__Úgetattrr   ÚnameÚ	old_valueÚAttributeErrorÚ
base_traitÚtyper   Ú
sync_valueÚfactoryr   )ÚselfÚparentÚtraitsZctrait© r'   ú5/tmp/pip-install-5sebvq2j/traitsui/traitsui/editor.pyr   Ž   s    zEditor.__init__c             C   s@   | j }|dkr"| jj| j|dd� | j|ƒ | jƒ  | jƒ  dS )z) Finishes setting up the editor.
        ÚNoneÚui)ÚdispatchN)Úextended_nameÚcontext_objectÚon_trait_changeÚ_update_editorÚinitÚ_sync_valuesÚupdate_editor)r$   r%   r   r'   r'   r(   Úprepare£   s    
zEditor.preparec             C   s   t ‚dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        N)ÚNotImplementedError)r$   r%   r'   r'   r(   r0   ³   s    zEditor.initc             C   s   t ‚dS )zB Assigns focus to the editor's underlying toolkit widget.
        N)r4   )r$   r'   r'   r(   Ú	set_focus½   s    zEditor.set_focusc             C   sÂ   | j dkrdS | j}|dkr0| jj| j|dd� | jdk	r^x"| jD ]\}}| j||dd� qBW | jdk	rŽx$| jD ]\}}}|j||dd� qpW d | _ | _  | _ | _	 | _
 | _ | _| _dS )z0 Disposes of the contents of an editor.
        Nr)   T)Úremove)r*   r,   r-   r.   r/   Ú
_user_fromÚ_user_tor   Úitemr#   ÚcontrolÚlabel_controlr   Z_context_object)r$   r   Úhandlerr   r'   r'   r(   ÚdisposeÆ   s    


zEditor.disposec             C   s<   | j }|jddƒd }|dkr6|| jjkr6| jj| S | jS )z_ Returns the context object the editor is using (Property
            implementation).
        Ú.r   r   Ú )Úobject_nameÚsplitr*   Úcontextr   )r$   r@   Zcontext_keyr'   r'   r(   Ú_get_context_objectá   s
    zEditor._get_context_objectc             C   s   d| j | jf jddƒd S )z] Returns the extended trait name being edited (Property
            implementation).
        z%s.%sr>   r   )r@   r   rA   )r$   r'   r'   r(   Ú_get_extended_nameó   s    zEditor._get_extended_namec             C   s   | j j| jƒS )zL Returns the trait the editor is editing (Property implementation).
        )r   Útraitr   )r$   r'   r'   r(   Ú_get_value_traitþ   s    zEditor._get_value_traitc             C   s   t | j| jtƒS )N)r   r   r   r   )r$   r'   r'   r(   Ú
_get_value  s    zEditor._get_valuec             C   s$   | j r | jdkr | j j| j|ƒ d S )Nr)   )r*   r   Zdo_undoableÚ_Editor__set_value)r$   Úvaluer'   r'   r(   Ú
_set_value
  s    zEditor._set_valuec             C   s¢   d| _ zŽyZ| jj}| j}| j}t|d||f d ƒpJt|d| d ƒpJt|dƒ}|| jj| j||ƒ W n. tk
r� } z| j	|ƒ ‚ W Y d d }~X nX W d d| _ X d S )NTz%s_%s_setattrz
%s_setattrÚsetattrF)
Ú
_no_updater*   r<   r@   r   r   Úinfor   r   Úerror)r$   rI   r<   Zobj_namer   ÚmethodÚexcpr'   r'   r(   Z__set_value  s    

zEditor.__set_valueNc             C   s   | j j||ƒS )z’ Returns the text representation of a specified object trait value.

        This simply delegates to the factorys `string_value` method.
        )r#   Ústring_value)r$   rI   Zformat_funcr'   r'   r(   rQ   $  s    zEditor.string_valuec             C   s   | j t| j| jtƒƒS )z> Returns the text representation of the object trait.
        )rQ   r   r   r   r   )r$   r'   r'   r(   Ú_get_str_value/  s    zEditor._get_str_valuec             C   s
   t j|ƒS )z? Returns the text representation of a specified value.
        )ÚsixÚ	text_type)r$   rI   r'   r'   r(   Ú_str8  s    zEditor._strc             C   s   dS )zN Handles an error that occurs while setting the object's trait value.
        Nr'   )r$   rP   r'   r'   r(   rN   D  s    zEditor.errorc             C   s”   | j dkrdS || jk	r.t| jtƒ | j jƒ| _| jdkrR| jj| j	| j
dd� dS | jjdkr‚|j|ƒjdkr‚| j| j||||ƒ | js�| jƒ  dS )z9 Performs updates when the object trait changes.
        NT)r6   Úreadonlyr   )r*   r   Úevalr@   ÚglobalsrB   r:   r-   r.   r/   r,   r9   Ústyler    r!   Ú
log_changeÚget_undo_itemrL   r2   )r$   r   r   r   Ú	new_valuer'   r'   r(   r/   M  s    


zEditor._update_editorc             G   s\   | j }d|_|j}|dkrX|j}|dk	rX||Ž }|dk	rX||jkrN|j|ƒ n
|j|ƒ dS )z+ Logs a change made in the editor.
        Tr   N)r*   ÚmodifiedZ	_undoableÚhistoryÚnowÚaddÚextend)r$   Zundo_factoryZ	undo_argsr*   Zundoabler^   r9   r'   r'   r(   rZ   v  s    
zEditor.log_changec             C   s   dS )z` Updates the editor when the object trait changes externally to the
            editor.
        Nr'   )r$   r'   r'   r(   r2   ‘  s    zEditor.update_editorc             C   s   t ||||d�S )z( Creates an undo history entry.
        )r   r   r   r\   )r   )r$   r   r   r   r\   r'   r'   r(   r[   �  s    zEditor.get_undo_itemc             C   sR   |j dƒ}|dk r| j}n&| jj|d|…  ||d d…  }}||td| ƒfS )zÆ Returns a tuple of the form ( context_object, 'name[.name...],
            callable ) for a specified extended name of the form: 'name' or
            'context_object_name.name[.name...]'.
        r>   r   Nr   zlambda obj=object: obj.)Úfindr-   r*   rB   rW   )r$   r   Úcolr   r'   r'   r(   Úparse_extended_name«  s
    
&zEditor.parse_extended_namec             C   sx   | j }xl|jtd�D ]\\}}t||ƒ}t|tƒr\| j|ƒ}| j|j||jpN|j|j	dkƒ q|t
k	rt| ||ƒ qW dS )z| Initializes and synchronizes (as needed) editor traits with the
            value of corresponding factory traits.
        )r"   TN)r#   r&   r   r   Ú
isinstancer   rE   r"   r   Úis_listr   rK   )r$   r#   r   rE   rI   Z
self_traitr'   r'   r(   r1   ½  s    




zEditor._sync_valuesÚbothc                s`  ˆdk�r\dˆˆ f ‰ˆj dkr&i ˆ_ d‰ˆjdƒ}|dk rHˆj‰ˆ}n^ˆjjˆd|…  ‰ˆ|d d…  ‰}ˆjdƒ}|dkr¦ˆdˆd|…  7 ‰ˆ|d d… ‰tdˆˆf d	d
ƒ‰tˆd	d
ƒ‰|dk�rˆ‡ ‡‡‡fdd„}ˆj||ƒ ˆjdk�r g ˆ_ˆjj	ˆ||fƒ |�rN‡ ‡‡fdd„}	ˆj|	|d ƒ ˆjj	ˆ|d |	fƒ |�sˆyt
ˆˆ tˆƒƒ W n   ddlm}
 |
ƒ  Y nX |dk�r\‡‡‡‡‡fdd„}ˆj|ˆ ƒ ˆjdk�rÄg ˆ_ˆjj	ˆ |fƒ |�r‡‡‡‡fdd„}ˆj|ˆ d ƒ ˆjj	ˆ d |fƒ |dk�r\| �r\yt
tˆƒˆtˆˆ ƒƒ W n   ddlm}
 |
ƒ  Y nX dS )ab  
        Set up synchronization between an editor trait and a user object
        trait.

        Also sets the initial value of the editor trait from the
        user object trait (for modes 'from' and 'both'), and the initial
        value of the user object trait from the editor trait (for mode
        'to').

        Parameters
        ----------
        user_name : string
            The name of the trait to be used on the user object. If empty, no
            synchronization will be set up.
        editor_name : string
            The name of the relevant editor trait.
        mode : string, optional; one of 'to', 'from' or 'both'
            The direction of synchronization. 'from' means that trait changes
            in the user object should be propagated to the editor. 'to' means
            that trait changes in the editor should be propagated to the user
            object. 'both' means changes should be propagated in both
            directions. The default is 'both'.
        is_list : bool, optional
            If true, synchronization for item events will be set up in
            addition to the synchronization for the object itself.
            The default is False.
        is_event : bool, optional
            If true, this method won't attempt to initialize the user
            object or editor trait values. The default is False.
        r?   z%s:%sNÚuser_objectr>   r   r   z%s.%sz<string>rW   r   rg   c          
      sT   ˆ ˆˆj krPd ˆj ˆ< ytˆˆ | ƒ W n   ddlm} |ƒ  Y nX ˆj ˆ= d S )Nr   )Úraise_to_debug)Ú_no_trait_updaterK   Útraitsui.apiri   )Únewri   )Úeditor_nameÚkeyr$   rh   r'   r(   Úuser_trait_modified	  s    

z.Editor.sync_value.<locals>.user_trait_modifiedc                sr   t | tƒrnˆˆjkrnd ˆjˆ< | j}y"| jtˆˆ ƒ||t| jƒ …< W n   ddlm	} |ƒ  Y nX ˆjˆ= d S )Nr   )ri   )
re   r   rj   ÚindexÚaddedr   ÚlenÚremovedrk   ri   )r   Únri   )rm   rn   r$   r'   r(   Úuser_list_modified  s    


"z-Editor.sync_value.<locals>.user_list_modifiedZ_items)ri   Útoc          
      sX   ˆ ˆ ˆj krTd ˆj ˆ < yttˆƒˆ| ƒ W n   ddlm} |ƒ  Y nX ˆj ˆ = d S )Nr   )ri   )rj   rK   rW   rk   ri   )rl   ri   )rn   r$   Ú	user_namerh   Úuser_refr'   r(   Úeditor_trait_modified9  s    

z0Editor.sync_value.<locals>.editor_trait_modifiedc                sj   ˆ ˆ ˆj krfd ˆj ˆ < | j}y | jtˆƒ||t| jƒ …< W n   ddlm} |ƒ  Y nX ˆj ˆ = d S )Nr   )ri   )rj   rp   rq   rW   rr   rs   rk   ri   )r   rt   ri   )rn   r$   rh   Ú
user_valuer'   r(   Úeditor_list_modifiedM  s    

 z/Editor.sync_value.<locals>.editor_list_modified)r   rg   )rv   rg   )rj   rb   r-   r*   rB   ÚrfindÚcompiler.   r8   ÚappendrK   rW   rk   ri   r7   r   )r$   rw   rm   Úmoderf   Zis_eventrc   Z
xuser_namero   ru   ri   ry   r{   r'   )rm   rn   r$   rw   rh   rx   rz   r(   r"   Ð  sp     









zEditor.sync_valuec             C   s   dS )z` Restores any saved user preference information associated with the
            editor.
        Nr'   )r$   Zprefsr'   r'   r(   Úrestore_prefso  s    zEditor.restore_prefsc             C   s   dS )zM Returns any user preference information associated with the editor.
        Nr'   )r$   r'   r'   r(   Ú
save_prefsy  s    zEditor.save_prefs)N)rg   FF):Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r
   r@   r   r   r	   r   r   r-   r,   r   r   Údescriptionr   r9   r:   r;   r   ZenabledZvisibleZ
scrollableÚfactory_traitr#   ZupdatingrI   Z	str_valueZvalue_traitr   r   r3   r0   r5   r=   r   rC   rD   rF   rG   rJ   rH   rQ   rR   rU   rN   r/   rZ   r2   r[   rd   r1   r"   r€   r�   r'   r'   r'   r(   r   B   s^   

		
		) 
 
r   )r…   Ú
__future__r   Z
traits.apir   r   r   r   r   r   r	   r
   r   r   r   r   r   Ztraits.trait_baser   Zeditor_factoryr   Zcontext_valuer   Zundor   r9   r   rS   r‡   r   r'   r'   r'   r(   Ú<module>   s   <