Tue, 28 Jun 2022 20:12:16 +0200
Added LED plugin.
314 | 1 | #include <math.h> |
2 | ||
3 | #include <QPainter> | |
4 | #include <QGradient> | |
5 | #include <QPaintDevice> | |
6 | #include <QTimer> | |
7 | ||
8 | #include "LED.h" | |
9 | ||
10 | LED:: | |
11 | LED(QWidget* parent) : | |
12 | QWidget(parent), | |
13 | diameter_(5), | |
14 | color_(QColor("red")), | |
15 | alignment_(Qt::AlignCenter), | |
16 | initialState_(true), | |
17 | state_(true), | |
18 | flashRate_(200), | |
19 | flashing_(false) | |
20 | { | |
21 | timer_ = new QTimer(this); | |
22 | connect(timer_, SIGNAL(timeout()), this, SLOT(toggleState())); | |
23 | ||
24 | setDiameter(diameter_); | |
25 | } | |
26 | ||
27 | LED:: | |
28 | ~LED() | |
29 | { | |
30 | } | |
31 | ||
32 | ||
33 | double LED:: | |
34 | diameter() const | |
35 | { | |
36 | return diameter_; | |
37 | } | |
38 | ||
39 | void LED:: | |
40 | setDiameter(double diameter) | |
41 | { | |
42 | diameter_ = diameter; | |
43 | ||
44 | pixX_ = round(double(height())/heightMM()); | |
45 | pixY_ = round(double(width())/widthMM()); | |
46 | ||
47 | diamX_ = diameter_*pixX_; | |
48 | diamY_ = diameter_*pixY_; | |
49 | ||
50 | update(); | |
51 | } | |
52 | ||
53 | ||
54 | QColor LED:: | |
55 | color() const | |
56 | { | |
57 | return color_; | |
58 | } | |
59 | ||
60 | void LED:: | |
61 | setColor(const QColor& color) | |
62 | { | |
63 | color_ = color; | |
64 | update(); | |
65 | } | |
66 | ||
67 | Qt::Alignment LED:: | |
68 | alignment() const | |
69 | { | |
70 | return alignment_; | |
71 | } | |
72 | ||
73 | void LED:: | |
74 | setAlignment(Qt::Alignment alignment) | |
75 | { | |
76 | alignment_ = alignment; | |
77 | ||
78 | update(); | |
79 | } | |
80 | ||
81 | void LED:: | |
82 | setFlashRate(int rate) | |
83 | { | |
84 | flashRate_ = rate; | |
85 | ||
86 | update(); | |
87 | } | |
88 | ||
89 | void LED:: | |
90 | setFlashing(bool flashing) | |
91 | { | |
92 | flashing_ = flashing; | |
93 | ||
94 | update(); | |
95 | } | |
96 | ||
97 | void LED:: | |
98 | startFlashing() | |
99 | { | |
100 | setFlashing(true); | |
101 | } | |
102 | ||
103 | void LED:: | |
104 | stopFlashing() | |
105 | { | |
106 | setFlashing(false); | |
107 | } | |
108 | ||
109 | ||
110 | void LED:: | |
111 | setState(bool state) | |
112 | { | |
113 | state_ = state; | |
114 | update(); | |
115 | } | |
116 | ||
117 | void LED:: | |
118 | toggleState() | |
119 | { | |
120 | state_ = !state_; | |
121 | update(); | |
122 | } | |
123 | ||
124 | int LED:: | |
125 | heightForWidth(int width) const | |
126 | { | |
127 | return width; | |
128 | } | |
129 | ||
130 | QSize LED:: | |
131 | sizeHint() const | |
132 | { | |
133 | return QSize(diamX_, diamY_); | |
134 | } | |
135 | ||
136 | QSize LED:: | |
137 | minimumSizeHint() const | |
138 | { | |
139 | return QSize(diamX_, diamY_); | |
140 | } | |
141 | ||
142 | void LED:: | |
143 | paintEvent(QPaintEvent *event) | |
144 | { | |
145 | Q_UNUSED(event); | |
146 | ||
147 | QPainter p(this); | |
148 | ||
149 | QRect geo = geometry(); | |
150 | int width = geo.width(); | |
151 | int height = geo.height(); | |
152 | ||
153 | int x=0, y=0; | |
154 | if ( alignment_ & Qt::AlignLeft ) | |
155 | x = 0; | |
156 | else if ( alignment_ & Qt::AlignRight ) | |
157 | x = width-diamX_; | |
158 | else if ( alignment_ & Qt::AlignHCenter ) | |
159 | x = (width-diamX_)/2; | |
160 | else if ( alignment_ & Qt::AlignJustify ) | |
161 | x = 0; | |
162 | ||
163 | if ( alignment_ & Qt::AlignTop ) | |
164 | y = 0; | |
165 | else if ( alignment_ & Qt::AlignBottom ) | |
166 | y = height-diamY_; | |
167 | else if ( alignment_ & Qt::AlignVCenter ) | |
168 | y = (height-diamY_)/2; | |
169 | ||
170 | QRadialGradient g(x+diamX_/2, y+diamY_/2, diamX_*0.4, | |
171 | diamX_*0.4, diamY_*0.4); | |
172 | ||
173 | g.setColorAt(0, Qt::white); | |
174 | if ( state_ ) | |
175 | g.setColorAt(1, color_); | |
176 | else | |
177 | g.setColorAt(1, Qt::black); | |
178 | QBrush brush(g); | |
179 | ||
180 | p.setPen(color_); | |
181 | p.setRenderHint(QPainter::Antialiasing, true); | |
182 | p.setBrush(brush); | |
183 | p.drawEllipse(x, y, diamX_-1, diamY_-1); | |
184 | ||
185 | if ( flashRate_ > 0 && flashing_ ) | |
186 | timer_->start(flashRate_); | |
187 | else | |
188 | timer_->stop(); | |
189 | } | |
190 | ||
191 | bool LED:: | |
192 | state() const | |
193 | { | |
194 | return state_; | |
195 | } | |
196 | ||
197 | bool LED:: | |
198 | isFlashing() const | |
199 | { | |
200 | return flashing_; | |
201 | } | |
202 | ||
203 | int LED:: | |
204 | flashRate() const | |
205 | { | |
206 | return flashRate_; | |
207 | } |