3
8Ad]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__)ÚselfÚtraits© r   úp/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.6/site-packages/traitsui-6.1.2-py3.6.egg/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bboxZroleÚmethodÚenabledr   Údefaultr   r    Z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)Z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   rA   ›   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   r5   Z	Key_EnterZ
Key_Returnr9   r3   Zdefault_buttonZ
Key_Escaper@   r   r.   ÚkeyPressEvent)r   r>   r   r   r   rD   ¦   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.   ÚsizeHintr9   r3   ÚwidthZsetWidthÚheightZ	setHeight)r   Úsizer3   r   r   r   rE   ³   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.   Údoner@   Úbool)r   Úrr   r   r   rI   ¾   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)r9   r3   Zclose_resultZisModalr   Úcloser   r:   )r   Zis_okZok_to_closer   r   r   r@   È   s    

z_StickyDialog._ok_to_close)N)r   r   r   r   r   r?   rA   rD   rE   rI   r@   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   r3   ZsetModalrM   ÚMODALZsetWindowTitleÚtitler
   Úfinishedr#   Z_on_finished)r   r;   rP   r   r3   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   r7   ZQWidgetr0   r2   r   r/   ZsetCentralWidgetr<   r1   Ú_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   rL     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)ÚownerrQ   r   Z_parentZ
prepare_uiZ	setParentr   Úresultr   Úpositionr   r   rM   ÚNONMODALÚshowZsetWindowModalityr   r5   ZApplicationModalÚexec_)r   r;   rP   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_resourcerd   rV   Zdefault_iconr   ZsetWindowIconZcreate_icon)r   Úiconrd   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   r3   ÚmenubarÚ_last_groupÚ_last_parentr   r<   Z
setMenuBarZcreate_menu_bar)r   rj   r   r   r   rW   @  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   r3   Útoolbarrk   rl   Zcreate_tool_barr   Z
setMovabler/   Z
addToolBar)r   rm   Z
qt_toolbarr   r   r   rX   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   r3   Z	statusbarr   Z
QStatusBarZsetSizeGripEnabledr4   r   ZQLabelÚsetTextZget_extended_valueÚabsrF   ÚintZsetMinimumWidthr1   ÚfindÚcontextÚ_set_status_textZon_trait_changeÚappendr   r/   ZsetStatusBarZ
_statusbar)r   r   Z	listenersÚitemr   Zitem_controlrF   ZstretchÚcolÚobjZset_textr   r   r   rY   ]  s2    



zBaseDialog._add_statusbarc                s   ‡ fdd„}|S )z- Helper function for _add_statusbar.
        c                s   ˆ j | ƒ d S )N)rr   )Útext)r   r   r   Úset_status_text„  s    z4BaseDialog._set_status_text.<locals>.set_status_textr   )r   r   r}   r   )r   r   rw   �  s    zBaseDialog._set_status_text)T)N)r   r   r   r   ÚlistÚranger`   rR   ZPOPUPrQ   rU   r[   rL   Ústaticmethodrc   rf   ri   rW   rX   rY   rw   r   r   r   r   rM   Þ   s   


	$rM   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-   rM   r   r   r   r   Ú<module>   s   9j