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.

235 lines
7.3 KiB

  1. import wx
  2. from gooey.gui.util import wx_util
  3. from gooey.gui.widgets import widget_pack
  4. class BaseGuiComponent(object):
  5. def __init__(self, data, widget_pack):
  6. '''
  7. :param data: field info (title, help, etc..)
  8. :param widget_pack: internal wxWidgets to render
  9. '''
  10. self.data = data
  11. # parent
  12. self.panel = None
  13. # Widgets
  14. self.title = None
  15. self.help_msg = None
  16. # Internal WidgetPack
  17. self.widget_pack = widget_pack
  18. def build(self, parent):
  19. return self.do_layout(parent)
  20. def do_layout(self, parent):
  21. self.panel = wx.Panel(parent)
  22. self.title = self.createTitle(self.panel)
  23. self.help_msg = self.createHelpMsgWidget(self.panel)
  24. self.help_msg.SetMinSize((0, -1))
  25. core_widget_set = self.widget_pack.build(self.panel, self.data)
  26. vertical_container = wx.BoxSizer(wx.VERTICAL)
  27. vertical_container.Add(self.title)
  28. vertical_container.AddSpacer(2)
  29. if self.help_msg.GetLabelText():
  30. vertical_container.Add(self.help_msg, 1, wx.EXPAND)
  31. vertical_container.AddSpacer(2)
  32. else:
  33. vertical_container.AddStretchSpacer(1)
  34. vertical_container.Add(core_widget_set, 0, wx.EXPAND)
  35. self.panel.SetSizer(vertical_container)
  36. # self.panel.Bind(wx.EVT_SIZE, self.onResize)
  37. return self.panel
  38. def createHelpMsgWidget(self, parent):
  39. label_text = (self.formatExtendedHelpMsg(self.data)
  40. if self.data['nargs']
  41. else self.data['help'])
  42. base_text = wx.StaticText(parent, label=label_text or '')
  43. wx_util.dark_grey(base_text)
  44. return base_text
  45. def createTitle(self, parent):
  46. text = wx.StaticText(parent, label=self.data['display_name'].title())
  47. wx_util.make_bold(text)
  48. return text
  49. def formatExtendedHelpMsg(self, data):
  50. base_text = data.get('help', '')
  51. nargs = data['nargs']
  52. if isinstance(nargs, int):
  53. return '{base}\n(Note: exactly {nargs} arguments are required)'.format(base=base_text, nargs=nargs)
  54. elif nargs == '+':
  55. return '{base}\n(Note: at least 1 or more arguments are required)'.format(base=base_text)
  56. return base_text
  57. def onResize(self, evt):
  58. # handle internal widgets
  59. # self.panel.Freeze()
  60. self._onResize(evt)
  61. # propagate event to child widgets
  62. self.widget_pack.onResize(evt)
  63. evt.Skip()
  64. # self.panel.Thaw()
  65. def _onResize(self, evt):
  66. if not self.help_msg:
  67. return
  68. self.panel.Size = evt.GetSize()
  69. container_width, _ = self.panel.Size
  70. text_width, _ = self.help_msg.Size
  71. if text_width != container_width:
  72. self.help_msg.SetLabel(self.help_msg.GetLabelText().replace('\n', ' '))
  73. self.help_msg.Wrap(container_width)
  74. evt.Skip()
  75. def GetValue(self):
  76. return self.widget_pack.getValue()
  77. def _GetWidget(self):
  78. # used only for unittesting
  79. return self.widget_pack.widget
  80. def __repr__(self):
  81. return self.__class__.__name__
  82. class CheckBox(BaseGuiComponent):
  83. def __init__(self, data, widget_pack=None):
  84. BaseGuiComponent.__init__(self, data, widget_pack)
  85. self.widget = None
  86. # data
  87. self.option_strings = data['commands'][0]
  88. def build(self, parent):
  89. return self.do_layout(parent)
  90. def do_layout(self, parent):
  91. self.panel = wx.Panel(parent)
  92. self.widget = wx.CheckBox(self.panel)
  93. self.title = self.createTitle(self.panel)
  94. self.help_msg = self.createHelpMsgWidget(self.panel)
  95. self.help_msg.SetMinSize((0, -1))
  96. vertical_container = wx.BoxSizer(wx.VERTICAL)
  97. vertical_container.Add(self.title)
  98. horizontal_sizer = wx.BoxSizer(wx.HORIZONTAL)
  99. horizontal_sizer.Add(self.widget, 0, wx.EXPAND | wx.RIGHT, 10)
  100. horizontal_sizer.Add(self.help_msg, 1, wx.EXPAND)
  101. vertical_container.Add(horizontal_sizer, 0, wx.EXPAND)
  102. self.panel.SetSizer(vertical_container)
  103. self.panel.Bind(wx.EVT_SIZE, self.onResize)
  104. return self.panel
  105. def onSetter(self, evt):
  106. self.getValue()
  107. def onResize(self, evt):
  108. msg = self.help_msg
  109. container_width, _ = self.panel.Size
  110. text_width, _ = msg.Size
  111. if text_width != container_width:
  112. msg.SetLabel(msg.GetLabelText().replace('\n', ' '))
  113. msg.Wrap(container_width)
  114. evt.Skip()
  115. def GetValue(self):
  116. return self.option_strings if self.widget.GetValue() else ''
  117. def _GetWidget(self):
  118. return self.widget
  119. class RadioGroup(object):
  120. def __init__(self, data):
  121. self.panel = None
  122. self.data = data
  123. self.radio_buttons = []
  124. self.option_stings = []
  125. self.help_msgs = []
  126. self.btn_names = []
  127. def build(self, parent):
  128. return self.do_layout(parent)
  129. def do_layout(self, parent):
  130. self.panel = wx.Panel(parent)
  131. self.radio_buttons = [wx.RadioButton(self.panel, -1) for _ in self.data]
  132. self.btn_names = [wx.StaticText(self.panel, label=btn_data['display_name'].title()) for btn_data in self.data]
  133. self.help_msgs = [wx.StaticText(self.panel, label=btn_data['help'].title()) for btn_data in self.data]
  134. self.option_stings = [btn_data['commands'] for btn_data in self.data]
  135. # box = wx.StaticBox(self.panel, -1, label=self.data['group_name'])
  136. box = wx.StaticBox(self.panel, -1, label='Set Verbosity Level')
  137. vertical_container = wx.StaticBoxSizer(box, wx.VERTICAL)
  138. for button, name, help in zip(self.radio_buttons, self.btn_names, self.help_msgs):
  139. hbox = wx.BoxSizer(wx.HORIZONTAL)
  140. hbox.Add(button, 0, wx.ALIGN_TOP | wx.ALIGN_LEFT)
  141. hbox.Add(name, 0, wx.LEFT, 10)
  142. vertical_container.Add(hbox, 0, wx.EXPAND)
  143. vertical_container.Add(help, 1, wx.EXPAND | wx.LEFT, 25)
  144. vertical_container.AddSpacer(5)
  145. # self.panel.Bind(wx.EVT_RADIOBUTTON, self.onSetter, button)
  146. self.panel.SetSizer(vertical_container)
  147. self.panel.Bind(wx.EVT_SIZE, self.onResize)
  148. return self.panel
  149. def onSetter(self, evt):
  150. self.getValue()
  151. def onResize(self, evt):
  152. msg = self.help_msgs[0]
  153. container_width, _ = self.panel.Size
  154. text_width, _ = msg.Size
  155. if text_width != container_width:
  156. msg.SetLabel(msg.GetLabelText().replace('\n', ' '))
  157. msg.Wrap(container_width)
  158. evt.Skip()
  159. def GetValue(self):
  160. vals = [button.GetValue() for button in self.radio_buttons]
  161. try:
  162. return self.option_stings[vals.index(True)][0]
  163. except:
  164. return ''
  165. def _GetWidget(self):
  166. return self.radio_buttons
  167. FileChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.FileChooserPayload())
  168. MultiFileChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.MultiFileSaverPayload())
  169. DirChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DirChooserPayload())
  170. FileSaver = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.FileSaverPayload())
  171. DateChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DateChooserPayload())
  172. TextField = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.TextInputPayload())
  173. Dropdown = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DropdownPayload())
  174. Counter = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.CounterPayload())