manuskript/manuskript/ui/collapsibleGroupBox.py

126 lines
4.8 KiB
Python
Raw Normal View History

2015-06-01 08:41:32 +12:00
#!/usr/bin/env python
2016-02-07 00:34:22 +13:00
# --!-- coding: utf8 --!--
from PyQt5.QtCore import Qt, QRect, QRectF
from PyQt5.QtGui import QColor, QBrush, QRegion, QTextOption, QFont
from PyQt5.QtWidgets import QSizePolicy, QGroupBox, QWidget, QStylePainter, QStyleOptionGroupBox, qApp, QVBoxLayout, \
QStyle, QStyleOptionFrame, QStyleOptionFocusRect
2015-06-08 08:06:57 +12:00
2015-06-01 08:41:32 +12:00
class collapsibleGroupBox(QGroupBox):
def __init__(self, parent=None):
QGroupBox.__init__(self)
2016-02-07 00:34:22 +13:00
2015-06-01 08:41:32 +12:00
self.toggled.connect(self.setExpanded)
self.tempWidget = QWidget()
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
self.customStyle = False
2016-02-07 00:34:22 +13:00
2015-06-01 08:41:32 +12:00
def setExpanded(self, val):
self.setCollapsed(not val)
2016-02-07 00:34:22 +13:00
2015-06-01 08:41:32 +12:00
def setCollapsed(self, val):
if val:
# Save layout
self.tempWidget.setLayout(self.layout())
# Set empty layout
l = QVBoxLayout()
2016-02-07 00:34:22 +13:00
# print(l.contentsMargins().left(), l.contentsMargins().bottom(), l.contentsMargins().top(), )
2015-06-01 08:41:32 +12:00
l.setContentsMargins(0, 0, 0, 0)
self.setLayout(l)
2015-06-07 00:21:48 +12:00
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Maximum)
2015-06-01 08:41:32 +12:00
else:
# Delete layout
QWidget().setLayout(self.layout())
# Set saved layout
self.setLayout(self.tempWidget.layout())
2015-06-07 00:21:48 +12:00
self.setSizePolicy(QSizePolicy.Preferred, QSizePolicy.Preferred)
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
def paintEvent(self, event):
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
if not self.customStyle:
return QGroupBox.paintEvent(self, event)
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
p = QStylePainter(self)
opt = QStyleOptionGroupBox()
self.initStyleOption(opt)
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
style = qApp.style()
groupBox = opt
2016-02-07 00:34:22 +13:00
# // Draw frame
2015-06-07 00:21:48 +12:00
textRect = style.subControlRect(style.CC_GroupBox, opt, style.SC_GroupBoxLabel)
checkBoxRect = style.subControlRect(style.CC_GroupBox, opt, style.SC_GroupBoxCheckBox)
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
p.save()
titleRect = style.subControlRect(style.CC_GroupBox, opt, style.SC_GroupBoxFrame)
2016-02-07 00:34:22 +13:00
# r.setBottom(style.subControlRect(style.CC_GroupBox, opt, style.SC_GroupBoxContents).top())
2015-06-07 00:21:48 +12:00
titleRect.setHeight(textRect.height())
titleRect.moveTop(textRect.top())
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
p.setBrush(QBrush(QColor(Qt.blue).lighter(190)))
p.setPen(Qt.NoPen)
p.drawRoundedRect(titleRect, 10, 10)
p.restore()
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
if groupBox.subControls & QStyle.SC_GroupBoxFrame:
frame = QStyleOptionFrame()
2016-02-07 00:34:22 +13:00
# frame.operator=(groupBox)
2015-06-07 00:21:48 +12:00
frame.state = groupBox.state
frame.features = groupBox.features
frame.lineWidth = groupBox.lineWidth
frame.midLineWidth = groupBox.midLineWidth
frame.rect = style.subControlRect(style.CC_GroupBox, opt, style.SC_GroupBoxFrame)
p.save()
region = QRegion(groupBox.rect)
if groupBox.text:
ltr = groupBox.direction == Qt.LeftToRight
finalRect = QRect()
if groupBox.subControls & QStyle.SC_GroupBoxCheckBox:
finalRect = checkBoxRect.united(textRect)
finalRect.adjust(-4 if ltr else 0, 0, 0 if ltr else 4, 0)
else:
finalRect = textRect
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
region -= QRegion(finalRect)
2016-02-07 00:34:22 +13:00
2015-06-07 00:21:48 +12:00
p.setClipRegion(region)
style.drawPrimitive(style.PE_FrameGroupBox, frame, p)
p.restore()
2016-02-07 00:34:22 +13:00
# // Draw title
2015-06-07 00:21:48 +12:00
if groupBox.subControls & QStyle.SC_GroupBoxLabel and groupBox.text:
2016-02-07 00:34:22 +13:00
# textColor = QColor(groupBox.textColor)
# if textColor.isValid():
# p.setPen(textColor)
# alignment = int(groupBox.textAlignment)
# if not style.styleHint(QStyle.SH_UnderlineShortcut, opt):
# alignment |= Qt.TextHideMnemonic
# style.drawItemText(p, textRect, Qt.TextShowMnemonic | Qt.AlignHCenter | alignment,
# groupBox.palette, groupBox.state & style.State_Enabled, groupBox.text,
# QPalette.NoRole if textColor.isValid() else QPalette.WindowText)
2015-06-07 00:21:48 +12:00
p.save()
topt = QTextOption(Qt.AlignHCenter | Qt.AlignVCenter)
f = QFont()
f.setBold(True)
p.setFont(f)
p.setPen(Qt.darkBlue)
p.drawText(QRectF(titleRect), groupBox.text.replace("&", ""), topt)
p.restore()
if groupBox.state & style.State_HasFocus:
fropt = QStyleOptionFocusRect()
2016-02-07 00:34:22 +13:00
# fropt.operator=(groupBox)
2015-06-07 00:21:48 +12:00
fropt.state = groupBox.state
fropt.rect = textRect
style.drawPrimitive(style.PE_FrameFocusRect, fropt, p)
2016-02-07 00:34:22 +13:00
# // Draw checkbox
# if groupBox.subControls & style.SC_GroupBoxCheckBox:
# box = QStyleOptionButton()
# box.operator=(groupBox)
# box.state = groupBox.state
# box.rect = checkBoxRect
# style.drawPrimitive(style.PE_IndicatorCheckBox, box, p)