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.

190 lines
5.2 KiB

  1. from functools import partial
  2. from gooey.gui.util.filedrop import FileDrop
  3. __author__ = 'Chris'
  4. from abc import ABCMeta, abstractmethod
  5. import wx
  6. from gooey.gui.widgets.calender_dialog import CalendarDlg
  7. class WidgetPack(object):
  8. """
  9. Interface specifying the contract to which
  10. all `WidgetPack`s will adhere
  11. """
  12. __metaclass__ = ABCMeta
  13. @abstractmethod
  14. def build(self, parent, data):
  15. pass
  16. @abstractmethod
  17. def getValue(self):
  18. pass
  19. def onResize(self, evt):
  20. pass
  21. @staticmethod
  22. def get_command(data):
  23. return data['commands'][0] if data['commands'] else ''
  24. class BaseChooser(WidgetPack):
  25. def __init__(self, button_text='Browse'):
  26. self.button_text = button_text
  27. self.option_string = None
  28. self.parent = None
  29. self.text_box = None
  30. self.button = None
  31. def build(self, parent, data=None):
  32. self.parent = parent
  33. self.option_string = data['commands'][0] if data['commands'] else ''
  34. self.text_box = wx.TextCtrl(self.parent)
  35. self.text_box.SetMinSize((0, -1))
  36. dt = FileDrop(self.text_box)
  37. self.text_box.SetDropTarget(dt)
  38. self.button = wx.Button(self.parent, label=self.button_text, size=(73, 23))
  39. widget_sizer = wx.BoxSizer(wx.HORIZONTAL)
  40. widget_sizer.Add(self.text_box, 1, wx.EXPAND)
  41. widget_sizer.AddSpacer(10)
  42. widget_sizer.Add(self.button, 0)
  43. parent.Bind(wx.EVT_BUTTON, self.onButton, self.button)
  44. return widget_sizer
  45. def getValue(self):
  46. value = self.text_box.GetValue()
  47. if self.option_string and value:
  48. return '{0} "{1}"'.format(self.option_string, value)
  49. else:
  50. return '"{}"'.format(value) if value else None
  51. def onButton(self, evt):
  52. raise NotImplementedError
  53. class BaseFileChooser(BaseChooser):
  54. def __init__(self, dialog):
  55. BaseChooser.__init__(self)
  56. self.dialog = dialog
  57. def onButton(self, evt):
  58. dlg = self.dialog(self.parent)
  59. result = (dlg.GetPath()
  60. if dlg.ShowModal() == wx.ID_OK
  61. else None)
  62. if result:
  63. # self.text_box references a field on the class this is passed into
  64. # kinda hacky, but avoided a buncha boilerplate
  65. self.text_box.SetValue(result)
  66. def build_dialog(style, exist_constraint=True, **kwargs):
  67. if exist_constraint:
  68. return lambda panel: wx.FileDialog(panel, style=style | wx.FD_FILE_MUST_EXIST, **kwargs)
  69. else:
  70. return lambda panel: wx.FileDialog(panel, style=style, **kwargs)
  71. FileChooserPayload = partial(BaseFileChooser, dialog=build_dialog(wx.FD_OPEN))
  72. FileSaverPayload = partial(BaseFileChooser, dialog=build_dialog(wx.FD_SAVE, False, defaultFile="Enter Filename"))
  73. MultiFileSaverPayload = partial(BaseFileChooser, dialog=build_dialog(wx.FD_MULTIPLE, False))
  74. DirChooserPayload = partial(BaseFileChooser, dialog=lambda parent: wx.DirDialog(parent, 'Select Directory', style=wx.DD_DEFAULT_STYLE))
  75. DateChooserPayload = partial(BaseFileChooser, dialog=CalendarDlg)
  76. class TextInputPayload(WidgetPack):
  77. def __init__(self):
  78. self.widget = None
  79. self.option_string = None
  80. def build(self, parent, data):
  81. self.option_string = self.get_command(data)
  82. self.widget = wx.TextCtrl(parent)
  83. dt = FileDrop(self.widget)
  84. self.widget.SetDropTarget(dt)
  85. self.widget.SetMinSize((0, -1))
  86. self.widget.SetDoubleBuffered(True)
  87. return self.widget
  88. def getValue(self):
  89. if self.widget.GetValue() and self.option_string:
  90. return '{} {}'.format(self.option_string, self.widget.GetValue())
  91. else:
  92. return self.widget.GetValue()
  93. def _SetValue(self, text):
  94. # used for testing
  95. self.widget.SetLabelText(text)
  96. class DropdownPayload(WidgetPack):
  97. default_value = 'Select Option'
  98. def __init__(self):
  99. self.option_string = None
  100. self.widget = None
  101. def build(self, parent, data):
  102. self.option_string = self.get_command(data)
  103. self.widget = wx.ComboBox(
  104. parent=parent,
  105. id=-1,
  106. value=self.default_value,
  107. choices=data['choices'],
  108. style=wx.CB_DROPDOWN
  109. )
  110. return self.widget
  111. def getValue(self):
  112. if self.widget.GetValue() == self.default_value:
  113. return ''
  114. elif self.widget.GetValue() and self.option_string:
  115. return '{} {}'.format(self.option_string, self.widget.GetValue())
  116. else:
  117. self.widget.GetValue()
  118. def _SetValue(self, text):
  119. # used for testing
  120. self.widget.SetLabelText(text)
  121. class CounterPayload(WidgetPack):
  122. def __init__(self):
  123. self.option_string = None
  124. self.widget = None
  125. def build(self, parent, data):
  126. self.option_string = self.get_command(data)
  127. self.widget = wx.ComboBox(
  128. parent=parent,
  129. id=-1,
  130. value='',
  131. choices=[str(x) for x in range(1, 7)],
  132. style=wx.CB_DROPDOWN
  133. )
  134. return self.widget
  135. def getValue(self):
  136. '''
  137. Returns
  138. str(option_string * DropDown Value)
  139. e.g.
  140. -vvvvv
  141. '''
  142. dropdown_value = self.widget.GetValue()
  143. if not str(dropdown_value).isdigit():
  144. return ''
  145. arg = str(self.option_string).replace('-', '')
  146. repeated_args = arg * int(dropdown_value)
  147. return '-' + repeated_args