📄 foldpanelbar.py
字号:
self._LastInsertPos = self._PanelSize
self._items = []
self.Bind(EVT_CAPTIONBAR, self.OnPressCaption)
self.Bind(wx.EVT_PAINT, self.OnPaint)
def AddWindow(self, window, flags=FPB_ALIGN_WIDTH, Spacing=FPB_DEFAULT_SPACING,
leftSpacing=FPB_DEFAULT_LEFTLINESPACING,
rightSpacing=FPB_DEFAULT_RIGHTLINESPACING):
"""
Adds a window item to the list of items on this panel.
The flags are FPB_ALIGN_LEFT for a non sizing window element,
and FPB_ALIGN_WIDTH for a width aligned item. The Spacing
parameter reserves a number of pixels before the window
element, and leftSpacing is an indent. rightSpacing is only
relevant when the style FPB_ALIGN_WIDTH is chosen.
"""
wi = FoldWindowItem(self, window, Type="WINDOW", flags=flags, Spacing=Spacing,
leftSpacing=leftSpacing, rightSpacing=rightSpacing)
self._items.append(wi)
vertical = self.IsVertical()
self._Spacing = Spacing
self._leftSpacing = leftSpacing
self._rightSpacing = rightSpacing
xpos = (vertical and [leftSpacing] or [self._LastInsertPos + Spacing])[0]
ypos = (vertical and [self._LastInsertPos + Spacing] or [leftSpacing])[0]
window.SetDimensions(xpos, ypos, -1, -1, wx.SIZE_USE_EXISTING)
self._LastInsertPos = self._LastInsertPos + wi.GetWindowLength(vertical)
self.ResizePanel()
def AddSeparator(self, colour=wx.BLACK, Spacing=FPB_DEFAULT_SPACING,
leftSpacing=FPB_DEFAULT_LEFTSPACING,
rightSpacing=FPB_DEFAULT_RIGHTSPACING):
"""
Adds a separator item to the list of items on this panel. """
wi = FoldWindowItem(self, window=None, Type="SEPARATOR",
flags=FPB_ALIGN_WIDTH, y=self._LastInsertPos,
colour=colour, Spacing=Spacing, leftSpacing=leftSpacing,
rightSpacing=rightSpacing)
self._items.append(wi)
self._LastInsertPos = self._LastInsertPos + \
wi.GetWindowLength(self.IsVertical())
self.ResizePanel()
def Reposition(self, pos):
"""
Repositions this FoldPanelBar and reports the length occupied
for the next FoldPanelBar in the list.
"""
# NOTE: Call Resize before Reposition when an item is added, because the new
# size needed will be calculated by Resize. Of course the relative position
# of the controls have to be correct in respect to the caption bar
self.Freeze()
vertical = self.IsVertical()
xpos = (vertical and [-1] or [pos])[0]
ypos = (vertical and [pos] or [-1])[0]
self.SetDimensions(xpos, ypos, -1, -1, wx.SIZE_USE_EXISTING)
self._itemPos = pos
self.Thaw()
return self.GetPanelLength()
def OnPressCaption(self, event):
""" Handles the EVT_CAPTIONBAR event in the FoldPanelItem. """
# tell the upper container we are responsible
# for this event, so it can fold the panel item
# and do a refresh
event.SetTag(self)
event.Skip()
def ResizePanel(self):
""" Resizes the panel. """
# prevent unnecessary updates by blocking repaints for a sec
self.Freeze()
vertical = self.IsVertical()
# force this panel to take the width of the parent panel and the y of the
# user or calculated width (which will be recalculated by the contents here)
if self._captionBar.IsCollapsed():
size = self._captionBar.GetSize()
self._PanelSize = (vertical and [size.GetHeight()] or [size.GetWidth()])[0]
else:
size = self.GetBestSize()
self._PanelSize = (vertical and [size.GetHeight()] or [size.GetWidth()])[0]
if self._UserSize:
if vertical:
size.SetHeight(self._UserSize)
else:
size.SetWidth(self._UserSize)
pnlsize = self.GetParent().GetSize()
if vertical:
size.SetWidth(pnlsize.GetWidth())
else:
size.SetHeight(pnlsize.GetHeight())
# resize caption bar
xsize = (vertical and [size.GetWidth()] or [-1])[0]
ysize = (vertical and [-1] or [size.GetHeight()])[0]
self._captionBar.SetSize((xsize, ysize))
# resize the panel
self.SetSize(size)
# go by all the controls and call Layout
for items in self._items:
items.ResizeItem((vertical and [size.GetWidth()] or \
[size.GetHeight()])[0], vertical)
self.Thaw()
def OnPaint(self, event):
""" Handles the EVT_PAINT event in the FoldPanelItem. """
# draw all the items that are lines
dc = wx.PaintDC(self)
vertical = self.IsVertical()
for item in self._items:
if item.GetType() == "SEPARATOR":
pen = wx.Pen(item.GetLineColour(), 1, wx.SOLID)
dc.SetPen(pen)
a = item.GetLeftSpacing()
b = item.GetLineY() + item.GetSpacing()
c = item.GetLineLength()
d = a + c
if vertical:
dc.DrawLine(a, b, d, b)
else:
dc.DrawLine(b, a, b, d)
event.Skip()
def IsVertical(self):
"""
Returns wether the CaptionBar Has Default Orientation Or Not.
Default is vertical.
"""
# grandparent of FoldPanelItem is FoldPanelBar
# default is vertical
if isinstance(self.GetGrandParent(), FoldPanelBar):
return self.GetGrandParent().IsVertical()
else:
raise "ERROR: Wrong Parent " + repr(self.GetGrandParent())
def IsExpanded(self):
"""
Returns expanded or collapsed status. If the panel is
expanded, True is returned.
"""
return not self._captionBar.IsCollapsed()
def GetItemPos(self):
""" Returns item's position. """
return self._itemPos
def Collapse(self):
# this should not be called by the user, because it doesn't trigger the
# parent to tell it that we are collapsed or expanded, it only changes
# visual state
self._captionBar.Collapse()
self.ResizePanel()
def Expand(self):
# this should not be called by the user, because it doesn't trigger the
# parent to tell it that we are collapsed or expanded, it only changes
# visual state
self._captionBar.Expand()
self.ResizePanel()
def GetPanelLength(self):
""" Returns size of panel. """
if self._captionBar.IsCollapsed():
return self.GetCaptionLength()
elif self._userSized:
return self._UserSize
return self._PanelSize
def GetCaptionLength(self):
"""
Returns height of caption only. This is for folding
calculation purposes.
"""
size = self._captionBar.GetSize()
return (self.IsVertical() and [size.GetHeight()] or [size.GetWidth()])[0]
def ApplyCaptionStyle(self, cbstyle):
""" Applies the style defined in cbstyle to the CaptionBar."""
self._captionBar.SetCaptionStyle(cbstyle)
def GetCaptionStyle(self):
"""
Returns the current style of the captionbar in a
CaptionBarStyle class.
This can be used to change and set back the changes.
"""
return self._captionBar.GetCaptionStyle()
# ----------------------------------------------------------------------------------- #
# class FoldWindowItem
# ----------------------------------------------------------------------------------- #
class FoldWindowItem:
"""
This class is a child sibling of the `FoldPanelItem` class. It
will contain wx.Window that can be either a separator (a colored
line simulated by a wx.Window) or a wxPython controls (such as a
wx.Button, a wx.ListCtrl etc...).
"""
def __init__(self, parent, window=None, **kw):
"""
Default Class Constructor
Initialize with::
Type = "WINDOW", flags = FPB_ALIGN_WIDTH,
Spacing = FPB_DEFAULT_SPACING,
leftSpacing = FPB_DEFAULT_LEFTSPACING,
rightSpacing = FPB_DEFAULT_RIGHTSPACING
or::
Type = "SEPARATOR"
y, lineColor = wx.BLACK,
flags = FPB_ALIGN_WIDTH,
Spacing = FPB_DEFAULT_SPACING,
leftSpacing = FPB_DEFAULT_LEFTLINESPACING,
rightSpacing = FPB_DEFAULT_RIGHTLINESPACING
"""
if not kw.has_key("Type"):
raise 'ERROR: Missing Window Type Information. This Should Be "WINDOW" Or "SEPARATOR"'
if kw.get("Type") == "WINDOW":
# Window constructor. This initialises the class as a wx.Window Type
if kw.has_key("flags"):
self._flags = kw.get("flags")
else:
self._flags = FPB_ALIGN_WIDTH
if kw.has_key("Spacing"):
self._Spacing = kw.get("Spacing")
else:
self._Spacing = FPB_DEFAULT_SPACING
if kw.has_key("leftSpacing"):
self._leftSpacing = kw.get("leftSpacing")
else:
self._leftSpacing = FPB_DEFAULT_LEFTSPACING
if kw.has_key("rightSpacing"):
self._rightSpacing = kw.get("rightSpacing")
else:
self._rightSpacing = FPB_DEFAULT_RIGHTSPACING
self._lineY = 0
self._sepLineColour = None
self._wnd = window
elif kw.get("Type") == "SEPARATOR":
# separator constructor. This initialises the class as a separator type
if kw.has_key("y"):
self._lineY = kw.get("y")
else:
raise "ERROR: Undefined Y Position For The Separator"
if kw.has_key("lineColour"):
self._sepLineColour = kw.get("lineColour")
else:
self._sepLineColour = wx.BLACK
if kw.has_key("flags"):
self._flags = kw.get("flags")
else:
self._flags = FPB_ALIGN_WIDTH
if kw.has_key("Spacing"):
self._Spacing = kw.get("Spacing")
else:
self._Spacing = FPB_DEFAULT_SPACING
if kw.has_key("leftSpacing"):
self._leftSpacing = kw.get("leftSpacing")
else:
self._leftSpacing = FPB_DEFAULT_LEFTSPACING
if kw.has_key("rightSpacing"):
self._rightSpacing = kw.get("rightSpacing")
else:
self._rightSpacing = FPB_DEFAULT_RIGHTSPACING
self._wnd = window
else:
raise "ERROR: Undefined Window Type Selected: " + repr(kw.get("Type"))
self._type = kw.get("Type")
self._lineLength = 0
def GetType(self):
return self._type
def GetLineY(self):
return self._lineY
def GetLineLength(self):
return self._lineLength
def GetLineColour(self):
return self._sepLineColour
def GetLeftSpacing(self):
return self._leftSpacing
def GetRightSpacing(self):
return self._rightSpacing
def GetSpacing(self):
return self._Spacing
def GetWindowLength(self, vertical=True):
"""
Returns space needed by the window if type is FoldWindowItem
"WINDOW" and returns
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -