You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
3.5 KiB

  1. import wx
  2. import wx.lib.agw.multidirdialog as MDD
  3. import os
  4. from gooey.gui.components.widgets.core.text_input import TextInput
  5. from gooey.gui.components.widgets.dialogs.calender_dialog import CalendarDlg
  6. from gooey.gui.lang.i18n import _
  7. from gooey.util.functional import merge
  8. class Chooser(wx.Panel):
  9. """
  10. Base 'Chooser' type.
  11. Launches a Dialog box that allows the user to pick files, directories,
  12. dates, etc.. and places the result into a TextInput in the UI
  13. """
  14. def __init__(self, parent, *args, **kwargs):
  15. super(Chooser, self).__init__(parent)
  16. buttonLabel = kwargs.pop('label', _('browse'))
  17. self.widget = TextInput(self, *args, **kwargs)
  18. self.button = wx.Button(self, label=buttonLabel)
  19. self.button.Bind(wx.EVT_BUTTON, self.spawnDialog)
  20. self.layout()
  21. def layout(self):
  22. layout = wx.BoxSizer(wx.HORIZONTAL)
  23. layout.Add(self.widget, 1, wx.EXPAND | wx.TOP, 2)
  24. layout.Add(self.button, 0, wx.LEFT, 10)
  25. v = wx.BoxSizer(wx.VERTICAL)
  26. v.Add(layout, 1, wx.EXPAND, wx.TOP, 1)
  27. self.SetSizer(v)
  28. def spawnDialog(self, event):
  29. fd = self.getDialog()
  30. if fd.ShowModal() == wx.ID_CANCEL:
  31. return
  32. self.processResult(self.getResult(fd))
  33. def getDialog(self):
  34. return wx.FileDialog(self, _('open_file'))
  35. def getResult(self, dialog):
  36. return dialog.GetPath()
  37. def processResult(self, result):
  38. self.setValue(result)
  39. def setValue(self, value):
  40. self.widget.setValue(value)
  41. def getValue(self):
  42. return self.widget.getValue()
  43. class FileChooser(Chooser):
  44. """ Retrieve an existing file from the system """
  45. def getDialog(self):
  46. return wx.FileDialog(self, _('open_file'), style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
  47. class MultiFileChooser(Chooser):
  48. """ Retrieve an multiple files from the system """
  49. def getDialog(self):
  50. return wx.FileDialog(self, _('open_files'), style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE)
  51. def getResult(self, dialog):
  52. return os.pathsep.join(dialog.GetPaths())
  53. class FileSaver(Chooser):
  54. """ Specify the path to save a new file """
  55. def getDialog(self):
  56. return wx.FileDialog(
  57. self,
  58. style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
  59. defaultFile=_("enter_filename"),
  60. message=_('choose_file')
  61. )
  62. class DirChooser(Chooser):
  63. """ Retrieve a path to the supplied directory """
  64. def getDialog(self):
  65. return wx.DirDialog(self, message=_('choose_folder'))
  66. class MultiDirChooser(Chooser):
  67. """ Retrieve an multiple directories from the system """
  68. def getDialog(self):
  69. return MDD.MultiDirDialog(self,
  70. message=_('choose_folders_msg'),
  71. title=_('choose_folders_title'),
  72. defaultPath=os.getcwd(),
  73. agwStyle=MDD.DD_MULTIPLE | MDD.DD_DIR_MUST_EXIST)
  74. def getResult(self, dialog):
  75. return os.pathsep.join(dialog.GetPaths())
  76. class DateChooser(Chooser):
  77. """ Launches a date picker which returns and ISO Date """
  78. def __init__(self, *args, **kwargs):
  79. defaults = {'label': _('choose_date')}
  80. super(DateChooser, self).__init__(*args, **merge(kwargs, defaults))
  81. def getDialog(self):
  82. return CalendarDlg(self)