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.

208 lines
5.7 KiB

9 years ago
9 years ago
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):
  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.option_string = None
  32. self.parent = None
  33. self.widget = None
  34. self.button = None
  35. def build(self, parent, data):
  36. self.parent = parent
  37. self.option_string = self.get_command(data)
  38. self.widget = wx.TextCtrl(self.parent)
  39. self.widget.AppendText(safe_default(data, ''))
  40. self.widget.SetMinSize((0, -1))
  41. dt = FileDrop(self.widget)
  42. self.widget.SetDropTarget(dt)
  43. self.button = wx.Button(self.parent, label=self.button_text, size=(73, 23))
  44. widget_sizer = wx.BoxSizer(wx.HORIZONTAL)
  45. widget_sizer.Add(self.widget, 1, wx.EXPAND)
  46. widget_sizer.AddSpacer(10)
  47. widget_sizer.Add(self.button, 0, wx.ALIGN_CENTER_VERTICAL)
  48. parent.Bind(wx.EVT_BUTTON, self.on_button, self.button)
  49. return widget_sizer
  50. def get_value(self):
  51. return self.widget.GetValue()
  52. def __repr__(self):
  53. return self.__class__.__name__
  54. class BaseFileChooser(BaseChooser):
  55. dialog = None
  56. def __init__(self):
  57. BaseChooser.__init__(self)
  58. def on_button(self, evt):
  59. dlg = self.dialog(self.parent)
  60. result = (self.get_path(dlg)
  61. if dlg.ShowModal() == wx.ID_OK
  62. else None)
  63. if result:
  64. self.widget.SetValue(result)
  65. def get_path(self, dlg):
  66. return dlg.GetPath()
  67. class BaseMultiFileChooser(BaseFileChooser):
  68. def __init__(self, dialog):
  69. BaseFileChooser.__init__(self)
  70. self.dialog = dialog
  71. def get_path(self, dlg):
  72. return os.pathsep.join(dlg.GetPaths())
  73. class MultiFileSaverPayload(BaseMultiFileChooser):
  74. def __init__(self, *args, **kwargs):
  75. BaseMultiFileChooser.__init__(self, build_dialog(wx.FD_MULTIPLE, False))
  76. class MultiDirChooserPayload(BaseMultiFileChooser):
  77. class MyMultiDirChooser(MDD.MultiDirDialog):
  78. def __init__(self, *args, **kwargs):
  79. kwargs.update({
  80. 'title': "Select Directories",
  81. 'defaultPath': os.getcwd(),
  82. 'agwStyle': MDD.DD_MULTIPLE|MDD.DD_DIR_MUST_EXIST
  83. })
  84. MDD.MultiDirDialog.__init__(self, *args, **kwargs)
  85. def GetPaths(self):
  86. return self.dirCtrl.GetPaths()
  87. def __init__(self, *args, **kwargs):
  88. BaseMultiFileChooser.__init__(self, MultiDirChooserPayload.MyMultiDirChooser)
  89. class TextInputPayload(WidgetPack):
  90. def __init__(self, no_quoting=False):
  91. self.widget = None
  92. self.option_string = None
  93. self.no_quoting = no_quoting
  94. def build(self, parent, data):
  95. self.option_string = self.get_command(data)
  96. if self.disable_quoting(data):
  97. self.no_quoting = True
  98. self.widget = wx.TextCtrl(parent)
  99. dt = FileDrop(self.widget)
  100. self.widget.SetDropTarget(dt)
  101. self.widget.SetMinSize((0, -1))
  102. self.widget.SetDoubleBuffered(True)
  103. self.widget.AppendText(safe_default(data, ''))
  104. return self.widget
  105. def get_value(self):
  106. return self.widget.GetValue()
  107. class DropdownPayload(WidgetPack):
  108. default_value = 'Select Option'
  109. def __init__(self, no_quoting=False):
  110. self.widget = None
  111. self.option_string = None
  112. self.no_quoting = no_quoting
  113. def build(self, parent, data):
  114. self.option_string = self.get_command(data)
  115. if self.disable_quoting(data):
  116. self.no_quoting = True
  117. self.widget = wx.ComboBox(
  118. parent=parent,
  119. id=-1,
  120. value=safe_default(data, self.default_value),
  121. choices=[],
  122. style=wx.CB_DROPDOWN
  123. )
  124. return self.widget
  125. def get_value(self):
  126. return self.widget.GetValue()
  127. def set_value(self, text):
  128. # TODO: can we set dropdowns this way?
  129. self.widget.SetValue(text)
  130. class CounterPayload(WidgetPack):
  131. def __init__(self):
  132. self.option_string = None
  133. self.widget = None
  134. def build(self, parent, data):
  135. self.option_string = self.get_command(data)
  136. self.widget = wx.ComboBox(
  137. parent=parent,
  138. id=-1,
  139. value=safe_default(data, ''),
  140. choices=map(str, range(1, 11)),
  141. style=wx.CB_DROPDOWN
  142. )
  143. return self.widget
  144. def get_value(self):
  145. return self.widget.GetValue()
  146. class DirDialog(wx.DirDialog):
  147. def __init__(self, parent, *args, **kwargs):
  148. wx.DirDialog.__init__(self, parent, 'Select Directory', style=wx.DD_DEFAULT_STYLE)
  149. def safe_default(data, default):
  150. return ''
  151. def build_dialog(style, exist_constraint=True, **kwargs):
  152. if exist_constraint:
  153. return lambda panel: wx.FileDialog(panel, style=style | wx.FD_FILE_MUST_EXIST, **kwargs)
  154. else:
  155. return lambda panel: wx.FileDialog(panel, style=style, **kwargs)
  156. def build_subclass(subclass, dialog):
  157. return type(subclass, (BaseFileChooser,), {'dialog': dialog})
  158. FileSaverPayload = build_subclass('FileSaverPayload', staticmethod(build_dialog(wx.FD_SAVE, False, defaultFile="Enter Filename")))
  159. FileChooserPayload = build_subclass('FileChooserPayload', staticmethod(build_dialog(wx.FD_OPEN)))
  160. DirChooserPayload = build_subclass('DirChooserPayload', DirDialog)
  161. DateChooserPayload = build_subclass('DateChooserPayload', CalendarDlg)