3
<Ad]1  ã               @   st   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	m
Z
mZ ddlmZ ddlmZ ddlZG dd	„ d	eƒZdS )
z. Abstract base class for all action managers. é    )Úprint_function)ÚBoolÚConstantÚEventÚ	HasTraitsÚInstance)ÚListÚPropertyÚStr)ÚActionController)ÚGroupNc                   sê   e Zd ZdZedƒZeeƒZe	dƒZ
eeeƒƒZeZe	dƒZeZeeƒZ‡ f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d„ Zdd„ Zdd„ Z dd„ Z!dd„ Z"d d!„ Z#d)d#d$„Z$d*d%d&„Z%d+d'd(„Z&‡  Z'S ),ÚActionManagera   Abstract base class for all action managers.

    An action manager contains a list of groups, with each group containing a
    list of items.

    There are currently three concrete sub-classes:

    1) 'MenuBarManager'
    2) 'MenuManager'
    3) 'ToolBarManager'

    Z	additionsTc                sh   t t| ƒjf |Ž | jƒ }xH|D ]@}t|tjƒr:t|d�}t|tƒrV| jd|ƒ |}q |j	|ƒ q W dS )aF   Creates a new action manager.

        Parameters
        ----------
        args : collection of strings, Group instances, or ActionManagerItem instances
            Positional arguments are interpreted as Items or Groups managed
            by the action manager.

        Notes
        -----

        If a Group is passed as a positional agrument then it is added to the
        manager and any subsequent Items arguments are appended to the Group
        until another Group is encountered.

        If a string is passed, a Group is created with id set to the string.
        )Úidé   Néÿÿÿÿ)
Úsuperr   Ú__init__Ú_get_default_groupÚ
isinstanceÚsixÚstring_typesr   ÚinsertÚappend)ÚselfÚargsÚtraitsÚgroupÚarg)Ú	__class__© úv/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/pyface-6.1.2-py3.6.egg/pyface/action/action_manager.pyr   L   s    


zActionManager.__init__c             C   s   | j d d … S )N)Ú_groups)r   r   r   r    Ú_get_groups‡   s    zActionManager._get_groupsc             C   s   x| j D ]
}||_qW d S )N)r!   Úenabled)r   Ú
trait_nameÚoldÚnewr   r   r   r    Ú_enabled_changedŒ   s    zActionManager._enabled_changedc             C   s   x| j D ]
}||_qW d S )N)r!   Úvisible)r   r$   r%   r&   r   r   r   r    Ú_visible_changed�   s    zActionManager._visible_changedc             C   s2   | j |ƒ}t|tƒr| j}n| jƒ }|j|ƒ |S )a,   Append an item to the manager.

        Parameters
        ----------
        item : string, Group instance or ActionManagerItem instance
            The item to append.

        Notes
        -----

        If the item is a group, the Group is appended to the manager's list
        of groups.  It the item is a string, then a group is created with
        the string as the ``id`` and the new group is appended to the list
        of groups.  If the item is an ActionManagerItem then the item is
        appended to the manager's defualt group.
        )Ú_prepare_itemr   r   r!   r   r   )r   Úitemr   r   r   r    r   –   s    


zActionManager.appendc             C   s   x| j D ]}|jƒ  qW dS )z— Called when the manager is no longer required.

        By default this method simply calls 'destroy' on all of the manager's
        groups.
        N)ÚgroupsÚdestroy)r   r   r   r   r    r-   ±   s    zActionManager.destroyc             C   s4   | j |ƒ}t|tƒr| j}n| jƒ }|j||ƒ |S )a–   Insert an item into the manager at the specified index.

        Parameters
        ----------
        index : int
            The position at which to insert the object
        item : string, Group instance or ActionManagerItem instance
            The item to insert.

        Notes
        -----

        If the item is a group, the Group is inserted into the manager's list
        of groups.  It the item is a string, then a group is created with
        the string as the ``id`` and the new group is inserted into the list
        of groups.  If the item is an ActionManagerItem then the item is
        inserted into the manager's defualt group.
        )r*   r   r   r!   r   r   )r   Úindexr+   r   r   r   r    r   º   s    

zActionManager.insertc             C   s&   x | j D ]}|j|kr|S qW dS dS )a   Find a group with a specified Id.

        Parameters
        ----------
        id : str
            The id of the group to find.

        Returns
        -------
        group : Group instance
            The group which matches the id, or None if no such group exists.
        N)r!   r   )r   r   r   r   r   r    Ú
find_groupØ   s    
zActionManager.find_groupc             C   sZ   |j dƒ}t|ƒdkrR| j|d ƒ}t|ƒdkrV|dk	rV|jdj|dd… ƒƒ}nd}|S )aQ   Find an item using a path.

        Parameters
        ----------
        path : str
            A '/' separated list of contribution Ids.

        Returns
        -------
        item : ActionManagerItem or None
            Returns the matching ActionManagerItem, or None if any component
            of the path is not found.
        ú/r   r   N)ÚsplitÚlenÚ
_find_itemÚ	find_itemÚjoin)r   ÚpathÚ
componentsr+   r   r   r    r4   ë   s    
zActionManager.find_itemc             C   s(   || ƒ x| j D ]}| j||ƒ qW dS )a   Walk the manager applying a function at every item.

        The components are walked in pre-order.

        Parameters
        ----------
        fn : callable
            A callable to apply to the tree of groups and items, starting with
            the manager.
        N)r!   Ú
walk_group)r   Úfnr   r   r   r    Úwalk  s    zActionManager.walkc             C   s@   ||ƒ x2|j D ](}t|tƒr,| j||ƒ q| j||ƒ qW dS )zè Walk a group applying a function at every item.

        The components are walked in pre-order.

        Parameters
        ----------
        fn : callable
            A callable to apply to the tree of groups and items.
        N)Úitemsr   r   r8   Ú	walk_item)r   r   r9   r+   r   r   r    r8     s
    

zActionManager.walk_groupc             C   s"   t |dƒr|j|ƒ n||ƒ dS )zï Walk an item (may be a sub-menu manager remember!).

        The components are walked in pre-order.

        Parameters
        ----------
        fn : callable
            A callable to apply to the tree of items and subgroups.
        r,   N)Úhasattrr:   )r   r+   r9   r   r   r    r<   (  s    

zActionManager.walk_itemc             C   s0   | j | jƒ}|dkr,| j| jƒ}| jj|ƒ |S )zÙ Returns the manager's default group.

        This will create this group if it doesn't already exist.

        Returns
        -------
        group : Group instance
            The manager's default group.
        N)r/   ÚDEFAULT_GROUPr*   r!   r   )r   r   r   r   r    r   ;  s
    
z ActionManager._get_default_groupc             C   s2   t |tƒr| |_nt |tjƒr.t|d�}| |_|S )aC   Prepare an item to be added to this ActionManager.

        Parameters
        ----------
        item : string, Group instance or ActionManagerItem instance
            The item to be added to this ActionManager

        Returns
        -------
        item : Group or ActionManagerItem
            Modified item
        )r   )r   r   Úparentr   r   )r   r+   r   r   r    r*   L  s    

zActionManager._prepare_itemc             C   s.   x(| j D ]}|j|ƒ}|dk	r|S qW dS dS )a8   Find an item with a spcified Id.

        Parameters
        ----------
        id : str
            The id of the item to be found.

        Returns
        -------
        item : ActionManagerItem or None
            Returns the item with the specified Id, or None if no such item
            exists.
        N)r,   Úfind)r   r   r   r+   r   r   r    r3   e  s
    
zActionManager._find_itemÚ c             C   s6   t |d| jƒ |d7 }x| jD ]}| j||ƒ qW dS )z Render a manager! ÚManagerz  N)Úprintr   r!   Úrender_group)r   Úindentr   r   r   r    Údump~  s    zActionManager.dumpc             C   sV   t |d|jƒ |d7 }x:|jD ]0}t|tƒrBt dƒ | j||ƒ q| j||ƒ qW dS )z Render a group! r   z    z0Surely, a group cannot contain another group!!!!N)rC   r   r;   r   r   rD   Úrender_item)r   r   rE   r+   r   r   r    rD   †  s    
zActionManager.render_groupc             C   s(   t |dƒr|j|ƒ nt|d|jƒ dS )z Render an item! r,   ZItemN)r=   rF   rC   r   )r   r+   rE   r   r   r    rG   “  s    
zActionManager.render_item)rA   )rA   )rA   )(Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r>   r   r   Z
controllerr   r#   r	   r   r   r,   r
   r   r(   r   Úchangedr!   r   r"   r'   r)   r   r-   r   r/   r4   r:   r8   r<   r   r*   r3   rF   rD   rG   Ú__classcell__r   r   )r   r    r      s6   ;	

r   )rK   Ú
__future__r   Z
traits.apir   r   r   r   r   r   r	   r
   Zpyface.action.action_controllerr   Zpyface.action.groupr   r   r   r   r   r   r    Ú<module>   s   