-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanimatedsplash.cpp
More file actions
177 lines (151 loc) · 5.07 KB
/
Copy pathanimatedsplash.cpp
File metadata and controls
177 lines (151 loc) · 5.07 KB
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
/**
* @file animatedsplash.cpp
* @brief Full-screen overlay with a centered circular splash.
*/
#include "animatedsplash.h"
#include <QCursor>
#include <QGuiApplication>
#include <QPainter>
#include <QPainterPath>
#include <QScreen>
#include <QShowEvent>
#include <QTimer>
#include <QtMath>
namespace {
const QColor kBrandGreen(0x8F, 0xCB, 0x57);
const QColor kInk(0x11, 0x11, 0x11);
const QColor kMuted(0x6B, 0x72, 0x80);
} // namespace
AnimatedSplash::AnimatedSplash(QWidget *parent)
: QWidget(parent,
Qt::Window | Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint)
{
setAttribute(Qt::WA_TranslucentBackground, true);
setAttribute(Qt::WA_OpaquePaintEvent, false);
setWindowTitle(QStringLiteral("Spectrum Analyzer"));
m_timer = new QTimer(this);
m_timer->setInterval(16);
connect(m_timer, &QTimer::timeout, this, &AnimatedSplash::onTick);
m_timer->start();
}
AnimatedSplash::~AnimatedSplash() = default;
void AnimatedSplash::applyScreenCover()
{
QScreen *screen = QGuiApplication::screenAt(QCursor::pos());
if (!screen) {
screen = QGuiApplication::primaryScreen();
}
if (!screen) {
return;
}
setGeometry(screen->geometry());
}
void AnimatedSplash::showCoveringScreen()
{
applyScreenCover();
showFullScreen();
applyScreenCover();
raise();
}
void AnimatedSplash::showEvent(QShowEvent *event)
{
QWidget::showEvent(event);
applyScreenCover();
if (!isFullScreen()) {
showFullScreen();
}
QTimer::singleShot(0, this, [this]() {
applyScreenCover();
if (!isFullScreen()) {
showFullScreen();
}
});
}
void AnimatedSplash::onTick()
{
m_phase += 0.14;
m_sweep += 0.045;
if (m_sweep > M_PI * 2.0) {
m_sweep -= M_PI * 2.0;
}
update();
}
void AnimatedSplash::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event);
QPainter p(this);
p.setRenderHint(QPainter::Antialiasing, true);
// Window fills the monitor; the badge is drawn at the geometric center.
const QPointF c(width() * 0.5, height() * 0.5);
const qreal diameter = qMin<qreal>(kCircleDiameter,
qMin(width(), height()) - 80.0);
const qreal r = diameter * 0.5;
// Soft drop shadow so the circle lifts off the desktop.
p.setPen(Qt::NoPen);
p.setBrush(QColor(0, 0, 0, 55));
p.drawEllipse(c + QPointF(0.0, 6.0), r, r);
// Breathing halo in brand green.
const qreal halo = 10.0 + 6.0 * (0.5 + 0.5 * qSin(m_phase * 0.85));
QColor haloColor = kBrandGreen;
haloColor.setAlpha(70);
p.setBrush(haloColor);
p.drawEllipse(c, r + halo, r + halo);
QPainterPath disc;
disc.addEllipse(c, r, r);
p.fillPath(disc, QColor(QStringLiteral("#FAFBFA")));
p.setPen(QPen(kBrandGreen, 4.0));
p.setBrush(Qt::NoBrush);
p.drawEllipse(c, r, r);
// Rotating spectrum sweep along the rim.
p.save();
p.setClipPath(disc);
QPen sweepPen(QColor(0x8F, 0xCB, 0x57, 180), 10.0, Qt::SolidLine, Qt::RoundCap);
p.setPen(sweepPen);
const int start = static_cast<int>(qRadiansToDegrees(-m_sweep) * 16.0);
p.drawArc(QRectF(c.x() - r + 8.0, c.y() - r + 8.0,
(r - 8.0) * 2.0, (r - 8.0) * 2.0),
start, 50 * 16);
p.restore();
p.setPen(kInk);
QFont title = font();
title.setFamily(QStringLiteral("Georgia"));
title.setPointSize(16);
title.setBold(true);
p.setFont(title);
p.drawText(QRectF(c.x() - r + 28.0, c.y() - r + 52.0, r * 2.0 - 56.0, 36.0),
Qt::AlignHCenter | Qt::AlignVCenter,
QStringLiteral("Spectrum Analyzer"));
const qreal barTop = c.y() - 28.0;
const qreal barBottom = c.y() + 78.0;
const qreal barAreaH = barBottom - barTop;
const qreal barLeft = c.x() - r * 0.55;
const qreal barWidthTotal = r * 1.10;
const qreal gap = 5.0;
const qreal barW = (barWidthTotal - gap * (kBarCount - 1)) / kBarCount;
p.save();
p.setClipPath(disc);
for (int i = 0; i < kBarCount; ++i) {
const qreal envelope = qSin((i - (kBarCount - 1) * 0.5) * 0.28);
const qreal wave = 0.28 + 0.55 * (0.5 + 0.5 * qSin(m_phase + i * 0.38));
const qreal bounce = 0.12 * qSin(m_phase * 1.6 + i * 0.85);
const qreal level = qBound(0.14, wave + bounce + 0.12 * envelope * envelope, 1.0);
const qreal h = barAreaH * level;
const qreal bx = barLeft + i * (barW + gap);
const qreal by = barBottom - h;
const bool accent = (i == kBarCount / 2) || (i % 5 == 0);
QColor fill = accent ? kBrandGreen : kInk;
QPainterPath bar;
bar.addRoundedRect(QRectF(bx, by, barW, h), barW * 0.45, barW * 0.45);
p.fillPath(bar, fill);
}
p.restore();
p.setPen(kMuted);
QFont nameFont = font();
nameFont.setFamily(QStringLiteral("Georgia"));
nameFont.setPointSize(11);
nameFont.setItalic(true);
p.setFont(nameFont);
p.drawText(QRectF(c.x() - r + 24.0, c.y() + r - 78.0, r * 2.0 - 48.0, 28.0),
Qt::AlignHCenter | Qt::AlignVCenter,
QStringLiteral("Mohammad Haghpanah"));
}