game_launcher 1.1.0
Modern Linux game launcher with Steam / Proton / Wine integration
Loading...
Searching...
No Matches
collapsible_section.h
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// ==============================================================================
3// qt/collapsible_section.h
4// ==============================================================================
5// A collapsible section widget with animated show/hide and persistent state
6// ==============================================================================
7
8#pragma once
9
10#include "icon_font.h"
11#include "theme_manager.h"
12
13#include <QEvent>
14#include <QHBoxLayout>
15#include <QLabel>
16#include <QPropertyAnimation>
17#include <QSettings>
18#include <QVBoxLayout>
19#include <QWidget>
20
21// ==============================================================================
22// CollapsibleSection - Wraps content in a clickable header that shows/hides
23// ==============================================================================
24
25class CollapsibleSection : public QWidget
26{
27 Q_OBJECT
28 Q_PROPERTY(int contentHeight READ contentHeight WRITE setContentHeight)
29
30 public:
31 explicit CollapsibleSection(const QString& title, const QString& settingsKey,
32 bool defaultCollapsed = false, QWidget* parent = nullptr)
33 : QWidget(parent), settingsKey_(settingsKey), defaultCollapsed_(defaultCollapsed)
34 {
35 QVBoxLayout* mainLayout = new QVBoxLayout(this);
36 mainLayout->setContentsMargins(0, 0, 0, 0);
37 mainLayout->setSpacing(0);
38
39 // Header button (clickable)
40 headerWidget_ = new QWidget(this);
41 headerWidget_->setCursor(Qt::PointingHandCursor);
42 QHBoxLayout* headerLayout = new QHBoxLayout(headerWidget_);
43 headerLayout->setContentsMargins(16, 6, 16, 6);
44 headerLayout->setSpacing(6);
45
46 arrowLabel_ = new QLabel(headerWidget_);
47 arrowLabel_->setFixedWidth(14);
48
49 titleLabel_ = new QLabel(title, headerWidget_);
50
51 headerLayout->addWidget(arrowLabel_);
52 headerLayout->addWidget(titleLabel_, 1);
53
54 mainLayout->addWidget(headerWidget_);
55
56 // Content container
57 contentWidget_ = new QWidget(this);
58 contentLayout_ = new QVBoxLayout(contentWidget_);
59 contentLayout_->setContentsMargins(0, 0, 0, 0);
60 contentLayout_->setSpacing(0);
61
62 mainLayout->addWidget(contentWidget_);
63
64 // Animation
65 animation_ = new QPropertyAnimation(this, "contentHeight", this);
66 animation_->setDuration(150);
67 animation_->setEasingCurve(QEasingCurve::OutCubic);
68
69 // Header click handler
70 headerWidget_->installEventFilter(this);
71
72 // Load persisted state
73 QSettings settings;
74 collapsed_ = settings.value(settingsKey_, defaultCollapsed_).toBool();
75
76 // Apply initial state without animation
77 contentWidget_->setVisible(!collapsed_);
78
79 applyTheme();
80 }
81
82 // Add a widget to the content area
83 void addWidget(QWidget* widget)
84 {
85 contentLayout_->addWidget(widget);
86 }
87
88 // Add a layout to the content area
89 void addLayout(QLayout* layout)
90 {
91 contentLayout_->addLayout(layout);
92 }
93
94 // Add spacing
95 void addSpacing(int size)
96 {
97 contentLayout_->addSpacing(size);
98 }
99
100 // Get the content layout for direct manipulation
101 QVBoxLayout* contentLayout() const
102 {
103 return contentLayout_;
104 }
105
106 // Check if collapsed
107 bool isCollapsed() const
108 {
109 return collapsed_;
110 }
111
112 // Programmatically set collapsed state
113 void setCollapsed(bool collapsed, bool animate = true)
114 {
115 if (collapsed_ == collapsed)
116 return;
117 collapsed_ = collapsed;
118
119 QSettings settings;
120 settings.setValue(settingsKey_, collapsed_);
121
122 if (animate && contentWidget_->isVisible())
123 {
124 animateToggle();
125 }
126 else
127 {
128 contentWidget_->setVisible(!collapsed_);
129 }
130
131 updateArrow();
132 }
133
135 {
137
138 // Arrow styling — FontAwesome chevron, accent colored
139 arrowLabel_->setStyleSheet(
140 QString("color: %1; font-size: %2px; font-family: '%3'; background: transparent;")
141 .arg(c.accent.name())
142 .arg(ThemeManager::instance().scaledFontSize(10))
143 .arg(IconFont::solidFamily()));
144
145 // Title styling - clean Title Case section headers
146 titleLabel_->setStyleSheet(
147 QString("font-weight: 600; font-size: %1px; color: %2; background: transparent;")
148 .arg(ThemeManager::instance().scaledFontSize(11))
149 .arg(c.textSecondary.name()));
150
151 // Header styling with subtle bottom border, hover brightens
152 // Use #collapsibleHeader to avoid hover leaking to child labels
153 headerWidget_->setObjectName("collapsibleHeader");
154 headerWidget_->setStyleSheet(QString("#collapsibleHeader { background-color: transparent; "
155 "border-bottom: 1px solid %1; "
156 "padding: 2px 0; }"
157 "#collapsibleHeader:hover { background-color: %2; }")
158 .arg(c.borderLight.name())
159 .arg(c.hoverBackground.name()));
160
161 updateArrow();
162 }
163
164 int contentHeight() const
165 {
166 return contentWidget_->maximumHeight();
167 }
168
169 void setContentHeight(int height)
170 {
171 contentWidget_->setMaximumHeight(height);
172 if (height == 0)
173 {
174 contentWidget_->setVisible(false);
175 }
176 else
177 {
178 contentWidget_->setVisible(true);
179 }
180 }
181
182 protected:
183 bool eventFilter(QObject* obj, QEvent* event) override
184 {
185 if (obj == headerWidget_ && event->type() == QEvent::MouseButtonRelease)
186 {
187 toggle();
188 return true;
189 }
190 return QWidget::eventFilter(obj, event);
191 }
192
193 private:
194 void toggle()
195 {
196 collapsed_ = !collapsed_;
197
198 QSettings settings;
199 settings.setValue(settingsKey_, collapsed_);
200
201 animateToggle();
202 updateArrow();
203 }
204
205 void animateToggle()
206 {
207 animation_->stop();
208
209 if (collapsed_)
210 {
211 // Collapse: animate from current height to 0
212 int startHeight = contentWidget_->sizeHint().height();
213 contentWidget_->setMaximumHeight(startHeight);
214 animation_->setStartValue(startHeight);
215 animation_->setEndValue(0);
216 connect(
217 animation_, &QPropertyAnimation::finished, this,
218 [this]()
219 {
220 contentWidget_->setVisible(false);
221 contentWidget_->setMaximumHeight(16777215); // QWIDGETSIZE_MAX
222 },
223 Qt::SingleShotConnection);
224 }
225 else
226 {
227 // Expand: animate from 0 to target height
228 contentWidget_->setVisible(true);
229 int targetHeight = contentWidget_->sizeHint().height();
230 contentWidget_->setMaximumHeight(0);
231 animation_->setStartValue(0);
232 animation_->setEndValue(targetHeight);
233 connect(
234 animation_, &QPropertyAnimation::finished, this,
235 [this]()
236 {
237 contentWidget_->setMaximumHeight(16777215); // QWIDGETSIZE_MAX
238 },
239 Qt::SingleShotConnection);
240 }
241
242 animation_->start();
243 }
244
245 void updateArrow()
246 {
247 arrowLabel_->setText(IconFont::text(collapsed_ ? FA::ChevronRight : FA::ChevronDown));
248 }
249
250 QString settingsKey_;
251 bool defaultCollapsed_;
252 bool collapsed_ = false;
253
254 QWidget* headerWidget_ = nullptr;
255 QLabel* arrowLabel_ = nullptr;
256 QLabel* titleLabel_ = nullptr;
257 QWidget* contentWidget_ = nullptr;
258 QVBoxLayout* contentLayout_ = nullptr;
259 QPropertyAnimation* animation_ = nullptr;
260};
CollapsibleSection(const QString &title, const QString &settingsKey, bool defaultCollapsed=false, QWidget *parent=nullptr)
void addWidget(QWidget *widget)
bool eventFilter(QObject *obj, QEvent *event) override
QVBoxLayout * contentLayout() const
void setContentHeight(int height)
void addSpacing(int size)
void addLayout(QLayout *layout)
void setCollapsed(bool collapsed, bool animate=true)
static const QString & solidFamily()
Get the loaded Solid font family name.
Definition icon_font.h:136
static QString text(char32_t codepoint)
Convert a codepoint to a QString glyph.
Definition icon_font.h:113
static ThemeManager & instance()
const ThemeColors & colors() const
Get effective colors (with accent customization applied).
constexpr char32_t ChevronDown
Definition icon_font.h:56
constexpr char32_t ChevronRight
Definition icon_font.h:55
QColor borderLight
QColor textSecondary
QColor hoverBackground