-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElaToggleButton.cpp
149 lines (137 loc) · 4.55 KB
/
ElaToggleButton.cpp
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
#include "ElaToggleButton.h"
#include <QEvent>
#include <QPainter>
#include <QPainterPath>
#include <QPropertyAnimation>
#include "ElaTheme.h"
#include "private/ElaToggleButtonPrivate.h"
Q_PROPERTY_CREATE_Q_CPP(ElaToggleButton, int, BorderRadius)
Q_PROPERTY_CREATE_Q_CPP(ElaToggleButton, QString, Text)
ElaToggleButton::ElaToggleButton(QWidget* parent)
: QWidget(parent), d_ptr(new ElaToggleButtonPrivate())
{
Q_D(ElaToggleButton);
d->q_ptr = this;
d->_pBorderRadius = 3;
d->_themeMode = eTheme->getThemeMode();
d->_pToggleAlpha = 0;
setMouseTracking(true);
setFixedSize(80, 32);
QFont font = this->font();
font.setPixelSize(15);
setFont(font);
setObjectName("ElaToggleButton");
setStyleSheet("#ElaToggleButton{background-color:transparent;}");
connect(eTheme, &ElaTheme::themeModeChanged, this, [=](ElaThemeType::ThemeMode themeMode) { d->_themeMode = themeMode; });
}
ElaToggleButton::ElaToggleButton(QString text, QWidget* parent)
: ElaToggleButton(parent)
{
Q_D(ElaToggleButton);
d->_pText = text;
}
ElaToggleButton::~ElaToggleButton()
{
}
void ElaToggleButton::setIsToggled(bool isToggled)
{
Q_D(ElaToggleButton);
d->_isToggled = isToggled;
update();
Q_EMIT toggled(isToggled);
}
bool ElaToggleButton::getIsToggled() const
{
Q_D(const ElaToggleButton);
return d->_isToggled;
}
bool ElaToggleButton::event(QEvent* event)
{
switch (event->type())
{
case QEvent::Enter:
case QEvent::Leave:
{
update();
break;
}
default:
{
break;
}
}
return QWidget::event(event);
}
void ElaToggleButton::mousePressEvent(QMouseEvent* event)
{
Q_D(ElaToggleButton);
d->_isPressed = true;
update();
QWidget::mouseReleaseEvent(event);
}
void ElaToggleButton::mouseReleaseEvent(QMouseEvent* event)
{
Q_D(ElaToggleButton);
d->_isPressed = false;
d->_isAlphaAnimationFinished = false;
d->_isToggled = !d->_isToggled;
QPropertyAnimation* alphaAnimation = new QPropertyAnimation(d, "pToggleAlpha");
connect(alphaAnimation, &QPropertyAnimation::valueChanged, this, [=](const QVariant& value) {
update();
});
connect(alphaAnimation, &QPropertyAnimation::finished, this, [=]() {
d->_isAlphaAnimationFinished = true;
});
alphaAnimation->setDuration(250);
alphaAnimation->setStartValue(d->_pToggleAlpha);
if (d->_isToggled)
{
alphaAnimation->setEndValue(255);
}
else
{
alphaAnimation->setEndValue(0);
}
alphaAnimation->start(QAbstractAnimation::DeleteWhenStopped);
Q_EMIT this->toggled(d->_isToggled);
QWidget::mouseReleaseEvent(event);
}
void ElaToggleButton::paintEvent(QPaintEvent* event)
{
Q_D(ElaToggleButton);
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
painter.save();
QRect foregroundRect(1, 1, width() - 2, height() - 2);
if (d->_isAlphaAnimationFinished)
{
if (d->_isToggled)
{
painter.setPen(ElaThemeColor(d->_themeMode, BasicBorder));
painter.setBrush(isEnabled() ? d->_isPressed ? ElaThemeColor(d->_themeMode, PrimaryPress) : (underMouse() ? ElaThemeColor(d->_themeMode, PrimaryHover) : ElaThemeColor(d->_themeMode, PrimaryNormal)) : ElaThemeColor(d->_themeMode, BasicDisable));
}
else
{
painter.setPen(ElaThemeColor(d->_themeMode, BasicBorder));
painter.setBrush(isEnabled() ? d->_isPressed ? ElaThemeColor(d->_themeMode, BasicPress) : (underMouse() ? ElaThemeColor(d->_themeMode, BasicHover) : ElaThemeColor(d->_themeMode, BasicBase)) : ElaThemeColor(d->_themeMode, BasicDisable));
}
}
else
{
painter.setPen(Qt::NoPen);
QColor toggleColor = ElaThemeColor(d->_themeMode, PrimaryNormal);
toggleColor.setAlpha(d->_pToggleAlpha);
painter.setBrush(toggleColor);
}
painter.drawRoundedRect(foregroundRect, d->_pBorderRadius, d->_pBorderRadius);
// 底边线绘制
if (!d->_isPressed && !d->_isToggled)
{
painter.setPen(ElaThemeColor(d->_themeMode, BasicBaseLine));
painter.drawLine(foregroundRect.x() + d->_pBorderRadius, height() - 1, foregroundRect.x() + foregroundRect.width() - d->_pBorderRadius, height() - 1);
}
//文字绘制
painter.setPen(isEnabled() ? d->_isToggled ? ElaThemeColor(d->_themeMode, BasicTextInvert) : ElaThemeColor(d->_themeMode, BasicText) : ElaThemeColor(d->_themeMode, BasicTextDisable));
painter.drawText(foregroundRect, Qt::AlignCenter, d->_pText);
painter.restore();
}