a
    §ˆc+#  ã                   @   s€   d Z ddlmZmZmZmZmZ ddlmZ ddl	m
Z
mZmZmZmZmZmZ ddlmZ G dd„ de
ƒZG d	d
„ d
eƒZdS )z¤ Provides a lightweight framework that removes some of the drudge work
    involved with implementing user-friendly saving behavior in a Traits
    UI application.
é    )Ú
FileDialogÚconfirmÚerrorÚYESÚCANCEL)ÚTimer)Ú	HasTraitsÚStrÚBoolÚAnyÚIntÚInstanceÚobserveé   )ÚHandlerc                       s>   e Zd ZdZeƒ ZedƒZ‡ fdd„Zdd„ Z	dd„ Z
‡  ZS )	ÚCanSaveMixinzëA mixin-class for objects that wish to support GUI saving via a
    SaveHandler. It is the responsiblity of the child class to manage
    its dirty flag, which describes whether its information has changed
    since its last save.
    Fc                    s   t ƒ  ¡ }|d= |d= |S )z„We don't want to pickle the filepath because this can change,
        obviously, if the user moves around the pickled file.
        ÚfilepathÚdirty)ÚsuperÚ__getstate__)ÚselfÚstate©Ú	__class__© úY/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/extras/saving.pyr   .   s    
zCanSaveMixin.__getstate__c                 C   s   dS )a"  Returns whether the information in the object is valid to be saved
        in tuple form. The first item is the validation state (boolean) and
        the second item is the message to display if the object did not
        validate.

        By default, an object always validates.
        )TÚ r   ©r   r   r   r   Úvalidate;   s    zCanSaveMixin.validatec                 C   s   t ‚dS )z�Saves the object to the path specified by its 'filepath' trait. This
        method should also reset the dirty flag on this object.
        N)ÚNotImplementedErrorr   r   r   r   ÚsaveE   s    zCanSaveMixin.save)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r	   r   r
   r   r   r   r    Ú__classcell__r   r   r   r   r       s   
r   c                   @   s¶   e Zd ZdZeƒ ZedƒZeƒ ZedƒZ	e
dƒZe
dƒZe
dƒZedƒZe
dƒZeeƒZdd„ Zd	d
„ Zdd„ Zdd„ Zdd„ Zdd„ Zddd„Zdd„ Zedƒdd„ ƒZdd„ ZdS )ÚSaveHandlerzBA Handler that facilates adding saving to a Traits UI application.zAll files (*.*)|*.*zWould you like to save?TFi,  c                 C   s*   |j j}|dur|j |j¡ |j| _dS )a  Set the default save object (the object being handled). Also,
        perform a questionable hack by which we remove the handled
        object from the keybinding's controllers. This means that a
        keybinding to 'save' only calls this object, not the object
        being edited as well. (For reasons unclear, the KeyBinding handler
        API is radically different from the Action API, which is the reason
        that this problem exists. Keybindings are a UI concept--they should
        *not* call the model by default.)
        NT)ÚuiZkey_bindingsZcontrollersÚremoveÚobjectÚ
saveObject)r   ÚinfoZkeybindingsr   r   r   Úinitx   s
    
zSaveHandler.initc                 C   s   | j r|  |¡S dS dS )z’Called when the user requests to close the interface. Returns a
        boolean indicating whether the window should be allowed to close.
        TN)ÚpromptOnExitÚpromptForSave©r   r+   Zis_okr   r   r   Úclose‰   s    
zSaveHandler.closec                 C   s   | j r| j  ¡  dS )zeCalled after the window is destroyed. Makes sure that the autosave
        timer is stopped.
        N)Ú_timerÚStopr/   r   r   r   Úclosed’   s    zSaveHandler.closedc                 C   s   |   |d¡r|j ¡  dS )zyCloses the UI unless a save prompt is cancelled. Provided for
        convenience to be used with a Menu action.
        TN)r0   r'   Zdispose©r   r+   r   r   r   Úexit�   s    zSaveHandler.exitc                 C   s"   | j jdkr|  |¡S |  ¡ S dS )z¬Saves the object to its current filepath. If this is not specified,
        opens a dialog to select this path. Returns whether the save
        actually occurred.
        r   N)r*   r   ÚsaveAsÚ_validateAndSaver4   r   r   r   r    ¤   s    
zSaveHandler.savec                 C   sŒ   t dd| j|jjd�}| ¡  |jdks2|jtkr6dS t| j	ƒ}|rv|j| d d… d| j	 krv| jd| j	 7  _|j| j
_|  ¡ S dS )	z‹Saves the object to a new path, and sets this as the 'filepath' on
        the object. Returns whether the save actually occurred.
        zsave aszSave As)ÚactionÚtitleÚwildcardÚparentr   Fé   NÚ.)r   r:   r'   ÚcontrolÚopenÚpathZreturn_coder   ÚlenÚ	extensionr*   r   r7   )r   r+   Z
fileDialogZextLenr   r   r   r6   ®   s"    ü
ÿþ
zSaveHandler.saveAsc                 C   sL   | j jrHt|jj| jd|d�}|tkr*dS |tkrH|  |¡sH|  	||¡S dS )zŒPrompts the user to save the object, if appropriate. Returns whether
        the user canceled the action that invoked this prompt.
        z	Save now?)r9   ÚcancelFT)
r*   r   r   r'   r>   ÚsavePromptMessager   r   r    r.   )r   r+   rC   Úcoder   r   r   r.   Å   s    ü
zSaveHandler.promptForSavec                 C   s@   | j jr<| j jdkr<| j  ¡ \}}|s2| jr<| jr<| j  ¡  dS )z7Called by the timer when an autosave should take place.r   N)r*   r   r   r   ÚallowValidationBypassÚautosaveValidationBypassr    )r   ÚsuccessÚmessager   r   r   Ú	_autosave×   s    ÿÿzSaveHandler._autosavez&autosave, autosaveInterval, saveObjectc                 C   s<   | j r| j  ¡  | jr2| jr2t| jd | jƒ| _ nd| _ dS )z2Creates, replaces, or destroys the autosave timer.iè  N)r1   r2   Úautosaver*   r   ÚautosaveIntervalrJ   )r   Úeventr   r   r   Ú_configure_timerà   s
    
zSaveHandler._configure_timerc                 C   s\   | j  ¡ \}}|r| j  ¡  n:d}| jrJtd||d�tkrJ| j  ¡  d}ntd||d� |S )zŽTry to save to the current filepath. Returns whether whether the
        validation was successful/overridden (and the object saved).
        zValidation errorN)r9   T)r*   r   r    rF   r   r   r   )r   rH   rI   r9   r   r   r   r7   ê   s    ÿþ
zSaveHandler._validateAndSaveN)T)r!   r"   r#   r$   r   r*   r	   r:   rB   rD   r
   r-   rF   rK   r   rL   rG   r   r   r1   r,   r0   r3   r5   r    r6   r.   rJ   r   rN   r7   r   r   r   r   r&   L   s,   	

	
	r&   N)r$   Z
pyface.apir   r   r   r   r   Zpyface.timer.apir   Z
traits.apir   r	   r
   r   r   r   r   Úapir   r   r&   r   r   r   r   Ú<module>   s   $	,