3
<Ad]2@  ã               @   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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 d dlZe j e!ƒZ"G dd„ deƒZ#G dd„ deƒZ$dS )é    N)ÚMenuBarManagerÚStatusBarManagerÚToolBarManager)ÚApplicationWindow)
ÚBoolÚCallableÚ	HasTraitsÚHasStrictTraitsÚInstanceÚListÚPropertyÚUnicodeÚVetoableÚon_trait_change)ÚTaskActionManagerBuilder)Ú	IDockPane)Ú	ITaskPane)ÚTaskÚ
TaskLayout)ÚTaskWindowBackend)ÚTaskWindowLayoutc                   sZ  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/d5d6„ Z0e1d7ƒd8d9„ ƒZ2e1d:ƒ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)Z
depends_onz"pyface.tasks.task_window.TaskStatec                s6   | j jƒ  x| jD ]}| j|ƒ qW tt| ƒjƒ  dS )zI Overridden to ensure that all task panes are cleanly destroyed.
        N)Ú_window_backendÚdestroyÚ_statesÚ_destroy_stateÚsuperr   )ÚselfÚstate)Ú	__class__© úr/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/pyface-6.1.2-py3.6.egg/pyface/tasks/task_window.pyr   G   s    
zTaskWindow.destroyc             C   sh   t ƒ  | _}|jsV| jdkr$| jƒ  | jdkrF| jrF| j| jd jƒ | j	dƒ | | _
| jdk	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ÚshowÚopened)r   Úeventr!   r!   r"   ÚopenX   s    

zTaskWindow.openc             C   s   | j j|ƒS )z, Delegate to the TaskWindowBackend.
        )r   Zcreate_contents)r   Úparentr!   r!   r"   Ú_create_contentsr   s    zTaskWindow._create_contentsc             C   sv   | j |ƒ}|r`|| jkr`| jdk	r0| jj| jƒ |jsD|jƒ  d|_| jj|ƒ || _|jƒ  n|srtj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Úwarn)r   r'   r   r!   r!   r"   r&   {   s    


zTaskWindow.activate_taskc             C   sè   |j dk	rtjd| ƒ dS | |_ t||jd�}| jj|ƒ | jdkrN| jƒ  |j	ƒ |_
||j
_|j
j| jƒ |jƒ |_x |jD ]}|jj||d�ƒ q€W x |jD ]}||_|j| jƒ q¢W | j|d�}|jƒ |_|j|_|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'   )Úwindowr1   Ú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–   s.    




zTaskWindow.add_taskc             C   sT   | j |ƒ}|rB| j|kr*| jj|ƒ d| _| j|ƒ | jj|ƒ ntj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   r/   r   r   Úremover1   r2   )r   r'   r   r!   r!   r"   Úremove_task¼   s    


zTaskWindow.remove_taskc             C   s@   | j r<| jƒ }d}| jr0|j| jƒd t|ƒ }|| jƒ  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   ÚpanesrG   r!   r!   r"   Úfocus_next_paneÐ   s    zTaskWindow.focus_next_panec             C   s8   | j r4| jƒ }d}| jr(|j| jƒd }|| jƒ  dS )zu Shifts focus to the "previous" pane, taking into account the active
            pane and the pane geometry.
        rD   Néÿÿÿÿ)r%   rE   rF   rG   rI   )r   rJ   rG   r!   r!   r"   Úfocus_previous_paneÛ   s    zTaskWindow.focus_previous_panec             C   s   | j |ƒ}|r|jS dS )z: Returns the central pane for the specified task.
        N)r.   r9   )r   r'   r   r!   r!   r"   Úget_central_paneæ   s    
zTaskWindow.get_central_paneNc             C   s,   |dkr| j }n
| j|ƒ}|r(|j|ƒ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"   rO   ì   s    
zTaskWindow.get_dock_panec             C   s    | j |ƒ}|r|jdd… S g S )z8 Returns the dock panes for the specified task.
        N)r.   r;   )r   r'   r   r!   r!   r"   Úget_dock_panes÷   s    
zTaskWindow.get_dock_panesc             C   s   | j |ƒ}|r|jS dS )z] Returns the task with the specified ID, or None if no such task
            exists.
        N)r.   r'   )r   rP   r   r!   r!   r"   Úget_taský   s    
zTaskWindow.get_taskc             C   s   | j r| j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"   rS     s    
zTaskWindow.get_layoutc             C   s   | j r| jj|ƒ dS )zh Applies a TaskLayout (which should be suitable for the active task)
            to the window.
        N)r%   r   Ú
set_layout)r   r3   r!   r!   r"   rT     s    zTaskWindow.set_layoutc             C   s   | j r| j| j jƒ dS )z8 Restores the active task's default TaskLayout.
        N)Úactive_taskrT   r7   )r   r!   r!   r"   Úreset_layout  s    zTaskWindow.reset_layoutc             C   sh   t | j| j| jd�}xN| jD ]D}|| jkr@|jj|_| j	j
ƒ }n
|jjƒ }|jj|_|jj|ƒ qW |S )zI Returns a TaskWindowLayout for the current state of the window.
        )ÚpositionÚsizeÚ
size_state)r   rW   rX   rY   r   r%   r'   rP   rU   r   rS   r3   Zclone_traitsÚitemsr8   )r   Úresultr   r3   r!   r!   r"   Úget_window_layout  s    



zTaskWindow.get_window_layoutc             C   sš   |j | _ |j| _|j| _xD|jD ]:}t|tjƒr2q | j|jƒ}|rJ||_	q t
jd|j ƒ q W | j|jƒ ƒ}|r–|| jkrŠ| jj|j	ƒ n| j|jƒ dS )z3 Applies a TaskWindowLayout to the window.
        zDCannot apply layout for task %r: task does not belong to the window.N)rW   rX   rY   rZ   Ú
isinstanceÚsixÚstring_typesr.   rP   r3   r1   r2   Zget_active_taskr%   r   rT   r&   r'   )r   Zwindow_layoutr3   r   r!   r!   r"   Úset_window_layout+  s"    
zTaskWindow.set_window_layoutc             C   sj   |j jƒ  || jkr<|jr$|jjƒ  x|jD ]}|jƒ  q,W x|jD ]}|jƒ  qDW |jjƒ  d|j _dS )z< Destroy all controls associated with a Task state.
        N)	r'   Zprepare_destroyr%   r=   r   r?   r;   r9   r4   )r   r   Ztool_bar_managerr@   r!   r!   r"   r   N  s    



zTaskWindow._destroy_statec                sZ   g }ˆ j rVˆ jƒ }|jˆ jƒ x6dD ].}t||ƒ}|r$|j‡ fdd„|jƒ D ƒƒ q$W |S )zF Returns a list of visible panes ordered for focus switching.
        ÚtopÚrightÚbottomÚleftc                s   g | ]}ˆ j |jƒ‘qS r!   )rO   rP   )Ú.0Z	pane_item)r   r!   r"   ú
<listcomp>o  s   z-TaskWindow._get_pane_ring.<locals>.<listcomp>)ra   rb   rc   rd   )r%   rS   r8   r9   ÚgetattrÚextendZ
iterleaves)r   rJ   r3   ZareaÚitemr!   )r   r"   rE   b  s    


zTaskWindow._get_pane_ringc             C   s.   x(| j D ]}|j|ks"|jj|kr|S qW dS )zn Returns the TaskState that contains the specified Task, or None if
            no such state exists.
        N)r   r'   rP   )r   Z
id_or_taskr   r!   r!   r"   r.   t  s    zTaskWindow._get_statec             C   s
   t | d�S )N)r4   )r   )r   r!   r!   r"   Z__window_backend_default  s    z#TaskWindow.__window_backend_defaultc             C   s   | j s| jd kr| j S | jjS )N)r   rU   Úname)r   r!   r!   r"   Ú
_get_title„  s    zTaskWindow._get_titlec             C   s
   || _ d S )N)r   )r   Útitler!   r!   r"   Ú
_set_title‰  s    zTaskWindow._set_titlec             C   sb   |d kr.d  | _ | _g | _d  | _| _g | _n0|j| _ |j| _|j| _|j| _|j| _|j| _d S )N)rU   r9   r;   r=   r>   r?   r'   )r   r   r!   r!   r"   Z__active_state_changedŽ  s    z!TaskWindow.__active_state_changedz,central_pane.has_focus, dock_panes.has_focusc             C   s   |dkr|r|| _ d S )NZ	has_focus)rF   )r   Úobjrj   ÚoldÚnewr!   r!   r"   Ú_focus_updatedœ  s    zTaskWindow._focus_updatedz	_states[]c             C   s   dd„ | j D ƒ| _d S )Nc             S   s   g | ]
}|j ‘qS r!   )r'   )re   r   r!   r!   r"   rf   £  s    z.TaskWindow._states_updated.<locals>.<listcomp>)r   Útasks)r   r!   r!   r"   Ú_states_updated¡  s    zTaskWindow._states_updated)N)5Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   rl   r
   r   rF   r   rU   r   rr   r9   r   r;   r   r   r<   r%   r   r   r   r   r   r+   r-   r&   rA   rC   rK   rM   rN   rO   rQ   rR   rS   rT   rV   r\   r`   r   rE   r.   Z#_TaskWindow__window_backend_defaultrk   rm   Z!_TaskWindow__active_state_changedr   rq   rs   Ú__classcell__r!   r!   )r    r"   r      sL   	&
	#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 )r6   zm An object used internally by TaskWindow to maintain the state associated
        with an attached Task.
    Fc             C   s"   x| j D ]}|j|kr|S qW dS )zg Returns the dock pane with the specified id, or None if no such dock
            pane exists.
        N)r;   rP   )r   rP   Zpaner!   r!   r"   rO   µ  s    
zTaskState.get_dock_paneN)rt   ru   rv   rw   r
   r   r'   r   r3   r   r0   r   r9   r   r   r;   r   r=   r   r>   r   r?   rO   r!   r!   r!   r"   r6   ¦  s   r6   )%ÚloggingZpyface.action.apir   r   r   Z
pyface.apir   Z
traits.apir   r   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   r^   Ú	getLoggerrt   r1   r   r6   r!   r!   r!   r"   Ú<module>   s    0
   