3
8Ad]ÓG  ã               @   sÀ   d Z ddlmZ ddl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 ejeef Zej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dS )zV Defines the manager for Undo and Redo history for Traits user interface
    support.
é    )Úabsolute_importN)
ÚEventÚHasPrivateTraitsÚHasStrictTraitsÚ	HasTraitsÚInstanceÚIntÚListÚPropertyÚStrÚTraitc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚAbstractUndoItemz) Abstract base class for undo items.
    c             C   s   t ‚dS )z Undoes the change.
        N)ÚNotImplementedError)Úself© r   úi/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/traitsui/undo.pyÚundo7   s    zAbstractUndoItem.undoc             C   s   t ‚dS )z Re-does the change.
        N)r   )r   r   r   r   Úredo@   s    zAbstractUndoItem.redoc             C   s   dS )z, Merges two undo items if possible.
        Fr   )r   Ú	undo_itemr   r   r   Ú
merge_undoI   s    zAbstractUndoItem.merge_undoN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   0   s   		r   c               @   sd   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S )ÚUndoItemz7 A change to an object trait, which can be undone.
    c             C   s   | j S )N)Ú
_old_value)r   r   r   r   Ú_get_old_valueg   s    zUndoItem._get_old_valuec             C   s    t |tƒr|d d … }|| _d S )N)Ú
isinstanceÚlistr   )r   Úvaluer   r   r   Ú_set_old_valuej   s    
zUndoItem._set_old_valuec             C   s   | j S )N)Ú
_new_value)r   r   r   r   Ú_get_new_valueo   s    zUndoItem._get_new_valuec             C   s    t |tƒr|d d … }|| _d S )N)r   r   r!   )r   r   r   r   r   Ú_set_new_valuer   s    
zUndoItem._set_new_valuec             C   sB   yt | j| j| jƒ W n& tk
r<   ddlm} |ƒ  Y nX dS )z Undoes the change.
        r   )Úraise_to_debugN)ÚsetattrÚobjectÚnameÚ	old_valueÚ	ExceptionÚtraitsui.apir$   )r   r$   r   r   r   r   {   s
    zUndoItem.undoc             C   sB   yt | j| j| jƒ W n& tk
r<   ddlm} |ƒ  Y nX dS )z Re-does the change.
        r   )r$   N)r%   r&   r'   Ú	new_valuer)   r*   r$   )r   r$   r   r   r   r   ˆ   s
    zUndoItem.redoc             C   sÄ  t || jƒo"| j|jko"| j|jk�rÀ| j}|j}t|ƒ}t ||ƒ�rÀt |tjƒrÞt|ƒ}t|ƒ}t	|| ƒdkrvdS t
||ƒ}d}x$||k r¨|| || kr¨|d7 }q†W ||||k d… ||||k d… krÜ|| _dS nât |tjƒ�r¬| j}t |tjƒ�rÀy’t|ƒt|ƒk�r�d}	x^t|ƒD ]R\}}
t|
ƒ}|| }|tk�s\t ||ƒ �s\|
|k�r"|	d7 }	|	dk�r"dS �q"W |	dk�r†dS || _dS W n tk
�r¨   Y nX n|tk�rÀ|| _dS dS )z, Merges two undo items if possible.
        é   Fr   NTé   )r   Ú	__class__r&   r'   r+   ÚtypeÚsixÚstring_typesÚlenÚabsÚminÚcollectionsÚSequencer(   Ú	enumerateÚSimpleTypesr)   ÚNumericTypes)r   r   Úv1Úv2Út1Zn1Ún2ÚnÚiÚdiffsÚitemZtitemÚitem2r   r   r   r   •   sX    
(





zUndoItem.merge_undoc             C   s(   | j }| jjj}d||| j||| jf S )z6 Returns a "pretty print" form of the object.
        z%undo( %s.%s = %s )
redo( %s.%s = %s ))r'   r&   r.   r   r(   r+   )r   r>   Zcnr   r   r   Ú__repr__Ù   s    
zUndoItem.__repr__N)r   r   r   r   r   r   r&   r   r'   r
   r(   r+   r   r    r"   r#   r   r   r   rC   r   r   r   r   r   S   s   	Dr   c               @   sH   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
S )ÚListUndoItemz. A change to a list, which can be undone.
    c             C   sZ   y.t | j| jƒ}| j|| j| jt| jƒ …< W n& tk
rT   ddlm	} |ƒ  Y nX dS )z Undoes the change.
        r   )r$   N)
Úgetattrr&   r'   ÚremovedÚindexr2   Úaddedr)   r*   r$   )r   r   r$   r   r   r   r   ü   s     zListUndoItem.undoc             C   sZ   y.t | j| jƒ}| j|| j| jt| jƒ …< W n& tk
rT   ddlm	} |ƒ  Y nX dS )z Re-does the change.
        r   )r$   N)
rE   r&   r'   rH   rG   r2   rF   r)   r*   r$   )r   r   r$   r   r   r   r   
  s     zListUndoItem.redoc             C   s´   t || jƒr°| j|jkr°| j|jkr°| j|jkr°|j}|j}t| jƒt|ƒkr°t| jƒt|ƒkr°xNt| jƒD ]\}}||| k	rlP qlW x(t| jƒD ]\}}||| k	r’P q’W dS dS )z, Merges two undo items if possible.
        TF)	r   r.   r&   r'   rG   rH   rF   r2   r7   )r   r   rH   rF   r?   rA   r   r   r   r     s     zListUndoItem.merge_undoc             C   s*   d| j jj| j| j| jt| jƒ | jf S )z6 Returns a 'pretty print' form of the object.
        zundo( %s.%s[%d:%d] = %s ))r&   r.   r   r'   rG   r2   rF   rH   )r   r   r   r   rC   5  s    zListUndoItem.__repr__N)r   r   r   r   r   r   r&   r   r'   r   rG   r	   rH   rF   r   r   r   rC   r   r   r   r   rD   æ   s   rD   c               @   sr   e Zd ZdZeZeZedƒZ	e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dd„ Zdd„ ZdS )ÚUndoHistoryz) Manages a list of undoable changes.
    Fc             C   s¨   |r| j |ƒ dS | j}|dkrZ| j|d  }t|ƒdkrZ|d j|ƒrZg | j|d…< dS t| jƒ}|gg| j|d…< |  jd7  _| jdkr”d| _| j|kr¤d| _dS )z* Adds an UndoItem to the history.
        Nr   r,   TF)ÚextendÚnowÚhistoryr2   r   ÚundoableÚredoable)r   r   rJ   rK   ÚpreviousÚold_lenr   r   r   ÚaddY  s     



zUndoHistory.addc             C   s6   | j dkr2| j| j d  }|d j|ƒs2|j|ƒ 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   r,   Néÿÿÿÿ)rK   rL   r   Úappend)r   r   Z	undo_listr   r   r   rJ   t  s    
zUndoHistory.extendc             C   sx   | j rt|  jd8  _| j| j }x(tt|ƒd ddƒD ]}|| jƒ  q6W | jdkrZd| _| jt| jƒd krtd| _dS )z Undoes an operation.
        r,   r   FTNrR   rR   )Úcan_undorK   rL   Úranger2   r   rM   rN   )r   Úitemsr?   r   r   r   r   ƒ  s    
zUndoHistory.undoc             C   s`   | j r\|  jd7  _x | j| jd  D ]}|jƒ  q&W | jdkrFd| _| jt| jƒkr\d| _dS )z Redoes an operation.
        r,   TFN)Úcan_redorK   rL   r   rM   r2   rN   )r   rA   r   r   r   r   ”  s    
zUndoHistory.redoc             C   sl   | j d| j… }| jƒ  xNtt|ƒd ddƒD ]6}|| }x(tt|ƒd ddƒD ]}|| jƒ  qPW q.W dS )zA Reverts all changes made so far and clears the history.
        Nr,   rR   rR   rR   rR   )rL   rK   ÚclearrU   r2   r   )r   rL   r?   rV   Újr   r   r   Úrevert¤  s    zUndoHistory.revertc             C   sB   t | jƒ}| j}d| _| jdd…= |dkr0d| _||k r>d| _dS )z" Clears the undo history.
        r   NF)r2   rL   rK   rM   rN   )r   rP   Zold_nowr   r   r   rX   ²  s    
zUndoHistory.clearc             C   s
   | j dkS )z, Are there any undoable operations?
        r   )rK   )r   r   r   r   Ú_get_can_undoÂ  s    zUndoHistory._get_can_undoc             C   s   | j t| jƒk S )z, Are there any redoable operations?
        )rK   r2   rL   )r   r   r   r   Ú_get_can_redoË  s    zUndoHistory._get_can_redoN)F)r   r   r   r   r	   rL   r   rK   r   rM   rN   r
   rT   rW   rQ   rJ   r   r   rZ   rX   r[   r\   r   r   r   r   rI   A  s   
	rI   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   sZ   | j }xNt|jd ddƒD ]8}|j | }x(tt|ƒd ddƒD ]}|| jƒ  q>W qW dS )z Undoes the change.
        r,   NrR   rR   rR   rR   )rL   rU   rK   r2   r   )r   rL   r?   rV   rY   r   r   r   r   ã  s
    
zUndoHistoryUndoItem.undoc             C   s<   | j }x0td|jƒD ] }x|j | D ]}|jƒ  q$W qW dS )z Re-does the change.
        r   N)rL   rU   rK   r   )r   rL   r?   rA   r   r   r   r   ð  s    zUndoHistoryUndoItem.redoN)	r   r   r   r   r   rI   rL   r   r   r   r   r   r   r]   Õ  s   r]   )r   Ú
__future__r   r5   r0   Z
traits.apir   r   r   r   r   r   r	   r
   r   r   Úinteger_typesÚfloatÚcomplexr9   Ú	text_typeÚbytesr8   r   r   rD   rI   r]   r   r   r   r   Ú<module>   s   0# [ 