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.

241 lines
7.3 KiB

  1. from gooey.gui.widgets import widget_pack
  2. __author__ = 'Chris'
  3. import wx
  4. from gooey.gui import styling
  5. class BaseGuiComponent(object):
  6. def __init__(self, data, widget_pack):
  7. self.data = data
  8. # parent
  9. self.panel = None
  10. # Widgets
  11. self.title = None
  12. self.help_msg = None
  13. # Internal WidgetPack
  14. self.widget_pack = widget_pack
  15. # used to throttle resizing (to avoid widget jiggle)
  16. # TODO: figure out anti-jiggle technology
  17. # self.event_stack = []
  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. styling.MakeDarkGrey(base_text)
  44. return base_text
  45. def createTitle(self, parent):
  46. text = wx.StaticText(parent, label=self.data['display_name'].title())
  47. styling.MakeBold(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. class CheckBox(BaseGuiComponent):
  81. def __init__(self, data, widget_pack=None):
  82. BaseGuiComponent.__init__(self, data, widget_pack)
  83. self.widget = None
  84. # data
  85. self.option_strings = data['commands'][0]
  86. def build(self, parent):
  87. return self.do_layout(parent)
  88. def do_layout(self, parent):
  89. self.panel = wx.Panel(parent)
  90. self.widget = wx.CheckBox(self.panel)
  91. self.title = self.createTitle(self.panel)
  92. self.help_msg = self.createHelpMsgWidget(self.panel)
  93. self.help_msg.SetMinSize((0, -1))
  94. vertical_container = wx.BoxSizer(wx.VERTICAL)
  95. vertical_container.Add(self.title)
  96. horizontal_sizer = wx.BoxSizer(wx.HORIZONTAL)
  97. horizontal_sizer.Add(self.widget, 0, wx.EXPAND | wx.RIGHT, 10)
  98. horizontal_sizer.Add(self.help_msg, 1, wx.EXPAND)
  99. vertical_container.Add(horizontal_sizer, 0, wx.EXPAND)
  100. self.panel.SetSizer(vertical_container)
  101. self.panel.Bind(wx.EVT_SIZE, self.onResize)
  102. return self.panel
  103. def onSetter(self, evt):
  104. self.getValue()
  105. def onResize(self, evt):
  106. msg = self.help_msg
  107. container_width, _ = self.panel.Size
  108. text_width, _ = msg.Size
  109. if text_width != container_width:
  110. msg.SetLabel(msg.GetLabelText().replace('\n', ' '))
  111. msg.Wrap(container_width)
  112. evt.Skip()
  113. def GetValue(self):
  114. return self.option_strings if self.widget.GetValue() else ''
  115. def _GetWidget(self):
  116. return self.widget
  117. class RadioGroup(object):
  118. def __init__(self, data):
  119. self.panel = None
  120. self.data = data
  121. self.radio_buttons = []
  122. self.option_stings = []
  123. self.help_msgs = []
  124. self.btn_names = []
  125. def build(self, parent):
  126. return self.do_layout(parent)
  127. def do_layout(self, parent):
  128. self.panel = wx.Panel(parent)
  129. self.radio_buttons = [wx.RadioButton(self.panel, -1) for _ in self.data]
  130. self.btn_names = [wx.StaticText(self.panel, label=btn_data['display_name'].title()) for btn_data in self.data]
  131. self.help_msgs = [wx.StaticText(self.panel, label=btn_data['help'].title()) for btn_data in self.data]
  132. self.option_stings = [btn_data['commands'] for btn_data in self.data]
  133. # box = wx.StaticBox(self.panel, -1, label=self.data['group_name'])
  134. box = wx.StaticBox(self.panel, -1, label='Set Verbosity Level')
  135. vertical_container = wx.StaticBoxSizer(box, wx.VERTICAL)
  136. for button, name, help in zip(self.radio_buttons, self.btn_names, self.help_msgs):
  137. hbox = wx.BoxSizer(wx.HORIZONTAL)
  138. hbox.Add(button, 0, wx.ALIGN_TOP | wx.ALIGN_LEFT)
  139. hbox.Add(name, 0, wx.LEFT, 10)
  140. vertical_container.Add(hbox, 0, wx.EXPAND)
  141. vertical_container.Add(help, 1, wx.EXPAND | wx.LEFT, 25)
  142. vertical_container.AddSpacer(5)
  143. # self.panel.Bind(wx.EVT_RADIOBUTTON, self.onSetter, button)
  144. self.panel.SetSizer(vertical_container)
  145. self.panel.Bind(wx.EVT_SIZE, self.onResize)
  146. return self.panel
  147. def onSetter(self, evt):
  148. self.getValue()
  149. def onResize(self, evt):
  150. msg = self.help_msgs[0]
  151. container_width, _ = self.panel.Size
  152. text_width, _ = msg.Size
  153. if text_width != container_width:
  154. msg.SetLabel(msg.GetLabelText().replace('\n', ' '))
  155. msg.Wrap(container_width)
  156. evt.Skip()
  157. def GetValue(self):
  158. vals = [button.GetValue() for button in self.radio_buttons]
  159. # print self.option_stings[vals.index(True)]
  160. try:
  161. opts = self.option_stings[vals.index(True)][0]
  162. except:
  163. return ''
  164. def _GetWidget(self):
  165. return self.radio_buttons
  166. FileChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.FileChooserPayload())
  167. MultiFileChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.MultiFileSaverPayload())
  168. DirChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DirChooserPayload())
  169. FileSaver = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.FileSaverPayload())
  170. DateChooser = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DateChooserPayload())
  171. TextField = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.TextInputPayload())
  172. Dropdown = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.DropdownPayload())
  173. Counter = lambda data: BaseGuiComponent(data=data, widget_pack=widget_pack.CounterPayload())