3
8ßL]<6  ã               @   sä   d Z ddlm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mZ ddl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dl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.
é    )Úabsolute_import)ÚQtCoreÚQtGui)ÚAdvancedCodeWidget)ÚStrÚUnicodeÚListÚIntÚEventÚBoolÚ
TraitErrorÚon_trait_change)ÚSequenceTypes)ÚToolkitEditorFactory)ÚKeyPressedEventé   )ÚOKColorÚ
ErrorColor)ÚEditor)Úpixmap_cacheNé   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 )*ÚSourceEditorzA Editor for source code which uses the advanced code widget.
    TFé   c             C   sÈ  t jƒ | _t j| jƒ}|jddddƒ td| jjd� | _}|j	|ƒ | j}| jj
}| jrf|jdƒ n$|jr||jj| jƒ n|jj| jƒ |jdkr¢|jj| jƒ |jdks¶|jdkrÄ|jj| jƒ |jj|jƒ | jƒ  | j|jdddd� | j|jd	dƒ | j|jd
dƒ | j|jdƒ | j|jdƒ | j|j ddƒ | j|j!ddƒ | j|j"dddd� | jj#dk�rtd| _#n| j|j#ddƒ | j|j$dddd� |j%dk�r¬d| _%n| j|j%ddƒ | j&ƒ  dS )za Finishes initializing the editor by creating the underlying toolkit
            widget.
        r   N)ÚlexerTÚ Ú
mark_linesÚfrom)Zis_listÚselected_lineÚselected_textZ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© r5   ú>/tmp/pip-install-5sebvq2j/traitsui/traitsui/qt4/code_editor.pyÚinitq   sJ    


zSourceEditor.initc                s,   | j js| jjjj| jƒ tt| ƒj	ƒ  dS )z0 Disposes of the contents of an editor.
        N)
r(   r,   r)   r*   r/   Z
disconnectr.   Úsuperr   Údispose)r3   )Ú	__class__r5   r6   r9   ³   s    zSourceEditor.disposec             C   s^   | j sZy0tj| jjjƒ ƒ}t| jtƒr.|j	ƒ }|| _W n" t
k
rX } zW Y dd}~X nX dS )zC Handles the user entering input data in the edit control.
        N)Ú_lockedÚsixÚ	text_typer)   r*   ÚtoPlainTextÚ
isinstanceÚvaluer   Úsplitr   )r3   r@   Úexcpr5   r5   r6   r.   Á   s    
zSourceEditor.update_objectc             C   s\   d| _ | j}t|tƒr*djdd„ |D ƒƒ}| j}|jjƒ |krR|jj|ƒ | j	j
rRd| _ dS )z` Updates the editor when the object trait changes externally to the
            editor.
        TÚ
c             S   s   g | ]}|j ƒ ‘qS r5   )Úrstrip)Ú.0r   r5   r5   r6   ú
<listcomp>Ø   s    z.SourceEditor.update_editor.<locals>.<listcomp>FN)r;   r@   r?   r   Újoinr)   r*   r>   ZsetPlainTextr(   r   )r3   Z	new_valuer'   r5   r5   r6   r2   Ñ   s    
zSourceEditor.update_editorc             C   s   dS )zN Handles an error that occurs while setting the object's trait value.
        Nr5   )r3   rB   r5   r5   r6   Úerroré   s    zSourceEditor.errorc             C   s0   | j jdk	r,|jdƒ}|dk	r,| j jj|ƒ dS )z` Restores any saved user preference information associated with the
            editor.
        NÚkey_bindings)r(   rI   ÚgetÚmerge)r3   ZprefsrI   r5   r5   r6   Úrestore_prefsõ   s    
zSourceEditor.restore_prefsc             C   s   d| j jiS )zM Returns any user preference information associated with the editor.
        rI   )r(   rI   )r3   r5   r5   r6   Ú
save_prefs  s    zSourceEditor.save_prefsc             C   s   dS )z8 Handles the set of marked lines being changed.
        Nr5   )r3   r5   r5   r6   Ú_mark_lines_changed  s    z SourceEditor._mark_lines_changedc             C   s   | j ƒ  d S )N)rN   )r3   r5   r5   r6   Ú_mark_lines_items_changed  s    z&SourceEditor._mark_lines_items_changedc             C   sZ   t j| jjjƒ jƒ ƒ| _| jjjƒ jƒ }| jjjƒ jƒ }||krJ|| }}|| _	|| _
d S )N)r<   r=   r)   r*   Z
textCursorZselectedTextr   ZselectionStartZselectionEndr!   r"   )r3   ÚstartÚendr5   r5   r6   r0     s    
zSourceEditor._selection_changedc             C   sH   | j }tdt|jƒ | jƒƒ}|jƒ \}}|j||ƒ | jjrD|j	ƒ  dS )z? Handles a change in which line is currently selected.
        r   N)
r)   ÚmaxÚminÚlinesr   Úget_line_columnÚset_line_columnr(   Úauto_scrollÚcenterCursor)r3   r'   r   Ú_r    r5   r5   r6   Ú_selected_line_changed$  s    z#SourceEditor._selected_line_changedc             C   s8   | j s4| jjƒ \}}| jj||ƒ | jjr4| jjƒ  d S )N)r;   r)   rU   rV   r(   rW   rX   )r3   r   rY   r    r5   r5   r6   Ú_line_changed2  s
    zSourceEditor._line_changedc             C   s&   | j s"| jjƒ \}}| jj||ƒ d S )N)r;   r)   rU   rV   )r3   r    r   rY   r5   r5   r6   Ú_column_changed=  s    zSourceEditor._column_changedc             C   sB   | j }d| _|jƒ \| _| _d| _|jƒ | _| jjr>| j j	ƒ  dS )z4 Handles the cursor position being changed.
        TFN)
r)   r;   rU   r   r    Zget_selected_textr   r(   rW   rX   )r3   r'   r5   r5   r6   r1   F  s    
zSourceEditor._position_changedc             C   sN   | j j}|r&|j|j| jj| jjƒ}nd}| rJ|jjtj	j
ƒrJ| jjƒ  dS )z8 Handles a key being pressed within the editor.
        FN)r(   rI   ZdoÚeventÚuiÚhandlerÚinfoÚmatchesr   ZQKeySequenceZFindZ_find_widgetÚshow)r3   r]   rI   Ú	processedr5   r5   r6   Ú_key_pressed_changedU  s    z!SourceEditor._key_pressed_changedc             C   s   d S )Nr5   )r3   r5   r5   r6   Ú_dim_color_changede  s    zSourceEditor._dim_color_changedc             C   s   d S )Nr5   )r3   r5   r5   r6   Ú_squiggle_color_changedh  s    z$SourceEditor._squiggle_color_changedzdim_lines, squiggle_linesc             C   s   | j j| jƒ d S )N)r)   Zset_warn_linesr%   )r3   r5   r5   r6   Ú_style_documentk  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   r$   Zdim_style_numberr%   r&   r   r7   r9   r.   r2   rH   rL   rM   rN   rO   r0   rZ   r[   r\   r1   rd   re   rf   r   rg   Ú__classcell__r5   r5   )r:   r6   r   3   sF   B		r   c               @   s   e Zd ZdZdS )ÚReadonlyEditorTN)rh   ri   rj   r+   r5   r5   r5   r6   rm   v  s   rm   )'rk   Ú
__future__r   Z	pyface.qtr   r   Z%pyface.ui.qt4.code_editor.code_widgetr   Z
traits.apir   r   r   r	   r
   r   r   r   Ztraits.trait_baser   Ztraitsui.editors.code_editorr   Zpyface.key_pressed_eventr   Ú	constantsr   r   Úeditorr   Úhelperr   r<   ZMARK_MARKERZSEARCH_MARKERZSELECTED_MARKERr   ZCustomEditorZSimpleEditorZ
TextEditorrm   r5   r5   r5   r6   Ú<module>   s&   (  B