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.

141 lines
3.4 KiB

  1. __author__ = 'Chris'
  2. import wx
  3. from gooey.gui import styling
  4. from gooey.gui.widgets.calender_dialog import CalendarDlg
  5. class AbstractChooser(object):
  6. def __init__(self, data):
  7. self.data = data
  8. # parent
  9. self.panel = None
  10. self.button_text = 'Browse'
  11. # Widgets
  12. self.title = None
  13. self.help_msg = None
  14. self.text_box = None
  15. self.button = None
  16. self.panel = None
  17. def build(self, parent):
  18. return self.do_layout(parent)
  19. def do_layout(self, parent):
  20. self.panel = wx.Panel(parent)
  21. self.title = self.CreateNameLabelWidget(self.panel)
  22. self.help_msg = self.CreateHelpMsgWidget(self.panel)
  23. self.text_box = wx.TextCtrl(self.panel)
  24. self.button = wx.Button(self.panel, label=self.button_text, size=(73, 23))
  25. vertical_container = wx.BoxSizer(wx.VERTICAL)
  26. widget_sizer = wx.BoxSizer(wx.HORIZONTAL)
  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. widget_sizer.Add(self.text_box, 1, wx.EXPAND)
  35. widget_sizer.AddSpacer(10)
  36. widget_sizer.Add(self.button, 0)
  37. vertical_container.Add(widget_sizer, 0, wx.EXPAND)
  38. self.panel.SetSizer(vertical_container)
  39. self.panel.Bind(wx.EVT_SIZE, self.OnResize)
  40. self.panel.Bind(wx.EVT_BUTTON, self.on_button, self.button)
  41. return self.panel
  42. def CreateHelpMsgWidget(self, parent):
  43. base_text = wx.StaticText(parent, label=self.data['help_msg'])
  44. # if self.data['nargs']:
  45. # base_text.SetLabelText(base_text.GetLabelText() + self.CreateNargsMsg(action))
  46. styling.MakeDarkGrey(base_text)
  47. return base_text
  48. def CreateNameLabelWidget(self, parent):
  49. label = self.data['title'].title()
  50. text = wx.StaticText(parent, label=label)
  51. styling.MakeBold(text)
  52. return text
  53. def OnResize(self, evt):
  54. if self.help_msg is None:
  55. return
  56. container_width, _ = self.panel.Size
  57. text_width, _ = self.help_msg.Size
  58. if text_width != container_width:
  59. self.help_msg.SetLabel(self.help_msg.GetLabelText().replace('\n', ' '))
  60. self.help_msg.Wrap(container_width)
  61. evt.Skip()
  62. def on_button(self, evt):
  63. raise NotImplementedError
  64. class FileChooser(AbstractChooser):
  65. def __init__(self, data):
  66. AbstractChooser.__init__(self, data)
  67. def on_button(self, evt):
  68. dlg = wx.FileDialog(self.panel, style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST)
  69. result = (dlg.GetPath()
  70. if dlg.ShowModal() == wx.ID_OK
  71. else None)
  72. if result:
  73. self.text_box.SetLabelText(result)
  74. class DirectoryChooser(AbstractChooser):
  75. def __init__(self, data):
  76. AbstractChooser.__init__(self, data)
  77. def on_button(self, evt):
  78. dlg = wx.DirDialog(self.panel, 'Select directory', style=wx.DD_DEFAULT_STYLE)
  79. result = (dlg.GetPath()
  80. if dlg.ShowModal() == wx.ID_OK
  81. else None)
  82. if result:
  83. self.text_box.SetLabelText(result)
  84. class CalendarChooser(AbstractChooser):
  85. def __init__(self, data):
  86. AbstractChooser.__init__(self, data)
  87. self.button_text = 'Choose Date'
  88. def on_button(self, evt):
  89. dlg = CalendarDlg(self.panel)
  90. dlg.ShowModal()
  91. if dlg.GetPath():
  92. self.text_box.SetLabelText(dlg.GetPath())