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
6.9 KiB

  1. from functools import partial
  2. import wx
  3. from gooey.gui.util import wx_util
  4. from gooey.gui.widgets import widget_pack
  5. class BaseGuiComponent(object):
  6. widget_class = None
  7. def __init__(self, parent, title, msg):
  8. '''
  9. :param data: field info (title, help, etc..)
  10. :param widget_pack: internal wxWidgets to render
  11. '''
  12. # parent
  13. self.parent = parent
  14. # Widgets
  15. self.title = None
  16. self.help_msg = None
  17. # Internal WidgetPack set in subclasses
  18. self.do_layout(parent, title, msg)
  19. def do_layout(self, parent, title, msg):
  20. self.panel = wx.Panel(parent)
  21. self.widget_pack = self.widget_class()
  22. self.title = self.format_title(self.panel, title)
  23. self.help_msg = self.format_help_msg(self.panel, msg)
  24. self.help_msg.SetMinSize((0, -1))
  25. core_widget_set = self.widget_pack.build(self.panel, {})
  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. return self.panel
  37. def bind(self, *args, **kwargs):
  38. print self.widget_pack.widget.Bind(*args, **kwargs)
  39. def get_title(self):
  40. return self.title.GetLabel()
  41. def set_title(self, text):
  42. self.title.SetLabel(text)
  43. def get_help_msg(self):
  44. return self.help_msg.GetLabelText()
  45. def set_label_text(self, text):
  46. self.help_msg.SetLabel(text)
  47. def format_help_msg(self, parent, msg):
  48. base_text = wx.StaticText(parent, label=msg or '')
  49. wx_util.dark_grey(base_text)
  50. return base_text
  51. def format_title(self, parent, title):
  52. text = wx.StaticText(parent, label=title)
  53. wx_util.make_bold(text)
  54. return text
  55. def onResize(self, evt):
  56. # handle internal widgets
  57. # self.panel.Freeze()
  58. self._onResize(evt)
  59. # propagate event to child widgets
  60. self.widget_pack.onResize(evt)
  61. evt.Skip()
  62. # self.panel.Thaw()
  63. def _onResize(self, evt):
  64. if not self.help_msg:
  65. return
  66. self.panel.Size = evt.GetSize()
  67. container_width, _ = self.panel.Size
  68. text_width, _ = self.help_msg.Size
  69. if text_width != container_width:
  70. self.help_msg.SetLabel(self.help_msg.GetLabelText().replace('\n', ' '))
  71. self.help_msg.Wrap(container_width)
  72. evt.Skip()
  73. def get_value(self):
  74. return self.widget_pack.get_value()
  75. def set_value(self, val):
  76. if val:
  77. self.widget_pack.widget.SetValue(str(val))
  78. def __repr__(self):
  79. return self.__class__.__name__
  80. class CheckBox(BaseGuiComponent):
  81. def __init__(self, parent, title, msg):
  82. BaseGuiComponent.__init__(self, parent, title, msg)
  83. def do_layout(self, parent, title, msg):
  84. self.panel = wx.Panel(parent)
  85. self.widget = wx.CheckBox(self.panel)
  86. # self.widget.SetValue(self.default_value)
  87. self.title = self.format_title(self.panel, title)
  88. self.help_msg = self.format_help_msg(self.panel, msg)
  89. self.help_msg.SetMinSize((0, -1))
  90. # self.help_msg.Bind(wx.EVT_LEFT_UP, lambda event: self.widget.SetValue(not self.widget.GetValue()))
  91. vertical_container = wx.BoxSizer(wx.VERTICAL)
  92. vertical_container.Add(self.title)
  93. horizontal_sizer = wx.BoxSizer(wx.HORIZONTAL)
  94. horizontal_sizer.Add(self.widget, 0, wx.EXPAND | wx.RIGHT, 10)
  95. horizontal_sizer.Add(self.help_msg, 1, wx.EXPAND)
  96. vertical_container.Add(horizontal_sizer, 0, wx.EXPAND)
  97. self.panel.SetSizer(vertical_container)
  98. self.panel.Bind(wx.EVT_SIZE, self.onResize)
  99. return self.panel
  100. def onResize(self, evt):
  101. msg = self.help_msg
  102. container_width, _ = self.panel.Size
  103. text_width, _ = msg.Size
  104. if text_width != container_width:
  105. msg.SetLabel(msg.GetLabelText().replace('\n', ' '))
  106. msg.Wrap(container_width)
  107. evt.Skip()
  108. def get_value(self):
  109. return self.widget.GetValue()
  110. def set_value(self, val):
  111. self.widget.SetValue(val)
  112. class RadioGroup(object):
  113. def __init__(self, parent, title, msg):
  114. self.panel = None
  115. self.radio_buttons = []
  116. self.option_strings = []
  117. self.help_msgs = []
  118. self.btn_names = []
  119. self.do_layout(parent, title, msg)
  120. def do_layout(self, parent, titles, msgs):
  121. self.panel = wx.Panel(parent)
  122. self.radio_buttons = [wx.RadioButton(self.panel, -1) for _ in titles]
  123. self.btn_names = [wx.StaticText(self.panel, label=title.title()) for title in titles]
  124. self.help_msgs = [wx.StaticText(self.panel, label=msg.title()) for msg in msgs]
  125. # box = wx.StaticBox(self.panel, -1, label=self.data['group_name'])
  126. box = wx.StaticBox(self.panel, -1, label='')
  127. vertical_container = wx.StaticBoxSizer(box, wx.VERTICAL)
  128. for button, name, help in zip(self.radio_buttons, self.btn_names, self.help_msgs):
  129. hbox = wx.BoxSizer(wx.HORIZONTAL)
  130. hbox.Add(button, 0, wx.ALIGN_TOP | wx.ALIGN_LEFT)
  131. hbox.Add(name, 0, wx.LEFT, 10)
  132. vertical_container.Add(hbox, 0, wx.EXPAND)
  133. vertical_container.Add(help, 1, wx.EXPAND | wx.LEFT, 25)
  134. vertical_container.AddSpacer(5)
  135. self.panel.SetSizer(vertical_container)
  136. self.panel.Bind(wx.EVT_SIZE, self.onResize)
  137. self.panel.Bind(wx.EVT_RADIOBUTTON, self.showz)
  138. return self.panel
  139. def showz(self, evt):
  140. print evt
  141. for i in self.radio_buttons:
  142. print i.GetValue()
  143. def onResize(self, evt):
  144. msg = self.help_msgs[0]
  145. container_width, _ = self.panel.Size
  146. text_width, _ = msg.Size
  147. if text_width != container_width:
  148. msg.SetLabel(msg.GetLabelText().replace('\n', ' '))
  149. msg.Wrap(container_width)
  150. evt.Skip()
  151. def get_value(self):
  152. return [button.GetValue() for button in self.radio_buttons]
  153. def set_value(self, val):
  154. pass
  155. def build_subclass(name, widget_class):
  156. # this seemed faster than typing class X a bunch
  157. return type(name, (BaseGuiComponent,), {'widget_class': widget_class})
  158. FileChooser = build_subclass('FileChooser', widget_pack.FileChooserPayload)
  159. MultiFileChooser = build_subclass('MultiFileChooser', widget_pack.MultiFileSaverPayload)
  160. DirChooser = build_subclass('DirChooser', widget_pack.DirChooserPayload)
  161. FileSaver = build_subclass('FileSaver', widget_pack.FileSaverPayload)
  162. DateChooser = build_subclass('DateChooser', widget_pack.DateChooserPayload)
  163. TextField = build_subclass('TextField', widget_pack.TextInputPayload)
  164. CommandField = build_subclass('CommandField', widget_pack.TextInputPayload(no_quoting=True))
  165. Dropdown = build_subclass('Dropdown', widget_pack.DropdownPayload)
  166. Counter = build_subclass('Counter', widget_pack.CounterPayload)
  167. MultiDirChooser = build_subclass('MultiDirChooser', widget_pack.MultiDirChooserPayload)