-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathqswitchcontrol.cpp
More file actions
90 lines (72 loc) · 2.9 KB
/
qswitchcontrol.cpp
File metadata and controls
90 lines (72 loc) · 2.9 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
#include "qswitchcontrol.h"
#include <QPushButton>
#include <QPropertyAnimation>
#include <QStyleOption>
#include <QPainter>
static const QSize FrameSize = QSize(68, 30);
static const QSize SwitchSize = QSize (26, 26);
static const int SwitchOffset = (FrameSize.height() - SwitchSize.height()) / 2;
static const QString CustomFrameOnStlye = QString("QAbstractButton { border: none; border-radius: %1; background-color: #4697d9;}").arg(FrameSize.height() / 2);
static const QString CustomFrameOffStlye = QString("QAbstractButton { border: none; border-radius: %1; background-color: #a09f9d;}").arg(FrameSize.height() / 2);
static const QString CustomButtonStlye = QString("QPushButton { min-width: 0em; border-radius: %1; background-color: white;}").arg(SwitchSize.height() / 2);
QSwitchControl::QSwitchControl(QWidget *parent):
QAbstractButton(parent)
{
this->setFixedSize(FrameSize);
pbSwitch = new QPushButton(this);
pbSwitch->setFixedSize(SwitchSize);
pbSwitch->setStyleSheet(CustomButtonStlye);
animation = new QPropertyAnimation(pbSwitch, "geometry", this);
animation->setDuration(200);
connect(this, &QSwitchControl::mouseClicked, this, &QSwitchControl::onStatusChanged);
connect(pbSwitch, &QPushButton::clicked, this, &QSwitchControl::onStatusChanged);
setCheckable(true);
setChecked(false);
}
void QSwitchControl::setChecked(bool checked)
{
if(checked)
{
pbSwitch->move(this->width() - pbSwitch->width() - SwitchOffset, this->y() + SwitchOffset);
this->setStyleSheet(CustomFrameOnStlye);
}
else
{
pbSwitch->move(this->x() + SwitchOffset, this->y() + SwitchOffset);
this->setStyleSheet(CustomFrameOffStlye);
}
QAbstractButton::setChecked(checked);
}
void QSwitchControl::onStatusChanged()
{
bool checked = !isChecked();
QRect currentGeometry(pbSwitch->x(), pbSwitch->y(), pbSwitch->width(), pbSwitch->height());
if(animation->state() == QAbstractAnimation::Running)
animation->stop();
if(checked)
{
this->setStyleSheet(CustomFrameOnStlye);
animation->setStartValue(currentGeometry);
animation->setEndValue(QRect(this->width() - pbSwitch->width() - SwitchOffset, pbSwitch->y(), pbSwitch->width(), pbSwitch->height()));
}
else
{
this->setStyleSheet(CustomFrameOffStlye);
animation->setStartValue(currentGeometry);
animation->setEndValue(QRect(SwitchOffset, pbSwitch->y(), pbSwitch->width(), pbSwitch->height()));
}
animation->start();
setChecked(checked);
Q_EMIT clicked(checked);
}
void QSwitchControl::mousePressEvent(QMouseEvent *)
{
Q_EMIT mouseClicked();
}
void QSwitchControl::paintEvent(QPaintEvent *)
{
QStyleOption opt;
opt.init(this);
QPainter p(this);
style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}