3
AAd]V&  ã               @   s”   d dl 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 G dd„ deƒZG d	d
„ d
e
ƒZeeƒG dd„ deƒƒZdS )é    )ÚBoolÚ	HasTraitsÚInstanceÚIntÚListÚPropertyÚUnicodeÚprovidesé   )ÚAbstractCommand)ÚICommand)ÚICommandStack)ÚIUndoManagerc               @   s$   e Zd ZdZedƒZeeƒZe	Z
dS )Ú_StackEntryz= The _StackEntry class is a single entry on a command stack. FN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úcleanr   r   Úcommandr   Úsequence_nr© r   r   úw/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/apptools-4.4.0-py3.6.egg/apptools/undo/command_stack.pyr      s   r   c               @   s<   e Zd ZdZeeeƒƒZdd„ Zdd„ Z	dd„ Z
dd	„ Zd
S )Ú_MacroCommandzE The _MacroCommand class is an internal command that handles macros. c             C   s   dS )z Invoke the command. Nr   )Úselfr   r   r   Údo7   s    z_MacroCommand.doc             C   s(   t | jƒdkrd}n| jd j|ƒ}|S )z Try and merge a command. r   Fr
   éÿÿÿÿ)ÚlenÚmacro_commandsÚmerge)r   ÚotherÚmergedr   r   r   r   =   s    z_MacroCommand.mergec             C   s   x| j D ]}|jƒ  qW dS )z Redo the sub-commands. N)r   Úredo)r   Úcmdr   r   r   r"   G   s    z_MacroCommand.redoc             C   s   x| j D ]}|jƒ  qW dS )z Undo the sub-commands. N)r   Úundo)r   r#   r   r   r   r$   P   s    z_MacroCommand.undoN)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eeƒZeeƒZe	e
ƒZeeƒZedƒZee	eƒƒZee	eƒƒZdd„ Zdd„ Zdd„ Zd	d
„ Zddd„Zddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS ) ÚCommandStackz^ The CommandStack class is the default implementation of the
    ICommandStack interface.
    r
   c             C   s$   t |d�}| j|ƒ | jj|ƒ dS )a¦   This begins a macro by creating an empty command with the given
        'name'.  All subsequent calls to 'push()' create commands that will be
        children of the empty command until the next call to 'end_macro()'.
        Macros may be nested.  The stack is disabled (ie. nothing can be undone
        or redone) while a macro is being created (ie. while there is an
        outstanding 'end_macro()' call).
        )ÚnameN)r   ÚpushÚ_macro_stackÚappend)r   r&   r   r   r   r   Úbegin_macro�   s    	

zCommandStack.begin_macroc             C   s   d| _ g | _g | _| | j_dS )zÈ This clears the stack, without undoing or redoing any commands, and
        leaves the stack in a clean state.  It is typically used when all
        changes to the data have been abandoned.
        r
   Nr   )Ú_indexÚ_stackr(   Úundo_managerÚstack_updated)r   r   r   r   ÚclearŽ   s    zCommandStack.clearc             C   s(   y| j jƒ  W n tk
r"   Y nX dS )z This ends a macro. N)r(   ÚpopÚ
IndexError)r   r   r   r   Ú	end_macroš   s    zCommandStack.end_macroc             C   sÎ   t | jƒdkr4| jdkr.| j| j jj|ƒ}qDd}n| jd j|ƒ}|sX| j jd7  _|jƒ }|rh|S t | jƒdkr¸|  jd7  _| j| jd…= t	|| jjd�}| jj
|ƒ | | j_n| jd jj
|ƒ |S )aQ   This executes a command and saves it on the command stack so that
        it can be subsequently undone and redone.  'command' is an instance
        that implements the ICommand interface.  Its 'do()' method is called
        to execute the command.  If any value is returned by 'do()' then it is
        returned by 'push()'.
        r   Fr
   N)r   r   r   r   )r   r(   r+   r,   r   r   r-   r   r   r   r)   r.   r   )r   r   r!   ÚresultÚentryr   r   r   r'   ¢   s&    	

zCommandStack.pushr   c             C   sj   | j dkrdS |dkr | jƒ }n>d}x8| jd t| jƒk r\| j| jd  j|krRP | jƒ }q&W | | j_|S )zü If 'sequence_nr' is 0 then the last command that was undone is
        redone and any result returned.  Otherwise commands are redone up to
        and including the given 'sequence_nr' and any result of the last of
        these is returned.
        Ú Nr   r
   )Ú	redo_nameÚ	_redo_oner+   r   r,   r   r-   r.   )r   r   r3   r   r   r   r"   Ñ   s    

zCommandStack.redoc             C   sX   | j dkrdS |dkr | jƒ  n,x*| jdkrJ| j| j j|kr@P | jƒ  q"W | | j_dS )z” If 'sequence_nr' is 0 then the last command is undone.  Otherwise
        commands are undone up to and including the given 'sequence_nr'.
        r5   Nr   )Ú	undo_nameÚ	_undo_oner+   r,   r   r-   r.   )r   r   r   r   r   r$   ë   s    

zCommandStack.undoc             C   s$   |  j d7  _ | j| j  }|jjƒ S )z> Redo the command at the current index and return the result. r
   )r+   r,   r   r"   )r   r4   r   r   r   r7     s    zCommandStack._redo_onec             C   s(   | j | j }|  jd8  _|jjƒ  dS )z( Undo the command at the current index. r
   N)r,   r+   r   r$   )r   r4   r   r   r   r9     s    zCommandStack._undo_onec             C   s"   | j dkr| j| j  j}nd}|S )z# Get the clean state of the stack. r   T)r+   r,   r   )r   r   r   r   r   Ú
_get_clean  s    
zCommandStack._get_cleanc             C   s   | j dkr|| j| j  _dS )z# Set the clean state of the stack. r   N)r+   r,   r   )r   r   r   r   r   Ú
_set_clean  s    
zCommandStack._set_cleanc             C   sF   d}t | jƒdkrB| jd t | jƒk rB| j| jd  jjjddƒ}|S )z+ Get the name of the redo command, if any. r5   r   r
   ú&)r   r(   r+   r,   r   r&   Úreplace)r   r6   r   r   r   Ú_get_redo_name#  s    "zCommandStack._get_redo_namec             C   s<   d}t | jƒdkr8| jdkr8| j| j j}|jjddƒ}|S )z+ Get the name of the undo command, if any. r5   r   r<   )r   r(   r+   r,   r   r&   r=   )r   r8   r   r   r   r   Ú_get_undo_name-  s
    zCommandStack._get_undo_nameNr   )r   )r   ) r   r   r   r   r   r   r   r   r6   r   r   r-   r8   r   r+   r   r   r(   r   r,   r*   r/   r2   r'   r"   r$   r7   r9   r:   r;   r>   r?   r   r   r   r   r%   W   s(   /



r%   N)Z
traits.apir   r   r   r   r   r   r   r	   Zabstract_commandr   Z	i_commandr   Zi_command_stackr   Zi_undo_managerr   r   r   r%   r   r   r   r   Ú<module>   s   (,