-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathLightWidget.h
More file actions
142 lines (117 loc) · 2.89 KB
/
LightWidget.h
File metadata and controls
142 lines (117 loc) · 2.89 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
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
#ifndef LIGHTWIDGET_H
#define LIGHTWIDGET_H
#include <QPainter>
#include <QWidget>
#include <QObject>
#include <QTimer>
#define LIGHTWIDGET_OFFSET 3
#define LIGHTWIDGET_SIZE_V 14
#define LIGHTWIDGET_SIZE_H 100
#define LIGHTWIDGET_BLINK 250 //ms
class LightWidget : public QWidget
{
Q_OBJECT
Q_PROPERTY(bool on READ isOn WRITE setOn)
Q_PROPERTY(QString text READ getText WRITE setText)
Q_PROPERTY(QString msg READ getMsg WRITE setMsg)
Q_PROPERTY(QColor color READ getColor WRITE setColor)
public:
LightWidget(const QColor &color, QString text = "", QWidget *parent = nullptr)
: QWidget(parent), m_color(color), m_on(true), m_text(text)
{
this->setFixedSize(LIGHTWIDGET_SIZE_H, LIGHTWIDGET_SIZE_V);
connect(&m_timer, SIGNAL(timeout()), this, SLOT(updateTime()));
}
~LightWidget()
{
m_timer.stop();
}
bool isOn() const
{
return m_on;
}
void setOn(bool on)
{
if (on == m_on)
return;
m_on = on;
update();
}
QColor getColor() const
{
return m_color;
}
void setColor(QColor color)
{
if (color == m_color)
return;
m_color = color;
update();
}
QString getText() const
{
return m_text;
}
void setText(QString text)
{
if (0==text.compare(m_text))
return;
m_text = text;
update();
}
QString getMsg() const
{
return m_msg;
}
void setMsg(QString msg)
{
if (0==msg.compare(m_msg))
return;
m_msg = msg;
update();
}
void blink(int time = 1500)
{
m_time = time;
m_timer.setInterval(LIGHTWIDGET_BLINK);
m_timer.start();
}
public slots:
void turnOff() { setOn(false); }
void turnOn() { setOn(true); }
private slots:
void updateTime()
{
m_time -= LIGHTWIDGET_BLINK;
if(m_time<=0)
{
m_timer.stop();
setOn(true);
}
else
{
setOn(!isOn());
}
}
protected:
void paintEvent(QPaintEvent *) override
{
paint(m_on? m_color:Qt::white);
}
void paint(QColor color)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setBrush(color);
painter.drawEllipse(LIGHTWIDGET_OFFSET, LIGHTWIDGET_OFFSET, LIGHTWIDGET_SIZE_V-2*LIGHTWIDGET_OFFSET, LIGHTWIDGET_SIZE_V-2*LIGHTWIDGET_OFFSET);
painter.drawText(LIGHTWIDGET_SIZE_V, LIGHTWIDGET_SIZE_V-LIGHTWIDGET_OFFSET, m_text + m_msg);
}
private:
QColor m_color;
bool m_on;
int m_time;
QTimer m_timer;
QString m_text;
QString m_msg;
};
#endif // LIGHTWIDGET_H