-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoggleswitch.cpp
More file actions
73 lines (59 loc) · 2.11 KB
/
toggleswitch.cpp
File metadata and controls
73 lines (59 loc) · 2.11 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
#include "toggleswitch.h"
ToggleSwitch::ToggleSwitch(QWidget *parent) :
QAbstractButton(parent),
m_status(false),
m_margin(3),
m_bodyBrush(QColor(223, 136, 86))
{
this->setBrush(QColor(223, 136, 86));
}
void ToggleSwitch::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setPen(Qt::NoPen);
if(this->isEnabled())
{
painter.setBrush(this->m_status ? this->brush(): Qt::gray);
painter.setOpacity(0.8);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.drawRoundedRect(QRect(this->m_margin, this->m_margin, this->width() - 2 * this->m_margin,
this->height() - 2 * this->m_margin), 18.0, 18.0);
painter.setBrush(this->m_bodyBrush);
painter.setOpacity(1.0);
if(!this->m_status)
{
painter.drawEllipse(QRectF(this->width() - this->height(), static_cast<int>((this->height() / 2) - (this->height() / 2)),
this->height(), this->height()));
}
else
{
painter.drawEllipse(QRectF(0, static_cast<int>((this->height() / 2) - (this->height() / 2)),
this->height(), this->height()));
}
}
else
{
painter.setBrush(QColor(213, 126, 76));
painter.setOpacity(1.0);
painter.drawRoundedRect(QRect(this->m_margin, this->m_margin, this->width() - 2 * this->m_margin,
this->height() - 2 * this->m_margin), 18.0, 18.0);
painter.setOpacity(0.3);
painter.setBrush(QColor(213, 126, 76));
painter.drawEllipse(QRectF(static_cast<int>(this->width() / 2), static_cast<int>((this->height()/2) - (this->height()/2)),
this->height(), this->height()));
}
event->setAccepted(true);
}
void ToggleSwitch::mouseReleaseEvent(QMouseEvent *event)
{
if(event->button() & Qt::LeftButton)
{
this->m_status = !this->m_status;
emit ToggleSwitch::statusChanged(this->m_status);
}
QAbstractButton::mouseReleaseEvent(event);
}
QSize ToggleSwitch::sizeHint() const
{
return {this->width() + (2 * this->m_margin), this->height() + (2 * this->m_margin)};
}