a
    §ˆcÙ)  ã                   @   sÀ   d Z ddlmZmZ ddlmZ ddl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mZ dd	lmZ dd
lmZ dZdZdZG dd„ deƒZe Z ZZG dd„ deƒZ dS )z€ Defines a source code editor and code editor factory, for the PyQt user
interface toolkit, useful for tools such as debuggers.
é    )ÚQtCoreÚQtGui)ÚAdvancedCodeWidget)ÚStrÚListÚIntÚEventÚBoolÚ
TraitErrorÚobserve)ÚSequenceTypes)ÚKeyPressedEventé   )ÚOKColorÚ
ErrorColor)ÚEditor)Úpixmap_cacheé   c                       s  e Zd ZdZeeƒZdZedƒZ	e
ƒ Ze
ƒ Ze
ƒ Zeƒ Zee
ƒZeƒ Zeƒ Zee
ƒZeƒ Ze
dƒZee
ƒZeƒ Zeƒ Zdd„ Z‡ f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#d$„ Z)d%d&„ Z*e+d'ƒd(d)„ ƒZ,‡  Z-S )*ÚSourceEditorz;Editor for source code which uses the advanced code widget.TFé   c                 C   sÈ  t  ¡ | _t  | j¡}| dddd¡ td| jjd� | _}| 	|¡ | j}| jj
}| jrf| d¡ n$|jr||j | j¡ n|j | j¡ |jdkr¢|j | j¡ |jdks¶|jdkrÄ|j | j¡ |j |j¡ |  ¡  | j|jdddd� |  |jd	d¡ |  |jd
d¡ |  |jd¡ |  |jd¡ |  |j dd¡ |  |j!dd¡ | j|j"dddd� | jj#dk�rtd| _#n|  |j#dd¡ | j|j$dddd� |j%dk�r¬d| _%n|  |j%dd¡ |  &¡  dS )z\Finishes initializing the editor by creating the underlying toolkit
        widget.
        r   N)ÚlexerTÚ Ú
mark_linesÚfrom)Zis_listÚselected_lineÚselected_textÚtoÚlineÚcolumnÚselected_start_posÚselected_end_posÚ	dim_linesZgreyÚ	dim_colorÚsquiggle_linesZredÚsquiggle_color)'r   ZQWidgetÚcontrolZQVBoxLayoutZsetContentsMarginsr   Úfactoryr   Ú_widgetZ	addWidgetÚcodeÚreadonlyZsetReadOnlyÚauto_setZtextChangedÚconnectÚupdate_objectÚ
focus_lostr   ZselectionChangedÚ_selection_changedr   r   ZcursorPositionChangedÚ_position_changedZline_number_widgetZ
setVisibleZshow_line_numbersÚupdate_editorZ
sync_valuer   r   r   r    r!   r"   r#   r$   Zset_tooltip)ÚselfÚparentZlayoutr%   r&   Zcode_editor© r3   ú[/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/traitsui/qt4/code_editor.pyÚinitt   sL    
ÿ


ÿzSourceEditor.initc                    s(   | j js| jjj | j¡ tƒ  ¡  dS )z&Disposes of the contents of an editor.N)	r&   r*   r'   r(   r-   Z
disconnectr,   ÚsuperÚdispose©r1   ©Ú	__class__r3   r4   r7   ³   s    zSourceEditor.disposec              
   C   s^   | j sZz.t| jj ¡ ƒ}t| jtƒr,| ¡ }|| _W n$ t	yX } zW Y d}~n
d}~0 0 dS )z9Handles the user entering input data in the edit control.N)
Ú_lockedÚstrr'   r(   ÚtoPlainTextÚ
isinstanceÚvaluer   Úsplitr
   )r1   r?   Úexcpr3   r3   r4   r,   ¼   s    
zSourceEditor.update_objectc                 C   s\   d| _ | j}t|tƒr*d dd„ |D ƒ¡}| j}|j ¡ |krR|j |¡ | j	j
rRd| _ dS )z[Updates the editor when the object trait changes externally to the
        editor.
        TÚ
c                 S   s   g | ]}|  ¡ ‘qS r3   )Úrstrip)Ú.0r   r3   r3   r4   Ú
<listcomp>Î   ó    z.SourceEditor.update_editor.<locals>.<listcomp>FN)r;   r?   r>   r   Újoinr'   r(   r=   ZsetPlainTextr&   r   )r1   Ú	new_valuer%   r3   r3   r4   r0   Ç   s    
zSourceEditor.update_editorc                 C   s   dS )zDHandles an error that occurs while setting the object's trait value.Nr3   )r1   rA   r3   r3   r4   ÚerrorÛ   s    zSourceEditor.errorc                 C   s0   | j jdur,| d¡}|dur,| j j |¡ dS )z[Restores any saved user preference information associated with the
        editor.
        NÚkey_bindings)r&   rJ   ÚgetÚmerge)r1   ZprefsrJ   r3   r3   r4   Úrestore_prefsá   s    
zSourceEditor.restore_prefsc                 C   s   d| j jiS )zCReturns any user preference information associated with the editor.rJ   )r&   rJ   r8   r3   r3   r4   Ú
save_prefsê   s    zSourceEditor.save_prefsc                 C   s   dS )z.Handles the set of marked lines being changed.Nr3   r8   r3   r3   r4   Ú_mark_lines_changedî   s    z SourceEditor._mark_lines_changedc                 C   s   |   ¡  d S ©N)rO   r8   r3   r3   r4   Ú_mark_lines_items_changedó   s    z&SourceEditor._mark_lines_items_changedc                 C   sX   t | jj ¡  ¡ ƒ| _| jj ¡  ¡ }| jj ¡  ¡ }||krH|| }}|| _|| _	d S rP   )
r<   r'   r(   Z
textCursorZselectedTextr   ZselectionStartZselectionEndr   r    )r1   ÚstartÚendr3   r3   r4   r.   ö   s    
zSourceEditor._selection_changedc                 C   sH   | j }tdt| ¡ | jƒƒ}| ¡ \}}| ||¡ | jjrD| 	¡  dS )z5Handles a change in which line is currently selected.r   N)
r'   ÚmaxÚminÚlinesr   Úget_line_columnÚset_line_columnr&   Úauto_scrollÚcenterCursor)r1   r%   r   Ú_r   r3   r3   r4   Ú_selected_line_changed  s    z#SourceEditor._selected_line_changedc                 C   s8   | j s4| j ¡ \}}| j ||¡ | jjr4| j ¡  d S rP   )r;   r'   rW   rX   r&   rY   rZ   )r1   r   r[   r   r3   r3   r4   Ú_line_changed
  s
    zSourceEditor._line_changedc                 C   s&   | j s"| j ¡ \}}| j ||¡ d S rP   )r;   r'   rW   rX   )r1   r   r   r[   r3   r3   r4   Ú_column_changed  s    zSourceEditor._column_changedc                 C   sB   | j }d| _| ¡ \| _| _d| _| ¡ | _| jjr>| j  	¡  dS )z*Handles the cursor position being changed.TFN)
r'   r;   rW   r   r   Zget_selected_textr   r&   rY   rZ   )r1   r%   r3   r3   r4   r/     s    
zSourceEditor._position_changedc                 C   sN   | j j}|r&| |j| jj| jj¡}nd}|sJ|j tj	j
j¡rJ| j ¡  dS )z.Handles a key being pressed within the editor.FN)r&   rJ   ZdoÚeventZuiÚhandlerÚinfoÚmatchesr   ZQKeySequenceZStandardKeyZFindZ_find_widgetÚshow)r1   r_   rJ   Ú	processedr3   r3   r4   Ú_key_pressed_changed   s    ÿz!SourceEditor._key_pressed_changedc                 C   s   d S rP   r3   r8   r3   r3   r4   Ú_dim_color_changed,  s    zSourceEditor._dim_color_changedc                 C   s   d S rP   r3   r8   r3   r3   r4   Ú_squiggle_color_changed/  s    z$SourceEditor._squiggle_color_changedzdim_lines, squiggle_linesc                 C   s   | j  | j¡ d S rP   )r'   Zset_warn_linesr#   )r1   r_   r3   r3   r4   Ú_style_document2  s    zSourceEditor._style_document).Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   Zkey_pressedZ
scrollabler	   r)   r   r   r   r    r   r   r   r   r   r   r!   r"   Zdim_style_numberr#   r$   r   r5   r7   r,   r0   rI   rM   rN   rO   rQ   r.   r\   r]   r^   r/   re   rf   rg   r   rh   Ú__classcell__r3   r3   r9   r4   r   7   sH   ?			
r   c                   @   s   e Zd ZdZdS )ÚReadonlyEditorTN)ri   rj   rk   r)   r3   r3   r3   r4   rn   =  s   rn   N)!rl   Z	pyface.qtr   r   Z%pyface.ui.qt4.code_editor.code_widgetr   Z
traits.apir   r   r   r   r	   r
   r   Ztraits.trait_baser   Zpyface.key_pressed_eventr   Ú	constantsr   r   Zeditorr   Úhelperr   ZMARK_MARKERZSEARCH_MARKERZSELECTED_MARKERr   ZCustomEditorZSimpleEditorZ
TextEditorrn   r3   r3   r3   r4   Ú<module>   s    $	  