3
8Ad]8$  ã               @   sŒ   d Z ddlm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.
é    )Úabsolute_import)Ú
FileDialogÚconfirmÚerrorÚYESÚCANCEL)ÚTimer)Ú	HasTraitsÚStrÚBoolÚAnyÚIntÚInstanceÚon_trait_changeé   )Ú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 t| ƒjƒ }|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)Úsuperr   Ú__getstate__)ÚselfÚstate)Ú	__class__© úr/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/extras/saving.pyr   .   s    zCanSaveMixin.__getstate__c             C   s   dS )a3   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Ú )Tr   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)ÚNotImplementedError)r   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 )ÚSaveHandlerzH A 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k	r|j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   Úinity   s
    
zSaveHandler.initc             C   s   | j r| j|ƒ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*   Úis_okr   r   r   ÚcloseŠ   s    
zSaveHandler.closec             C   s   | j r| j jƒ  dS )zj Called after the window is destroyed. Makes sure that the autosave
            timer is stopped.
        N)Ú_timerÚStop)r   r*   r.   r   r   r   Úclosed“   s    zSaveHandler.closedc             C   s   | j |dƒr|jjƒ  dS )z~ Closes the UI unless a save prompt is cancelled. Provided for
            convenience to be used with a Menu action.
        TN)r/   r&   Zdispose)r   r*   r   r   r   Úexitž   s    zSaveHandler.exitc             C   s"   | j jdkr| j|ƒS | jƒ 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Ú_validateAndSave)r   r*   r   r   r   r   ¥   s    
zSaveHandler.savec             C   sŒ   t dd| j|jjd�}|jƒ  |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
_| 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   r8   r&   ÚcontrolÚopenÚpathZreturn_coder   ÚlenÚ	extensionr)   r   r5   )r   r*   Z
fileDialogZextLenr   r   r   r4   ¯   s    

zSaveHandler.saveAsc             C   sL   | j jrHt|jj| jd|d�}|tkr*dS |tkrH| j|ƒsH| j	||ƒ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?)r7   ÚcancelFT)
r)   r   r   r&   r<   ÚsavePromptMessager   r   r   r-   )r   r*   rA   Úcoder   r   r   r-   Á   s    

zSaveHandler.promptForSavec             C   s@   | j jr<| j jdkr<| j jƒ \}}|s2| jr<| jr<| j jƒ  dS )zA Called 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ƒ  | jr2| jr2t| jd | jƒ| _ nd| _ dS )z< Creates, replaces, or destroys the autosave timer.
        iè  N)r0   r1   Úautosaver)   r   ÚautosaveIntervalrH   )r   r   r   r   Ú_configure_timerØ   s
    
zSaveHandler._configure_timerc             C   s\   | j jƒ \}}|r| j jƒ  n:d}| jrJtd||d�tkrJ| j 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)r7   T)r)   r   r   rD   r   r   r   )r   rF   rG   r7   r   r   r   r5   ã   s    
zSaveHandler._validateAndSaveN)T)r    r!   r"   r#   r   r)   r
   r8   r@   rB   r   r,   rD   rI   r   rJ   rE   r   r   r0   r+   r/   r2   r3   r   r4   r-   rH   r   rK   r5   r   r   r   r   r%   L   s*   	

	r%   N)r#   Ú
__future__r   Z
pyface.apir   r   r   r   r   Zpyface.timer.apir   Z
traits.apir	   r
   r   r   r   r   r   Zapir   r   r%   r   r   r   r   Ú<module>   s   $,