<s> import sys <EOL> from pyface . tasks . i_editor_area_pane import IEditorAreaPane , MEditorAreaPane <EOL> from traits . api import on_trait_change , provides <EOL> from pyface . qt import QtCore , QtGui <EOL> from . task_pane import TaskPane <EOL> from . util import set_focus <EOL> @ provides ( IEditorAreaPane ) <EOL> class EditorAreaPane ( TaskPane , MEditorAreaPane ) : <EOL> """<STR_LIT>""" <EOL> def create ( self , parent ) : <EOL> """<STR_LIT>""" <EOL> self . control = control = EditorAreaWidget ( self , parent ) <EOL> self . _filter = EditorAreaDropFilter ( self ) <EOL> control . installEventFilter ( self . _filter ) <EOL> control . tabBar ( ) . setVisible ( not self . hide_tab_bar ) <EOL> control . currentChanged . connect ( self . _update_active_editor ) <EOL> control . tabCloseRequested . connect ( self . _close_requested ) <EOL> if sys . platform == '<STR_LIT>' : <EOL> next_seq = '<STR_LIT>' <EOL> prev_seq = '<STR_LIT>' <EOL> else : <EOL> next_seq = '<STR_LIT>' <EOL> prev_seq = '<STR_LIT>' <EOL> shortcut = QtGui . QShortcut ( QtGui . QKeySequence ( next_seq ) , self . control ) <EOL> shortcut . activated . connect ( self . _next_tab ) <EOL> shortcut = QtGui . QShortcut ( QtGui . QKeySequence ( prev_seq ) , self . control ) <EOL> shortcut . activated . connect ( self . _previous_tab ) <EOL> mod = '<STR_LIT>' if sys . platform == '<STR_LIT>' else '<STR_LIT>' <EOL> mapper = QtCore . QSignalMapper ( self . control ) <EOL> mapper . mapped . connect ( self . control . setCurrentIndex ) <EOL> for i in xrange ( <NUM_LIT:1> , <NUM_LIT:10> ) : <EOL> sequence = QtGui . QKeySequence ( mod + str ( i ) ) <EOL> shortcut = QtGui . QShortcut ( sequence , self . control ) <EOL> shortcut . activated . connect ( mapper . map ) <EOL> mapper . setMapping ( shortcut , i - <NUM_LIT:1> ) <EOL> def destroy ( self ) : <EOL> """<STR_LIT>""" <EOL> self . control . removeEventFilter ( self . _filter ) <EOL> self . _filter = None <EOL> for editor in self . editors : <EOL> self . remove_editor ( editor ) <EOL> super ( EditorAreaPane , self ) . destroy ( ) <EOL> def activate_editor ( self , editor ) : <EOL> """<STR_LIT>""" <EOL> self . control . setCurrentWidget ( editor . control ) <EOL> def add_editor ( self , editor ) : <EOL> """<STR_LIT>""" <EOL> editor . editor_area = self <EOL> editor . create ( self . control ) <EOL> index = self . control . addTab ( editor . control , self . _get_label ( editor ) ) <EOL> self . control . setTabToolTip ( index , editor . tooltip ) <EOL> self . editors . append ( editor ) <EOL> self . _update_tab_bar ( ) <EOL> if len ( self . editors ) == <NUM_LIT:1> : <EOL> self . active_editor = editor <EOL> def remove_editor ( self , editor ) : <EOL> """<STR_LIT>""" <EOL> self . editors . remove ( editor ) <EOL> self . control . removeTab ( self . control . indexOf ( editor . control ) ) <EOL> editor . destroy ( ) <EOL> editor . editor_area = None <EOL> self . _update_tab_bar ( ) <EOL> if not self . editors : <EOL> self . active_editor = None <EOL> def _get_label ( self , editor ) : <EOL> """<STR_LIT>""" <EOL> label = editor . name <EOL> if editor . dirty : <EOL> label = '<STR_LIT:*>' + label <EOL> return label <EOL> def _get_editor_with_control ( self , control ) : <EOL> """<STR_LIT>""" <EOL> for editor in self . editors : <EOL> if editor . control == control : <EOL> return editor <EOL> return None <EOL> def _next_tab ( self ) : <EOL> """<STR_LIT>""" <EOL> self . control . setCurrentIndex ( self . control . currentIndex ( ) + <NUM_LIT:1> ) <EOL> def _previous_tab ( self ) : <EOL> """<STR_LIT>""" <EOL> self . control . setCurrentIndex ( self . control . currentIndex ( ) - <NUM_LIT:1> ) <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _update_label ( self , editor , name , new ) : <EOL> index = self . control . indexOf ( editor . control ) <EOL> self . control . setTabText ( index , self . _get_label ( editor ) ) <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _update_tooltip ( self , editor , name , new ) : <EOL> index = self . control . indexOf ( editor . control ) <EOL> self . control . setTabToolTip ( index , editor . tooltip ) <EOL> def _close_requested ( self , index ) : <EOL> control = self . control . widget ( index ) <EOL> editor = self . _get_editor_with_control ( control ) <EOL> editor . close ( ) <EOL> def _update_active_editor ( self ) : <EOL> index = self . control . currentIndex ( ) <EOL> if index == - <NUM_LIT:1> : <EOL> self . active_editor = None <EOL> else : <EOL> control = self . control . widget ( index ) <EOL> self . active_editor = self . _get_editor_with_control ( control ) <EOL> @ on_trait_change ( '<STR_LIT>' ) <EOL> def _update_tab_bar ( self ) : <EOL> if self . control is not None : <EOL> visible = self . control . count ( ) > <NUM_LIT:1> if self . hide_tab_bar else True <EOL> self . control . tabBar ( ) . setVisible ( visible ) <EOL> class EditorAreaWidget ( QtGui . QTabWidget ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , editor_area , parent = None ) : <EOL> super ( EditorAreaWidget , self ) . __init__ ( parent ) <EOL> self . editor_area = editor_area <EOL> self . setAcceptDrops ( True ) <EOL> self . setDocumentMode ( True ) <EOL> self . setFocusPolicy ( QtCore . Qt . StrongFocus ) <EOL> self . setFocusProxy ( None ) <EOL> self . setMovable ( True ) <EOL> self . setTabsClosable ( True ) <EOL> self . setUsesScrollButtons ( True ) <EOL> def focusInEvent ( self , event ) : <EOL> """<STR_LIT>""" <EOL> active_editor = self . editor_area . active_editor <EOL> if active_editor : <EOL> set_focus ( active_editor . control ) <EOL> class EditorAreaDropFilter ( QtCore . QObject ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , editor_area ) : <EOL> super ( EditorAreaDropFilter , self ) . __init__ ( ) <EOL> self . editor_area = editor_area <EOL> def eventFilter ( self , object , event ) : <EOL> """<STR_LIT>""" <EOL> if event . type ( ) in ( QtCore . QEvent . DragEnter , QtCore . QEvent . Drop ) : <EOL> extensions = tuple ( self . editor_area . file_drop_extensions ) <EOL> file_paths = [ ] <EOL> for url in event . mimeData ( ) . urls ( ) : <EOL> file_path = url . toLocalFile ( ) <EOL> if file_path . endswith ( extensions ) : <EOL> file_paths . append ( file_path ) <EOL> if event . type ( ) == QtCore . QEvent . DragEnter : <EOL> if file_paths : <EOL> event . acceptProposedAction ( ) <EOL> elif event . type ( ) == QtCore . QEvent . Drop : <EOL> for file_path in file_paths : <EOL> self . editor_area . file_dropped = file_path <EOL> return True <EOL> return super ( EditorAreaDropFilter , self ) . eventFilter ( object , event ) </s>
