3
7ßL]¬   ã               @   s�   d Z ddlmZ ddl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 ddlmZ G d	d
„ d
eƒZG dd„ deƒZdS )z" A group of action manager items. é    )Úpartial)ÚAnyÚBoolÚ	HasTraitsÚInstanceÚListÚProperty)ÚStr)Úuser_name_for)ÚAction)Ú
ActionItem)ÚActionManagerItemc                   s¤   e Zd ZdZedƒZedƒZeZe	Z
eZedƒZ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edd„ ƒZ‡  ZS )ÚGroupzË A group of action manager items.

    By default, a group declares itself as requiring a separator when it is
    visualized, but this can be changed by setting its 'separator' trait to
    False.

    Tc                s.   t t| ƒjf |Ž x|D ]}| j|ƒ qW dS )z¤ Creates a new menu manager.

        Parameters
        ----------
        items : collection of ActionManagerItems
            Items to add to the group.
        N)Úsuperr   Ú__init__Úappend)ÚselfÚitemsZtraitsÚitem)Ú	__class__© ú7/tmp/pip-install-5sebvq2j/pyface/pyface/action/group.pyr   D   s    	
zGroup.__init__c             C   s   | j d d … S )N)Ú_items)r   r   r   r   Ú
_get_itemsY   s    zGroup._get_itemsc             C   s   x| j D ]
}||_qW d S )N)r   Úenabled)r   Z
trait_nameÚoldÚnewr   r   r   r   Ú_enabled_changed^   s    zGroup._enabled_changedc             C   s   | j t| jƒ|ƒS )a[   Appends an item to the group.

        Parameters
        ----------
        item : ActionManagerItem, Action or callable
            The item to append.

        Returns
        -------
        item : ActionManagerItem
            The actually inserted item.

        Notes
        -----
        If the item is an ActionManagerItem instance it is simply appended.
        If the item is an Action instance, an ActionItem is created for the
        action, and that is appended.  If the item is a callable, then an
        Action is created for the callable, and then that is handled as above.
        )ÚinsertÚlenr   )r   r   r   r   r   r   d   s    zGroup.appendc             C   s
   g | _ dS )z" Remove all items from the group. N)r   )r   r   r   r   Úclearz   s    zGroup.clearc             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 items in the
        group.
        N)r   Údestroy)r   r   r   r   r   r!   ~   s    zGroup.destroyc             C   sR   t |tƒrt|d�}n$t|ƒr:t|jƒ}tt||d�d�}| |_| jj||ƒ |S )a¶   Inserts an item into the group at the specified index.

        Parameters
        ----------
        index : int
            The position to insert the item.
        item : ActionManagerItem, Action or callable
            The item to insert.

        Returns
        -------
        item : ActionManagerItem
            The actually inserted item.

        Notes
        -----

        If the item is an ActionManagerItem instance it is simply inserted.
        If the item is an Action instance, an ActionItem is created for the
        action, and that is inserted.  If the item is a callable, then an
        Action is created for the callable, and then that is handled as above.
        )Úaction)ÚnameZ
on_perform)	Ú
isinstancer   r   Úcallabler
   Ú__name__Úparentr   r   )r   Úindexr   r#   r   r   r   r   ‡   s    

zGroup.insertc             C   s   | j j|ƒ d|_dS )z‘ Removes an item from the group.

        Parameters
        ----------
        item : ActionManagerItem
            The item to remove.
        N)r   Úremover'   )r   r   r   r   r   r)   ©   s    zGroup.removec             C   s    | j j|ƒ}| j||ƒ ||fS )aé   Inserts an item into the group before the specified item.

        Parameters
        ----------
        before : ActionManagerItem
            The item to insert before.
        item : ActionManagerItem, Action or callable
            The item to insert.

        Returns
        -------
        index, item : int, ActionManagerItem
            The position inserted, and the item actually inserted.

        Notes
        -----

        If the item is an ActionManagerItem instance it is simply inserted.
        If the item is an Action instance, an ActionItem is created for the
        action, and that is inserted.  If the item is a callable, then an
        Action is created for the callable, and then that is handled as above.
        )r   r(   r   )r   Zbeforer   r(   r   r   r   Úinsert_before´   s    zGroup.insert_beforec             C   s$   | j j|ƒ}| j|d |ƒ ||fS )aç   Inserts an item into the group after the specified item.

        Parameters
        ----------
        before : ActionManagerItem
            The item to insert after.
        item : ActionManagerItem, Action or callable
            The item to insert.

        Returns
        -------
        index, item : int, ActionManagerItem
            The position inserted, and the item actually inserted.

        Notes
        -----

        If the item is an ActionManagerItem instance it is simply inserted.
        If the item is an Action instance, an ActionItem is created for the
        action, and that is inserted.  If the item is a callable, then an
        Action is created for the callable, and then that is handled as above.
        é   )r   r(   r   )r   Úafterr   r(   r   r   r   Úinsert_afterÏ   s    zGroup.insert_afterc             C   s&   x | j D ]}|j|kr|S qW dS dS )a   Find the item with the specified id.

        Parameters
        ----------
        id : str
            The id of the item

        Returns
        -------
        item : ActionManagerItem
            The item with the specified Id, or None if no such item exists.
        N)r   Úid)r   r.   r   r   r   r   Úfindê   s    
z
Group.findc             O   s   t | f|ž|ŽS )zœ Create a factory for a group with the given arguments.

        This is particularly useful for passing context to Tasks schema
        additions.
        )r   )ÚclsÚargsÚkwargsr   r   r   Úfactoryý   s    zGroup.factory)r&   Ú
__module__Ú__qualname__Ú__doc__r   r   Zvisibler	   r.   r   r   r   r'   Ú	separatorr   r   r   r   r   r   r    r!   r   r)   r*   r-   r/   Úclassmethodr3   Ú__classcell__r   r   )r   r   r      s(   	"r   c               @   s   e Zd ZdZdS )Ú	Separatora(   A convenience class.

    This is only used in 'cheap and cheerful' applications that create menus
    like::

        file_menu = MenuManager(
            CopyAction(),
            Separator(),
            ExitAction()
        )

    Hopefully, 'Separator' is more readable than 'Group'...
    N)r&   r4   r5   r6   r   r   r   r   r:     s   r:   N)r6   Ú	functoolsr   Z
traits.apir   r   r   r   r   r   r	   Ztraits.trait_baser
   Zpyface.action.actionr   Zpyface.action.action_itemr   Z!pyface.action.action_manager_itemr   r   r:   r   r   r   r   Ú<module>   s     k