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.

125 lines
3.8 KiB

  1. """
  2. Created on Dec 28, 2013
  3. @author: Chris
  4. """
  5. from itertools import chain
  6. import wx
  7. from wx.lib.scrolledpanel import ScrolledPanel
  8. from gooey.gui import component_builder
  9. from gooey.gui.lang import i18n
  10. from gooey.gui.option_reader import OptionReader
  11. from gooey.gui import styling
  12. PADDING = 10
  13. class AdvancedConfigPanel(ScrolledPanel, OptionReader):
  14. """
  15. Abstract class for the Footer panels.
  16. """
  17. def __init__(self, parent, build_spec=None, **kwargs):
  18. ScrolledPanel.__init__(self, parent, **kwargs)
  19. self.SetupScrolling(scroll_x=False, scrollToTop=False)
  20. self.SetDoubleBuffered(True)
  21. self._action_groups = build_spec
  22. self._positionals = build_spec.get('required', None)
  23. self.components = component_builder.ComponentBuilder(build_spec)
  24. self._msg_req_args = None
  25. self._msg_opt_args = None
  26. self._controller = None
  27. self._init_components()
  28. self._do_layout()
  29. self.Bind(wx.EVT_SIZE, self.OnResize)
  30. def _init_components(self):
  31. self._msg_req_args = (styling.H1(self, i18n.translate("required_args_msg"))
  32. if self._positionals else None)
  33. self._msg_opt_args = styling.H1(self, i18n.translate("optional_args_msg"))
  34. def _do_layout(self):
  35. STD_LAYOUT = (0, wx.LEFT | wx.RIGHT | wx.EXPAND, PADDING)
  36. container = wx.BoxSizer(wx.VERTICAL)
  37. container.AddSpacer(15)
  38. if self._positionals:
  39. container.Add(self._msg_req_args, 0, wx.LEFT | wx.RIGHT, PADDING)
  40. container.AddSpacer(5)
  41. container.Add(styling.HorizontalRule(self), *STD_LAYOUT)
  42. container.AddSpacer(20)
  43. self.AddWidgets(container, self.components.required_args, add_space=True)
  44. container.AddSpacer(10)
  45. container.AddSpacer(10)
  46. container.Add(self._msg_opt_args, 0, wx.LEFT | wx.RIGHT, PADDING)
  47. container.AddSpacer(5)
  48. container.Add(styling.HorizontalRule(self), *STD_LAYOUT)
  49. container.AddSpacer(20)
  50. self.CreateComponentGrid(container, self.components.general_options, cols=2)
  51. self.CreateComponentGrid(container, self.components.flags, cols=3)
  52. # container.Add(general_opts_grid, *STD_LAYOUT)
  53. # container.AddSpacer(30)
  54. # container.Add(flag_grids, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, PADDING)
  55. self.SetSizer(container)
  56. def AddWidgets(self, sizer, components, add_space=False, padding=PADDING):
  57. for component in components:
  58. widget_group = component.build(parent=self)
  59. sizer.Add(widget_group, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, padding)
  60. if add_space:
  61. sizer.AddSpacer(8)
  62. def CreateComponentGrid(self, parent_sizer, components, cols=2):
  63. rows = [components[i:i+cols] for i in range(0, len(components), cols)]
  64. for row in rows:
  65. hsizer = wx.BoxSizer(wx.HORIZONTAL)
  66. for widget in row:
  67. hsizer.Add(widget.build(self), 1, wx.EXPAND | wx.LEFT | wx.RIGHT, 10)
  68. # hsizer.FitInside(parent_sizer)
  69. parent_sizer.Add(hsizer, 0, wx.EXPAND)
  70. parent_sizer.AddSpacer(20)
  71. def OnResize(self, evt):
  72. self.Freeze()
  73. for component in self.components:
  74. component.onResize(evt)
  75. self.SetupScrolling(scroll_x=False, scrollToTop=False)
  76. evt.Skip()
  77. self.Thaw()
  78. def RegisterController(self, controller):
  79. if self._controller is None:
  80. self._controller = controller
  81. def GetOptions(self):
  82. """
  83. returns the collective values from all of the
  84. widgets contained in the panel"""
  85. values = [c.GetValue()
  86. for c in self.components
  87. if c.GetValue() is not None]
  88. return ' '.join(values)
  89. def GetRequiredArgs(self):
  90. return [arg.GetValue() for arg in self.components.required_args]
  91. def GetOptionalArgs(self):
  92. return [arg.GetValue() for arg in
  93. chain(self.components.general_options, self.components.flags)]
  94. if __name__ == '__main__':
  95. pass