a
    §ˆcÍN  ã                   @   s¼   d Z ddlmZ ddl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 d
d„ ZG dd„ deƒZeƒ addd„ZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )zp Defines the Handler class used to manage and control the editing process in
    a Traits-based user interface.
é    )Úgetfullargspec)ÚHasPrivateTraitsÚ	HasTraitsÚInstanceé   )Útoolkit)Úon_help_call)ÚViewElement)Úuser_name_for)ÚUIInfoc                 C   s&   | j }|j |jd¡sdS | ¡  dS )zFCloses a DockControl (if allowed by the associated Traits UI Handler).TF)ÚdataÚhandlerÚcloseÚinfoZdispose)Údock_controlÚui© r   úS/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/handler.pyÚclose_dock_control   s
    r   c                       sØ   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	dd„ Z
d3dd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ Zdd „ Zd4d"d#„Zd$d%„ Zd5d'd(„Zd6‡ fd)d*„	Zd+d,„ Zd-d.„ Zd/d0„ Zd1d2„ Z‡  ZS )7ÚHandlerzdProvides access to and control over the run-time workings of a
    Traits-based user interface.
    c                 C   s   dS )ay  Informs the handler what the UIInfo object for a View will be.

        This method is called before the UI for the View has been
        constructed. It is provided so that the handler can save the
        reference to the UIInfo object in case it exposes viewable traits
        whose values are properties that depend upon items in the context
        being edited.
        Nr   ©Úselfr   r   r   r   Ú	init_info5   s    	zHandler.init_infoc                 C   s   dS )a  Initializes the controls of a user interface.

        This method is called after all user interface elements have been
        created, but before the user interface is displayed. Override this
        method to customize the user interface before it is displayed.

        Parameters
        ----------
        info : UIInfo object
            The UIInfo object associated with the view

        Returns
        -------
        initialized : bool
            A Boolean, indicating whether the user interface was successfully
            initialized. A True value indicates that the UI can be displayed;
            a False value indicates that the display operation should be
            cancelled. The default implementation returns True without taking
            any other action.
        Tr   r   r   r   r   Úinit@   s    zHandler.initc                 C   s   t ƒ  |j¡ dS )a¢  Positions a dialog-based user interface on the display.

        This method is called after the user interface is initialized (by
        calling init()), but before the user interface is displayed. Override
        this method to position the window on the display device. The default
        implementation calls the position() method of the current toolkit.

        Usually, you do not need to override this method, because you can
        control the window's placement using the **x** and **y** attributes
        of the View object.

        Parameters
        ----------
        info : UIInfo object
            The UIInfo object associated with the window
        N)r   Úpositionr   r   r   r   r   r   W   s    zHandler.positionc                 C   s   dS )a¼  Handles the user attempting to close a dialog-based user interface.

        This method is called when the user attempts to close a window, by
        clicking an **OK** or **Cancel** button, or clicking a Close control
        on the window). It is called before the window is actually destroyed.
        Override this method to perform any checks before closing a window.

        While Traits UI handles "OK" and "Cancel" events automatically, you
        can use the value of the *is_ok* parameter to implement additional
        behavior.

        Parameters
        ----------
        info : UIInfo object
            The UIInfo object associated with the view
        is_ok : Boolean
            Indicates whether the user confirmed the changes (such as by
            clicking **OK**.)

        Returns
        -------
        allow_close : bool
            A Boolean, indicating whether the window should be allowed to
            close.
        Tr   ©r   r   Zis_okr   r   r   r   j   s    zHandler.closec                 C   s   dS )aØ  Handles a dialog-based user interface being closed by the user.

        This method is called *after* the window is destroyed. Override this
        method to perform any clean-up tasks needed by the application.

        Parameters
        ----------
        info : UIInfo object
            The UIInfo object associated with the view
        is_ok : Boolean
            Indicates whether the user confirmed the changes (such as by
            clicking **OK**.)
        Nr   r   r   r   r   Úclosed†   s    zHandler.closedc                 C   s   dS )z,Handles the **Revert** button being clicked.Nr   r   r   r   r   Úrevert–   s    zHandler.revertc                 C   s   dS )z+Handles the **Apply** button being clicked.Nr   r   r   r   r   Úapplyš   s    zHandler.applyNc                 C   s    |du r|j j}tƒ ||ƒ dS )aG  Shows the help associated with the view.

        This method is called when the user clicks a **Help** button in a
        Traits user interface. The method calls the global help handler, which
        might be the default help handler, or might be a custom help handler.
        See **traitsui.help** for details about the setting the
        global help handler.

        Parameters
        ----------
        info : UIInfo object
            The UIInfo object associated with the view
        control : UI control
            The control that invokes the help dialog box
        N)r   Úcontrolr   )r   r   r   r   r   r   Ú	show_helpž   s    zHandler.show_helpc                 C   s¢   |j dkr|j }nd |j ¡ ¡}|  |¡D ]D}t||dƒ}|dur,t|ƒ}t|jƒdkrb|ƒ  n||ƒ  dS q,t|j	ƒ}t|jƒdkr”| 	¡  n
| 	|¡ dS )ak  Perform computation for an action.

        The default method looks for a method matching ``action.action`` and
        calls it (sniffing the signature to determine how to call it for
        historical reasons).  If this is not found, then it calls the
        :py:meth:`~traitsui.menu.Action.perform` method of the action.

        Parameters
        ----------
        info : UIInfo instance
            The UIInfo assicated with the view, if available.
        action : Action instance
            The Action that the user invoked.
        event : ActionEvent instance
            The ActionEvent associated with the user action.

        Notes
        -----
        If overriding in a subclass, the method needs to ensure that any
        standard menu action items that are needed (eg. "Close", "Undo",
        "Redo", "Help", etc.) get dispatched correctly.
        Ú z_{}_clickedNr   )
ÚactionÚformatÚnameÚlowerÚget_perform_handlersÚgetattrr   ÚlenÚargsÚperform)r   r   r"   ÚeventÚmethod_nameÚobjectÚmethodÚspecificationr   r   r   r*   ²   s    


zHandler.performc                    s0   | g}ˆ dur,ddg}|‡ fdd„|D ƒ7 }|S )a´  Return a list of objects which can handle actions.

        This method may be overridden by sub-classes to return a more relevant
        set of objects.

        Parameters
        ----------
        info : UIInfo instance or None
            The UIInfo associated with the view, or None.

        Returns
        -------
        handlers : list
            A list of objects that may potentially have action methods on them.
        Nr-   Úmodelc                    s$   g | ]}|ˆ j jv rˆ j j| ‘qS r   )r   Úcontext)Ú.0r$   ©r   r   r   Ú
<listcomp>ô   s   þz0Handler.get_perform_handlers.<locals>.<listcomp>r   )r   r   ÚhandlersZadditional_objectsr   r3   r   r&   á   s    þzHandler.get_perform_handlersc                 C   s   t |||ƒ dS )a  Handles the user setting a specified object trait's value.

        This method is called when an editor attempts to set a new value for
        a specified object trait attribute. Use this method to control what
        happens when a trait editor tries to set an attribute value. For
        example, you can use this method to record a history of changes, in
        order to implement an "undo" mechanism. No result is returned. The
        default implementation simply calls the built-in setattr() function.
        If you override this method, make sure that it actually sets the
        attribute, either by calling the parent method or by setting the
        attribute directly

        Parameters
        ----------
        info : UIInfo instance
            The UIInfo for the current UI
        object : object
            The object whose attribute is being set
        name : string
            The name of the attribute being set
        value
            The value to which the attribute is being set

        N)Úsetattr)r   r   r-   r$   Úvaluer   r   r   r6   û   s    zHandler.setattrc                 C   sÌ   t |tƒr|S |jj}d||f }d}|r2d| }d|||f d||f d||f g}	|rd|	 |¡ |	D ]P}
|  |
¡}|dur†|  S t| d|
 dƒ}t|ƒrh|||ƒ}|durh|  S qh| |¡pÊ| ¡ S )zGets a specified View object.z%s_%sr!   Ú_z%s_%s%sz%s%sNztrait_view_for_%s)Ú
isinstancer	   Ú	__class__Ú__name__ÚappendÚ
trait_viewr'   Úcallable)r   r   Úviewr-   Zobject_nameZ
trait_nameÚklassÚcnameZaviewÚnamesr$   Úresultr.   r   r   r   Útrait_view_for  s.    


ý



zHandler.trait_view_forc                 C   s@   ddl m} t||ƒr$|  ||j¡S |jjj}|duo>t||ƒS )z3Can the specified object be inserted into the view?r   )ÚDockControlN)Úpyface.dock.apirE   r9   Ú
can_importÚexportr   r?   Ú
drop_class)r   r   r-   rE   rI   r   r   r   Úcan_dropD  s
    

zHandler.can_dropc                 C   s   ||j jjv S )N)r   r?   Zimports)r   r   Úcategoryr   r   r   rG   N  s    zHandler.can_importc              	   C   s:  ddl m}m} ddlm} z
|j}W n(   z
|j}W n   d}Y n0 Y n0 t|ƒdkrht|j	j
ƒ}d}d}	t||ƒrØ|}
|
j}|
j}	|
j}| ¡ }|r¨|
jdd� | |¡}|rÖ|
j||jjd	� | |
¡ |
S n<t||ƒrò|}| |¡}n"| |¡}||d
�}|jj}	|j}||||	|jj|dd�}| |¡ |S )z6Returns the DockControl object for a specified object.r   )Ú	IDockablerE   r   )ÚDockableViewElementr!   NT)Úforce)r   Ústyle)r   )r   r$   rH   rO   ÚimageZ	closeable)rF   rL   rE   Zdockable_view_elementrM   r$   Úlabelr(   r
   r:   r;   r9   rP   rH   ÚdockableZdockable_should_closer   Zdockable_get_controlZ	trait_setÚownerrO   Zdockable_init_dockcontrolZget_dockable_uir?   r   )r   r   Úparentr-   rL   rE   rM   r$   rP   rH   r   rR   r   r   r   Údcr   r   r   Údock_control_forQ  sZ    



ÿ



ú	
zHandler.dock_control_forTc                 C   s   ddl m} |||d� dS )z*Creates a new view of a specified control.r   )ÚDockWindowShell)Ú	use_mouseN)rF   rW   )r   r   rX   rW   r   r   r   Úopen_view_forŒ  s    zHandler.open_view_forc                 C   s   |j r|jj ¡  dS )z$Handles a DockWindow becoming empty.N)Ú
auto_closer   Z	GetParentZDestroy)r   Zdock_windowr   r   r   Údock_window_empty’  s    zHandler.dock_window_emptyr!   c           	   
   K   s:   |du r| }|du r| }|   |¡ ||||  ¡ ||||¡S )zEdits the object's traits.N)r=   r   Ztrait_view_elements)	r   r?   rT   Úkindr1   r   ÚidÚ
scrollabler)   r   r   r   Úedit_traits™  s    
øzHandler.edit_traitsc	           
   	      s&   t ƒ j||||||p| ||fi |	¤ŽS )zConfigures the object's traits.)ÚsuperÚconfigure_traits)
r   Úfilenamer?   r\   Zeditr1   r   r]   r^   r)   ©r:   r   r   ra   ¶  s    ø	÷zHandler.configure_traitsc                 C   s   |j jdur|j j ¡  dS )z!Handles an "Undo" change request.N)r   ÚhistoryZundor   r   r   r   Ú_on_undoÑ  s    zHandler._on_undoc                 C   s   |j jdur|j j ¡  dS )z Handles a "Redo" change request.N)r   rd   Zredor   r   r   r   Ú_on_redoÖ  s    zHandler._on_redoc                 C   s&   |j jdur"|j j ¡  |  |¡ dS )z'Handles a "Revert all changes" request.N)r   rd   r   r   r   r   r   Ú
_on_revertÛ  s    zHandler._on_revertc                 C   s(   |j jdur$|  |d¡r$|j j ¡  dS )zHandles a "Close" request.NT)r   rS   r   r   r   r   r   Ú	_on_closeá  s    zHandler._on_close)N)T)NNNNNr!   N)NNNTNNr!   N)r;   Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r    r*   r&   r6   rD   rJ   rG   rV   rY   r[   r_   ra   re   rf   rg   rh   Ú__classcell__r   r   rc   r   r   0   sN   
/.
;
	       ø
        ÷r   Nc                 C   s   t | tƒr| atS )zŽReturns the global default handler.

    If *handler* is an instance of Handler, this function sets it as the
    global default handler.
    )r9   r   Ú_default_handler)r   r   r   r   Údefault_handlerî  s    
rn   c                       sJ   e Zd ZdZeeƒZeeƒZd‡ fdd„	Z	dd„ Z
dd„ Zd	d
„ Z‡  ZS )Ú
Controllerap  Defines a handler class which provides a view and controller for a
    specified model.

    This class is used when implementing a standard MVC-based design. The
    **model** trait contains most, if not all, of the data being viewed,
    and can be referenced in a Controller instance's View definition using
    unadorned trait names. (e.g., ``Item('name')``).
    Nc                    s$   t ƒ jf i |¤Ž |dur || _dS )z8Initializes the object and sets the model (if supplied).N)r`   Ú__init__r0   )r   r0   Úmetadatarc   r   r   rp     s    zController.__init__c                 C   s   | j | | dœS )úVReturns the default context to use for editing or configuring
        traits.
        )r-   Ú
controllerr   ©r0   ©r   r   r   r   Útrait_context  s    zController.trait_contextc                 C   s
   | | j gS )a”  Return a list of objects which can handle actions.

        By default this returns the Controller instance and the model.

        Parameters
        ----------
        info : UIInfo instance or None
            The UIInfo associated with the view, or None.

        Returns
        -------
        handlers : list
            A list of objects that may potentially have action methods on them.
        rt   r   r   r   r   r&     s    zController.get_perform_handlersc                 C   s
   || _ dS )z>Informs the handler what the UIInfo object for a View will be.Nr3   r   r   r   r   r   .  s    zController.init_info)N)r;   ri   rj   rk   r   r   r0   r   r   rp   rv   r&   r   rl   r   r   rc   r   ro   û  s   ro   c                   @   s   e Zd ZdZdd„ ZdS )Ú	ModelViewap  Defines a handler class which provides a view and controller for a
    specified model.

    This class is useful when creating a variant of the standard MVC-based
    design. A subclass of ModelView reformulates a number of traits on
    its **model** object as properties on the ModelView subclass itself,
    usually in order to convert them into a more user-friendly format. In
    this design, the ModelView subclass supplies not only the view and
    the controller, but also, in effect, the model (as a set of properties
    wrapped around the original model). Because of this, the ModelView
    context dictionary specifies the ModelView instance itself as the
    special *object* value, and assigns the original model object as the
    *model* value. Thus, the traits of the ModelView object can be
    referenced in its View definition using unadorned trait names.
    c                 C   s   | | | j dœS )rr   )r-   r   r0   rt   ru   r   r   r   rv   F  s    zModelView.trait_contextN)r;   ri   rj   rk   rv   r   r   r   r   rw   3  s   rw   c                   @   s   e Zd ZdS )ÚViewHandlerN)r;   ri   rj   r   r   r   r   rx   M  s   rx   )N)rk   Úinspectr   Z
traits.apir   r   r   r   Úhelpr   Zview_elementr	   Úhelperr
   Zui_infor   r   r   rm   rn   ro   rw   rx   r   r   r   r   Ú<module>   s"      >
8