3
7ßL]·'  ã               @   s²   d Z ddlmZmZmZmZ ddl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 dd
lmZ ejeƒZdd„ ZG dd„ deƒZdS )aÑ  
This module defines a :py:class:`GUIApplication` subclass of
:py:class:`pyface.application.Application`.  This adds cross-platform GUI
application support to the base class via :py:class:`pyface.application.GUI`.

At a minimum this class expects to be provided with a factory that returns
:py:class:`pyface.i_window.IWindow` instances.  For pure Pyface applications
this is most likely to be a subclass of
:py:class:`pyface.application_window.ApplicationWindow`.

é    )Úabsolute_importÚdivisionÚprint_functionÚunicode_literalsN)	ÚBoolÚCallableÚInstanceÚListÚReadOnlyÚTupleÚ	UndefinedÚVetoableÚon_trait_changeé   )ÚApplication)ÚIDialog)ÚISplashScreen)ÚIWindow)ÚImagec             K   s   ddl m} |f |ŽS )zª The default window factory returns an application window.

    This is almost never the right thing, but allows users to get off the
    ground with the base class.
    r   )ÚApplicationWindow)Úpyface.application_windowr   )ÚapplicationÚkwargsr   © r   ú:/tmp/pip-install-5sebvq2j/pyface/pyface/gui_application.pyÚdefault_window_factory(   s    r   c                   sô   e Zd ZdZeeƒZeeƒZe	Z
e	ZeeƒZed&ƒZeeƒZeeeƒƒZeZedƒZdd„ Zdd„ Zd	d
„ Z‡ fdd„Zdd„ Zdd„ Z‡ fdd„Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$e%dƒdd„ ƒZ&e%d ƒd!d"„ ƒZ'e%d#ƒd$d%„ ƒZ(‡  Z)S )'ÚGUIApplicationz! A basic Pyface GUI application. é   éX  Fc             K   sF   | j f d| i|—Ž}|jdkr&| j|_|js4| j|_| jrB| j|_|S )a¼   Create a new application window.

        By default uses the :py:attr:`window_factory` to do this.  Subclasses
        can override if they want to do something different or additional.

        Parameters
        ----------
        **kwargs : dict
            Additional keyword arguments to pass to the window factory.

        Returns
        -------
        window : IWindow instance or None
            The new IWindow instance.
        r   r   éÿÿÿÿr   )r   r   )Úwindow_factoryÚsizeÚwindow_sizeÚtitleÚnameÚicon)Úselfr   Úwindowr   r   r   Úcreate_windowc   s    
zGUIApplication.create_windowc             C   s$   | j j|ƒ |jƒ }|r |jƒ  dS )z2 Add a new window to the windows we are tracking. N)ÚwindowsÚappendÚopenÚactivate)r&   r'   Zopenedr   r   r   Ú
add_window~   s    zGUIApplication.add_windowc             C   s   | j dk	r| j jƒ  dS )z) Display the about dialog, if it exists. N)Úabout_dialogr+   )r&   r   r   r   Údo_aboutŒ   s    
zGUIApplication.do_aboutc                sB   ddl m} tt| ƒjƒ }|r>| jtkr6|| jd�| _| jƒ  |S )a   Start the application, setting up things that are required

        Subclasses should open at least one ApplicationWindow or subclass in
        their start method, and should call the superclass start() method
        before doing any work themselves.
        r   )ÚGUI)Úsplash_screen)	Z
pyface.guir0   Úsuperr   ÚstartÚguir   r1   Ú_create_windows)r&   r0   Úok)Ú	__class__r   r   r3   •   s    
zGUIApplication.startc             C   s   | j ƒ }| j|ƒ dS )z– Create the initial windows to display.

        By default calls :py:meth:`create_window` once. Subclasses can
        override this method.
        N)r(   r-   )r&   r'   r   r   r   r5   ­   s    zGUIApplication._create_windowsc             C   s   | j j| jdƒ | j jƒ  dS )z` Actual implementation of running the application: starting the GUI
        event loop.
        Zapplication_initializedT)r4   Zinvoke_laterZ_fire_application_eventZstart_event_loop)r&   r   r   r   Ú_runº   s    

zGUIApplication._runc                sD   t t| ƒjƒ sdS x,t| jƒD ]}tƒ  |_}|jrdS qW dS dS )zŸ Check with each window to see if it can be closed

        The fires closing events for each window, and returns False if any
        listener vetos.
        FTN)r2   r   Ú	_can_exitÚreversedr)   r   ÚclosingZveto)r&   r'   Úevent)r7   r   r   r9   Ì   s    zGUIApplication._can_exitc             C   s*   x$t t| jƒƒD ]}|jƒ  ||_qW dS )z Close each window N)Úlistr:   r)   ÚdestroyÚclosed)r&   r'   r   r   r   Ú_prepare_exitÜ   s    zGUIApplication._prepare_exitc             C   s   | j jƒ  dS )z Shut down the event loop N)r4   Zstop_event_loop)r&   r   r   r   Ú_exitã   s    zGUIApplication._exitc                s   ddl m‰  ‡ fdd„S )z™ Default to ApplicationWindow

        This is almost never the right thing, but allows users to get off the
        ground with the base class.
        r   )r   c                s
   ˆ f |ŽS )Nr   )r   r   )r   r   r   Ú<lambda>ð   s    z8GUIApplication._window_factory_default.<locals>.<lambda>)r   r   )r&   r   )r   r   Ú_window_factory_defaulté   s    z&GUIApplication._window_factory_defaultc             C   s$   ddl m} |ƒ }| jr | j|_|S )z Default SplashScreen r   )ÚSplashScreen)Zpyface.splash_screenrD   ÚlogoÚimage)r&   rD   Údialogr   r   r   Ú_splash_screen_defaultò   s
    z%GUIApplication._splash_screen_defaultc                s¨   ddl m} |j|jfdkr*ddlm‰  nddlm‰  ddlm} dj	ˆ | j
ƒƒdj	ˆ | jƒƒd	g}|‡ fd
d„| jjdƒD ƒ7 }|dj	| j
ƒ|d�}| jr¤| j|_|S )z Default AboutDialog r   )Úversion_infoé   é   )Úescape)ÚAboutDialogz<h1>{}</h1>z-Copyright &copy; 2018 {}, all rights reservedÚ c                s   g | ]}ˆ |ƒ‘qS r   r   )Ú.0Úline)rL   r   r   ú
<listcomp>  s    z8GUIApplication._about_dialog_default.<locals>.<listcomp>z

zAbout {})r#   Ú	additions)rJ   rK   )ÚsysrI   ÚmajorÚminorÚhtmlrL   ÚcgiZpyface.about_dialogrM   Úformatr$   ZcompanyÚdescriptionÚsplitrE   rF   )r&   rI   rM   rR   rG   r   )rL   r   Ú_about_dialog_defaultû   s     

z$GUIApplication._about_dialog_defaultzwindows:activatedc             C   s   || j kr|| _dS )z7 Listener that tracks currently active window.
        N)r)   Úactive_window)r&   r'   ÚtraitÚoldÚnewr   r   r   Ú_on_activate_window  s    
z"GUIApplication._on_activate_windowzwindows:deactivatedc             C   s
   d| _ dS )z7 Listener that tracks currently active window.
        N)r\   )r&   r'   r]   r^   r_   r   r   r   Ú_on_deactivate_window  s    z$GUIApplication._on_deactivate_windowzwindows:closedc             C   s   || j kr| j j|ƒ dS )zH Listener that ensures window handles are released when closed.
        N)r)   Úremove)r&   r'   r]   r^   r_   r   r   r   Ú_on_window_closed$  s    
z GUIApplication._on_window_closed)r   r   )*Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r1   r   r.   r   r%   rE   r   r   r    r   r"   r   r\   r	   r)   r
   r4   r   Z_explicit_exitr(   r-   r/   r3   r5   r8   r9   r@   rA   rC   rH   r[   r   r`   ra   rc   Ú__classcell__r   r   )r7   r   r   2   s4   			r   ) rg   Ú
__future__r   r   r   r   ÚloggingZ
traits.apir   r   r   r	   r
   r   r   r   r   r   r   Zi_dialogr   Zi_splash_screenr   Zi_windowr   Z	ui_traitsr   Ú	getLoggerrd   Úloggerr   r   r   r   r   r   Ú<module>   s   ,

