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.

143 lines
3.5 KiB

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