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.

86 lines
2.8 KiB

  1. import wx
  2. from gooey.gui.util import wx_util
  3. class Sidebar(wx.Panel):
  4. """
  5. Sidebar handles the show/hide logic so that it mirrors the functionality
  6. of the wx.Notebook class (which wants to control everything)
  7. """
  8. def __init__(self, parent, buildSpec, configPanels, *args, **kwargs):
  9. super(Sidebar, self).__init__(parent, *args, **kwargs)
  10. self._parent = parent
  11. self.buildSpec = buildSpec
  12. self.configPanels = configPanels
  13. self.activeSelection = 0
  14. self.options = list(self.buildSpec['widgets'].keys())
  15. self.leftPanel = wx.Panel(self)
  16. self.label = wx_util.h1(self.leftPanel, self.buildSpec.get('sidebar_title'))
  17. self.listbox = wx.ListBox(self.leftPanel, -1, choices=self.options)
  18. self.Bind(wx.EVT_LISTBOX, self.swapConfigPanels, self.listbox)
  19. self.layoutComponent()
  20. self.listbox.SetSelection(0)
  21. def getSelectedGroup(self):
  22. """Return the currently active 'group' i.e. the root SubParser """
  23. return self.options[self.activeSelection]
  24. def getActiveConfig(self):
  25. """Return the currently visible config screen"""
  26. return self.configPanels[self.activeSelection]
  27. def swapConfigPanels(self, event):
  28. """Hide/show configuration panels based on the currently selected
  29. option in the sidebar """
  30. for id, panel in enumerate(self.configPanels):
  31. panel.Hide()
  32. self.activeSelection = event.Selection
  33. self.configPanels[event.Selection].Show()
  34. self._parent.Layout()
  35. def layoutComponent(self):
  36. left = self.layoutLeftSide()
  37. hsizer = wx.BoxSizer(wx.HORIZONTAL)
  38. hsizer.Add(left, 0, wx.EXPAND)
  39. if not self.buildSpec['tabbed_groups']:
  40. # only add it for non-tabbed layouts as it looks
  41. # weird against the tabbed ones
  42. hsizer.Add(wx_util.vertical_rule(self), 0, wx.EXPAND)
  43. for body in self.configPanels:
  44. body.Reparent(self)
  45. hsizer.Add(body, 1, wx.EXPAND)
  46. body.Hide()
  47. self.configPanels[0].Show()
  48. self.SetSizer(hsizer)
  49. if not self.buildSpec['show_sidebar']:
  50. left.Show(False)
  51. self.Layout()
  52. def layoutLeftSide(self):
  53. self.leftPanel.SetBackgroundColour(self.buildSpec['sidebar_bg_color'])
  54. self.leftPanel.SetSize((180, 0))
  55. self.leftPanel.SetMinSize((180, 0))
  56. container = wx.BoxSizer(wx.VERTICAL)
  57. container.AddSpacer(15)
  58. container.Add(self.label, 0, wx.LEFT | wx.RIGHT | wx.EXPAND, 10)
  59. container.AddSpacer(5)
  60. container.Add(self.listbox, 1, wx.LEFT | wx.RIGHT | wx.EXPAND, 10)
  61. container.AddSpacer(20)
  62. self.leftPanel.SetSizer(container)
  63. return self.leftPanel