class SimpleEditor ( SimpleTextEditor ): """ Simple style of file editor, consisting of a text field and a **Browse** button that opens a file-selection dialog box. The user can also drag and drop a file onto this control. """ #-- Class Constants -------------------------------------------------------- # Is the editor implementation GUI toolkit neutral? is_toolkit_neutral = False #-- Facet Definitions ------------------------------------------------------ # The history control (used if the factory 'entries' > 0): history = Any # The popup file control (an Instance( PopupFile )): popup = Any #-- Public Methods --------------------------------------------------------- def init ( self, parent ): """ Finishes initializing the editor by creating the underlying toolkit widget. """ self.control = panel = FacetsUIPanel( parent, -1 ) sizer = wx.BoxSizer( wx.HORIZONTAL ) factory = self.factory if factory.entries > 0: from facets.ui.controls.history_control import HistoryControl self.history = HistoryControl( entries = factory.entries, auto_set = factory.auto_set ) control = self.history.create_control( panel )() pad = 3 button = wx.Button( panel, -1, '...', size = wx.Size( 28, -1 ) ) else: if factory.enter_set: control = wx.TextCtrl( panel, -1, '', style = wx.TE_PROCESS_ENTER ) wx.EVT_TEXT_ENTER( panel, control.GetId(), self.update_object ) else: control = wx.TextCtrl( panel, -1, '' ) wx.EVT_KILL_FOCUS( control, self.update_object ) if factory.auto_set: wx.EVT_TEXT( panel, control.GetId(), self.update_object ) button = wx.Button( panel, -1, 'Browse...' ) pad = 8 self._file_name = control sizer.Add( control, 1, wx.EXPAND | wx.ALIGN_CENTER ) sizer.Add( button, 0, wx.LEFT | wx.ALIGN_CENTER, pad ) wx.EVT_BUTTON( panel, button.GetId(), self.show_file_dialog ) panel.SetDropTarget( FileDropTarget( self ) ) panel.SetSizerAndFit( sizer ) self._button = button self.set_tooltip( control ) def dispose ( self ): """ Disposes of the contents of an editor. """ panel = self.control wx.EVT_BUTTON( panel, self._button.GetId(), None ) self._button = None if self.history is not None: self.history.dispose() self.history = None else: factory = self.factory control, self._file_name = self._file_name, None wx.EVT_KILL_FOCUS( control, None ) wx.EVT_TEXT_ENTER( panel, control.GetId(), None ) wx.EVT_TEXT( panel, control.GetId(), None ) super( SimpleEditor, self ).dispose() def update_object ( self, event ): """ Handles the user changing the contents of the edit control. """ self._update( self._file_name.GetValue() ) def update_editor ( self ): """ Updates the editor when the object facet changes externally to the editor. """ if self.history is not None: self._no_update = True self.history.value = self.str_value self._no_update = False else: self._file_name.SetValue( self.str_value ) def show_file_dialog ( self, event ): """ Displays the pop-up file dialog. """ if self.history is not None: self.popup = self._create_file_popup() else: dlg = self._create_file_dialog() rc = ( dlg.ShowModal() == wx.ID_OK ) file_name = abspath( dlg.GetPath() ) dlg.Destroy() if rc: if self.factory.truncate_ext: file_name = splitext( file_name )[ 0 ] self.value = file_name self.update_editor() def get_error_control ( self ): """ Returns the editor's control for indicating error status. """ return self._file_name #-- Facet Event Handlers --------------------------------------------------- @on_facet_set( 'history:value' ) def _history_value_modified ( self, value ): """ Handles the history 'value' facet being changed. """ if not self._no_update: self._update( value ) @on_facet_set( 'popup:value' ) def _popup_value_modified ( self, file_name ): """ Handles the popup value being changed. """ if self.factory.truncate_ext: file_name = splitext( file_name )[ 0 ] self.value = file_name self._no_update = True self.history.set_value( self.str_value ) self._no_update = False @on_facet_set( 'popup:closed' ) def _popup_closed_modified ( self ): """ Handles the popup control being closed. """ self.popup = None #-- UI preference save/restore interface ----------------------------------- def restore_prefs ( self, prefs ): """ Restores any saved user preference information associated with the editor. """ if self.history is not None: self.history.history = \ prefs.get( 'history', [] )[ : self.factory.entries ] def save_prefs ( self ): """ Returns any user preference information associated with the editor. """ if self.history is not None: return { 'history': self.history.history[:] } return None #-- Private Methods -------------------------------------------------------- def _create_file_dialog ( self ): """ Creates the correct type of file dialog. """ if len( self.factory.filter ) > 0: wildcard = '|'.join( self.factory.filter[ : ] ) else: wildcard = 'All Files (*.*)|*.*' dlg = wx.FileDialog( self.control, message = 'Select a File', wildcard = wildcard ) dlg.SetFilename( self._get_value() ) return dlg def _create_file_popup ( self ): """ Creates the correct type of file popup. """ return PopupFile( control = self.control, file_name = self.str_value, filter = self.factory.filter, height = 300 ) def _update ( self, file_name ): """ Updates the editor value with a specified file name. """ try: if self.factory.truncate_ext: file_name = splitext( file_name )[ 0 ] self.value = file_name except FacetError, excp: pass
class SimpleEditor ( SimpleTextEditor ): """ Simple style of file editor, consisting of a text field and a **Browse** button that opens a file-selection dialog box. The user can also drag and drop a file onto this control. """ #-- Facet Definitions ------------------------------------------------------ # The history control (used if the factory 'entries' > 0): history = Any # The popup file control (an Instance( PopupFile )): popup = Any # The value assigned using the popup file control: popup_file_name = Str # The ThemedButton control used when history entries are used: themed_button = Any #-- Public Methods --------------------------------------------------------- def init ( self, parent ): """ Finishes initializing the editor by creating the underlying toolkit widget. """ tk = toolkit() self.adapter = control = tk.create_panel( parent ) control.layout = layout = tk.create_box_layout( False ) factory = self.factory if factory.entries > 0: from facets.ui.controls.history_control import HistoryControl from facets.ui.controls.themed_button import ThemedButton pad = 3 self.history = HistoryControl( entries = factory.entries, auto_set = factory.auto_set ) file_name = self.history.create_control( control ) self.themed_button = ThemedButton( image = '@icons2:Folder', parent = control ) button = self.themed_button() else: pad = 8 file_name = tk.create_text_input( control ) if factory.auto_set: file_name.set_event_handler( text_change = self.update_object ) else: # Assume 'enter_set' is set, otherwise the value will never get # updated: file_name.set_event_handler( text_enter = self.update_object ) button = tk.create_button( control, 'Browse...' ) button.set_event_handler( clicked = self.show_file_dialog ) self._file_name = file_name file_name.size_policy = ( 'expanding', 'fixed' ) layout.add( file_name ) self._button = button layout.add( button, left = pad ) self.set_tooltip( file_name ) self.set_tooltip( button ) def update_object ( self, event ): """ Handles the user changing the contents of the edit control. """ self._update( self._file_name.value ) def update_editor ( self ): """ Updates the editor when the object facet changes externally to the editor. """ self._file_name.value = self.str_value def dispose ( self ): """ Disposes of the editor. """ factory = self.factory if factory.entries == 0: self._button.unset_event_handler( clicked = self.show_file_dialog ) if factory.auto_set: self._file_name.unset_event_handler( text_change = self.update_object ) else: self._file_name.unset_event_handler( text_enter = self.update_object ) # Skip our parent class 'dispose', since we use an incompatible # implementation: super( SimpleTextEditor, self ).dispose() @on_facet_set( 'themed_button:clicked' ) def show_file_dialog ( self, event ): """ Displays the pop-up file dialog. """ if self.history is not None: self.popup_file_name = self.str_value self.edit_facets( parent = self._file_name, view = View( Item( 'popup_file_name', style = 'custom', show_label = False, editor = self._popup_editor() ), kind = 'popup', height = 300 ) ) else: # We don't used the canned functions because we don't know how the # file name is to be used (i.e. an existing one to be opened or a # new one to be created): dlg = self._create_file_dialog() if dlg.exec_() == QDialog.Accepted: files = dlg.selectedFiles() if len( files ) > 0: file_name = normpath( unicode( files[0] ) ) if self.factory.truncate_ext: file_name = splitext( file_name )[0] self.value = file_name self.update_editor() def get_error_control ( self ): """ Returns the editor's control for indicating error status. """ return self._file_name #-- Facet Event Handlers --------------------------------------------------- @on_facet_set( 'history:value' ) def _history_modified ( self ): self._update( self.history.value ) def _popup_file_name_set ( self, file_name ): """ Handles the 'popup_file_name' facet being changed. """ self.history.set_value( file_name ) #-- UI preference save/restore interface ----------------------------------- def restore_prefs ( self, prefs ): """ Restores any saved user preference information associated with the editor. """ if self.history is not None: self.history.history = \ prefs.get( 'history', [] )[ : self.factory.entries ] def save_prefs ( self ): """ Returns any user preference information associated with the editor. """ if self.history is not None: return { 'history': self.history.history[:] } return None #-- Private Methods -------------------------------------------------------- def _create_file_dialog ( self ): """ Creates the correct type of file dialog. """ dlg = QFileDialog( self.control.parentWidget() ) dlg.selectFile( self._file_name.value ) if len( self.factory.filter ) > 0: dlg.setFilters( self.factory.filter ) return dlg def _popup_editor ( self ): """ Returns the editor to use when creating a pop-up editor. """ return FileEditor() def _update ( self, file_name ): """ Updates the editor value with a specified file name. """ if self.factory is not None: try: file_name = unicode( file_name ) if self.factory.truncate_ext: file_name = splitext( file_name )[0] self.value = file_name except FacetError: pass