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.

179 lines
5.8 KiB

  1. from functools import reduce
  2. import wx
  3. from gooey.gui import formatters, events
  4. from gooey.gui.util import wx_util
  5. from gooey.util.functional import getin, ifPresent
  6. from gooey.gui.validators import runValidator
  7. from gooey.gui.components.util.wrapped_static_text import AutoWrappedStaticText
  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 = AutoWrappedStaticText(self, label=widgetInfo['data']['help'] or '')
  36. self.error = AutoWrappedStaticText(self, label='')
  37. self.error.Hide()
  38. self.widget = self.getWidget(self)
  39. self.layout = self.arrange(*args, **kwargs)
  40. self.setColors()
  41. self.SetSizer(self.layout)
  42. self.Bind(wx.EVT_SIZE, self.onSize)
  43. if self._meta['default']:
  44. self.setValue(self._meta['default'])
  45. def arrange(self, *args, **kwargs):
  46. wx_util.make_bold(self.label)
  47. wx_util.withColor(self.label, self._options['label_color'])
  48. wx_util.withColor(self.help_text, self._options['help_color'])
  49. wx_util.withColor(self.error, self._options['error_color'])
  50. self.help_text.SetMinSize((0,-1))
  51. layout = wx.BoxSizer(wx.VERTICAL)
  52. if self._options.get('show_label', True):
  53. layout.Add(self.label, 0, wx.EXPAND)
  54. else:
  55. self.label.Show(False)
  56. layout.AddStretchSpacer(1)
  57. layout.AddSpacer(2)
  58. if self.help_text and self._options.get('show_help', True):
  59. layout.Add(self.help_text, 1, wx.EXPAND)
  60. layout.AddSpacer(2)
  61. else:
  62. self.help_text.Show(False)
  63. layout.AddStretchSpacer(1)
  64. layout.Add(self.getSublayout(), 0, wx.EXPAND)
  65. layout.Add(self.error, 1, wx.EXPAND)
  66. self.error.Hide()
  67. return layout
  68. def setColors(self):
  69. wx_util.make_bold(self.label)
  70. wx_util.withColor(self.label, self._options['label_color'])
  71. wx_util.withColor(self.help_text, self._options['help_color'])
  72. wx_util.withColor(self.error, self._options['error_color'])
  73. if self._options.get('label_bg_color'):
  74. self.label.SetBackgroundColour(self._options.get('label_bg_color'))
  75. if self._options.get('help_bg_color'):
  76. self.help_text.SetBackgroundColour(self._options.get('help_bg_color'))
  77. if self._options.get('error_bg_color'):
  78. self.error.SetBackgroundColour(self._options.get('error_bg_color'))
  79. def getWidget(self, *args, **options):
  80. return self.widget_class(*args, **options)
  81. def getWidgetValue(self):
  82. raise NotImplementedError
  83. def getSublayout(self, *args, **kwargs):
  84. layout = wx.BoxSizer(wx.HORIZONTAL)
  85. layout.Add(self.widget, 1, wx.EXPAND)
  86. return layout
  87. def onSize(self, event):
  88. # print(self.GetSize())
  89. # self.error.Wrap(self.GetSize().width)
  90. # self.help_text.Wrap(500)
  91. # self.Layout()
  92. event.Skip()
  93. def getValue(self):
  94. userValidator = getin(self._options, ['validator', 'test'], 'True')
  95. message = getin(self._options, ['validator', 'message'], '')
  96. testFunc = eval('lambda user_input: bool(%s)' % userValidator)
  97. satisfies = testFunc if self._meta['required'] else ifPresent(testFunc)
  98. value = self.getWidgetValue()
  99. return {
  100. 'id': self._id,
  101. 'cmd': self.formatOutput(self._meta, value),
  102. 'rawValue': value,
  103. 'test': runValidator(satisfies, value),
  104. 'error': None if runValidator(satisfies, value) else message,
  105. 'clitype': 'positional'
  106. if self._meta['required'] and not self._meta['commands']
  107. else 'optional'
  108. }
  109. def setValue(self, value):
  110. self.widget.SetValue(value)
  111. def setErrorString(self, message):
  112. self.error.SetLabel(message)
  113. self.error.Wrap(self.Size.width)
  114. self.Layout()
  115. def showErrorString(self, b):
  116. self.error.Wrap(self.Size.width)
  117. self.error.Show(b)
  118. def setOptions(self, values):
  119. return None
  120. def receiveChange(self, metatdata, value):
  121. raise NotImplementedError
  122. def dispatchChange(self, value, **kwargs):
  123. raise NotImplementedError
  124. def formatOutput(self, metadata, value):
  125. raise NotImplementedError
  126. class BaseChooser(TextContainer):
  127. """ Base Class for the Chooser widget types """
  128. def setValue(self, value):
  129. self.widget.setValue(value)
  130. def getWidgetValue(self):
  131. return self.widget.getValue()
  132. def formatOutput(self, metatdata, value):
  133. return formatters.general(metatdata, value)