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.

127 lines
3.9 KiB

3 years ago
3 years ago
3 years ago
  1. import wx # type: ignore
  2. from gooey.gui import formatters
  3. from gooey.gui.components.widgets.bases import TextContainer
  4. from gooey.gui.lang.i18n import _
  5. from gooey.gui.util import wx_util
  6. from gooey.python_bindings import types as t
  7. class CheckBox(TextContainer):
  8. widget_class = wx.CheckBox
  9. def arrange(self, *args, **kwargs):
  10. wx_util.make_bold(self.label)
  11. wx_util.withColor(self.label, self._options['label_color'])
  12. wx_util.withColor(self.help_text, self._options['help_color'])
  13. wx_util.withColor(self.error, self._options['error_color'])
  14. self.error.Hide()
  15. self.help_text.SetMinSize((0,-1))
  16. layout = wx.BoxSizer(wx.VERTICAL)
  17. if self._options.get('show_label', True):
  18. layout.Add(self.label, 0, wx.EXPAND)
  19. else:
  20. self.label.Show(False)
  21. layout.AddStretchSpacer(1)
  22. layout.AddSpacer(2)
  23. if self.help_text:
  24. hsizer = wx.BoxSizer(wx.HORIZONTAL)
  25. hsizer.Add(self.widget, 0)
  26. hsizer.Add(self.help_text, 1)
  27. layout.Add(hsizer, 1, wx.EXPAND)
  28. layout.AddSpacer(2)
  29. else:
  30. layout.Add(self.widget, 0, wx.EXPAND)
  31. layout.AddStretchSpacer(1)
  32. return layout
  33. def setValue(self, value):
  34. self.widget.SetValue(value)
  35. def getWidgetValue(self):
  36. return self.widget.GetValue()
  37. def formatOutput(self, metatdata, value):
  38. return formatters.checkbox(metatdata, value)
  39. def hideInput(self):
  40. self.widget.Hide()
  41. def getUiState(self) -> t.FormField:
  42. return t.Checkbox(
  43. id=self._id,
  44. type='Checkbox',
  45. checked=self.widget.GetValue(),
  46. error=self.error.GetLabel() or None, # type: ignore
  47. enabled=self.IsEnabled(),
  48. visible=self.IsShown()
  49. )
  50. def syncUiState(self, state: t.Checkbox): # type: ignore
  51. checkbox: wx.CheckBox = self.widget
  52. checkbox.SetValue(state['checked'])
  53. checkbox.Enable(state['enabled'])
  54. self.Show(state['visible'])
  55. self.error.SetLabel(state['error'] or '')
  56. self.error.Show(state['error'] is not None and state['error'] is not '')
  57. class BlockCheckbox(CheckBox):
  58. """
  59. A block style layout which places the help text in the normal
  60. location rather than inline next to the checkbox. A replacement label
  61. called `block_label` is shown next to the checkbox control.
  62. +-----------------+
  63. |label |
  64. |help_text |
  65. |[ ] block_label |
  66. +-----------------+
  67. This option tends to look better when there is a large amount of
  68. help text.
  69. """
  70. def arrange(self, *args, **kwargs):
  71. wx_util.make_bold(self.label)
  72. wx_util.withColor(self.label, self._options['label_color'])
  73. wx_util.withColor(self.help_text, self._options['help_color'])
  74. wx_util.withColor(self.error, self._options['error_color'])
  75. self.error.Hide()
  76. self.help_text.SetMinSize((0,-1))
  77. layout = wx.BoxSizer(wx.VERTICAL)
  78. if self._options.get('show_label', True):
  79. layout.Add(self.label, 0, wx.EXPAND)
  80. else:
  81. layout.AddStretchSpacer(1)
  82. layout.AddSpacer(2)
  83. if self.help_text and self._options.get('show_help', True):
  84. layout.Add(self.help_text, 1, wx.EXPAND)
  85. layout.AddSpacer(2)
  86. else:
  87. layout.AddStretchSpacer(1)
  88. layout.AddSpacer(2)
  89. block_label = self._options.get('checkbox_label', _('checkbox_label'))
  90. hsizer = wx.BoxSizer(wx.HORIZONTAL)
  91. hsizer.Add(self.widget, 0)
  92. hsizer.Add(wx.StaticText(self, label=block_label), 1)
  93. layout.Add(hsizer, 1, wx.EXPAND)
  94. layout.AddSpacer(2)
  95. return layout