a
    §ˆc6  ã                   @   s¾   d Z ddlmZmZ ddlmZ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.
é    )ÚQtCoreÚQtGui)ÚActionManagerBuilderÚMenuBarSchemaÚToolBarSchema)Ú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i |¤Ž d S ©N)r   Ú__init__)ÚselfZtraits© r   úW/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/ui_base.pyr   2   s    zButtonEditor.__init__c                 C   s&   | j j}| j  |j| j j| jd¡ dS )z,Handles the associated button being clicked.N)ÚuiÚhandlerZdo_undoableÚperformÚinfoÚaction)r   r   r   r   r   r   6   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¡s dS |du r.|j}|j}	| ||¡}
|
 d¡ |
 |¡ |
 |¡ |du sx|j	dksx|	dkrÚt
|||
d�}|	dkrœ|j |	|¡ |jdkr´| |j|¡ |j	dkrÌ| |j	|¡ |du rÚ|j}|durî|
j |¡ |jdk�r|
 |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?   s6    

ÿ




zBasePanel.add_buttonc                 C   s   |j }| j |¡ dS )z<Handles a change to the "undoable" state of the undo historyN)ÚnewZundor#   ©r   ÚeventÚstater   r   r   Ú_on_undoablej   s    zBasePanel._on_undoablec                 C   s   |j }| j |¡ dS )z=Handles a change to the "redoable" state of the undo history.N)r,   Zredor#   r-   r   r   r   Ú_on_redoableo   s    zBasePanel._on_redoablec                 C   s   |j }| j |¡ dS )z'Handles a change to the "revert" state.N)r,   Úrevertr#   r-   r   r   r   Ú_on_revertablet   s    zBasePanel._on_revertable)NTNF)r   r   r   r   r+   r0   r1   r3   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 | |¡ t  ¡ | _t  ¡ }| dddd¡ | | j¡ |  |¡ t	j
jj}|jjsv|t	j
jjO }| t jjj¡ z6|t	j
jjO }|jjrª|tt	j
jjt	j
jjB ƒO }W n ty¾   Y n0 |  |¡ || _d| _dS )zInitialise the dialog.r   N)r   ÚQDialogr   ZQMainWindowÚ_mwZQVBoxLayoutÚsetContentsMarginsÚ	addWidgetÚ	setLayoutr   ÚQtZ
WindowTypeZWindowÚviewÚ	resizableZWindowSystemMenuHintZsetSizeConstraintÚQLayoutZSizeConstraintZSetFixedSizeZWindowCloseButtonHintÚintZWindowMinimizeButtonHintZWindowMaximizeButtonHintÚAttributeErrorZsetWindowFlagsÚ_uiÚ_result)r   r   ÚparentÚlayoutÚflagsr   r   r   r   }   s.    


ÿÿ

z_StickyDialog.__init__c                 C   s   |   ¡  d S r   )Zraise_©r   Úer   r   r   Ú	showEvent§   s    z_StickyDialog.showEventc                 C   s$   |   ¡ rtj | |¡ n| ¡  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   r5   Ú
closeEventÚignorerE   r   r   r   rI   ª   s    z_StickyDialog.closeEventc                 C   sZ   |  ¡ tjjjtjjjfv r*| jjjs*dS |  ¡ tjjj	krH|  
¡ sHdS t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   r:   ZKeyZ	Key_EnterZ
Key_Returnr@   r;   Zdefault_buttonZ
Key_EscaperH   r   r5   ÚkeyPressEventrE   r   r   r   rL   µ   s    ÿþz_StickyDialog.keyPressEventc                 C   sL   t j | ¡}| jj}|jdkr.| t|jƒ¡ |jdkrH| 	t|jƒ¡ |S )zAReimplemented to provide an appropriate size hint for the window.r   )
r   r5   ÚsizeHintr@   r;   ÚwidthZsetWidthr>   ÚheightZ	setHeight)r   Úsizer;   r   r   r   rM   Ä   s    

z_StickyDialog.sizeHintc                 C   s<   | j durtj | | j ¡ n|  t|ƒ¡r8tj | |¡ dS )zMReimplemented to ignore calls to accept() or reject() if
        appropriate.N)rA   r   r5   ÚdonerH   Úbool)r   Úrr   r   r   rQ   Î   s    
z_StickyDialog.doneNc                 C   sF   |du r$| j jj}|du r$|  ¡  }| j j | j j|¡}|rB|| _|S )z6Let the handler decide if the dialog should be closed.N)r@   r;   Zclose_resultZisModalr   Úcloser   rA   )r   Zis_okZok_to_closer   r   r   rH   Ø   s    

z_StickyDialog._ok_to_close)N)r   r   r   r   r   rG   rI   rL   rM   rQ   rH   r   r   r   r   r4   z   s   *

r4   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   rB   Ústyler   r   r   Úinitô   s    zBaseDialog.initc                 C   sL   t | j|ƒ | _}| jj}| |tjk¡ | |jp6t	¡ |j
 | j¡ dS )zCreate the dialog control.N)r4   r   r    r;   ZsetModalrU   ÚMODALZsetWindowTitleÚtitler   Úfinishedr%   Z_on_finished)r   rB   rX   r    r;   r   r   r   Úcreate_dialogù   s
    zBaseDialog.create_dialogc                 C   s|   t |tjƒr2t ¡ }| dddd¡ | |¡ |}|durH| jj |¡ |dur`| j 	¡  
|¡ |  ¡  |  ¡  |  ¡  dS )zYAdd a panel (either a widget, layout or None) and optional buttons
        to the dialog.r   N)Ú
isinstancer   r=   ZQWidgetr7   r9   r    r6   ZsetCentralWidgetrC   r8   Ú_add_menubarÚ_add_toolbarÚ_add_statusbar)r   ZpanelZbuttonsÚwr   r   r   Úadd_contents  s    
zBaseDialog.add_contentsTc                 C   s   | j  |¡ d | _ | _dS )z/Close the dialog and set the given return code.N)r   Zdisposer    )r   Úrcr   r   r   rT     s    zBaseDialog.closec                 C   sÐ   | j  | ||¡ | j j| _|| j_z|  ¡  W n: tyh   | j d¡ d| j_d| _d| _ d| _‚ Y n0 | j	 
| j¡ t| ƒ | jdurš| j ¡  d| _|tjkr°| j ¡  n| j tjjj¡ | j ¡  dS )zDisplay the UI.NF)ÚownerrY   r    Ú_parentZ
prepare_uiÚBaseExceptionÚ	setParentr   Úresultr   Úpositionr   r   Z_focus_controlZsetFocusrU   ÚNONMODALÚshowZsetWindowModalityr   r:   ZWindowModalityZApplicationModalÚexec_)r   rB   rX   r   r   r   Ú
display_ui"  s*    



zBaseDialog.display_uiNc                 C   s2   ddl m} t||ƒs|  ¡ }| j | ¡ ¡ dS )zSets the dialog's icon.r   )ÚImageResourceN)Zpyface.image_resourcero   r^   Zdefault_iconr    ZsetWindowIconZcreate_icon)r   Ziconro   r   r   r   Úset_iconB  s    
zBaseDialog.set_iconc                 C   s   |j }| j |dk¡ dS )zHandles editing errors.r   N)r,   Úokr#   )r   r.   Úerrorsr   r   r   Ú	_on_errorL  s    zBaseDialog._on_errorc                 C   sf   | j jj}t|tƒr(| j jj}| |¡}|durbd | _| _| j	 
¡  | | j	| ¡¡ d | _| _dS )zAdds a menu bar to the dialog.N)r   r;   Úmenubarr^   r   Úaction_manager_builderÚcreate_action_managerÚ_last_groupÚ_last_parentr    rC   Z
setMenuBarZcreate_menu_bar)r   rt   Úbuilderr   r   r   r_   Q  s    




ÿzBaseDialog._add_menubarc                 C   sr   | j jj}t|tƒr(| j jj}| |¡}|durnd | _| _| 	| j
| ¡}| d¡ | j
j |¡ d | _| _dS )zAdds a toolbar to the dialog.NF)r   r;   Útoolbarr^   r   ru   rv   rw   rx   Zcreate_tool_barr    Z
setMovabler6   Z
addToolBar)r   rz   ry   Z
qt_toolbarr   r   r   r`   ^  s    




zBaseDialog._add_toolbarc                 C   s  | j jjdu�rt ¡ }| | j jj¡ g }| j jjD ]Ê}|j}t ¡ }| 	| j  
|¡¡ t|jƒ}d}|dkr~td| ƒ}n| t|ƒ¡ | ||¡ | d¡}d}	|dkrÊ|d|… }	||d d… }| j j|	 }	|  |¡}
|	j|
|dd	� | |	|
|f¡ q6| jj |¡ || j _dS )
zAdds a statusbar to the dialog.Nr   g      ð?éd   Ú.Úobjectr   r   )Údispatch)r   r;   Z	statusbarr   Z
QStatusBarZsetSizeGripEnabledr<   r!   ZQLabelÚsetTextZget_extended_valueÚabsrN   r>   ZsetMinimumWidthr8   ÚfindÚcontextÚ_set_status_textZobserveÚappendr    r6   ZsetStatusBarZ
_statusbar)r   r    Z	listenersÚitemr!   Zitem_controlrN   ZstretchÚcolÚobjZset_textr   r   r   ra   k  s2    


zBaseDialog._add_statusbarc                    s   ‡ fdd„}|S )z#Helper function for _add_statusbar.c                    s   | j }ˆ  |¡ d S r   )r,   r   )r.   Útext©r    r   r   Úset_status_text‘  s    z4BaseDialog._set_status_text.<locals>.set_status_textr   )r   r    rŠ   r   r‰   r   rƒ   Ž  s    zBaseDialog._set_status_text)T)N)r   r   r   r   ÚlistÚrangerk   rZ   ZPOPUPrY   r]   rc   rT   Ústaticmethodrn   rp   rs   r_   r`   ra   rƒ   r   r   r   r   rU   î   s   



#rU   N)r   Z	pyface.qtr   r   Zpyface.action.schema.apir   r   r   Z
traits.apir   r   Ztraitsui.base_panelr	   Z
_BasePanelZtraitsui.menur
   Ú	constantsr   r*   r   Úhelperr   r   r   r5   r4   rU   r   r   r   r   Ú<module>   s   >t