1 /*!
2 * @file switchbutton.cpp
3 * Copyright (c) 2018
4 * @brief des
5 * detailed des
6 *
7 * @date 2018
8 * @author lee
9 */
10 #include "switchbutton.h"
11
12
13 #include <QPainter>
14 #include <QMouseEvent>
15 #include <QDebug>
SwitchButton(QWidget * parent)16 SwitchButton::SwitchButton(QWidget *parent)
17 : QWidget(parent),
18 m_nHeight(36),
19 m_bChecked(false),
20 m_radius(5.0),
21 m_nMargin(0),
22 m_CheckedColor(0, 0, 150),
23 m_thumbColorOn(127,170,255),
24 m_thumbColorOff(Qt::white),
25 m_disableColor(190, 190, 190),
26 m_backgroundColor(Qt::black)
27 {
28 setCursor(Qt::PointingHandCursor);
29
30 connect(&m_timer,SIGNAL(timeout()),this, SLOT(onTimeout()));
31 }
32 /*!
33 * @function
34 * @brief des this func is paint the switch button
35 */
paintEvent(QPaintEvent * event)36 void SwitchButton::paintEvent(QPaintEvent *event)
37 {
38 Q_UNUSED(event);
39
40 QPainter painter(this);
41 painter.setPen(Qt::NoPen);
42 painter.setRenderHint(QPainter::Antialiasing);
43
44 QPainterPath path;
45 QColor background;
46 QColor thumbColor;
47 qreal dOpacity;
48 if(isEnabled()){//switchbutton is enabled
49 if(m_bChecked){//switchbutton is open
50 background = m_CheckedColor;
51 thumbColor = m_thumbColorOn;
52 dOpacity = 0.6;
53 } else { //switchbutton is close
54 background = m_backgroundColor;
55 thumbColor = m_thumbColorOff;
56 dOpacity = 0.8;
57 }
58 } else {//switchbutton is disable
59 background = m_backgroundColor;
60 dOpacity = 0.26;
61 thumbColor = m_disableColor;
62 }
63 //paint the big rect
64 painter.setBrush(background);
65 painter.setOpacity(dOpacity);
66 path.addRoundedRect(QRectF(m_nMargin,m_nMargin,width() - 2 * m_nMargin, \
67 height() - 2 * m_nMargin), m_radius, m_radius);
68 painter.drawPath(path.simplified());
69
70 //paint the small rect
71 painter.setBrush(thumbColor);
72 painter.setOpacity(1.0);
73 painter.drawRoundedRect(QRectF(m_nX - (m_nHeight / 2),m_nY - (m_nHeight / 2),\
74 height(), height()),m_radius,m_radius);// ellipse center poiont and x & y radius
75 //painter.drawEllipse(QRectF(72,0,42,42));// ellipse center poiont and x & y radius
76 }
77
mousePressEvent(QMouseEvent * event)78 void SwitchButton::mousePressEvent(QMouseEvent *event)
79 {
80 if (isEnabled()) {
81 if (event->buttons() & Qt::LeftButton) {
82 event->accept();
83 } else {
84 event->ignore();
85 }
86 }
87 }
88
mouseReleaseEvent(QMouseEvent * event)89 void SwitchButton::mouseReleaseEvent(QMouseEvent *event)
90 {
91 if (isEnabled()) {
92 if ((event->type() == QMouseEvent::MouseButtonRelease) && \
93 (event->button() == Qt::LeftButton)) {
94 event->accept();
95 m_bChecked = !m_bChecked;
96 emit toggled(m_bChecked);
97 m_timer.start(10);
98 } else {
99 event->ignore();
100 }
101 }
102 }
103
resizeEvent(QResizeEvent * event)104 void SwitchButton::resizeEvent(QResizeEvent *event)
105 {
106 m_nX = m_nHeight / 2;
107 m_nY = m_nHeight / 2;
108 QWidget::resizeEvent(event);
109 }
110
sizeHint() const111 QSize SwitchButton::sizeHint() const
112 {
113 return minimumSizeHint();
114 }
115
minimumSizeHint() const116 QSize SwitchButton::minimumSizeHint() const
117 {
118 return QSize(2 * (m_nHeight + m_nMargin), m_nHeight + 2 * m_nMargin);
119 }
120
onTimeout()121 void SwitchButton::onTimeout()
122 {
123 if (m_bChecked) {
124 m_nX += 1;
125 if (m_nX >= width() - m_nHeight / 2)
126 m_timer.stop();
127 } else {
128 m_nX -= 1;
129 if (m_nX <= m_nHeight / 2)
130 m_timer.stop();
131 }
132 update();
133 }
134
isToggled() const135 bool SwitchButton::isToggled() const
136 {
137 return m_bChecked;
138 }
139
setToggled(bool checked)140 void SwitchButton::setToggled(bool checked)
141 {
142 m_bChecked = checked;
143 m_timer.start(10);
144 }
145
setBackgroundColor(QColor color)146 void SwitchButton::setBackgroundColor(QColor color)
147 {
148 m_backgroundColor = color;
149 }
150
setCheckedColor(QColor color)151 void SwitchButton::setCheckedColor(QColor color)
152 {
153 m_CheckedColor = color;
154 }
155
setDisabledColor(QColor color)156 void SwitchButton::setDisabledColor(QColor color)
157 {
158 m_disableColor = color;
159 }
160