Mercurial > hg > svgui
comparison widgets/LEDButton.cpp @ 1324:13d9b422f7fe zoom
Merge from default branch
author | Chris Cannam |
---|---|
date | Mon, 17 Sep 2018 13:51:31 +0100 |
parents | a34a2a25907c |
children | d39db4673676 |
comparison
equal
deleted
inserted
replaced
1183:57d192e26331 | 1324:13d9b422f7fe |
---|---|
21 Ported to Qt4 by Chris Cannam. | 21 Ported to Qt4 by Chris Cannam. |
22 */ | 22 */ |
23 | 23 |
24 | 24 |
25 #include "LEDButton.h" | 25 #include "LEDButton.h" |
26 #include "WidgetScale.h" | |
26 | 27 |
27 #include <QPainter> | 28 #include <QPainter> |
28 #include <QImage> | 29 #include <QImage> |
29 #include <QColor> | 30 #include <QColor> |
30 #include <QMouseEvent> | 31 #include <QMouseEvent> |
85 LEDButton::mousePressEvent(QMouseEvent *e) | 86 LEDButton::mousePressEvent(QMouseEvent *e) |
86 { | 87 { |
87 cerr << "LEDButton(" << this << ")::mousePressEvent" << endl; | 88 cerr << "LEDButton(" << this << ")::mousePressEvent" << endl; |
88 | 89 |
89 if (e->buttons() & Qt::LeftButton) { | 90 if (e->buttons() & Qt::LeftButton) { |
90 toggle(); | 91 toggle(); |
91 bool newState = state(); | 92 bool newState = state(); |
92 SVDEBUG << "emitting new state " << newState << endl; | 93 SVDEBUG << "emitting new state " << newState << endl; |
93 emit stateChanged(newState); | 94 emit stateChanged(newState); |
94 } | 95 } |
95 } | 96 } |
96 | 97 |
97 void | 98 void |
98 LEDButton::enterEvent(QEvent *) | 99 LEDButton::enterEvent(QEvent *) |
111 { | 112 { |
112 QPainter paint; | 113 QPainter paint; |
113 QColor color; | 114 QColor color; |
114 QBrush brush; | 115 QBrush brush; |
115 QPen pen; | 116 QPen pen; |
116 | 117 |
117 // First of all we want to know what area should be updated | 118 // First of all we want to know what area should be updated |
118 // Initialize coordinates, width, and height of the LED | 119 // Initialize coordinates, width, and height of the LED |
119 int width = this->width(); | 120 int width = this->width(); |
120 | 121 |
121 // Make sure the LED is round! | 122 // Make sure the LED is round! |
122 if (width > this->height()) | 123 if (width > this->height()) |
123 width = this->height(); | 124 width = this->height(); |
124 width -= 2; // leave one pixel border | 125 width -= 2; // leave one pixel border |
125 if (width < 0) | 126 if (width < 0) |
126 width = 0; | 127 width = 0; |
127 | 128 |
128 paint.begin(this); | 129 paint.begin(this); |
129 | 130 |
130 paint.setRenderHint(QPainter::Antialiasing, true); | 131 paint.setRenderHint(QPainter::Antialiasing, true); |
131 | 132 |
151 // shrink the light on the LED to a size about 2/3 of the complete LED | 152 // shrink the light on the LED to a size about 2/3 of the complete LED |
152 int pos = width/5 + 1; | 153 int pos = width/5 + 1; |
153 int light_width = width; | 154 int light_width = width; |
154 light_width *= 2; | 155 light_width *= 2; |
155 light_width /= 3; | 156 light_width /= 3; |
156 | 157 |
157 // Calculate the LEDīs "light factor": | 158 // Calculate the LEDīs "light factor": |
158 int light_quote = (130*2/(light_width?light_width:1))+100; | 159 int light_quote = (130*2/(light_width?light_width:1))+100; |
159 | 160 |
160 // Now draw the bright spot on the LED: | 161 // Now draw the bright spot on the LED: |
161 while (light_width) { | 162 while (light_width) { |
162 color = color.light( light_quote ); // make color lighter | 163 color = color.light( light_quote ); // make color lighter |
163 pen.setColor( color ); // set color as pen color | 164 pen.setColor( color ); // set color as pen color |
164 paint.setPen( pen ); // select the pen for drawing | 165 paint.setPen( pen ); // select the pen for drawing |
165 paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle) | 166 paint.drawEllipse( pos, pos, light_width, light_width ); // draw the ellipse (circle) |
166 light_width--; | 167 light_width--; |
167 if (!light_width) | 168 if (!light_width) |
168 break; | 169 break; |
169 paint.drawEllipse( pos, pos, light_width, light_width ); | 170 paint.drawEllipse( pos, pos, light_width, light_width ); |
170 light_width--; | 171 light_width--; |
171 if (!light_width) | 172 if (!light_width) |
172 break; | 173 break; |
173 paint.drawEllipse( pos, pos, light_width, light_width ); | 174 paint.drawEllipse( pos, pos, light_width, light_width ); |
174 pos++; light_width--; | 175 pos++; light_width--; |
175 } | 176 } |
176 | 177 |
177 paint.drawPoint(pos, pos); | 178 paint.drawPoint(pos, pos); |
178 | 179 |
179 // Drawing of bright spot finished, now draw a thin border | 180 // Drawing of bright spot finished, now draw a thin border |
189 | 190 |
190 int angle = -720; | 191 int angle = -720; |
191 color = palette().light().color(); | 192 color = palette().light().color(); |
192 | 193 |
193 for (int arc = 120; arc < 2880; arc += 240) { | 194 for (int arc = 120; arc < 2880; arc += 240) { |
194 pen.setColor(color); | 195 pen.setColor(color); |
195 paint.setPen(pen); | 196 paint.setPen(pen); |
196 int w = width - pen.width()/2; | 197 int w = width - pen.width()/2; |
197 paint.drawArc(pen.width()/2 + 1, pen.width()/2 + 1, w - 2, w - 2, angle + arc, 240); | 198 paint.drawArc(pen.width()/2 + 1, pen.width()/2 + 1, w - 2, w - 2, angle + arc, 240); |
198 paint.drawArc(pen.width()/2 + 1, pen.width()/2 + 1, w - 2, w - 2, angle - arc, 240); | 199 paint.drawArc(pen.width()/2 + 1, pen.width()/2 + 1, w - 2, w - 2, angle - arc, 240); |
199 color = color.dark(110); //FIXME: this should somehow use the contrast value | 200 color = color.dark(110); //FIXME: this should somehow use the contrast value |
200 } // end for ( angle = 720; angle < 6480; angle += 160 ) | 201 } // end for ( angle = 720; angle < 6480; angle += 160 ) |
201 | 202 |
202 paint.end(); | 203 paint.end(); |
203 } | 204 } |
204 | 205 |
205 bool | 206 bool |
217 void | 218 void |
218 LEDButton::setState( bool state ) | 219 LEDButton::setState( bool state ) |
219 { | 220 { |
220 if (led_state != state) | 221 if (led_state != state) |
221 { | 222 { |
222 led_state = state; | 223 led_state = state; |
223 update(); | 224 update(); |
224 } | 225 } |
225 } | 226 } |
226 | 227 |
227 void | 228 void |
228 LEDButton::toggleState() | 229 LEDButton::toggleState() |
234 | 235 |
235 void | 236 void |
236 LEDButton::setColor(const QColor& col) | 237 LEDButton::setColor(const QColor& col) |
237 { | 238 { |
238 if(led_color!=col) { | 239 if(led_color!=col) { |
239 led_color = col; | 240 led_color = col; |
240 d->offcolor = col.dark(d->dark_factor); | 241 d->offcolor = col.dark(d->dark_factor); |
241 update(); | 242 update(); |
242 } | 243 } |
243 } | 244 } |
244 | 245 |
245 void | 246 void |
246 LEDButton::setDarkFactor(int darkfactor) | 247 LEDButton::setDarkFactor(int darkfactor) |
247 { | 248 { |
248 if (d->dark_factor != darkfactor) { | 249 if (d->dark_factor != darkfactor) { |
249 d->dark_factor = darkfactor; | 250 d->dark_factor = darkfactor; |
250 d->offcolor = led_color.dark(darkfactor); | 251 d->offcolor = led_color.dark(darkfactor); |
251 update(); | 252 update(); |
252 } | 253 } |
253 } | 254 } |
254 | 255 |
255 int | 256 int |
256 LEDButton::darkFactor() const | 257 LEDButton::darkFactor() const |
277 } | 278 } |
278 | 279 |
279 QSize | 280 QSize |
280 LEDButton::sizeHint() const | 281 LEDButton::sizeHint() const |
281 { | 282 { |
282 return QSize(17, 17); | 283 return WidgetScale::scaleQSize(QSize(17, 17)); |
283 } | 284 } |
284 | 285 |
285 QSize | 286 QSize |
286 LEDButton::minimumSizeHint() const | 287 LEDButton::minimumSizeHint() const |
287 { | 288 { |
288 return QSize(17, 17); | 289 return WidgetScale::scaleQSize(QSize(17, 17)); |
289 } | 290 } |
290 | 291 |