3
8ßL]z3  ã               @   s¶   d Z ddlmZ ddlmZmZ ddl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mZ G dd„ deƒZG dd„ deƒZ
G dd„ dejƒZG dd„ de
ƒZdS )zTDefines the base class for the PyQt-based Traits UI modal and non-modal
   dialogs.
é    )Úabsolute_import)ÚQtCoreÚQtGui)ÚHasPrivateTraitsÚInstance)Ú	BasePanel)ÚActioné   )ÚDefaultTitle)ÚEditor)Úrestore_windowÚsave_windowc               @   s(   e Zd ZdZeeƒZdd„ Zdd„ ZdS )ÚButtonEditorz Editor for buttons.
    c             K   s   t j| f|Ž d S )N)r   Ú__init__)ÚselfZtraits© r   ú:/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/ui_base.pyr   ,   s    zButtonEditor.__init__c             C   s&   | j j}| j j|j| j j| jdƒ dS )z6 Handles the associated button being clicked.
        N)ÚuiÚhandlerZdo_undoableÚperformÚinfoÚaction)r   r   r   r   r   r   4   s    zButtonEditor.performN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r   r      s   r   c               @   s2   e Zd ZdZddd„Zdd„ Zd	d
„ Zdd„ ZdS )r   z%Base class for Traits UI panels.
    NTFc             C   s  | j }|jdkr"|j|jƒ r"dS |dkr0|j}|j}	|j||ƒ}
|
jdƒ |
j|ƒ |
j|ƒ |dksz|j	dksz|	dkrÜt
|||
d�}|	dkrž|jj|	|ƒ |jdkr¶|j|j|ƒ |j	dkrÎ|j|j	|ƒ |dkrÜ|j}|dk	rð|
jj|ƒ |jdk�r|
j|jƒ |
S )z Creates a button.
        Ú NF)r   r   Úcontrol)r   Zdefined_whenZ	eval_whenÚnameÚidZ	addButtonZsetAutoDefaultZ
setDefaultÚ
setEnabledZenabled_whenr   r   ÚbindZvisible_whenZadd_visibleZadd_enabledr   ZclickedÚconnectZtooltipZ
setToolTip)r   r   ZbboxÚroleÚmethodZenabledr   Údefaultr   r   ÚbuttonÚeditorr   r   r   Ú
add_button?   s8    





zBasePanel.add_buttonc             C   s   | j j|ƒ dS )zEHandles a change to the "undoable" state of the undo history
        N)Zundor    )r   Ústater   r   r   Ú_on_undoabled   s    zBasePanel._on_undoablec             C   s   | j j|ƒ dS )zFHandles a change to the "redoable" state of the undo history.
        N)Zredor    )r   r)   r   r   r   Ú_on_redoablei   s    zBasePanel._on_redoablec             C   s   | j j|ƒ dS )z1 Handles a change to the "revert" state.
        N)Úrevertr    )r   r)   r   r   r   Ú_on_revertablen   s    zBasePanel._on_revertable)NTNF)r   r   r   r   r(   r*   r+   r-   r   r   r   r   r   ;   s    
$r   c               @   sJ   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d„Z
dS )Ú_StickyDialogz?A QDialog that will only close if the traits handler allows it.c             C   sî   t jj| |ƒ t jƒ | _t jƒ }|jddddƒ |j| jƒ | j|ƒ |j	j
rXtjj}n:tjjtjjB }|jt jjƒ |j	j
r’|tjjtjjB O }y,|tjjO }|j	j
r¼|tjjtjjB O }W n tk
rÒ   Y nX | j|ƒ || _d| _dS )zInitialise the dialog.r   N)r   ÚQDialogr   ZQMainWindowÚ_mwZQVBoxLayoutÚsetContentsMarginsÚ	addWidgetÚ	setLayoutÚviewÚ	resizabler   ÚQtZWindowÚDialogZWindowSystemMenuHintZsetSizeConstraintÚQLayoutZSetFixedSizeZWindowMinimizeButtonHintZWindowMaximizeButtonHintZWindowCloseButtonHintÚAttributeErrorZsetWindowFlagsÚ_uiÚ_result)r   r   ÚparentÚlayoutÚflagsr   r   r   r   w   s,    



z_StickyDialog.__init__c             C   s   | j ƒ  d S )N)Zraise_)r   Úer   r   r   Ú	showEvent˜   s    z_StickyDialog.showEventc             C   s$   | j ƒ rtjj| |ƒ n|jƒ  dS )z©Reimplemented to check when the clicks the window close button.
        (Note that QDialog doesn't get a close event when the dialog is closed
        in any other way.)N)Ú_ok_to_closer   r/   Ú
closeEventÚignore)r   r?   r   r   r   rB   ›   s    z_StickyDialog.closeEventc             C   sX   |j ƒ tjjtjjfkr(| jjj r(dS |j ƒ tjjkrF| j	ƒ  rFdS t
jj| |ƒ dS )zˆReimplemented to ignore the Escape key if appropriate, and to ignore
        the Enter key if no default button has been explicitly set.N)Úkeyr   r6   Z	Key_EnterZ
Key_Returnr:   r4   Zdefault_buttonZ
Key_EscaperA   r   r/   ÚkeyPressEvent)r   r?   r   r   r   rE   ¦   s    z_StickyDialog.keyPressEventc             C   sD   t jj| ƒ}| jj}|jdkr*|j|jƒ |jdkr@|j|jƒ |S )zJReimplemented to provide an appropriate size hint for the window.
        r   )	r   r/   ÚsizeHintr:   r4   ÚwidthZsetWidthÚheightZ	setHeight)r   Úsizer4   r   r   r   rF   ³   s    

z_StickyDialog.sizeHintc             C   s<   | j dk	rtjj| | j ƒ n| jt|ƒƒr8tjj| |ƒ dS )zMReimplemented to ignore calls to accept() or reject() if
        appropriate.N)r;   r   r/   ÚdonerA   Úbool)r   Úrr   r   r   rJ   ¾   s    
z_StickyDialog.doneNc             C   sF   |dkr$| j jj}|dkr$| jƒ  }| j jj| j j|ƒ}|rB|| _|S )z6Let the handler decide if the dialog should be closed.N)r:   r4   Zclose_resultZisModalr   Úcloser   r;   )r   Zis_okZok_to_closer   r   r   rA   È   s    

z_StickyDialog._ok_to_close)N)r   r   r   r   r   r@   rB   rE   rF   rJ   rA   r   r   r   r   r.   t   s   !
r.   c               @   s‚   e Zd ZdZeedƒƒ\ZZZdd„ Z	dd„ Z
dd„ Zdd
d„Zedd„ ƒZddd„Zdd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdS )Ú
BaseDialogz&Base class for Traits UI dialog boxes.é   c             C   s   t ‚dS )z/Initialise the dialog by creating the controls.N)ÚNotImplementedError)r   r   r<   Ústyler   r   r   Úinitä   s    zBaseDialog.initc             C   sL   t | j|ƒ | _}| jj}|j|tjkƒ |j|jp6t	ƒ |j
j| jƒ dS )zCreate the dialog control.N)r.   r   r   r4   ZsetModalrN   ÚMODALZsetWindowTitleÚtitler
   Úfinishedr"   Z_on_finished)r   r<   rQ   r   r4   r   r   r   Úcreate_dialogé   s
    zBaseDialog.create_dialogc             C   s|   t |tjƒr2tjƒ }|jddddƒ |j|ƒ |}|dk	rH| jjj|ƒ |dk	r`| jj	ƒ j
|ƒ | jƒ  | jƒ  | jƒ  dS )zYAdd a panel (either a widget, layout or None) and optional buttons
        to the dialog.r   N)Ú
isinstancer   r8   ZQWidgetr1   r3   r   r0   ZsetCentralWidgetr=   r2   Ú_add_menubarÚ_add_toolbarÚ_add_statusbar)r   ZpanelZbuttonsÚwr   r   r   Úadd_contentsõ   s    
zBaseDialog.add_contentsTc             C   s   | j j|ƒ d | _ | _dS )z/Close the dialog and set the given return code.N)r   Zdisposer   )r   Úrcr   r   r   rM     s    zBaseDialog.closec          	   C   s®   | j j| ||ƒ | j j| _|| j_y| jƒ  W n4   | jjdƒ d| j_d| _d| _ d| _‚ Y nX | jj	| j
ƒ t| ƒ |tjkr�| jjƒ  n| jjtjjƒ | jjƒ  dS )zDisplay the UI.NF)ÚownerrR   r   Ú_parentZ
prepare_uiÚ	setParentr   Úresultr   Úpositionr   r   rN   ÚNONMODALÚshowZsetWindowModalityr   r6   ZApplicationModalÚexec_)r   r<   rQ   r   r   r   Ú
display_ui  s$    

zBaseDialog.display_uiNc             C   s2   ddl m} t||ƒs| jƒ }| jj|jƒ ƒ dS )zSets the dialog's icon.r   )ÚImageResourceN)Zpyface.image_resourcerg   rW   Zdefault_iconr   ZsetWindowIconZcreate_icon)r   Ziconrg   r   r   r   Úset_icon-  s    
zBaseDialog.set_iconc             C   s   | j j|dkƒ dS )zHandles editing errors.r   N)Úokr    )r   Úerrorsr   r   r   Ú	_on_error7  s    zBaseDialog._on_errorc             C   sH   | j jj}|dk	rDd | _| _| jjƒ j|j| j| ƒƒ d | _| _dS )z'Adds a menu bar to the dialog.
        N)	r   r4   ÚmenubarÚ_last_groupÚ_last_parentr   r=   Z
setMenuBarZcreate_menu_bar)r   rl   r   r   r   rX   @  s    

zBaseDialog._add_menubarc             C   sT   | j jj}|dk	rPd | _| _|j| j| ƒ}|jdƒ | jjj	|ƒ d | _| _dS )z' Adds a toolbar to the dialog.
        NF)
r   r4   Útoolbarrm   rn   Zcreate_tool_barr   Z
setMovabler0   Z
addToolBar)r   ro   Z
qt_toolbarr   r   r   rY   N  s    

zBaseDialog._add_toolbarc             C   s  | j jjdk	�rtjƒ }|j| j jjƒ g }xÔ| j jjD ]Æ}|j}tjƒ }|j	| j j
|ƒƒ t|jƒ}d}|dkr€td| ƒ}n
|j|ƒ |j||ƒ |jdƒ}d}	|dkrÈ|d|… }	||d d… }| j j|	 }	| j|ƒ}
|	j|
|dd	� |j|	|
|fƒ q8W | jjj|ƒ || j _dS )
z) Adds a statusbar to the dialog.
        Nr   g      ð?éd   Ú.Úobjectr	   r   )Údispatch)r   r4   Z	statusbarr   Z
QStatusBarZsetSizeGripEnabledr5   r   ZQLabelÚsetTextZget_extended_valueÚabsrG   ÚintZsetMinimumWidthr2   ÚfindÚcontextÚ_set_status_textZon_trait_changeÚappendr   r0   ZsetStatusBarZ
_statusbar)r   r   Z	listenersÚitemr   Zitem_controlrG   ZstretchÚcolÚobjZset_textr   r   r   rZ   ]  s2    



zBaseDialog._add_statusbarc                s   ‡ fdd„}|S )z- Helper function for _add_statusbar.
        c                s   ˆ j | ƒ d S )N)rt   )Útext)r   r   r   Úset_status_text„  s    z4BaseDialog._set_status_text.<locals>.set_status_textr   )r   r   r   r   )r   r   ry   �  s    zBaseDialog._set_status_text)T)N)r   r   r   r   ÚlistÚrangerc   rS   ZPOPUPrR   rV   r\   rM   Ústaticmethodrf   rh   rk   rX   rY   rZ   ry   r   r   r   r   rN   Þ   s   


	$rN   N)r   Ú
__future__r   Z	pyface.qtr   r   Z
traits.apir   r   Ztraitsui.base_panelr   Z
_BasePanelZtraitsui.menur   Ú	constantsr
   r'   r   Úhelperr   r   r   r/   r.   rN   r   r   r   r   Ú<module>   s   9j