a
    §ˆcCD  ã                   @   s¸   d 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 ddl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 d	dlmZmZmZ G dd„ deƒZdS )zA Traits UI simple, scrubber-based integer or float value editor.
é    N)Úlog10Úpow)ÚAnyÚ	BaseRangeÚBaseEnumÚStrÚFloatÚ
TraitErrorÚobserve)ÚViewÚItemÚ
EnumEditor)ÚScrubberEditor)ÚEditor)Údo_afteré   )Ú
ErrorColor)Úpaint_parent)Ú
disconnectÚdisconnect_no_idÚBufferDCc                       sd  e Zd ZdZeƒ Zeƒ Zeƒ Ze	ƒ Z
eƒ ZejejejdœZdd„ Z‡ fdd„Zdd„ Zd	d
„ Zdd„ Zdd„ ZedƒdId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&„ Z"d'd(„ Z#d)d*„ Z$d+d,„ Z%d-d.„ Z&d/d0„ Z'd1d2„ Z(d3d4„ Z)d5d6„ Z*d7d8„ Z+d9d:„ Z,d;d<„ Z-d=d>„ Z.d?d@„ Z/dAdB„ Z0dCdD„ Z1dJdEdF„Z2dGdH„ Z3‡  Z4S )KÚ_ScrubberEditorz?Traits UI simple, scrubber-based integer or float value editor.)ÚleftÚcenterÚrightc           	      C   s¶  | j }d }}|j|j }}||kr¬d }}| j | j¡j}t|tƒrr|j	|j
 }}|dkrb|j}|dkr¬|j}n:t|tƒr¬|jdkr�|j| _n|  |jdd¡ d| j }}tj|dt dd¡tjtjB d	� | _}| tj| j¡ | tj| j¡ | tj| j¡ | tj| j¡ | tj | j!¡ | tj"| j#¡ | tj$| j%¡ | tj&| j'¡ | tj(| j)¡ | tj*| j+¡ |  ,¡  | _-| j.||d
� |  |dd¡ |  |dd¡ |  /¡  dS )z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        Ú NÚmappingÚfromr   éÿÿÿÿé2   é   ©ÚsizeÚstyle)ÚlowÚhighr$   r%   )0Úfactoryr$   r%   ÚobjectZtraitÚnameÚhandlerÚ
isinstancer   Z	_low_nameZ
_high_nameZ_lowZ_highr   Úvaluesr   Z
sync_valueÚwxZWindowÚSizeZFULL_REPAINT_ON_RESIZEZTAB_TRAVERSALÚcontrolÚBindÚEVT_ERASE_BACKGROUNDÚ_erase_backgroundÚ	EVT_PAINTÚ	_on_paintÚEVT_SET_FOCUSÚ
_set_focusÚEVT_LEAVE_WINDOWÚ_leave_windowÚEVT_ENTER_WINDOWÚ_enter_windowÚEVT_LEFT_DOWNÚ
_left_downÚEVT_LEFT_UPÚ_left_upÚ
EVT_MOTIONÚ_motionÚEVT_MOUSEWHEELÚ_mouse_wheelÚEVT_SIZEÚ_resizeZset_tooltipÚ_can_set_tooltipZ	trait_setÚ_reset_scrubber)	ÚselfÚparentr&   Zlow_nameZ	high_namer$   r%   r)   r.   © rH   ú^/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/wx/scrubber_editor.pyÚinitI   sL    





üz_ScrubberEditor.initc                    sH   t | jtjtjtjtjtjtjtj	tj
tjtjƒ |  ¡  tƒ  ¡  dS )z&Disposes of the contents of an editor.N)r   r.   r,   r0   r2   r4   r6   r8   r:   r<   r>   r@   rB   Ú_disconnect_textÚsuperÚdispose©rF   ©Ú	__class__rH   rI   rM   Š   s    õz_ScrubberEditor.disposec                 C   s(   |   | j¡| _d| _|  ¡  |  ¡  dS )z[Updates the editor when the object trait changes externally to the
        editor.
        N)Zstring_valueÚvalueÚtextÚ
_text_sizeÚ_refreshÚ_enum_completedrN   rH   rH   rI   Úupdate_editor    s    z_ScrubberEditor.update_editorc                 C   sl   | j dur| j t|ƒ }|| jkrhz|| _|  ¡  W n2 tyf   t|ƒ}|| jkrb|| _|  ¡  Y n0 dS )z3Updates the object when the scrubber value changes.N)r   ÚintrQ   rV   r	   )rF   rQ   rH   rH   rI   Úupdate_objectª   s    


z_ScrubberEditor.update_objectc                 C   s   dS )zDHandles an error that occurs while setting the object's trait value.NrH   )rF   ZexcprH   rH   rI   Úerror¹   s    z_ScrubberEditor.errorc                 C   s   t |ƒd | _dS )z'Handles the Enum mapping being changed.r   N)Úlenr%   )rF   r   rH   rH   rI   Ú_mapping_changed¿   s    z _ScrubberEditor._mapping_changedz	low, highNc                 C   sä   | j | j }}| jrˆ| jdur@dd | j¡ }t|ƒdkr|d}n<|du r^d}|dur|d| }n|du rpd| }nd||f }| j |¡ | jj	}|d	krÒ|du s²|du s²t
|tƒr¸d
}ntdtt|| d ƒƒƒ}|| _	|  ¡  dS )zSets the the current tooltip.Nz[%s]z, éP   r   z[%g..]z[..%g]z[%g..%g]g        g      ð?é
   g      Y@)r$   r%   rD   r   ÚjoinrZ   r.   Z
SetToolTipr&   Ú	incrementr*   rW   r   Úroundr   rV   )rF   Úeventr$   r%   Ztooltipr_   rH   rH   rI   rE   Å   s*    


z_ScrubberEditor._reset_scrubberc           
      C   sr   |   ¡ \}}}}| j ¡ \}}|||  d d }| jj}|dkrHd}	n|dkr^|| d }	n|| }	|	|||fS )zUGet the window bounds of where the current text should be
        displayed.
        é   r   r   r   r   )Ú_get_text_sizer.   ÚGetClientSizer&   Ú	alignment)
rF   ÚtdxÚtdyZdescentÚleadingÚwdxÚwdyÚtyre   ÚtxrH   rH   rI   Ú_get_text_boundså   s    z _ScrubberEditor._get_text_boundsc                 C   s(   | j du r"| j | j ¡ pd¡| _ | j S )z1Returns the text size information for the window.NÚM)rS   r.   ZGetFullTextExtentrR   ÚstriprN   rH   rH   rI   rc   ö   s
    
ÿz_ScrubberEditor._get_text_sizec                 C   s   | j dur| j  ¡  dS )z&Refreshes the contents of the control.N)r.   ÚRefreshrN   rH   rH   rI   rT   ÿ   s    
z_ScrubberEditor._refreshc                 C   s|   d}| j }| ¡ r |d9 }d}n| ¡ r0|d }| j|| |  }| jdurXt|| jƒ}| jdurnt|| jƒ}|  |¡ dS )zWCalculates a new scrubber value for a specified mouse position
        change.
        é   g      $@é   N)	r_   Ú	ShiftDownZControlDownÚ_valuer$   Úmaxr%   ÚminrX   )rF   ra   ÚdeltaÚclicksr_   rQ   rH   rH   rI   Ú_set_scrubber_position  s    

z&_ScrubberEditor._set_scrubber_positionc                 C   s
   d| _ dS )z Handle a delayed click response.FN)Ú_pendingrN   rH   rH   rI   Ú_delayed_click  s    z_ScrubberEditor._delayed_clickc                 C   s4   | j  t tj¡¡ | jdur(|  ¡  n|  ¡  dS )z]Pop-up a text control to allow the user to enter a value using
        the keyboard.
        N)r.   Ú	SetCursorr,   ÚCursorZCURSOR_ARROWr   Ú_pop_up_enumÚ_pop_up_textrN   rH   rH   rI   Ú_pop_up_editor  s    

z_ScrubberEditor._pop_up_editorc              
   C   s‚   | j jtt| jdddtdd�d�dd�| j| j | d	œd
�| _| j ¡ \}}| jj	j
j}| dd||¡ | ¡  | tj| j¡ d S )NÚ	drop_downFéüÿÿÿzeditor.mapping)r(   )ÚidZ
show_labelÚpaddingÚeditorZsubpanel)Úkind)r'   r…   )ÚviewrG   Úcontextr   )r'   Zedit_traitsr   r   r(   r   r.   Ú_uiÚGetSizeÚinfor�   ÚSetSizeÚSetFocusr/   r,   ÚEVT_KILL_FOCUSrU   )rF   ÚdxÚdyr�   rH   rH   rI   r~   )  s&    ûø

ôz_ScrubberEditor._pop_up_enumc                 C   sš   | j }tj|dt| jƒ| ¡ | j| jj tj	B d� | _
}| dd¡ | ¡  |jtj| j| ¡ d� | tj| j¡ | tj| j¡ | tj| j¡ d S )Nr   r!   )rƒ   )r.   r,   ZTextCtrlÚstrrQ   rŠ   Útext_stylesr&   re   ZTE_PROCESS_ENTERÚ_textZSetSelectionr�   r/   ÚEVT_TEXT_ENTERÚ_text_completedZGetIdr8   Ú_enter_textr6   Ú_leave_textÚEVT_CHARÚ_key_entered)rF   r.   rR   rH   rH   rI   r   ?  s     ÿûz_ScrubberEditor._pop_up_textc                 C   s$   | j | _|  ¡  | j ¡  d| _dS )z"Destroys the current text control.N)Ú_in_text_windowÚ_ignore_focusrK   r.   ZDestroyChildrenr“   rN   rH   rH   rI   Ú_destroy_textP  s    
z_ScrubberEditor._destroy_textc                 C   s6   | j dur2t| j tjƒ t| j tjtjtjtjƒ dS )z:Disconnects the event handlers for the pop up text editor.N)	r“   r   r,   r”   r   rŽ   r8   r6   r˜   rN   rH   rH   rI   rK   Z  s    
ûz _ScrubberEditor._disconnect_textc                 C   sD   | j dur8zt| j ƒ | j¡| _W q@   d| _Y q@0 n| j| _dS )zdInitializes the current value when the user begins a drag or moves
        the mouse wheel.
        Nr   )r   ÚlistÚindexrQ   rt   rN   rH   rH   rI   Ú_init_valuef  s    
z_ScrubberEditor._init_valuec                 C   s   dS )zCDo not erase the background here (do it in the 'on_paint' handler).NrH   ©rF   ra   rH   rH   rI   r1   t  s    z!_ScrubberEditor._erase_backgroundc                 C   s&  | j }t|ƒ}| j}|j}| jdur6|jdurL|j}n| jrL|jdurL|j}|du rft||ƒ t	j
}n
t	 |¡}|j}|durŠt	 |¡}nt	j}|t	jks¤|t	j
krÔ| ¡ \}}	| |¡ | |¡ | dd||	¡ | t	j¡ | |j¡ | | ¡ ¡ |  ¡ \}
}}}| | j|
|¡ | ¡  dS )z<Paint the background using the associated ImageSlice object.Nr   )r.   r   r&   Úcolor_Ú_xÚactive_color_Ú_hoverÚhover_color_r   r,   ZTRANSPARENT_BRUSHZBrushZborder_color_ZPenZTRANSPARENT_PENrd   ZSetBrushZSetPenZDrawRectangleZSetBackgroundModeZTRANSPARENTZSetTextForegroundZtext_color_ZSetFontZGetFontrm   ZDrawTextrR   Úcopy)rF   ra   r.   Údcr&   ÚcolorZbrushZpenri   rj   rl   rk   rf   rg   rH   rH   rI   r3   x  s:    






z_ScrubberEditor._on_paintc                 C   s    | j dur| j  | j ¡ ¡ dS )z"Handles the control being resized.N)r“   rŒ   r.   rŠ   r    rH   rH   rI   rC   £  s    
z_ScrubberEditor._resizec                 C   s.   | j s"| jdu r"| jdu r"|  ¡  | ¡  dS )z.Handle the control getting the keyboard focus.N)r›   r¢   r“   r€   ÚSkipr    rH   rH   rI   r5   ¨  s    ÿþýz_ScrubberEditor._set_focusc                 C   sz   d| _ | j t tj¡¡ | js0d| _| j ¡  d| _| jdur\| j	j
| j	jkrv| j ¡  n| j	j| j	jkrv| j ¡  dS )z&Handles the mouse entering the window.TFN)r¤   r.   r|   r,   r}   ZCURSOR_HANDr›   r�   r¢   r&   r£   r¡   rp   r¥   r    rH   rH   rI   r9   ²  s    

z_ScrubberEditor._enter_windowc                 C   s$   d| _ | jj| jjkr | j ¡  dS )z%Handles the mouse leaving the window.FN)r¤   r&   r¥   r¡   r.   rp   r    rH   rH   rI   r7   Ã  s    z_ScrubberEditor._leave_windowc                 C   sX   |  ¡ | ¡  | _| _d| _|  ¡  | j ¡  | jj	| jj
krH| j ¡  td| jƒ dS )z%Handles the left mouse being pressed.TéÈ   N)ÚGetXÚGetYr¢   Ú_yrz   rŸ   r.   ZCaptureMouser&   r£   r¥   rp   r   r{   r    rH   rH   rI   r;   Ê  s    

z_ScrubberEditor._left_downc                 C   sT   | j  ¡  | jr|  ¡  d | _ | _ | _| _| jsF| jj	| jj
krP| j  ¡  dS )z-Handles the left mouse button being released.N)r.   ZReleaseMouserz   r€   r¢   r­   rt   r¤   r&   r£   r¡   rp   r    rH   rH   rI   r=   Ø  s    
z_ScrubberEditor._left_upc           	      C   s€   | j dur|| ¡ | ¡  }}|| j  }t|ƒ}|| j }t|ƒ}| jr\|| dk rVdS d| _||krj|}n| }|  ||¡ dS )zHandles the mouse moving.Nrq   F)r¢   r«   r¬   Úabsr­   rz   ry   )	rF   ra   ÚxÚyr�   Zadxr�   Zadyrw   rH   rH   rI   r?   ã  s    


z_ScrubberEditor._motionc                 C   sB   | j r>|  ¡  d}| ¡ rd}|| ¡ | ¡   }|  ||¡ dS )zHandles the mouse wheel moving.rq   rr   N)r¤   rŸ   rs   ZGetWheelRotationZGetWheelDeltary   )rF   ra   rx   rw   rH   rH   rI   rA   ÷  s    ÿz_ScrubberEditor._mouse_wheelc                 C   sL   |  ¡ }z|  t| ¡ ƒ¡ W dS  tyF   | t¡ | ¡  Y dS 0 dS )z=Updates the object value from the current text control value.TFN)ZGetEventObjectrX   ÚfloatZGetValuer	   ZSetBackgroundColourr   rp   )rF   ra   r.   rH   rH   rI   Ú_update_value  s    
z_ScrubberEditor._update_valuec                 C   s
   d| _ dS )z3Handles the mouse entering the pop-up text control.TN©rš   r    rH   rH   rI   r–     s    z_ScrubberEditor._enter_textc                 C   s
   d| _ dS )z2Handles the mouse leaving the pop-up text control.FNr³   r    rH   rH   rI   r—     s    z_ScrubberEditor._leave_textc                 C   s*   t |tjƒr| ¡  |  |¡r&|  ¡  dS )z>Handles the user pressing the 'Enter' key in the text control.N)r*   r,   Z
FocusEventr©   r²   rœ   r    rH   rH   rI   r•     s    
z_ScrubberEditor._text_completedc                 C   s6   | j dur2d| _t| j jjjtjƒ | j  ¡  | ` dS )z0Handles the Enum drop-down control losing focus.NT)	r‰   r›   r   r‹   r�   r.   r,   rŽ   rM   r    rH   rH   rI   rU      s    
ÿ
z_ScrubberEditor._enum_completedc                 C   sb   |  ¡ }|tjkr|  ¡  dS |tjkrV|  |¡rR| ¡ rH| j d¡ n
| j ¡  dS | 	¡  dS )z@Handles individual key strokes while the text control is active.Nr   )
Z
GetKeyCoder,   Z
WXK_ESCAPErœ   ZWXK_TABr²   rs   r.   ZNavigater©   )rF   ra   Zkey_coderH   rH   rI   r™   *  s    



z_ScrubberEditor._key_entered)N)N)5Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r$   r%   r   r_   r   rR   r   r,   ZTE_LEFTZ	TE_CENTREZTE_RIGHTr’   rJ   rM   rV   rX   rY   r[   r
   rE   rm   rc   rT   ry   r{   r€   r~   r   rœ   rK   rŸ   r1   r3   rC   r5   r9   r7   r;   r=   r?   rA   r²   r–   r—   r•   rU   r™   Ú__classcell__rH   rH   rO   rI   r   /   sZ   ýA
	
+


r   ) r·   r,   Úmathr   r   Z
traits.apir   r   r   r   r   r	   r
   Ztraitsui.apir   r   r   Z traitsui.editors.scrubber_editorr   Ztraitsui.wx.editorr   Zpyface.timer.apir   Ú	constantsr   Zimage_slicer   Úhelperr   r   r   r   rH   rH   rH   rI   Ú<module>   s   $
