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.

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