Loading...
Searching...
No Matches
CollapsibleSection.hpp
1#pragma once
2
3#include <QPainter>
4#include <QToolButton>
5#include <QVBoxLayout>
6#include <QWidget>
7
8namespace Gfx
9{
10class FlatHeaderButton : public QToolButton
11{
12 W_OBJECT(FlatHeaderButton)
13
14public:
15 using QToolButton::QToolButton;
16
17protected:
18 void paintEvent(QPaintEvent*) override
19 {
20 QPainter p(this);
21 p.setRenderHint(QPainter::Antialiasing);
22
23 const auto& pal = palette();
24
25 if(underMouse())
26 p.fillRect(rect(), pal.color(QPalette::Midlight));
27 else if(hasFocus())
28 p.fillRect(rect(), pal.color(QPalette::Midlight).darker(110));
29
30 if(hasFocus())
31 {
32 p.setPen(pal.color(QPalette::Highlight));
33 p.drawRect(rect().adjusted(0, 0, -1, -1));
34 }
35
36 const int arrowSize = 6;
37 const int arrowX = 8;
38 const int arrowY = (height() - arrowSize) / 2;
39
40 p.setPen(Qt::NoPen);
41 p.setBrush(pal.color(QPalette::WindowText));
42
43 QPolygonF arrow;
44 if(isChecked())
45 {
46 arrow << QPointF(arrowX, arrowY) << QPointF(arrowX + arrowSize, arrowY)
47 << QPointF(arrowX + arrowSize / 2.0, arrowY + arrowSize);
48 }
49 else
50 {
51 arrow << QPointF(arrowX, arrowY)
52 << QPointF(arrowX + arrowSize, arrowY + arrowSize / 2.0)
53 << QPointF(arrowX, arrowY + arrowSize);
54 }
55 p.drawPolygon(arrow);
56
57 p.setPen(pal.color(QPalette::WindowText));
58 const int textLeft = arrowX + arrowSize + 8;
59 QRect textRect(textLeft, 0, width() - textLeft, height());
60 p.drawText(textRect, Qt::AlignLeft | Qt::AlignVCenter, text());
61 }
62};
63
64class CollapsibleSection : public QWidget
65{
66 W_OBJECT(CollapsibleSection)
67
68public:
69 explicit CollapsibleSection(const QString& title, QWidget* parent = nullptr)
70 : QWidget(parent)
71 {
72 m_header = new FlatHeaderButton(this);
73 m_header->setText(title);
74 m_header->setCheckable(true);
75 m_header->setChecked(true);
76 m_header->setFocusPolicy(Qt::TabFocus);
77 m_header->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
78 m_header->setFixedHeight(24);
79
80 m_contentWidget = new QWidget(this);
81
82 auto* mainLayout = new QVBoxLayout(this);
83 mainLayout->setContentsMargins(0, 0, 0, 0);
84 mainLayout->setSpacing(0);
85 mainLayout->addWidget(m_header);
86 mainLayout->addWidget(m_contentWidget);
87
88 connect(m_header, &QToolButton::toggled, this, [this](bool checked) {
89 m_contentWidget->setVisible(checked);
90 m_header->update();
91 });
92 }
93
94 void setContentLayout(QLayout* layout)
95 {
96 if(m_contentWidget->layout())
97 {
98 qWarning(
99 "CollapsibleSection::setContentLayout: content layout already set, ignoring.");
100 return;
101 }
102 layout->setContentsMargins(10, 8, 10, 8);
103 m_contentWidget->setLayout(layout);
104 }
105
106 void setExpanded(bool expanded) { m_header->setChecked(expanded); }
107 bool isExpanded() const { return m_header->isChecked(); }
108
109private:
110 FlatHeaderButton* m_header = nullptr;
111 QWidget* m_contentWidget = nullptr;
112};
113}
Definition CollapsibleSection.hpp:65
Definition CollapsibleSection.hpp:11
Binds the rendering pipeline to ossia processes.
Definition CameraDevice.cpp:30