Files
MayoStainGuide/collaps2.py
2025-09-03 19:03:54 +02:00

76 lines
2.7 KiB
Python

from PySide6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QToolButton, QFrame, QLabel, QSizePolicy
)
from PySide6.QtCore import Qt, QSize
def hline():
line = QFrame()
line.setFrameShape(QFrame.HLine)
line.setFrameShadow(QFrame.Plain) # prosta, 1-pikselowa linia
line.setLineWidth(1)
return line
class CollapsiblePanel(QWidget):
def __init__(self, title="", content_margins=(12, 10, 12, 12), parent=None):
super().__init__(parent)
# Nagłówek
self.toggle_button = QToolButton(text=title, checkable=True, checked=False)
self.toggle_button.setStyleSheet("QToolButton { border: none; font-weight: 600;}")
self.toggle_button.setToolButtonStyle(Qt.ToolButtonTextBesideIcon)
self.toggle_button.setArrowType(Qt.RightArrow)
self.toggle_button.setIconSize(QSize(36, 36)) # domyślnie 16x16
self.toggle_button.clicked.connect(self.on_toggle)
# Obszar zawartości
self.content_area = QFrame()
self.content_area.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Fixed)
self.content_area.setMaximumHeight(0) # startowo schowane
# Wewnętrzny layout na Twoje widgety + PADDING (marginesy)
self.body_layout = QVBoxLayout(self.content_area)
l, t, r, b = content_margins
self.body_layout.setContentsMargins(l, t, r, b)
self.body_layout.setSpacing(6)
# Layout zewnętrzny panelu (bez dodatkowych marginesów)
root = QVBoxLayout(self)
root.setContentsMargins(0, 0, 0, 0)
root.setSpacing(0)
root.addWidget(self.toggle_button)
root.addWidget(self.content_area)
def on_toggle(self, checked):
if checked:
self.toggle_button.setArrowType(Qt.DownArrow)
self.content_area.setMaximumHeight(16777215)
else:
self.toggle_button.setArrowType(Qt.RightArrow)
self.content_area.setMaximumHeight(0)
if __name__ == "__main__":
app = QApplication([])
main = QWidget()
layout = QVBoxLayout(main)
# layout.setContentsMargins(50, 12, 12, 12) # marginesy całego widoku
layout.setSpacing(8) # odstęp m.in. na linię
panel1 = CollapsiblePanel("Opcje", content_margins=(50, 10, 12, 12))
panel1.body_layout.addWidget(QLabel("Opcja A"))
panel1.body_layout.addWidget(QLabel("Opcja B"))
panel2 = CollapsiblePanel("Zaawansowane", content_margins=(16, 12, 16, 16))
panel2.body_layout.addWidget(QLabel("Parametr 1"))
panel2.body_layout.addWidget(QLabel("Parametr 2"))
layout.addWidget(panel1)
layout.addWidget(hline()) # separator
layout.addWidget(panel2)
layout.addStretch()
main.show()
app.exec()