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.

151 lines
4.6 KiB

  1. import wx
  2. # from rx.subjects import Subject
  3. from gooey.gui import formatters, events
  4. from gooey.gui.pubsub import pub
  5. from gooey.gui.util import wx_util
  6. from gooey.util.functional import getin, ifPresent
  7. from gooey.gui.validators import runValidator
  8. class BaseWidget(wx.Panel):
  9. widget_class = None
  10. def arrange(self, label, text):
  11. raise NotImplementedError
  12. def getWidget(self, parent, **options):
  13. return self.widget_class(parent, **options)
  14. def connectSignal(self):
  15. raise NotImplementedError
  16. def getSublayout(self, *args, **kwargs):
  17. raise NotImplementedError
  18. def setValue(self, value):
  19. raise NotImplementedError
  20. def receiveChange(self, *args, **kwargs):
  21. raise NotImplementedError
  22. def dispatchChange(self, value, **kwargs):
  23. raise NotImplementedError
  24. def formatOutput(self, metatdata, value):
  25. raise NotImplementedError
  26. class TextContainer(BaseWidget):
  27. widget_class = None
  28. def __init__(self, parent, widgetInfo, *args, **kwargs):
  29. super(TextContainer, self).__init__(parent, *args, **kwargs)
  30. self.info = widgetInfo
  31. self._id = widgetInfo['id']
  32. self._meta = widgetInfo['data']
  33. self._options = widgetInfo['options']
  34. self.label = wx.StaticText(self, label=widgetInfo['data']['display_name'])
  35. self.help_text = wx.StaticText(self, label=widgetInfo['data']['help'] or '')
  36. self.error = wx.StaticText(self, label='')
  37. self.error.Hide()
  38. self.widget = self.getWidget(self)
  39. self.layout = self.arrange(*args, **kwargs)
  40. self.SetSizer(self.layout)
  41. self.Bind(wx.EVT_SIZE, self.onSize)
  42. if self._meta['default']:
  43. self.setValue(self._meta['default'])
  44. def arrange(self, *args, **kwargs):
  45. wx_util.make_bold(self.label)
  46. wx_util.dark_grey(self.help_text)
  47. wx_util.withColor(self.error, self._options['error_color'])
  48. self.help_text.SetMinSize((0,-1))
  49. layout = wx.BoxSizer(wx.VERTICAL)
  50. layout.Add(self.label)
  51. layout.AddSpacer(2)
  52. if self.help_text:
  53. layout.Add(self.help_text, 1, wx.EXPAND)
  54. layout.AddSpacer(2)
  55. else:
  56. layout.AddStretchSpacer(1)
  57. layout.Add(self.getSublayout(), 0, wx.EXPAND)
  58. layout.Add(self.error)
  59. self.error.Hide()
  60. return layout
  61. def getWidget(self, *args, **options):
  62. return self.widget_class(*args, **options)
  63. def getWidgetValue(self):
  64. raise NotImplementedError
  65. def getSublayout(self, *args, **kwargs):
  66. layout = wx.BoxSizer(wx.HORIZONTAL)
  67. layout.Add(self.widget, 1, wx.EXPAND)
  68. return layout
  69. def onSize(self, event):
  70. self.error.Wrap(self.GetSize().width)
  71. event.Skip()
  72. def getValue(self):
  73. userValidator = getin(self._options, ['validator', 'test'], 'True')
  74. message = getin(self._options, ['validator', 'message'], '')
  75. testFunc = eval('lambda user_input: bool(%s)' % userValidator)
  76. satisfies = testFunc if self._meta['required'] else ifPresent(testFunc)
  77. value = self.getWidgetValue()
  78. return {
  79. 'id': self._id,
  80. 'cmd': self.formatOutput(self._meta, value),
  81. 'rawValue': value,
  82. 'test': runValidator(satisfies, value),
  83. 'error': None if runValidator(satisfies, value) else message,
  84. 'clitype': 'positional'
  85. if self._meta['required'] and not self._meta['commands']
  86. else 'optional'
  87. }
  88. def setValue(self, value):
  89. self.widget.SetValue(value)
  90. def setErrorString(self, message):
  91. self.error.SetLabel(message)
  92. self.error.Wrap(self.Size.width)
  93. self.Layout()
  94. def showErrorString(self, b):
  95. self.error.Wrap(self.Size.width)
  96. self.error.Show(b)
  97. def setOptions(self, values):
  98. return None
  99. def receiveChange(self, metatdata, value):
  100. raise NotImplementedError
  101. def dispatchChange(self, value, **kwargs):
  102. raise NotImplementedError
  103. def formatOutput(self, metadata, value):
  104. raise NotImplementedError
  105. class BaseChooser(TextContainer):
  106. """ Base Class for the Chooser widget types """
  107. def setValue(self, value):
  108. self.widget.setValue(value)
  109. def getWidgetValue(self):
  110. return self.widget.getValue()
  111. def formatOutput(self, metatdata, value):
  112. return formatters.general(metatdata, value)