3
6ßL]õH  ã               @   sš   d dl Z d dlZd dlmZ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mZ e jeƒZG dd„ deƒZG dd	„ d	eƒZdS )
é    N)ÚApplicationÚExtensionPoint)
ÚBoolÚCallableÚ	DirectoryÚEventÚHasStrictTraitsÚInstanceÚIntÚListÚUnicodeÚVetoable)Ú	ETSConfig)ÚpickleÚSTRING_BASE_CLASSc               @   sl  e Zd ZdZdZdZedƒZedƒZeddd�Z	e
Zed	ƒZeZeed
�Zeed
�ZeedƒƒZeZeedƒƒZedƒZeZeZeedƒƒZeedƒƒZeedƒƒZeedƒƒZeedƒƒZ edƒZ!edƒZ"dd„ Z#dd„ Z$d7dd„Z%d8d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,„ Z0d-d.„ Z1d/d0„ Z2d1d2„ Z3d3d4„ Z4d5d6„ Z5dS )9ÚTasksApplicationzÄThe entry point for an Envisage Tasks application.

    This class handles the common case for Tasks applications and is
    intended to be subclassed to modify its start/stop behavior, etc.

    zenvisage.ui.tasks.tasksz!envisage.ui.tasks.task_extensionsz(envisage.ui.tasks.task_window.TaskWindowzpyface.gui.GUIz#pyface.image_resource.ImageResourceT)Ú
allow_nonez!pyface.splash_screen.SplashScreen)Úidz0pyface.tasks.task_window_layout.TaskWindowLayoutFz3envisage.ui.tasks.task_window_event.TaskWindowEventz;envisage.ui.tasks.task_window_event.VetoableTaskWindowEventz9envisage.ui.tasks.tasks_application.TasksApplicationStatec             C   s4   | j }| jƒ }|r0| jƒ  |j| d| ƒ |jƒ  |S )zŽ Run the application.

        Returns:
        --------
        Whether the application started successfully (i.e., without a veto).
        Úapplication_initialized)ÚguiÚstartÚ_create_windowsZset_trait_laterZstart_event_loop)Úselfr   Ústarted© r   úI/tmp/pip-install-5sebvq2j/envisage/envisage/ui/tasks/tasks_application.pyÚrun~   s    	zTasksApplication.runc                s@   | j ˆ ƒ}|dkrdS ‡ fdd„| jD ƒ}|j|ƒ}|j|_|S )zš Creates the Task with the specified ID.

        Returns:
        --------
        The new Task, or None if there is not a suitable TaskFactory.
        Nc                s"   g | ]}|j ˆ ks|j  r|‘qS r   )Útask_id)Ú.0Úext)r   r   r   ú
<listcomp>¥   s    z0TasksApplication.create_task.<locals>.<listcomp>)Ú_get_task_factoryÚtask_extensionsZcreate_with_extensionsr   )r   r   ÚfactoryÚ
extensionsÚtaskr   )r   r   Úcreate_task˜   s    

zTasksApplication.create_taskNc       	      K   sÞ   ddl m} ddlm} | jf d| i|—Ž}|j| jdƒ |j| jdƒ |j| jdƒ |j| j	d	ƒ |j| j
d
ƒ ||d�| _|rÊx6|jƒ D ]*}| j|ƒ}|rª|j|ƒ qŒtjd|ƒ qŒW |rÐ| j|ƒ}n|ƒ }|j|ƒ |S )a?  Creates a new TaskWindow, possibly with some Tasks.

        Parameters:
        -----------
        layout : TaskWindowLayout, optional
             The layout to use for the window. The tasks described in
             the layout will be created and added to the window
             automatically. If not specified, the window will contain
             no tasks.

        restore : bool, optional (default True)
             If set, the application will restore old size and
             positions for the window and its panes, if possible. If a
             layout is not provided, this parameter has no effect.

        **traits : dict, optional
             Additional parameters to pass to ``window_factory()``
             when creating the TaskWindow.

        Returns:
        --------
        The new TaskWindow.

        é   )ÚTaskWindowEventr   )ÚTaskWindowLayoutZapplicationZ	activatedZopeningZopenedÚclosingÚclosed)Úwindowz#Missing factory for task with ID %r)Útask_window_eventr(   Úpyface.tasks.task_window_layoutr)   Úwindow_factoryZon_trait_changeÚ_on_window_activatedÚ_on_window_openingÚ_on_window_openedÚ_on_window_closingÚ_on_window_closedÚwindow_createdÚ	get_tasksr&   Zadd_taskÚloggerÚerrorÚ_restore_layout_from_stateZset_window_layout)	r   ÚlayoutÚrestoreZtraitsr(   r)   r,   r   r%   r   r   r   Úcreate_window«   s*    

zTasksApplication.create_windowc             C   sp   d| _ z\|s6x(t| jƒD ]}tƒ  |_}|jrdS qW | jƒ  x t| jƒD ]}|jƒ  d|_qJW W dd| _ X dS )aÄ  Exits the application, closing all open task windows.

        Each window is sent a veto-able closing event. If any window vetoes the
        close request, no window will be closed. Otherwise, all windows will be
        closed and the GUI event loop will terminate.

        This method is not called when the user clicks the close
        button on a window or otherwise closes a window through his or
        her window manager. It is only called via the File->Exit menu
        item. It can also, of course, be called programatically.

        Parameters:
        -----------
        force : bool, optional (default False)
            If set, windows will receive no closing events and will be
            destroyed unconditionally. This can be useful for reliably
            tearing down regression tests, but should be used with
            caution.

        Returns:
        --------
        A boolean indicating whether the application exited.

        TFN)	Ú_explicit_exitÚreversedÚwindowsr   r*   ÚvetoÚ_prepare_exitÚdestroyr+   )r   Úforcer,   Úeventr   r   r   Úexitè   s    zTasksApplication.exitc             C   sª   | j ƒ  | js| jj r | j}nL| jj}xB|D ]:}x2|jƒ D ]$}| j|ƒs<tjd| ƒ | j}P q<W q.P q.W x8|D ]0}| jrŒ| j	|dd�}n| j	|dd�}|j
ƒ  qrW dS )zk Called at startup to create TaskWindows from the default or saved
            application layout.
        zeSaved application layout references non-existent task %r. Falling back to default application layout.F)r;   TN)Ú_load_stateÚalways_use_default_layoutÚ_stateÚprevious_window_layoutsÚdefault_layoutr6   r!   r7   Úwarningr<   Úopen)r   Úwindow_layoutsr:   r   Úwindow_layoutr,   r   r   r   r     s&    



z TasksApplication._create_windowsc             C   s"   x| j D ]}|j|kr|S qW dS )zA Returns the TaskFactory with the specified ID, or None.
        N)Útask_factoriesr   )r   r   r#   r   r   r   r!   6  s    
z"TasksApplication._get_task_factoryc             C   s   | | _ | jƒ  dS )zv Called immediately before the extant windows are destroyed and the
            GUI event loop is terminated.
        N)Úapplication_exitingÚ_save_state)r   r   r   r   rA   >  s    zTasksApplication._prepare_exitc             C   s†   t ƒ }tjj| jdƒ}tjj|ƒr|y@t|dƒ�}tj|ƒ}W dQ R X |j	|j	krV|}n
t
jdƒ W n   t
jd|ƒ Y nX || _dS )z5 Loads saved application state, if possible.
        Úapplication_mementoÚrbNz&Discarding outdated application layoutz$Restoring application layout from %s)ÚTasksApplicationStateÚosÚpathÚjoinÚstate_locationÚexistsrL   r   ÚloadÚversionr7   ÚwarnÚ	exceptionrH   )r   ÚstateÚfilenameÚfZrestored_stater   r   r   rF   E  s    zTasksApplication._load_statec             C   sr   | j j|ƒ}|r |jƒ |_|}nN|jƒ }xDt|jƒD ]6\}}t|tƒrJ|n|j	}| j j
|ƒ}|r4||j|< q4W |S )zE Restores an equivalent layout from saved application state.
        )rH   Úget_equivalent_window_layoutZget_active_taskZactive_taskZclone_traitsÚ	enumerateÚitemsÚ
isinstancer   r   Úget_task_layout)r   r:   ÚmatchÚiÚitemr   r   r   r   r9   Y  s    
z+TasksApplication._restore_layout_from_statec             C   sl   dd„ | j D ƒ}|| j_tjj| jdƒ}y(t|dƒ�}tj	| j|ƒ W dQ R X W n   t
jdƒ Y nX dS )z& Saves the application state.
        c             S   s   g | ]}|j ƒ ‘qS r   )Úget_window_layout)r   Úwr   r   r   r    t  s    z0TasksApplication._save_state.<locals>.<listcomp>rR   ÚwbNzSaving application layout)r?   rH   rI   rU   rV   rW   rX   rL   r   Údumpr7   r]   )r   rM   r_   r`   r   r   r   rQ   p  s    zTasksApplication._save_statec             C   s   ddl m} |S )Nr   )Ú
TaskWindow)Zenvisage.ui.tasks.task_windowrm   )r   rm   r   r   r   Ú_window_factory_default‚  s    z(TasksApplication._window_factory_defaultc             C   s.   ddl m} |ƒ }| jr(| jd jg|_|gS )Nr   )r)   )r.   r)   rO   r   rc   )r   r)   rN   r   r   r   Ú_default_layout_default†  s
    z(TasksApplication._default_layout_defaultc             C   s   ddl m} || jd�S )Nr   )ÚGUI)Úsplash_screen)Z
pyface.guirp   rq   )r   rp   r   r   r   Ú_gui_default�  s    zTasksApplication._gui_defaultc             C   s:   t jjtjdtjƒ}t jj|ƒs*t j|ƒ tj	d|ƒ |S )NZtaskszTasks state location is %s)
rU   rV   rW   r   Zapplication_homeZtoolkitrY   Úmakedirsr7   Údebug)r   rX   r   r   r   Ú_state_location_default‘  s    


z(TasksApplication._state_location_defaultc             C   s
   || _ d S )N)Úactive_window)r   r,   Ú
trait_namerD   r   r   r   r0   �  s    z%TasksApplication._on_window_activatedc             C   s,   ddl m} ||d� | _}|jr(d|_d S )Nr'   )ÚVetoableTaskWindowEvent)r,   T)r-   rx   Úwindow_openingr@   )r   r,   rw   rD   rx   Úwindow_eventr   r   r   r1      s
    z#TasksApplication._on_window_openingc             C   s(   ddl m} | jj|ƒ ||d�| _d S )Nr'   )r(   )r,   )r-   r(   r?   ÚappendÚwindow_opened)r   r,   rw   rD   r(   r   r   r   r2   ©  s    z"TasksApplication._on_window_openedc             C   s`   ddl m} ||d� | _}|jr*d|_n2|jƒ }| jj|ƒ t| jƒdkr\| j	 r\| j
ƒ  d S )Nr'   )rx   )r,   T)r-   rx   Úwindow_closingr@   ri   rH   Úpush_window_layoutÚlenr?   r=   rA   )r   r,   rw   rD   rx   rz   rN   r   r   r   r3   °  s    z#TasksApplication._on_window_closingc             C   s>   ddl m} | jj|ƒ ||d�| _t| jƒdkr:| jƒ  d S )Nr'   )r(   )r,   r   )r-   r(   r?   ÚremoveÚwindow_closedr   Ústop)r   r,   rw   rD   r(   r   r   r   r4   Â  s
    z"TasksApplication._on_window_closed)NT)F)6Ú__name__Ú
__module__Ú__qualname__Ú__doc__ZTASK_FACTORIESZTASK_EXTENSIONSr	   rv   r   Ziconr   Únamerq   r   rX   r   rO   r"   r   r?   r   r/   rJ   r   rG   r   r   rP   r5   ry   r|   r}   r�   r=   rH   r   r&   r<   rE   r   r!   rA   rF   r9   rQ   rn   ro   rr   ru   r0   r1   r2   r3   r4   r   r   r   r   r      sh   






=
-!	r   c               @   sH   e Zd ZdZeedƒƒZeedƒƒZedƒZ	dd„ Z
dd„ Zdd	„ Zd
S )rT   ze A class used internally by TasksApplication for saving and restoring
        application state.
    z0pyface.tasks.task_window_layout.TaskWindowLayoutr'   c             C   s"   x| j D ]}|j|ƒr|S qW dS )z? Gets an equivalent TaskWindowLayout, if there is one.
        N)rM   Úis_equivalent_to)r   rN   r:   r   r   r   ra   â  s    
z2TasksApplicationState.get_equivalent_window_layoutc             C   s2   x,| j D ]"}x|jD ]}|j|kr|S qW qW dS )z@ Gets a TaskLayout with the specified ID, there is one.
        N)rM   rc   r   )r   r   rN   r:   r   r   r   re   ê  s
    
z%TasksApplicationState.get_task_layoutc                s(   ‡ fdd„| j D ƒ| _ | j jdˆ ƒ dS )z= Merge a TaskWindowLayout into the accumulated list.
        c                s   g | ]}|j ˆ ƒs|‘qS r   )rˆ   )r   r:   )rN   r   r   r    ö  s    z<TasksApplicationState.push_window_layout.<locals>.<listcomp>r   N)rM   Úinsert)r   rN   r   )rN   r   r~   ó  s    z(TasksApplicationState.push_window_layoutN)rƒ   r„   r…   r†   r   r	   rI   rM   r
   r[   ra   re   r~   r   r   r   r   rT   Î  s   

	rT   )ÚloggingÚos.pathrU   Zenvisage.apir   r   Z
traits.apir   r   r   r   r   r	   r
   r   r   r   Ztraits.etsconfig.apir   Zenvisage._compatr   r   Ú	getLoggerrƒ   r7   r   rT   r   r   r   r   Ú<module>   s   0
   =