163 lines
5.1 KiB
Python
163 lines
5.1 KiB
Python
# Copyright (C) 2026 Seth Hillbrand
|
|
#
|
|
# This program is free software: you can redistribute it and/or modify
|
|
# it under the terms of the GNU General Public License as published by
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
# (at your option) any later version.
|
|
#
|
|
# This program is distributed in the hope that it will be useful,
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
# GNU General Public License for more details.
|
|
#
|
|
# You should have received a copy of the GNU General Public License
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
"""Collapsible section widget with animated expand/collapse."""
|
|
|
|
from PySide6.QtCore import Qt, QPropertyAnimation, QEasingCurve, Property, QPointF
|
|
from PySide6.QtWidgets import (
|
|
QWidget, QVBoxLayout, QHBoxLayout, QLabel, QFrame, QSizePolicy,
|
|
)
|
|
from PySide6.QtGui import QPainter, QColor, QFont
|
|
|
|
from ..theme import COLOR_BG_SECTION_HEADER, COLOR_TEXT_PRIMARY, COLOR_BORDER
|
|
|
|
|
|
class _HeaderBar(QWidget):
|
|
"""Clickable header bar with triangle arrow and title."""
|
|
|
|
def __init__(self, title: str, parent=None):
|
|
super().__init__(parent)
|
|
self.title = title
|
|
self._expanded = True
|
|
self.setFixedHeight(32)
|
|
self.setCursor(Qt.CursorShape.PointingHandCursor)
|
|
|
|
@property
|
|
def expanded(self) -> bool:
|
|
return self._expanded
|
|
|
|
@expanded.setter
|
|
def expanded(self, value: bool):
|
|
self._expanded = value
|
|
self.update()
|
|
|
|
def paintEvent(self, event):
|
|
p = QPainter(self)
|
|
p.setRenderHint(QPainter.RenderHint.Antialiasing)
|
|
|
|
p.fillRect(self.rect(), QColor(COLOR_BG_SECTION_HEADER))
|
|
|
|
arrow_x = 12
|
|
arrow_y = self.height() // 2
|
|
|
|
p.setPen(Qt.PenStyle.NoPen)
|
|
p.setBrush(QColor(COLOR_TEXT_PRIMARY))
|
|
|
|
if self._expanded:
|
|
p.drawPolygon([
|
|
QPointF(arrow_x - 4, arrow_y - 2),
|
|
QPointF(arrow_x + 4, arrow_y - 2),
|
|
QPointF(arrow_x, arrow_y + 3),
|
|
])
|
|
else:
|
|
p.drawPolygon([
|
|
QPointF(arrow_x - 2, arrow_y - 4),
|
|
QPointF(arrow_x + 3, arrow_y),
|
|
QPointF(arrow_x - 2, arrow_y + 4),
|
|
])
|
|
|
|
p.setPen(QColor(COLOR_TEXT_PRIMARY))
|
|
font = QFont()
|
|
font.setBold(True)
|
|
font.setPointSize(10)
|
|
p.setFont(font)
|
|
p.drawText(
|
|
28, 0, self.width() - 32, self.height(),
|
|
Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignLeft,
|
|
self.title,
|
|
)
|
|
|
|
p.end()
|
|
|
|
def mousePressEvent(self, event):
|
|
if event.button() == Qt.MouseButton.LeftButton:
|
|
self.parent().toggle()
|
|
|
|
|
|
class CollapsibleSection(QWidget):
|
|
"""A section with a clickable header that expands and collapses its content area."""
|
|
|
|
def __init__(self, title: str, parent=None, expanded: bool = False):
|
|
super().__init__(parent)
|
|
self._expanded = expanded
|
|
|
|
layout = QVBoxLayout(self)
|
|
layout.setContentsMargins(0, 0, 0, 0)
|
|
layout.setSpacing(0)
|
|
|
|
self._header = _HeaderBar(title, self)
|
|
layout.addWidget(self._header)
|
|
|
|
self._content = QWidget()
|
|
self._content.setSizePolicy(QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Maximum)
|
|
self._content_layout = QVBoxLayout(self._content)
|
|
self._content_layout.setContentsMargins(8, 6, 8, 6)
|
|
self._content_layout.setSpacing(6)
|
|
layout.addWidget(self._content)
|
|
|
|
self._animation = QPropertyAnimation(self, b"content_height")
|
|
self._animation.setDuration(200)
|
|
self._animation.setEasingCurve(QEasingCurve.Type.InOutQuad)
|
|
|
|
if not expanded:
|
|
self._content.setMaximumHeight(0)
|
|
self._header.expanded = False
|
|
|
|
@property
|
|
def content_layout(self) -> QVBoxLayout:
|
|
return self._content_layout
|
|
|
|
def add_widget(self, widget: QWidget):
|
|
self._content_layout.addWidget(widget)
|
|
|
|
def add_layout(self, layout):
|
|
self._content_layout.addLayout(layout)
|
|
|
|
def _get_content_height(self) -> int:
|
|
return self._content.maximumHeight()
|
|
|
|
def _set_content_height(self, h: int):
|
|
self._content.setMaximumHeight(h)
|
|
|
|
content_height = Property(int, _get_content_height, _set_content_height)
|
|
|
|
def toggle(self):
|
|
self._expanded = not self._expanded
|
|
self._header.expanded = self._expanded
|
|
|
|
target_height = self._content.sizeHint().height() if self._expanded else 0
|
|
|
|
self._animation.stop()
|
|
self._animation.setStartValue(self._content.maximumHeight())
|
|
self._animation.setEndValue(target_height)
|
|
|
|
if self._expanded:
|
|
self._animation.finished.connect(self._unlock_height)
|
|
|
|
self._animation.start()
|
|
|
|
def _unlock_height(self):
|
|
"""Remove height cap after expansion so content can resize freely."""
|
|
if self._expanded:
|
|
self._content.setMaximumHeight(16777215)
|
|
|
|
try:
|
|
self._animation.finished.disconnect(self._unlock_height)
|
|
except TypeError:
|
|
pass
|
|
|
|
def is_expanded(self) -> bool:
|
|
return self._expanded
|