-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch_button.cpp
358 lines (290 loc) · 6.45 KB
/
switch_button.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
#pragma execution_character_set("utf-8")
#include "switch_button.h"
#include <QPainter>
SwitchButton::SwitchButton(QWidget *parent) : QWidget(parent)
{
m_space = 2;
m_radius = 5;
m_checked = false;
m_showText = true;
m_showText = false;
m_animation = true;
m_bgColorOn = QColor(21, 156, 119);
m_bgColorOff = QColor(111, 122, 126);
m_sliderColorOn = QColor(255, 255, 255);
m_sliderColorOff = QColor(255, 255, 255);
m_textColor = QColor(255, 255, 255);
m_textOn = "ON";
m_textOff = "OFF";
m_step = 0;
m_startX = 0;
m_endX = 0;
m_timer = new QTimer(this);
m_timer->setInterval(30);
connect(m_timer, SIGNAL(timeout()), this, SLOT(updateValue()));
}
void SwitchButton::drawBackGround(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QColor bgColor = m_checked ? m_bgColorOn : m_bgColorOff;
if (isEnabled()) {
bgColor.setAlpha(60);
}
painter->setBrush(bgColor);
QRect rect(0, 0, width(), height());
int side = qMin(width(), height());
//左侧半圆
QPainterPath path1;
path1.addEllipse(rect.x(), rect.y(), side, side);
//右侧半圆
QPainterPath path2;
path2.addEllipse(rect.width() - side, rect.y(), side, side);
//中间的矩形
QPainterPath path3;
path3.addRect(rect.x() + side / 2, rect.y(), rect.width() - side, height());
QPainterPath path = path1 + path2 + path3;
painter->drawPath(path);
//绘制文本
//滑块半径
int sliderWidth = qMin(height(), width()) - m_space * 2 - 5;
if (m_checked){
QRect textRect(0, 0, width() - sliderWidth, height());
painter->setPen(QPen(m_textColor));
painter->drawText(textRect, Qt::AlignCenter, m_textOn);
} else {
QRect textRect(sliderWidth, 0, width() - sliderWidth, height());
painter->setPen(QPen(m_textColor));
painter->drawText(textRect, Qt::AlignCenter, m_textOff);
}
painter->restore();
}
void SwitchButton::drawSlider(QPainter *painter)
{
painter->save();
painter->setPen(Qt::NoPen);
QColor color = m_checked ? m_sliderColorOn : m_sliderColorOff;
painter->setBrush(QBrush(color));
int sliderWidth = qMin(width(), height()) - m_space * 2;
QRect rect(m_space + m_startX, m_space, sliderWidth, sliderWidth);
painter->drawEllipse(rect);
painter->restore();
}
void SwitchButton::paintEvent(QPaintEvent *ev)
{
//启用反锯齿
QPainter painter(this);
painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
//绘制背景
drawBackGround(&painter);
//绘制滑块
drawSlider(&painter);
}
void SwitchButton::mousePressEvent(QMouseEvent *ev)
{
Q_UNUSED(ev)
m_checked = !m_checked;
emit statusChanged(m_checked);
//计算步长
m_step = width() / 10;
//计算滑块X轴终点坐标
if (m_checked) {
m_endX = width() - height();
} else {
m_endX = 0;
}
//判断是否使用动画
if (m_animation) {
m_timer->start();
} else{
m_startX = m_endX;
update();
}
}
void SwitchButton::updateValue()
{
if (m_checked) {
if (m_startX < m_endX) {
m_startX += m_step;
} else {
m_startX = m_endX;
m_timer->stop();
}
} else {
if (m_startX > m_endX) {
m_startX -= m_step;
} else {
m_startX = m_endX;
m_timer->stop();
}
}
update();
}
int SwitchButton::space() const
{
return m_space;
}
int SwitchButton::radius() const
{
return m_radius;
}
bool SwitchButton::checked() const
{
return m_checked;
}
bool SwitchButton::showText() const
{
return m_showText;
}
bool SwitchButton::showCircel() const
{
return m_showCircle;
}
bool SwitchButton::animation() const
{
return m_animation;
}
QColor SwitchButton::bgColorOn() const
{
return m_bgColorOn;
}
QColor SwitchButton::bgColorOff() const
{
return m_bgColorOff;
}
QColor SwitchButton::sliderColorOn() const
{
return m_sliderColorOn;
}
QColor SwitchButton::sliderColorOff() const
{
return m_sliderColorOff;
}
QColor SwitchButton::textColor() const
{
return m_textColor;
}
QString SwitchButton::textOn() const
{
return m_textOn;
}
QString SwitchButton::textOff() const
{
return m_textOff;
}
int SwitchButton::step() const
{
return m_step;
}
int SwitchButton::startX() const
{
return m_startX;
}
int SwitchButton::endX() const
{
return m_endX;
}
void SwitchButton::setSpace(int space)
{
if (m_space != space) {
m_space = space;
update();
}
}
void SwitchButton::setRadius(int radius)
{
if (m_radius != radius) {
m_radius = radius;
update();
}
}
void SwitchButton::setChecked(bool checked)
{
if (m_checked != checked) {
m_checked = checked;
update();
}
}
void SwitchButton::setShowText(bool show)
{
if (m_showText != show) {
m_showText = show;
update();
}
}
void SwitchButton::setShowCircle(bool show)
{
if (m_showCircle != show) {
m_showCircle = show;
update();
}
}
void SwitchButton::setAnimation(bool ok)
{
if (m_animation != ok) {
m_animation = ok;
update();
}
}
void SwitchButton::setBgColorOn(const QColor &color)
{
if (m_bgColorOn != color) {
m_bgColorOn = color;
update();
}
}
void SwitchButton::setBgColorOff(const QColor &color)
{
if (m_bgColorOff != color) {
m_bgColorOff = color;
update();
}
}
void SwitchButton::setSliderColorOn(const QColor &color)
{
if (m_sliderColorOn != color) {
m_sliderColorOn = color;
update();
}
}
void SwitchButton::setSliderColorOff(const QColor &color)
{
if (m_sliderColorOff != color) {
m_sliderColorOff = color;
update();
}
}
void SwitchButton::setTextColor(const QColor &color)
{
if (m_textColor != color) {
m_textColor = color;
update();
}
}
void SwitchButton::setTextOn(const QString &text)
{
if (m_textOn != text) {
m_textOn = text;
update();
}
}
void SwitchButton::setTextOff(const QString &text)
{
if (m_textOff != text) {
m_textOff = text;
update();
}
}
//void SwitchButton::setStep(int step)
//{
// if (m_step != step) {
// m_step = step;
// update();
// }
//}
//void SwitchButton::setStartX(int startX)
//{
//}
//void SwitchButton::setEndX(int endX)
//{
//}