a
    §ˆcÇA  ã                   @   sÎ   d dl Z d dlmZmZmZ d dlm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 d dlmZ d dlmZmZ d d	lmZ d d
lmZ e  e¡Z G dd„ deƒZ!G dd„ de
ƒZ"dS )é    N)ÚMenuBarManagerÚStatusBarManagerÚToolBarManager)ÚApplicationWindow)	ÚBoolÚCallableÚHasStrictTraitsÚInstanceÚListÚPropertyÚStrÚVetoableÚobserve)ÚTaskActionManagerBuilder)Ú	IDockPane)Ú	ITaskPane)ÚTaskÚ
TaskLayout©ÚTaskWindowBackend)ÚTaskWindowLayoutc                       sd  e Zd ZdZeeddgd�ZeeƒZ	ee
ƒZee
ƒZeeƒZeeƒZeeƒZedƒZe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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/d0„ Z-d1d2„ Z.d3d4„ Z/e0d5ƒd6d7„ ƒZ1e0d8ƒd9d:„ ƒZ2e0d;ƒd<d=„ ƒZ3‡  Z4S )?Ú
TaskWindowzœ The the top-level window to which tasks can be assigned.

    A TaskWindow is responsible for creating and the managing the controls of
    its tasks.
    zactive_task.nameÚ_title)r   z"pyface.tasks.task_window.TaskStatec                    s.   | j  ¡  | jD ]}|  |¡ qtƒ  ¡  dS )zI Overridden to ensure that all task panes are cleanly destroyed.
        N)Ú_window_backendÚdestroyÚ_statesÚ_destroy_stateÚsuper)ÚselfÚstate©Ú	__class__© ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/pyface/tasks/task_window.pyr   Z   s    

zTaskWindow.destroyc                 C   sh   t ƒ  | _}|jsV| jdu r$|  ¡  | jdu rF| jrF|  | jd j¡ |  	d¡ | | _
| jduof|j S )zÉ Opens the window.

        Overridden to make the 'opening' event vetoable and to activate a task
        if one has not already been activated. Returns whether the window was
        opened.
        Nr   T)r   ZopeningZvetoÚcontrolÚ_createÚ_active_stater   Úactivate_taskÚtaskÚshowZopened©r   Úeventr"   r"   r#   Úopenk   s    

zTaskWindow.openc                 C   s   | j  |¡S )z, Delegate to the TaskWindowBackend.
        )r   Zcreate_contents)r   Úparentr"   r"   r#   Ú_create_contents…   s    zTaskWindow._create_contentsc                 C   sv   |   |¡}|r`|| jkr`| jdur0| j | j¡ |jsD| ¡  d|_| j |¡ || _| ¡  n|srt d| ¡ dS )zE Activates a task that has already been added to the window.
        NTz<Cannot activate task %r: task does not belong to the window.)	Ú
_get_stater&   r   Ú	hide_taskÚinitializedZ	show_taskZ	activatedÚloggerÚwarning©r   r(   r   r"   r"   r#   r'   Ž   s     


ÿÿzTaskWindow.activate_taskc                 C   sà   |j durt d| ¡ dS | |_ t||jd�}| j |¡ | jdu rN|  ¡  | 	¡ |_
||j
_|j
 | j¡ | ¡ |_|jD ]}|j ||d�¡ q~|jD ]}||_| | j¡ qœ| j|d�}| ¡ |_|j|_| ¡ |_dS )z? Adds a task to the window. The task is not activated.
        Nz<Cannot add task %r: task has already been added to a window!)r(   Úlayout©r(   )Úwindowr2   ÚerrorÚ	TaskStateÚdefault_layoutr   Úappendr$   r%   Zcreate_central_paneÚcentral_paner(   ÚcreateZcreate_dock_panesÚ
dock_panesZextra_dock_pane_factoriesÚaction_manager_builder_factoryZcreate_menu_bar_managerÚmenu_bar_managerZ
status_barÚstatus_bar_managerZcreate_tool_bar_managersÚtool_bar_managers)r   r(   r   Zdock_pane_factoryÚ	dock_paneZbuilderr"   r"   r#   Úadd_task©   s2    
ÿÿ





zTaskWindow.add_taskc                 C   sT   |   |¡}|rB| j|kr*| j |¡ d| _|  |¡ | j |¡ nt d| ¡ dS )zs Removes a task that has already been added to the window. All the
            task's panes are destroyed.
        Nz:Cannot remove task %r: task does not belong to the window.)	r/   r&   r   r0   r   r   Úremover2   r3   r4   r"   r"   r#   Úremove_taskÏ   s    


ÿÿzTaskWindow.remove_taskc                 C   s@   | j r<|  ¡ }d}| jr0| | j¡d t|ƒ }||  ¡  dS )zq Shifts focus to the "next" pane, taking into account the active pane
            and the pane geometry.
        r   é   N)r&   Ú_get_pane_ringÚactive_paneÚindexÚlenÚ	set_focus©r   ÚpanesrJ   r"   r"   r#   Úfocus_next_paneã   s    zTaskWindow.focus_next_panec                 C   s8   | j r4|  ¡ }d}| jr(| | j¡d }||  ¡  dS )zu Shifts focus to the "previous" pane, taking into account the active
            pane and the pane geometry.
        éÿÿÿÿrG   N)r&   rH   rI   rJ   rL   rM   r"   r"   r#   Úfocus_previous_paneî   s    zTaskWindow.focus_previous_panec                 C   s   |   |¡}|r|jS dS )z: Returns the central pane for the specified task.
        N)r/   r<   r4   r"   r"   r#   Úget_central_paneù   s    
zTaskWindow.get_central_paneNc                 C   s,   |du r| j }n
|  |¡}|r(| |¡S dS )z´ Returns the dock pane in the task with the specified ID, or
            None if no such dock pane exists. If a task is not specified, the
            active task is used.
        N)r&   r/   Úget_dock_pane)r   Úidr(   r   r"   r"   r#   rS   ÿ   s    
zTaskWindow.get_dock_panec                 C   s    |   |¡}|r|jdd… S g S )z8 Returns the dock panes for the specified task.
        N)r/   r>   r4   r"   r"   r#   Úget_dock_panes
  s    
zTaskWindow.get_dock_panesc                 C   s   |   |¡}|r|jS dS )z] Returns the task with the specified ID, or None if no such task
            exists.
        N)r/   r(   )r   rT   r   r"   r"   r#   Úget_task  s    
zTaskWindow.get_taskc                 C   s   | j r| j ¡ S dS )zg Returns a TaskLayout (for the active task) that reflects the state
            of the window.
        N)r&   r   Ú
get_layout©r   r"   r"   r#   rW     s    
zTaskWindow.get_layoutc                 C   s   | j r| j |¡ dS )zh Applies a TaskLayout (which should be suitable for the active task)
            to the window.
        N)r&   r   Ú
set_layout)r   r5   r"   r"   r#   rY   !  s    zTaskWindow.set_layoutc                 C   s   | j r|  | j j¡ dS )z8 Restores the active task's default TaskLayout.
        N)Úactive_taskrY   r:   rX   r"   r"   r#   Úreset_layout(  s    zTaskWindow.reset_layoutc                 C   sd   t | j| j| jd�}| jD ]D}|| jkr>|jj|_| j	 
¡ }n
|j ¡ }|jj|_|j |¡ q|S )zI Returns a TaskWindowLayout for the current state of the window.
        )ÚpositionÚsizeÚ
size_state)r   r\   r]   r^   r   r&   r(   rT   rZ   r   rW   r5   Zclone_traitsÚitemsr;   )r   Úresultr   r5   r"   r"   r#   Úget_window_layout.  s    ÿ




zTaskWindow.get_window_layoutc                 C   s”   |j | _ |j| _|j| _|jD ]8}t|tƒr.q|  |j¡}|rF||_qt	 
d|j ¡ q|  | ¡ ¡}|r�|| jkr„| j |j¡ n|  |j¡ dS )z3 Applies a TaskWindowLayout to the window.
        zDCannot apply layout for task %r: task does not belong to the window.N)r\   r]   r^   r_   Ú
isinstanceÚstrr/   rT   r5   r2   r3   Zget_active_taskr&   r   rY   r'   r(   )r   Zwindow_layoutr5   r   r"   r"   r#   Úset_window_layout>  s&    

ÿÿ
zTaskWindow.set_window_layoutc                 C   sb   |j  ¡  || jkr8|jr$|j ¡  |jD ]}| ¡  q*|jD ]}| ¡  q>|j ¡  d|j _dS )z< Destroy all controls associated with a Task state.
        N)	r(   Zprepare_destroyr&   r@   r   rB   r>   r<   r7   )r   r   Ztool_bar_managerrC   r"   r"   r#   r   a  s    







zTaskWindow._destroy_statec                    sV   g }ˆ j rRˆ  ¡ }| ˆ j¡ dD ].}t||ƒ}|r"| ‡ fdd„| ¡ D ƒ¡ q"|S )zF Returns a list of visible panes ordered for focus switching.
        )ÚtopÚrightZbottomÚleftc                    s   g | ]}ˆ   |j¡‘qS r"   )rS   rT   )Ú.0Z	pane_itemrX   r"   r#   Ú
<listcomp>‚  s   ÿz-TaskWindow._get_pane_ring.<locals>.<listcomp>)r&   rW   r;   r<   ÚgetattrÚextendZ
iterleaves)r   rN   r5   ÚareaÚitemr"   rX   r#   rH   u  s    

þÿzTaskWindow._get_pane_ringc                 C   s.   | j D ]"}|j|ks |jj|kr|  S qdS )zn Returns the TaskState that contains the specified Task, or None if
            no such state exists.
        N)r   r(   rT   )r   Z
id_or_taskr   r"   r"   r#   r/   ‰  s    

zTaskWindow._get_statec                 C   s
   t | d�S )N)r7   r   rX   r"   r"   r#   Z__window_backend_default”  s    z#TaskWindow.__window_backend_defaultc                 C   s   | j s| jd u r| j S | jjS ©N)r   rZ   ÚnamerX   r"   r"   r#   Ú
_get_title™  s    zTaskWindow._get_titlec                 C   s
   || _ d S rn   )r   )r   Útitler"   r"   r#   Ú
_set_titlež  s    zTaskWindow._set_titler&   c                 C   sh   |j }|d u r4d  | _| _g | _d  | _| _g | _n0|j| _|j| _|j| _|j| _|j| _|j| _d S rn   )ÚnewrZ   r<   r>   r@   rA   rB   r(   )r   r+   r   r"   r"   r#   Ú%_update_traits_given_new_active_state£  s    z0TaskWindow._update_traits_given_new_active_statez2central_pane:has_focus, dock_panes:items:has_focusc                 C   s   |j r|j| _d S rn   )rs   ÚobjectrI   r*   r"   r"   r#   Ú_focus_updated³  s    zTaskWindow._focus_updatedz_states.itemsc                 C   s   dd„ | j D ƒ| _d S )Nc                 S   s   g | ]
}|j ‘qS r"   r6   )rh   r   r"   r"   r#   ri   º  ó    z.TaskWindow._states_updated.<locals>.<listcomp>)r   Útasksr*   r"   r"   r#   Ú_states_updated¸  s    zTaskWindow._states_updated)N)5Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   rq   r	   r   rI   r   rZ   r
   rx   r<   r   r>   r   r   r?   r&   r   r   r   r   r   r,   r.   r'   rD   rF   rO   rQ   rR   rS   rU   rV   rW   rY   r[   ra   rd   r   rH   r/   Z#_TaskWindow__window_backend_defaultrp   rr   r   rt   rv   ry   Ú__classcell__r"   r"   r    r#   r   *   sR   		&
	#

r   c                   @   sX   e Zd ZdZeeƒZeeƒZe	dƒZ
eeƒZeeƒZeeƒZeeƒZeeƒZdd„ ZdS )r9   zm An object used internally by TaskWindow to maintain the state associated
        with an attached Task.
    Fc                 C   s"   | j D ]}|j|kr|  S qdS )zg Returns the dock pane with the specified id, or None if no such dock
            pane exists.
        N)r>   rT   )r   rT   Zpaner"   r"   r#   rS   Ì  s    


zTaskState.get_dock_paneN)rz   r{   r|   r}   r	   r   r(   r   r5   r   r1   r   r<   r
   r   r>   r   r@   r   rA   r   rB   rS   r"   r"   r"   r#   r9   ½  s   r9   )#ÚloggingZpyface.action.apir   r   r   Z
pyface.apir   Z
traits.apir   r   r   r	   r
   r   r   r   r   Z/pyface.tasks.action.task_action_manager_builderr   Zpyface.tasks.i_dock_paner   Zpyface.tasks.i_task_paner   Zpyface.tasks.taskr   r   Z pyface.tasks.task_window_backendr   Zpyface.tasks.task_window_layoutr   Ú	getLoggerrz   r2   r   r9   r"   r"   r"   r#   Ú<module>   s   ,
   