コード例 #1
0
ファイル: file_editor.py プロジェクト: raphmur/traitsui
    def init(self, parent):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self.control = panel = TraitsUIPanel(parent, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        factory = self.factory

        if factory.entries > 0:
            from 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)

            bmp = wx.ArtProvider.GetBitmap(wx.ART_FOLDER_OPEN,
                                           size=(15, 15))
            button = wx.BitmapButton(panel, -1, bitmap=bmp)

            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)
コード例 #2
0
    def init(self, parent):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self.history = history = HistoryControl(value=self.value,
                                                entries=self.factory.entries,
                                                auto_set=self.factory.auto_set)
        self.control = history.create_control(parent)

        self.set_tooltip()
コード例 #3
0
ファイル: file_editor.py プロジェクト: satishgoda/traitsui
    def init ( self, parent ):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self.control = panel = TraitsUIPanel( parent, -1 )
        sizer        = wx.BoxSizer( wx.HORIZONTAL )
        factory      = self.factory

        if factory.entries > 0:
            from 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 )

            bmp    = wx.ArtProvider.GetBitmap( wx.ART_FOLDER_OPEN,
                                               size = ( 15, 15 ) )
            button = wx.BitmapButton( panel, -1, bitmap = bmp )
            
            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 )
コード例 #4
0
ファイル: file_editor.py プロジェクト: raphmur/traitsui
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.
    """

    # The history control (used if the factory 'entries' > 0):
    history = Any

    # The popup file control (an Instance( PopupFile )):
    popup = Any

    #-------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    #-------------------------------------------------------------------------

    def init(self, parent):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self.control = panel = TraitsUIPanel(parent, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        factory = self.factory

        if factory.entries > 0:
            from 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)

            bmp = wx.ArtProvider.GetBitmap(wx.ART_FOLDER_OPEN,
                                           size=(15, 15))
            button = wx.BitmapButton(panel, -1, bitmap=bmp)

            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()

    #-------------------------------------------------------------------------
    #  Handles the history 'value' trait being changed:
    #-------------------------------------------------------------------------

    @on_trait_change('history:value')
    def _history_value_changed(self, value):
        """ Handles the history 'value' trait being changed.
        """
        if not self._no_update:
            self._update(value)

    #-------------------------------------------------------------------------
    #  Handles the user changing the contents of the edit control:
    #-------------------------------------------------------------------------

    def update_object(self, event):
        """ Handles the user changing the contents of the edit control.
        """
        if isinstance(event, wx.FocusEvent):
            event.Skip()
        self._update(self._file_name.GetValue())

    #-------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    #-------------------------------------------------------------------------

    def update_editor(self):
        """ Updates the editor when the object trait 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)

    #-------------------------------------------------------------------------
    #  Displays the pop-up file dialog:
    #-------------------------------------------------------------------------

    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()

    #-------------------------------------------------------------------------
    #  Returns the editor's control for indicating error status:
    #-------------------------------------------------------------------------

    def get_error_control(self):
        """ Returns the editor's control for indicating error status.
        """
        return self._file_name

    #-- Traits Event Handlers ------------------------------------------------

    @on_trait_change('popup:value')
    def _popup_value_changed(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_trait_change('popup:closed')
    def _popup_closed_changed(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 (*.*)|*.*'

        if self.factory.dialog_style == 'save':
            style = wx.FD_SAVE
        elif self.factory.dialog_style == 'open':
            style = wx.FD_OPEN
        else:
            style = wx.FD_DEFAULT_STYLE

        directory, filename = split(self._get_value())

        dlg = wx.FileDialog(
            self.control,
            defaultDir=directory,
            defaultFile=filename,
            message='Select a File',
            wildcard=wildcard,
            style=style
        )

        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 TraitError as excp:
            pass

    def _get_value(self):
        """ Returns the current file name from the edit control.
        """
        if self.history is not None:
            return self.history.value

        return self._file_name.GetValue()
コード例 #5
0
ファイル: file_editor.py プロジェクト: jdmarch/traitsui
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.
    """

    # The history control (used if the factory 'entries' > 0):
    history = Any

    # The popup file control (an Instance( PopupFile )):
    popup = Any

    # ---------------------------------------------------------------------------
    #  Finishes initializing the editor by creating the underlying toolkit
    #  widget:
    # ---------------------------------------------------------------------------

    def init(self, parent):
        """ Finishes initializing the editor by creating the underlying toolkit
            widget.
        """
        self.control = panel = TraitsUIPanel(parent, -1)
        sizer = wx.BoxSizer(wx.HORIZONTAL)
        factory = self.factory

        if factory.entries > 0:
            from 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()

    # ---------------------------------------------------------------------------
    #  Handles the history 'value' trait being changed:
    # ---------------------------------------------------------------------------

    @on_trait_change("history:value")
    def _history_value_changed(self, value):
        """ Handles the history 'value' trait being changed.
        """
        if not self._no_update:
            self._update(value)

    # ---------------------------------------------------------------------------
    #  Handles the user changing the contents of the edit control:
    # ---------------------------------------------------------------------------

    def update_object(self, event):
        """ Handles the user changing the contents of the edit control.
        """
        self._update(self._file_name.GetValue())

    # ---------------------------------------------------------------------------
    #  Updates the editor when the object trait changes external to the editor:
    # ---------------------------------------------------------------------------

    def update_editor(self):
        """ Updates the editor when the object trait 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)

    # ---------------------------------------------------------------------------
    #  Displays the pop-up file dialog:
    # ---------------------------------------------------------------------------

    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()

    # ---------------------------------------------------------------------------
    #  Returns the editor's control for indicating error status:
    # ---------------------------------------------------------------------------

    def get_error_control(self):
        """ Returns the editor's control for indicating error status.
        """
        return self._file_name

    # -- Traits Event Handlers --------------------------------------------------

    @on_trait_change("popup:value")
    def _popup_value_changed(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_trait_change("popup:closed")
    def _popup_closed_changed(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 (*.*)|*.*"

        if self.factory.dialog_style == "save":
            style = wx.FD_SAVE
        elif self.factory.dialog_style == "open":
            style = wx.FD_OPEN
        else:
            style = wx.FD_DEFAULT_STYLE

        dlg = wx.FileDialog(self.control, message="Select a File", wildcard=wildcard, style=style)

        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 TraitError, excp:
            pass