a
    §ˆcJ;  ã                   @   sâ   d Z ddl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mZmZmZmZmZ eeefZeefe ZG dd„ deƒZG dd„ deƒZ G d	d
„ d
eƒZ!G dd„ deƒZ"G dd„ deƒZ#G dd„ deƒZ$dS )zV Defines the manager for Undo and Redo history for Traits user interface
    support.
é    N)ÚBoolÚEventÚHasPrivateTraitsÚHasStrictTraitsÚ	HasTraitsÚInstanceÚIntÚListÚPropertyÚStrÚTraitÚcached_propertyÚobserve)ÚAbstractCommandÚCommandStackÚICommandÚICommandStackÚIUndoManagerÚUndoManagerc                   @   s<   e Zd ZdZdZdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ Z	dS )ÚAbstractUndoItemzæAbstract base class for undo items.

    This class is deprecated and will be removed in TraitsUI 8.  Any custom
    subclasses of this class should either subclass from AbstractCommand, or
    provide the ICommand interface.
    ÚEditc                 C   s   dS )zœDoes nothing.

        All undo items log events after they have happened, so by default
        they do not do anything when added to the history.
        N© ©Úselfr   r   úP/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/undo.pyÚdo9   s    zAbstractUndoItem.doc                 C   s   t ‚dS )úUndoes the change.N©ÚNotImplementedErrorr   r   r   r   ÚundoA   s    zAbstractUndoItem.undoc                 C   s   t ‚dS )úRe-does the change.Nr   r   r   r   r   ÚredoE   s    zAbstractUndoItem.redoc                 C   s   ddl }| dt¡ |  |¡S )ú"Merges two undo items if possible.r   NzQ'merge_undo' is deprecated and will be removed in TraitsUI 8, use 'merge' instead)ÚwarningsÚwarnÚDeprecationWarningÚ
merge_undo)r   Úotherr#   r   r   r   ÚmergeI   s    ýzAbstractUndoItem.mergec                 C   s   dS )zOMerges two undo items if possible.

        This method is deprecated.
        Fr   ©r   Ú	undo_itemr   r   r   r&   T   s    zAbstractUndoItem.merge_undoN)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Únamer   r   r!   r(   r&   r   r   r   r   r   .   s   r   c                   @   sr   e Zd ZdZeeƒZeƒ Ze	ƒ Z
e	ƒ Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )ÚUndoItemz1A change to an object trait, which can be undone.c                 C   s   | j S ©N)Ú
_old_valuer   r   r   r   Ú_get_old_valueo   s    zUndoItem._get_old_valuec                 C   s    t |tƒr|d d … }|| _d S r1   )Ú
isinstanceÚlistr2   ©r   Úvaluer   r   r   Ú_set_old_valuer   s    
zUndoItem._set_old_valuec                 C   s   | j S r1   )Ú
_new_valuer   r   r   r   Ú_get_new_valuew   s    zUndoItem._get_new_valuec                 C   s    t |tƒr|d d … }|| _d S r1   )r4   r5   r9   r6   r   r   r   Ú_set_new_valuez   s    
zUndoItem._set_new_valuec                 C   s@   zt | j| j| jƒ W n$ ty:   ddlm} |ƒ  Y n0 dS ©r   r   ©Úraise_to_debugN)ÚsetattrÚobjectr/   Ú	old_valueÚ	ExceptionÚtraitsui.apir>   ©r   r>   r   r   r   r      s
    zUndoItem.undoc                 C   s@   zt | j| j| jƒ W n$ ty:   ddlm} |ƒ  Y n0 dS ©r    r   r=   N)r?   r@   r/   Ú	new_valuerB   rC   r>   rD   r   r   r   r!   ˆ   s
    zUndoItem.redoc                 C   sÆ  t || jƒ�rÂ| j|ju �rÂ| j|jk�rÂ| j}|j}t|ƒ}t ||ƒ�rÂt |tƒrÜt|ƒ}t|ƒ}t|| ƒdkrxdS t	||ƒ}d}||k r¨|| || kr¨|d7 }q†||||k d… ||||k d… krÚ|| _dS næt |t
jjƒ�r®| j}t |t
jjƒ�rÂz”t|ƒt|ƒk�r”d}	t|ƒD ]T\}}
t|
ƒ}|| }|tv�sZt ||ƒ�rZ|
|k�r"|	d7 }	|	dk�r" W dS �q"|	dk�rˆW dS || _W dS W n t�yª   Y n0 n|tv �rÂ|| _dS dS )r"   é   Fr   NTé   )r4   Ú	__class__r@   r/   rF   ÚtypeÚstrÚlenÚabsÚminÚcollectionsÚabcÚSequencerA   Ú	enumerateÚSimpleTypesrB   ÚNumericTypes)r   r*   Zv1Zv2Út1Zn1Zn2ÚnÚiZdiffsÚitemZtitemZitem2r   r   r   r(   ‘   sd    
ÿ
þ
ý


(ÿþý



zUndoItem.mergec                 C   s
   |   |¡S ©zHMerges two undo items if possible.

        This is deprecated.
        ©r(   r)   r   r   r   r&   Ô   s    zUndoItem.merge_undoc                 C   s(   | j }| jjj}d||| j||| jf S )z,Returns a "pretty print" form of the object.z%undo( %s.%s = %s )
redo( %s.%s = %s ))r/   r@   rI   r+   rA   rF   )r   rV   Zcnr   r   r   Ú__repr__Û   s    
úzUndoItem.__repr__N)r+   r,   r-   r.   r   r   r@   r   r/   r
   rA   rF   r3   r8   r:   r;   r   r!   r(   r&   r[   r   r   r   r   r0   \   s   		Cr0   c                   @   sX   e Zd ZdZeeƒZeƒ Ze	ƒ Z
eƒ Zeƒ Zdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚListUndoItemz(A change to a list, which can be undone.c                 C   sX   z.t | j| jƒ}| j|| j| jt| jƒ …< W n$ tyR   ddlm	} |ƒ  Y n0 dS r<   )
Úgetattrr@   r/   ÚremovedÚindexrL   ÚaddedrB   rC   r>   ©r   r5   r>   r   r   r   r   ÿ   s     zListUndoItem.undoc                 C   sX   z.t | j| jƒ}| j|| j| jt| jƒ …< W n$ tyR   ddlm	} |ƒ  Y n0 dS rE   )
r]   r@   r/   r`   r_   rL   r^   rB   rC   r>   ra   r   r   r   r!   	  s     zListUndoItem.redoc                 C   s°   t || jƒr¬| j|ju r¬| j|jkr¬| j|jkr¬|j}|j}t| jƒt|ƒkr¬t| jƒt|ƒkr¬t| jƒD ]\}}||| urj q¬qjt| jƒD ]\}}||| urŽ q¬qŽdS dS )r"   TF)	r4   rI   r@   r/   r_   r`   r^   rL   rR   )r   r*   r`   r^   rW   rX   r   r   r   r(     s*    
ÿ
þ
ý
üÿzListUndoItem.mergec                 C   s
   |   |¡S rY   rZ   r)   r   r   r   r&   .  s    zListUndoItem.merge_undoc                 C   s*   d| j jj| j| j| jt| jƒ | jf S )z,Returns a 'pretty print' form of the object.zundo( %s.%s[%d:%d] = %s ))r@   rI   r+   r/   r_   rL   r^   r`   r   r   r   r   r[   5  s    ûzListUndoItem.__repr__N)r+   r,   r-   r.   r   r   r@   r   r/   r   r_   r	   r`   r^   r   r!   r(   r&   r[   r   r   r   r   r\   é   s   

r\   c                   @   s@   e Zd ZdZdZeeeƒƒZdd„ Z	dd„ Z
dd„ Zd	d
„ ZdS )Ú_MultiUndoItemzFThe _MultiUndoItem class is an internal command that unifies commands.r   c                 C   s6   t | jƒdkr&| jd  |¡}|r&dS | j |¡ dS )z&Append a command, merging if possible.r   éÿÿÿÿN)rL   Úcommandsr(   Úappend)r   ÚcommandÚmergedr   r   r   ÚpushH  s
    z_MultiUndoItem.pushc                 C   s   dS )zTry and merge a command.Fr   )r   r'   r   r   r   r(   Q  s    z_MultiUndoItem.mergec                 C   s   | j D ]}| ¡  qdS )zRedo the sub-commands.N)rd   r!   ©r   Úcmdr   r   r   r!   U  s    
z_MultiUndoItem.redoc                 C   s   | j D ]}| ¡  qdS )zUndo the sub-commands.N)rd   r   ri   r   r   r   r   [  s    
z_MultiUndoItem.undoN)r+   r,   r-   r.   r/   r	   r   r   rd   rh   r(   r!   r   r   r   r   r   rb   @  s   	rb   c                   @   sü   e Zd ZdZeedd�Zeedd�Ze	ƒ Z
eedd�ZedƒZedƒZeedd�Zeedd�Zeƒ Zeƒ Zd&dd	„Zd
d„ Zdd„ Zdd„ Zdd„ Zdd„ Zedƒdd„ ƒZedƒdd„ ƒZedƒdd„ ƒZe dd„ ƒZ!dd„ Z"dd „ Z#d!d"„ Z$d#d$„ Z%d%S )'ÚUndoHistoryz#Manages a list of undoable changes.F)Ú
allow_nonezstack._index)r   Ú	_can_undoÚ	_can_redoc                 C   s*   |r|   |¡ n| j| j_| j |¡ dS )z Adds an UndoItem to the history.N)ÚextendÚstackÚmanagerÚactive_stackrh   )r   r*   ro   r   r   r   Úaddˆ  s    
zUndoHistory.addc                 C   sb   | j | j_| j j}t|ƒdkrJ|d j}t|tƒsTt|gd�}||d _n
tg d�}| |¡ dS )z©Extends the undo history.

        If possible the method merges the new UndoItem with the last item in
        the history; otherwise, it appends the new item.
        r   rc   )rd   N)	rp   rq   rr   Ú_stackrL   rf   r4   rb   rh   )r   r*   Úentriesrf   r   r   r   ro   �  s    



zUndoHistory.extendc                 C   s   | j r| j ¡  dS )zUndoes an operation.N)Úcan_undorq   r   r   r   r   r   r   ¥  s    zUndoHistory.undoc                 C   s   | j r| j ¡  dS )zRedoes an operation.N)Úcan_redorq   r!   r   r   r   r   r!   ª  s    zUndoHistory.redoc                 C   s$   | j | j_| j jdd� |  ¡  dS )z7Reverts all changes made so far and clears the history.rc   )Zsequence_nrN)rp   rq   rr   r   Úclearr   r   r   r   Úrevert¯  s    
zUndoHistory.revertc                 C   s   | j | j_| j  ¡  dS )zClears the undo history.N)rp   rq   rr   rx   r   r   r   r   rx   ¶  s    
zUndoHistory.clearzmanager.stack_updatedc                 C   s,   | j o| j jdk| _| j o$| j jdk| _dS )zUpdate undo/redo state.Ú N)rq   Z	undo_namerm   Z	redo_namern   ©r   Úeventr   r   r   Ú_observe_stack_updated»  s    z"UndoHistory._observe_stack_updatedc                 C   s   |j | _d S r1   )ÚnewÚundoabler{   r   r   r   Ú_observe_can_undoÁ  s    zUndoHistory._observe_can_undoc                 C   s   |j | _d S r1   )r~   Úredoabler{   r   r   r   Ú_observe_can_redoÅ  s    zUndoHistory._observe_can_redoc                 C   s   | j jd S )NrG   )rp   Ú_indexr   r   r   r   Ú_get_nowÉ  s    zUndoHistory._get_nowc                 C   s   | j S r1   )rm   r   r   r   r   Ú_get_can_undoÍ  s    zUndoHistory._get_can_undoc                 C   s   | j S r1   )rn   r   r   r   r   Ú_get_can_redoÐ  s    zUndoHistory._get_can_redoc                 C   s
   t ƒ }|S r1   )r   )r   rq   r   r   r   Ú_manager_defaultÓ  s    zUndoHistory._manager_defaultc                 C   s   t | jd�}|S )N)Zundo_manager)r   rq   )r   rp   r   r   r   Ú_stack_default×  s    zUndoHistory._stack_defaultN)F)&r+   r,   r-   r.   r   r   rq   r   rp   r	   Úhistoryr
   r   Únowr   r   r�   r   rv   rw   rm   rn   rs   ro   r   r!   ry   rx   r   r}   r€   r‚   r   r„   r…   r†   r‡   rˆ   r   r   r   r   rk   b  s:   




rk   c                   @   s(   e Zd ZdZeeƒZdd„ Zdd„ ZdS )ÚUndoHistoryUndoItemz"An undo item for the undo history.c                 C   sR   | j }t|jd ddƒD ]4}|j | }tt|ƒd ddƒD ]}||  ¡  q:qdS )r   rG   rc   N)r‰   ÚrangerŠ   rL   r   )r   r‰   rW   ÚitemsÚjr   r   r   r   æ  s
    
zUndoHistoryUndoItem.undoc                 C   s4   | j }td|jƒD ]}|j | D ]}| ¡  q qdS )r    r   N)r‰   rŒ   rŠ   r!   )r   r‰   rW   rX   r   r   r   r!   î  s    zUndoHistoryUndoItem.redoN)	r+   r,   r-   r.   r   rk   r‰   r   r!   r   r   r   r   r‹   Ü  s   r‹   )%r.   Úcollections.abcrO   Z
traits.apir   r   r   r   r   r   r   r	   r
   r   r   r   r   Zpyface.undo.apir   r   r   r   r   r   ÚintÚfloatÚcomplexrT   rK   ÚbytesrS   r   r0   r\   rb   rk   r‹   r   r   r   r   Ú<module>   s   < 

. W"z