a
    ¦ˆc{.  ã                   @   s\   d Z ddlmZ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 G dd„ deƒZdS )z. Abstract base class for all action managers. é    )	ÚBoolÚConstantÚEventÚ	HasTraitsÚInstanceÚListÚobserveÚPropertyÚStr)ÚActionController)ÚGroupc                       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edƒd	d
„ ƒZe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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                    sb   t ƒ jf i |¤Ž |  ¡ }|D ]>}t|tƒr6t|d�}t|tƒrR|  d|¡ |}q| |¡ q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)ÚsuperÚ__init__Ú_get_default_groupÚ
isinstanceÚstrr   ÚinsertÚappend)ÚselfÚargsZtraitsÚgroupÚarg©Ú	__class__© ú_/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/action/action_manager.pyr   F   s    


zActionManager.__init__c                 C   s   | j d d … S ©N)Ú_groups)r   r   r   r   Ú_get_groups{   s    zActionManager._get_groupsÚenabledc                 C   s   | j D ]}|j|_qd S r    )r!   Únewr#   ©r   Úeventr   r   r   r   Ú_enabled_updated€   s    
zActionManager._enabled_updatedÚvisiblec                 C   s   | j D ]}|j|_qd S r    )r!   r$   r(   r%   r   r   r   Ú_visible_updated…   s    
zActionManager._visible_updatedc                 C   s2   |   |¡}t|tƒr| j}n|  ¡ }| |¡ |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 default group.
        )Ú_prepare_itemr   r   r!   r   r   )r   Úitemr   r   r   r   r   Œ   s    


zActionManager.appendc                 C   s   | j D ]}| ¡  q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   |   |¡}t|tƒr| j}n|  ¡ }| ||¡ |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"   | j D ]}|j|kr|  S q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   |  d¡}t|ƒdkrR|  |d ¡}t|ƒdkrV|durV| d |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   é   N)ÚsplitÚlenÚ
_find_itemÚ	find_itemÚjoin)r   ÚpathÚ
componentsr+   r   r   r   r6   á   s    
zActionManager.find_itemc                 C   s$   || ƒ | j D ]}|  ||¡ q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<   ||ƒ |j D ](}t|tƒr*|  ||¡ q|  ||¡ q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   r:   Ú	walk_item)r   r   r;   r+   r   r   r   r:     s
    


zActionManager.walk_groupc                 C   s"   t |dƒr| |¡ 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+   r;   r   r   r   r>     s    

zActionManager.walk_itemc                 C   s0   |   | j¡}|du r,|  | 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)r0   ÚDEFAULT_GROUPr*   r!   r   r.   r   r   r   r   1  s
    
z ActionManager._get_default_groupc                 C   s0   t |tƒr| |_nt |tƒ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*   B  s    


zActionManager._prepare_itemc                 C   s*   | j D ]}| |¡}|dur|  S q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   r5   [  s
    


zActionManager._find_itemÚ c                 C   s2   t |d| jƒ |d7 }| jD ]}|  ||¡ qdS )z Render a manager! ÚManagerz  N)Úprintr   r!   Úrender_group)r   Úindentr   r   r   r   Údumpt  s    
zActionManager.dumpc                 C   sR   t |d|jƒ |d7 }|jD ]0}t|tƒr@t dƒ |  ||¡ q|  ||¡ qdS )z Render a group! r   z    z0Surely, a group cannot contain another group!!!!N)rE   r   r=   r   r   rF   Úrender_item)r   r   rG   r+   r   r   r   rF   |  s    

zActionManager.render_groupc                 C   s(   t |dƒr| |¡ nt|d|jƒ dS )z Render an item! r,   ZItemN)r?   rH   rE   r   )r   r+   rG   r   r   r   rI   ‰  s    
zActionManager.render_item)rC   )rC   )rC   ))Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r@   r   r   Ú
controllerr   r#   r	   r   r   r,   r
   r   r(   r   Úchangedr!   r   r"   r   r'   r)   r   r-   r   r0   r6   r<   r:   r>   r   r*   r5   rH   rF   rI   Ú__classcell__r   r   r   r   r      s:   5

	

r   N)rM   Z
traits.apir   r   r   r   r   r   r   r	   r
   Zpyface.action.action_controllerr   Zpyface.action.groupr   r   r   r   r   r   Ú<module>   s   ,