Package Gnumed :: Package timelinelib :: Package wxgui :: Package components :: Module filechooser |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.components.filechooser

     1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  Rickard Lindberg, Roger Lindberg
     2  #
     3  # This file is part of Timeline.
     4  #
     5  # Timeline is free software: you can redistribute it and/or modify
     6  # it under the terms of the GNU General Public License as published by
     7  # the Free Software Foundation, either version 3 of the License, or
     8  # (at your option) any later version.
     9  #
    10  # Timeline is distributed in the hope that it will be useful,
    11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  # GNU General Public License for more details.
    14  #
    15  # You should have received a copy of the GNU General Public License
    16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
    17
    18
    19  import wx
    20  import wx.lib.newevent
    21
    22
    23 -class FileChooser(wx.Panel):
    24
    25      FilePathChangedEvent, EVT_FILE_PATH_CHANGED = wx.lib.newevent.NewEvent()
    26
    27      BORDER = 1
    28
    29 -    def __init__(self, parent,
    30                   dialog_message=_("Choose file"),
    31                   dialog_dir="",
    32                   dialog_wildcard="*",
    33                   **kwargs):
    34          wx.Panel.__init__(self, parent, **kwargs)
    35          self._dialog_message = dialog_message
    36          self._dialog_dir = dialog_dir
    37          self._dialog_wildcard = dialog_wildcard
    38          self._create_gui()
    39
    40 -    def GetFilePath(self):
    41          return self._path_text_field.GetValue()
    42
    43 -    def _create_gui(self):
    44          self._create_path_text_field()
    45          self._create_browse_button()
    46          self._layout_components()
    47
    48 -    def _create_path_text_field(self):
    49          self._path_text_field = wx.TextCtrl(self)
    50          self._path_text_field.Bind(wx.EVT_TEXT, self._on_path_text_changed)
    51
    52 -    def _on_path_text_changed(self, evt):
    53          wx.PostEvent(self, self.FilePathChangedEvent())
    54
    55 -    def _create_browse_button(self):
    56          self._browse_button = wx.Button(self, wx.ID_OPEN)
    57          self._browse_button.Bind(wx.EVT_BUTTON, self._on_browse_button_click)
    58
    59 -    def _on_browse_button_click(self, evt):
    60          dialog = wx.FileDialog(self,
    61                                 message=self._dialog_message,
    62                                 defaultDir=self._dialog_dir,
    63                                 wildcard=self._dialog_wildcard,
    64                                 style=wx.FD_OPEN)
    65          if dialog.ShowModal() == wx.ID_OK:
    66              self._path_text_field.SetValue(dialog.GetPath())
    67          dialog.Destroy()
    68
    69 -    def _layout_components(self):
    70          sizer = wx.BoxSizer(wx.HORIZONTAL)
    71          sizer.Add(self._path_text_field,
    72                    proportion=1,
    73                    flag=wx.ALL|wx.ALIGN_CENTER_VERTICAL,
    74                    border=self.BORDER)
    75          sizer.Add(self._browse_button,
    76                    proportion=0,
    77                    flag=wx.ALL|wx.ALIGN_CENTER_VERTICAL,
    78                    border=self.BORDER)
    79          self.SetSizer(sizer)
    80