-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanimatedbutton.cpp
More file actions
185 lines (150 loc) · 5.23 KB
/
animatedbutton.cpp
File metadata and controls
185 lines (150 loc) · 5.23 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
178
179
180
181
182
183
184
185
#include "animatedbutton.h"
const QList<QString> AnimatedButton::RESIZEABLE_BUTTONS = {
"btnExplorer",
"btnKeyboard",
"btnSplitH",
"btnSplitV"
};
AnimatedButton::AnimatedButton(QWidget *parent)
: QPushButton(parent) {
// ---- Background Color ----
m_hoverBgColor = QColor("#05080e");
m_bgAnimation = new QPropertyAnimation(this, "hoverBgColor", this);
m_bgAnimation->setDuration(150);
// ---- Border Color ----
m_shiftBorderColor = QColor("#05080e");
m_borderAnimation = new QPropertyAnimation(this, "shiftBorderColor", this);
m_borderAnimation->setDuration(150);
// ---- Caps Lock ----
m_capsBgColor = QColor("#05080e");
m_capsAnimation = new QPropertyAnimation(this, "capsBgColor", this);
m_capsAnimation->setDuration(150);
// ---- Icon size ----
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
// ---- Icon Fade ----
if (!graphicsEffect()) {
QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect(this);
setGraphicsEffect(opacityEffect);
opacityEffect->setOpacity(1.0);
}
this->installEventFilter(this);
updateStyleSheet();
}
void AnimatedButton::updateStyleSheet() {
QColor finalBgColor;
if (m_capsBgColor == QColor("#05080e"))
finalBgColor = m_hoverBgColor;
else
finalBgColor = QColor("#1f2e40");
QString borderColor = m_shiftBorderColor.name();
QString borderWidth = "0px";
if (m_shiftBorderColor != QColor("#05080e"))
borderWidth = "2px";
QString style = QString(R"(
QPushButton {
font-weight: 700;
border-radius: 6px;
text-align: center;
border: %1 solid %2;
background-color: %3;
}
QPushButton:pressed {
background-color: #1f2e40;
}
#btnSpace {
border: 2px solid #99a0b2;
}
)").arg(borderWidth)
.arg(borderColor)
.arg(finalBgColor.name());
this->setStyleSheet(style);
}
// ---- Background Color ----
void AnimatedButton::setHoverBgColor(const QColor &color) {
if (m_hoverBgColor != color) {
m_hoverBgColor = color;
updateStyleSheet();
}
}
bool AnimatedButton::eventFilter(QObject *obj, QEvent *event) {
// Overrides button's hover event
if (obj == this) {
if (event->type() == QEvent::Enter) {
m_bgAnimation->stop();
m_bgAnimation->setStartValue(m_hoverBgColor);
m_bgAnimation->setEndValue(QColor("#1f2e40"));
m_bgAnimation->start();
return true;
} else if (event->type() == QEvent::Leave) {
m_bgAnimation->stop();
m_bgAnimation->setStartValue(m_hoverBgColor);
m_bgAnimation->setEndValue(QColor("#05080e"));
m_bgAnimation->start();
return true;
}
}
return QObject::eventFilter(obj, event);
}
// ---- Border Color ----
void AnimatedButton::setShiftBorderColor(const QColor &color) {
if (m_shiftBorderColor != color) {
m_shiftBorderColor = color;
updateStyleSheet();
}
}
void AnimatedButton::animateBorder(bool pressState) {
m_borderAnimation->stop();
m_borderAnimation->setStartValue(m_shiftBorderColor);
if (pressState)
m_borderAnimation->setEndValue(QColor("#99a0b2"));
else
m_borderAnimation->setEndValue(QColor("#05080e"));
m_borderAnimation->start();
}
// ---- Caps Lock ----
void AnimatedButton::setCapsBgColor(const QColor &color) {
if (m_capsBgColor != color) {
m_capsBgColor = color;
updateStyleSheet();
}
}
void AnimatedButton::animateCapsState(bool isOn) {
m_capsAnimation->stop();
m_capsAnimation->setStartValue("#1f2e40");
if (isOn) m_capsAnimation->setEndValue(QColor("#1f2e40"));
else m_capsAnimation->setEndValue(QColor("#05080e"));
m_capsAnimation->start();
}
// ---- Icon size ----
void AnimatedButton::resizeEvent(QResizeEvent *event) {
QPushButton::resizeEvent(event);
if (RESIZEABLE_BUTTONS.contains(this->objectName())) {
int btnWidth = width();
int btnHeight = height();
int btnReference = (btnWidth <= btnHeight) ? btnWidth : btnHeight;
int newIconSize = static_cast<int>(btnReference * 0.6);
setIconSize(QSize(newIconSize, newIconSize));
}
}
// ---- Icon Fade ----
void AnimatedButton::fadeIconSwap(const QIcon &newIcon) {
QGraphicsOpacityEffect *opacityEffect = qobject_cast<QGraphicsOpacityEffect*>(graphicsEffect());
if (!opacityEffect) {
this->setIcon(newIcon);
return;
}
QPropertyAnimation *fade = new QPropertyAnimation(opacityEffect, "opacity", this);
fade->setDuration(150);
fade->setEasingCurve(QEasingCurve::InQuad);
QObject::connect(fade, &QPropertyAnimation::finished, this, [this, newIcon, fade](){
this->setIcon(newIcon);
fade->disconnect();
fade->setStartValue(0.0);
fade->setEndValue(1.0);
QObject::connect(fade, &QPropertyAnimation::finished, fade, &QObject::deleteLater, Qt::SingleShotConnection);
fade->start();
}, Qt::SingleShotConnection);
fade->setStartValue(1.0);
fade->setEndValue(0.0);
fade->start();
}