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.

233 lines
6.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. import os
  2. import wx
  3. import wx.lib.agw.multidirdialog as MDD
  4. from abc import ABCMeta, abstractmethod
  5. from gooey.gui.lang import i18n
  6. from gooey.gui.util.filedrop import FileDrop
  7. from gooey.gui.widgets.calender_dialog import CalendarDlg
  8. class WidgetPack(object):
  9. """
  10. Interface specifying the contract to which
  11. all `WidgetPack`s will adhere
  12. """
  13. __metaclass__ = ABCMeta
  14. @abstractmethod
  15. def build(self, parent, data, choices=None):
  16. pass
  17. def onResize(self, evt):
  18. pass
  19. @staticmethod
  20. def get_command(data):
  21. return ''
  22. @staticmethod
  23. def disable_quoting(data):
  24. nargs = data.get('nargs', None)
  25. if not nargs:
  26. return False
  27. return nargs not in (1, '?')
  28. class BaseChooser(WidgetPack):
  29. def __init__(self):
  30. self.button_text = i18n._('browse')
  31. self.parent = None
  32. self.widget = None
  33. self.button = None
  34. def build(self, parent, data, choices=None):
  35. self.parent = parent
  36. self.widget = wx.TextCtrl(self.parent)
  37. self.widget.AppendText('')
  38. self.widget.SetMinSize((0, -1))
  39. dt = FileDrop(self.widget)
  40. self.widget.SetDropTarget(dt)
  41. self.button = wx.Button(self.parent, label=self.button_text, size=(73, 23))
  42. widget_sizer = wx.BoxSizer(wx.HORIZONTAL)
  43. widget_sizer.Add(self.widget, 1, wx.EXPAND)
  44. widget_sizer.AddSpacer(10)
  45. widget_sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
  46. parent.Bind(wx.EVT_BUTTON, self.on_button, self.button)
  47. return widget_sizer
  48. def get_value(self):
  49. return self.widget.GetValue()
  50. def __repr__(self):
  51. return self.__class__.__name__
  52. class BaseFileChooser(BaseChooser):
  53. dialog = None
  54. def __init__(self):
  55. BaseChooser.__init__(self)
  56. def on_button(self, evt):
  57. dlg = self.dialog(self.parent)
  58. result = (self.get_path(dlg)
  59. if dlg.ShowModal() == wx.ID_OK
  60. else None)
  61. if result:
  62. self.widget.SetValue(result)
  63. def get_path(self, dlg):
  64. return dlg.GetPath()
  65. class BaseMultiFileChooser(BaseFileChooser):
  66. def __init__(self, dialog):
  67. BaseFileChooser.__init__(self)
  68. self.dialog = dialog
  69. def get_path(self, dlg):
  70. return os.pathsep.join(dlg.GetPaths())
  71. class MultiFileSaverPayload(BaseMultiFileChooser):
  72. def __init__(self, *args, **kwargs):
  73. BaseMultiFileChooser.__init__(self, build_dialog(wx.FD_MULTIPLE, False))
  74. class MultiDirChooserPayload(BaseMultiFileChooser):
  75. class MyMultiDirChooser(MDD.MultiDirDialog):
  76. def __init__(self, *args, **kwargs):
  77. kwargs.update({
  78. 'title': "Select Directories",
  79. 'defaultPath': os.getcwd(),
  80. 'agwStyle': MDD.DD_MULTIPLE|MDD.DD_DIR_MUST_EXIST
  81. })
  82. MDD.MultiDirDialog.__init__(self, *args, **kwargs)
  83. def GetPaths(self):
  84. return self.dirCtrl.GetPaths()
  85. def __init__(self, *args, **kwargs):
  86. BaseMultiFileChooser.__init__(self, MultiDirChooserPayload.MyMultiDirChooser)
  87. class TextInputPayload(WidgetPack):
  88. def __init__(self, no_quoting=False):
  89. self.widget = None
  90. self.option_string = None
  91. self.no_quoting = no_quoting
  92. def build(self, parent, data, choices=None):
  93. self.widget = wx.TextCtrl(parent)
  94. dt = FileDrop(self.widget)
  95. self.widget.SetDropTarget(dt)
  96. self.widget.SetMinSize((0, -1))
  97. self.widget.SetDoubleBuffered(True)
  98. self.widget.AppendText('')
  99. return self.widget
  100. def get_value(self):
  101. return self.widget.GetValue()
  102. class TextAreaPayload(WidgetPack):
  103. def __init__(self, no_quoting=False):
  104. self.widget = None
  105. self.option_string = None
  106. self.no_quoting = no_quoting
  107. def build(self, parent, data, choices=None):
  108. self.widget = wx.TextCtrl(parent, style=wx.TE_MULTILINE)
  109. dt = FileDrop(self.widget)
  110. self.widget.SetDropTarget(dt)
  111. self.widget.SetMinSize((0, -1))
  112. self.widget.SetDoubleBuffered(True)
  113. self.widget.AppendText('')
  114. return self.widget
  115. def get_value(self):
  116. return self.widget.GetValue()
  117. class DropdownPayload(WidgetPack):
  118. default_value = 'Select Option'
  119. def __init__(self, no_quoting=False):
  120. self.widget = None
  121. self.option_string = None
  122. self.no_quoting = no_quoting
  123. def build(self, parent, data, choices=None):
  124. self.widget = wx.ComboBox(
  125. parent=parent,
  126. id=-1,
  127. value=self.default_value,
  128. choices=[self.default_value] + choices,
  129. style=wx.CB_DROPDOWN
  130. )
  131. return self.widget
  132. def get_value(self):
  133. return self.widget.GetValue()
  134. def set_value(self, text):
  135. self.widget.SetValue(text)
  136. class CounterPayload(WidgetPack):
  137. def __init__(self):
  138. self.widget = None
  139. def build(self, parent, data, choices=None):
  140. self.widget = wx.ComboBox(
  141. parent=parent,
  142. id=-1,
  143. value='',
  144. choices=list(map(str, range(1, 11))),
  145. style=wx.CB_DROPDOWN
  146. )
  147. return self.widget
  148. def get_value(self):
  149. return self.widget.GetValue()
  150. class DirDialog(wx.DirDialog):
  151. def __init__(self, parent, *args, **kwargs):
  152. wx.DirDialog.__init__(self, parent, 'Select Directory', style=wx.DD_DEFAULT_STYLE)
  153. class PasswordInputPayload(WidgetPack):
  154. def __init__(self, no_quoting=False):
  155. self.widget = None
  156. self.option_string = None
  157. self.no_quoting = no_quoting
  158. def build(self, parent, data, choices=None):
  159. self.widget = wx.TextCtrl(parent, style=wx.TE_PASSWORD)
  160. dt = FileDrop(self.widget)
  161. self.widget.SetDropTarget(dt)
  162. self.widget.SetMinSize((0, -1))
  163. self.widget.SetDoubleBuffered(True)
  164. self.widget.AppendText('')
  165. return self.widget
  166. def get_value(self):
  167. return self.widget.GetValue()
  168. def safe_default(data, default):
  169. return ''
  170. def build_dialog(style, exist_constraint=True, **kwargs):
  171. if exist_constraint:
  172. return lambda panel: wx.FileDialog(panel, style=style | wx.FD_FILE_MUST_EXIST, **kwargs)
  173. else:
  174. return lambda panel: wx.FileDialog(panel, style=style, **kwargs)
  175. def build_subclass(subclass, dialog):
  176. return type(subclass, (BaseFileChooser,), {'dialog': dialog})
  177. FileSaverPayload = build_subclass('FileSaverPayload', staticmethod(build_dialog(wx.FD_SAVE, False, defaultFile="Enter Filename")))
  178. FileChooserPayload = build_subclass('FileChooserPayload', staticmethod(build_dialog(wx.FD_OPEN)))
  179. DirChooserPayload = build_subclass('DirChooserPayload', DirDialog)
  180. DateChooserPayload = build_subclass('DateChooserPayload', CalendarDlg)