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.

178 lines
6.3 KiB

  1. import wx
  2. import wx.lib.agw.multidirdialog as MDD
  3. import os
  4. import re
  5. from gooey.gui.components.widgets.core.text_input import TextInput
  6. from gooey.gui.components.widgets.dialogs.calender_dialog import CalendarDlg
  7. from gooey.gui.components.widgets.dialogs.time_dialog import TimeDlg
  8. from gooey.gui.lang.i18n import _
  9. from gooey.util.functional import merge
  10. class Chooser(wx.Panel):
  11. """
  12. Base 'Chooser' type.
  13. Launches a Dialog box that allows the user to pick files, directories,
  14. dates, etc.. and places the result into a TextInput in the UI
  15. """
  16. def __init__(self, parent, *args, **kwargs):
  17. super(Chooser, self).__init__(parent)
  18. buttonLabel = kwargs.pop('label', _('browse'))
  19. self.widget = TextInput(self, *args, **kwargs)
  20. self.button = wx.Button(self, label=buttonLabel)
  21. self.button.Bind(wx.EVT_BUTTON, self.spawnDialog)
  22. self.layout()
  23. def layout(self):
  24. layout = wx.BoxSizer(wx.HORIZONTAL)
  25. layout.Add(self.widget, 1, wx.EXPAND | wx.TOP, 2)
  26. layout.Add(self.button, 0, wx.LEFT, 10)
  27. v = wx.BoxSizer(wx.VERTICAL)
  28. v.Add(layout, 1, wx.EXPAND, wx.TOP, 1)
  29. self.SetSizer(v)
  30. def spawnDialog(self, event):
  31. fd = self.getDialog()
  32. if fd.ShowModal() == wx.ID_CANCEL:
  33. return
  34. self.processResult(self.getResult(fd))
  35. def getDialog(self):
  36. return wx.FileDialog(self, _('open_file'))
  37. def getResult(self, dialog):
  38. return dialog.GetPath()
  39. def processResult(self, result):
  40. self.setValue(result)
  41. def setValue(self, value):
  42. self.widget.setValue(value)
  43. def getValue(self):
  44. return self.widget.getValue()
  45. class FileChooser(Chooser):
  46. """ Retrieve an existing file from the system """
  47. def getDialog(self):
  48. options = self.Parent._options
  49. return wx.FileDialog(self, message=options.get('message', _('open_file')),
  50. style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST,
  51. defaultFile=options.get('default_file', _("enter_filename")),
  52. defaultDir=options.get('default_dir', _('')),
  53. wildcard=options.get('wildcard', wx.FileSelectorDefaultWildcardStr))
  54. class MultiFileChooser(Chooser):
  55. """ Retrieve an multiple files from the system """
  56. def getDialog(self):
  57. options = self.Parent._options
  58. return wx.FileDialog(self, message=options.get('message', _('open_files')),
  59. style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST | wx.FD_MULTIPLE,
  60. defaultFile=options.get('default_file', _("enter_filename")),
  61. defaultDir=options.get('default_dir', _('')),
  62. wildcard=options.get('wildcard', wx.FileSelectorDefaultWildcardStr))
  63. def getResult(self, dialog):
  64. return os.pathsep.join(dialog.GetPaths())
  65. class FileSaver(Chooser):
  66. """ Specify the path to save a new file """
  67. def getDialog(self):
  68. options = self.Parent._options
  69. return wx.FileDialog(
  70. self,
  71. style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT,
  72. defaultFile=options.get('default_file', _("enter_filename")),
  73. defaultDir=options.get('default_dir', _('')),
  74. message=options.get('message', _('choose_file')),
  75. wildcard=options.get('wildcard', wx.FileSelectorDefaultWildcardStr)
  76. )
  77. class DirChooser(Chooser):
  78. """ Retrieve a path to the supplied directory """
  79. def getDialog(self):
  80. options = self.Parent._options
  81. return wx.DirDialog(self, message=options.get('message', _('choose_folder')),
  82. defaultPath=options.get('default_path', os.getcwd()))
  83. class MultiDirChooser(Chooser):
  84. """ Retrieve multiple directories from the system """
  85. def getDialog(self):
  86. options = self.Parent._options
  87. return MDD.MultiDirDialog(self,
  88. message=options.get('message', _('choose_folders')),
  89. title=_('choose_folders_title'),
  90. defaultPath=options.get('default_path', os.getcwd()),
  91. agwStyle=MDD.DD_MULTIPLE | MDD.DD_DIR_MUST_EXIST)
  92. def getResult(self, dialog):
  93. paths = dialog.GetPaths()
  94. # Remove volume labels from Windows paths
  95. if 'nt' == os.name:
  96. for i, path in enumerate(paths):
  97. if path:
  98. parts = path.split(os.sep)
  99. vol = parts[0]
  100. drives = re.match(r'.*\((?P<drive>\w:)\)', vol)
  101. paths[i] = os.sep.join([drives.group('drive')] + parts[1:])
  102. return os.pathsep.join(paths)
  103. class DateChooser(Chooser):
  104. """ Launches a date picker which returns an ISO Date """
  105. def __init__(self, *args, **kwargs):
  106. defaults = {'label': _('choose_date')}
  107. super(DateChooser, self).__init__(*args, **merge(kwargs, defaults))
  108. def getDialog(self):
  109. return CalendarDlg(self)
  110. class TimeChooser(Chooser):
  111. """ Launches a time picker which returns and ISO Time """
  112. def __init__(self, *args, **kwargs):
  113. defaults = {'label': _('choose_time')}
  114. super(TimeChooser, self).__init__(*args, **merge(kwargs, defaults))
  115. def getDialog(self):
  116. return TimeDlg(self)
  117. class ColourChooser(Chooser):
  118. """ Launches a color picker which returns a hex color code"""
  119. def __init__(self, *args, **kwargs):
  120. defaults = {'label': _('choose_colour'),
  121. 'style': wx.TE_RICH}
  122. super(ColourChooser, self).__init__(*args, **merge(kwargs, defaults))
  123. def setValue(self, value):
  124. colour = wx.Colour(value)
  125. self.widget.widget.SetForegroundColour(colour)
  126. self.widget.widget.SetBackgroundColour(colour)
  127. self.widget.setValue(value)
  128. def getResult(self, dialog):
  129. colour = dialog.GetColourData().GetColour()
  130. # Set text box back/foreground to selected colour
  131. self.widget.widget.SetForegroundColour(colour)
  132. self.widget.widget.SetBackgroundColour(colour)
  133. return colour.GetAsString(wx.C2S_HTML_SYNTAX)
  134. def getDialog(self):
  135. return wx.ColourDialog(self)