File: section.cpp

package info (click to toggle)
obs-advanced-scene-switcher 1.32.8-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,492 kB
  • sloc: xml: 297,593; cpp: 147,875; python: 387; sh: 280; ansic: 170; makefile: 33
file content (186 lines) | stat: -rw-r--r-- 5,080 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "section.hpp"
#include "layout-helpers.hpp"

#include <QPropertyAnimation>
#include <QEvent>

namespace advss {

Section::Section(const int animationDuration, QWidget *parent)
	: QWidget(parent),
	  _animationDuration(animationDuration)
{
	_toggleButton = new QToolButton(this);
	_headerLine = new QFrame(this);
	_mainLayout = new QGridLayout(this);
	_headerWidgetLayout = new QHBoxLayout();

	_toggleButton->setStyleSheet(
		"QToolButton {border: none; background-color: rgba(0,0,0,0);}");
	_toggleButton->setToolButtonStyle(Qt::ToolButtonIconOnly);
	_toggleButton->setArrowType(Qt::ArrowType::RightArrow);
	_toggleButton->setCheckable(true);
	_toggleButton->setChecked(true);

	_headerLine->setFrameShape(QFrame::HLine);
	_headerLine->setFrameShadow(QFrame::Sunken);
	_headerLine->setSizePolicy(QSizePolicy::Expanding,
				   QSizePolicy::Maximum);

	// Don't waste space
	_mainLayout->setVerticalSpacing(0);
	_mainLayout->setContentsMargins(0, 0, 0, 0);

	// But add some spacing for widgets in header
	_headerWidgetLayout->setSpacing(11);
	_headerWidgetLayout->addWidget(_toggleButton);

	_mainLayout->addLayout(_headerWidgetLayout, 0, 0, 1, 1, Qt::AlignLeft);
	_mainLayout->addWidget(_headerLine, 0, 2, 1, 1);
	setLayout(_mainLayout);

	connect(_toggleButton, &QToolButton::toggled, this, &Section::Collapse);
}

void Section::Collapse(bool collapse)
{
	if (_contentHeight != _content->sizeHint().height()) {
		_contentHeight = _content->sizeHint().height();
	}
	SetupAnimations();

	_toggleButton->setChecked(collapse);
	_toggleButton->setArrowType(collapse ? Qt::ArrowType::RightArrow
					     : Qt::ArrowType::DownArrow);
	if (!_toggleAnimation) {
		_collapsed = collapse;
		emit Collapsed(collapse);
		return;
	}

	_toggleAnimation->setDirection(collapse ? QAbstractAnimation::Backward
						: QAbstractAnimation::Forward);
	_transitioning = true;
	_collapsed = collapse;
	_toggleAnimation->start();
	emit Collapsed(collapse);
}

void Section::SetContent(QWidget *w)
{
	SetContent(w, _collapsed);
}

void Section::SetContent(QWidget *w, bool collapsed)
{
	CleanUpPreviousContent();
	delete _contentArea;

	// Setup contentArea
	_contentArea = new QWidget(this);
	_contentArea->setObjectName("macroSegmentContent");
	_contentArea->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
	_contentArea->setStyleSheet(
		"#macroSegmentContent { border: none; background-color: rgba(0,0,0,0); }");
	_contentArea->setMaximumHeight(0);
	_contentArea->setMinimumHeight(0);

	_content = w;
	_content->installEventFilter(this);
	auto newLayout = new QVBoxLayout();
	newLayout->setContentsMargins(0, 0, 0, 0);
	newLayout->addWidget(w);
	_contentArea->setLayout(newLayout);
	_mainLayout->addWidget(_contentArea, 1, 0, 1, 3);

	_headerHeight = sizeHint().height() - _contentArea->maximumHeight();
	_contentHeight = _content->sizeHint().height();

	SetupAnimations();

	setMinimumHeight(_headerHeight);
	if (collapsed) {
		_contentArea->setMaximumHeight(0);
	} else {
		_contentArea->setMaximumHeight(QWIDGETSIZE_MAX);
	}
	const QSignalBlocker b(_toggleButton);
	_toggleButton->setChecked(collapsed);
	_toggleButton->setArrowType(collapsed ? Qt::ArrowType::RightArrow
					      : Qt::ArrowType::DownArrow);
	_collapsed = collapsed;
}

void Section::AddHeaderWidget(QWidget *w)
{
	_headerWidgetLayout->addWidget(w);
}

void Section::SetCollapsed(bool collapsed)
{
	if (_collapsed == collapsed) {
		return;
	}
	Collapse(collapsed);
}

void Section::SetupAnimations()
{
	if (_toggleAnimation) {
		_toggleAnimation->deleteLater();
	}

	_toggleAnimation = new QParallelAnimationGroup(this);
	_toggleAnimation->addAnimation(
		new QPropertyAnimation(this, "minimumHeight"));
	_toggleAnimation->addAnimation(
		new QPropertyAnimation(this, "maximumHeight"));
	_toggleAnimation->addAnimation(
		new QPropertyAnimation(_contentArea, "maximumHeight"));

	for (int i = 0; i < _toggleAnimation->animationCount() - 1; ++i) {
		QPropertyAnimation *SectionAnimation =
			static_cast<QPropertyAnimation *>(
				_toggleAnimation->animationAt(i));
		SectionAnimation->setDuration(_animationDuration);
		SectionAnimation->setStartValue(_headerHeight);
		SectionAnimation->setEndValue(_headerHeight + _contentHeight);
	}

	auto contentAnimation =
		static_cast<QPropertyAnimation *>(_toggleAnimation->animationAt(
			_toggleAnimation->animationCount() - 1));
	contentAnimation->setDuration(_animationDuration);
	contentAnimation->setStartValue(0);
	contentAnimation->setEndValue(_contentHeight);

	QWidget::connect(_toggleAnimation, SIGNAL(finished()), this,
			 SLOT(AnimationFinish()));
}

void Section::CleanUpPreviousContent()
{
	if (_contentArea) {
		auto oldLayout = _contentArea->layout();
		if (oldLayout) {
			ClearLayout(oldLayout);
			delete oldLayout;
		}
	}
}

void Section::AnimationFinish()
{
	_transitioning = false;

	// Allow resizing if we are not currently transitioning or collapsed
	if (!_collapsed) {
		setMinimumHeight(0);
		setMaximumHeight(QWIDGETSIZE_MAX);
		_contentArea->setMaximumHeight(QWIDGETSIZE_MAX);
	}

	emit AnimationFinished();
}

} // namespace advss