<s> from os . path import basename <EOL> from pyface . qt import QtCore , QtGui <EOL> from traits . api import Bool , Event , Instance , File , Unicode , Property , provides <EOL> from pyface . tasks . api import Editor <EOL> from i_python_editor import IPythonEditor <EOL> from pyface . key_pressed_event import KeyPressedEvent <EOL> @ provides ( IPythonEditor ) <EOL> class PythonEditor ( Editor ) : <EOL> """<STR_LIT>""" <EOL> obj = Instance ( File ) <EOL> path = Unicode <EOL> dirty = Bool ( False ) <EOL> name = Property ( Unicode , depends_on = '<STR_LIT:path>' ) <EOL> tooltip = Property ( Unicode , depends_on = '<STR_LIT:path>' ) <EOL> show_line_numbers = Bool ( True ) <EOL> changed = Event <EOL> key_pressed = Event ( KeyPressedEvent ) <EOL> def _get_tooltip ( self ) : <EOL> return self . path <EOL> def _get_name ( self ) : <EOL> return basename ( self . path ) or '<STR_LIT>' <EOL> def create ( self , parent ) : <EOL> self . control = self . _create_control ( parent ) <EOL> def load ( self , path = None ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> path = self . path <EOL> if len ( path ) > <NUM_LIT:0> : <EOL> f = open ( self . path , '<STR_LIT:r>' ) <EOL> text = f . read ( ) <EOL> f . close ( ) <EOL> else : <EOL> text = '<STR_LIT>' <EOL> self . control . code . setPlainText ( text ) <EOL> self . dirty = False <EOL> def save ( self , path = None ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> path = self . path <EOL> f = file ( path , '<STR_LIT:w>' ) <EOL> f . write ( self . control . code . toPlainText ( ) ) <EOL> f . close ( ) <EOL> self . dirty = False <EOL> def select_line ( self , lineno ) : <EOL> """<STR_LIT>""" <EOL> self . control . code . set_line_column ( lineno , <NUM_LIT:0> ) <EOL> self . control . code . moveCursor ( QtGui . QTextCursor . EndOfLine , <EOL> QtGui . QTextCursor . KeepAnchor ) <EOL> def _path_changed ( self ) : <EOL> if self . control is not None : <EOL> self . load ( ) <EOL> def _show_line_numbers_changed ( self ) : <EOL> if self . control is not None : <EOL> self . control . code . line_number_widget . setVisible ( <EOL> self . show_line_numbers ) <EOL> self . control . code . update_line_number_width ( ) <EOL> def _create_control ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> from pyface . ui . qt4 . code_editor . code_widget import AdvancedCodeWidget <EOL> self . control = control = AdvancedCodeWidget ( parent ) <EOL> self . _show_line_numbers_changed ( ) <EOL> event_filter = PythonEditorEventFilter ( self , self . control ) <EOL> self . control . installEventFilter ( event_filter ) <EOL> self . control . code . installEventFilter ( event_filter ) <EOL> control . code . modificationChanged . connect ( self . _on_dirty_changed ) <EOL> control . code . textChanged . connect ( self . _on_text_changed ) <EOL> self . load ( ) <EOL> return control <EOL> def _on_dirty_changed ( self , dirty ) : <EOL> """<STR_LIT>""" <EOL> self . dirty = dirty <EOL> def _on_text_changed ( self ) : <EOL> """<STR_LIT>""" <EOL> self . changed = True <EOL> class PythonEditorEventFilter ( QtCore . QObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , editor , parent ) : <EOL> super ( PythonEditorEventFilter , self ) . __init__ ( parent ) <EOL> self . __editor = editor <EOL> def eventFilter ( self , obj , event ) : <EOL> """<STR_LIT>""" <EOL> if ( self . __editor . control and obj == self . __editor . control and <EOL> event . type ( ) == QtCore . QEvent . FocusOut ) : <EOL> self . __editor . control . emit ( QtCore . SIGNAL ( '<STR_LIT>' ) ) <EOL> elif ( self . __editor . control and obj == self . __editor . control . code and <EOL> event . type ( ) == QtCore . QEvent . KeyPress ) : <EOL> kstr = event . text ( ) <EOL> try : <EOL> kcode = ord ( str ( kstr ) ) <EOL> except : <EOL> kcode = <NUM_LIT:0> <EOL> mods = event . modifiers ( ) <EOL> self . key_pressed = KeyPressedEvent ( <EOL> alt_down = ( ( mods & QtCore . Qt . AltModifier ) == <EOL> QtCore . Qt . AltModifier ) , <EOL> control_down = ( ( mods & QtCore . Qt . ControlModifier ) == <EOL> QtCore . Qt . ControlModifier ) , <EOL> shift_down = ( ( mods & QtCore . Qt . ShiftModifier ) == <EOL> QtCore . Qt . ShiftModifier ) , <EOL> key_code = kcode , <EOL> event = event ) <EOL> return super ( PythonEditorEventFilter , self ) . eventFilter ( obj , event ) </s>
