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.

194 lines
4.9 KiB

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