comparison widgets/LEDButton.cpp @ 1216:dc2af6616c83

Merge from branch 3.0-integration
author Chris Cannam
date Fri, 13 Jan 2017 10:29:50 +0000
parents 3f5c82034f9b
children a34a2a25907c
comparison
equal deleted inserted replaced
1048:e8102ff5573b 1216:dc2af6616c83
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>
36 { 37 {
37 friend class LEDButton; 38 friend class LEDButton;
38 39
39 int dark_factor; 40 int dark_factor;
40 QColor offcolor; 41 QColor offcolor;
41 QPixmap *off_map;
42 QPixmap *on_map;
43 }; 42 };
44 43
45 44
46 LEDButton::LEDButton(QWidget *parent) : 45 LEDButton::LEDButton(QWidget *parent) :
47 QWidget(parent), 46 QWidget(parent),
49 { 48 {
50 QColor col(Qt::green); 49 QColor col(Qt::green);
51 d = new LEDButton::LEDButtonPrivate; 50 d = new LEDButton::LEDButtonPrivate;
52 d->dark_factor = 300; 51 d->dark_factor = 300;
53 d->offcolor = col.dark(300); 52 d->offcolor = col.dark(300);
54 d->off_map = 0;
55 d->on_map = 0;
56 53
57 setColor(col); 54 setColor(col);
58 } 55 }
59 56
60 57
63 led_state(true) 60 led_state(true)
64 { 61 {
65 d = new LEDButton::LEDButtonPrivate; 62 d = new LEDButton::LEDButtonPrivate;
66 d->dark_factor = 300; 63 d->dark_factor = 300;
67 d->offcolor = col.dark(300); 64 d->offcolor = col.dark(300);
68 d->off_map = 0;
69 d->on_map = 0;
70 65
71 setColor(col); 66 setColor(col);
72 } 67 }
73 68
74 LEDButton::LEDButton(const QColor& col, bool state, QWidget *parent) : 69 LEDButton::LEDButton(const QColor& col, bool state, QWidget *parent) :
76 led_state(state) 71 led_state(state)
77 { 72 {
78 d = new LEDButton::LEDButtonPrivate; 73 d = new LEDButton::LEDButtonPrivate;
79 d->dark_factor = 300; 74 d->dark_factor = 300;
80 d->offcolor = col.dark(300); 75 d->offcolor = col.dark(300);
81 d->off_map = 0;
82 d->on_map = 0;
83 76
84 setColor(col); 77 setColor(col);
85 } 78 }
86 79
87 LEDButton::~LEDButton() 80 LEDButton::~LEDButton()
88 { 81 {
89 delete d->off_map;
90 delete d->on_map;
91 delete d; 82 delete d;
92 } 83 }
93 84
94 void 85 void
95 LEDButton::mousePressEvent(QMouseEvent *e) 86 LEDButton::mousePressEvent(QMouseEvent *e)
133 width = this->height(); 124 width = this->height();
134 width -= 2; // leave one pixel border 125 width -= 2; // leave one pixel border
135 if (width < 0) 126 if (width < 0)
136 width = 0; 127 width = 0;
137 128
138 QPixmap *tmpMap = 0; 129 paint.begin(this);
139
140 if (led_state) {
141 if (d->on_map) {
142 if (d->on_map->size() == size()) {
143 paint.begin(this);
144 paint.drawPixmap(0, 0, *d->on_map);
145 paint.end();
146 return;
147 } else {
148 delete d->on_map;
149 d->on_map = 0;
150 }
151 }
152 } else {
153 if (d->off_map) {
154 if (d->off_map->size() == size()) {
155 paint.begin(this);
156 paint.drawPixmap(0, 0, *d->off_map);
157 paint.end();
158 return;
159 } else {
160 delete d->off_map;
161 d->off_map = 0;
162 }
163 }
164 }
165
166 int scale = 1;
167 width *= scale;
168
169 tmpMap = new QPixmap(width, width);
170 tmpMap->fill(palette().background().color());
171 paint.begin(tmpMap);
172 130
173 paint.setRenderHint(QPainter::Antialiasing, true); 131 paint.setRenderHint(QPainter::Antialiasing, true);
174 132
175 // Set the color of the LED according to given parameters 133 // Set the color of the LED according to given parameters
176 color = (led_state) ? led_color : d->offcolor; 134 color = (led_state) ? led_color : d->offcolor;
180 brush.setStyle(Qt::SolidPattern); 138 brush.setStyle(Qt::SolidPattern);
181 brush.setColor(color); 139 brush.setColor(color);
182 paint.setBrush(brush); 140 paint.setBrush(brush);
183 141
184 // Draws a "flat" LED with the given color: 142 // Draws a "flat" LED with the given color:
185 paint.drawEllipse( scale, scale, width - scale*2, width - scale*2 ); 143 paint.drawEllipse( 1, 1, width - 2, width - 2 );
186 144
187 // Draw the bright light spot of the LED now, using modified "old" 145 // Draw the bright light spot of the LED now, using modified "old"
188 // painter routine taken from KDEUI´s LEDButton widget: 146 // painter routine taken from KDEUI´s LEDButton widget:
189 147
190 // Setting the new width of the pen is essential to avoid "pixelized" 148 // Setting the new width of the pen is essential to avoid "pixelized"
191 // shadow like it can be observed with the old LED code 149 // shadow like it can be observed with the old LED code
192 pen.setWidth( 2 * scale ); 150 pen.setWidth( 2 );
193 151
194 // 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
195 int pos = width/5 + 1; 153 int pos = width/5 + 1;
196 int light_width = width; 154 int light_width = width;
197 light_width *= 2; 155 light_width *= 2;
215 break; 173 break;
216 paint.drawEllipse( pos, pos, light_width, light_width ); 174 paint.drawEllipse( pos, pos, light_width, light_width );
217 pos++; light_width--; 175 pos++; light_width--;
218 } 176 }
219 177
178 paint.drawPoint(pos, pos);
179
220 // Drawing of bright spot finished, now draw a thin border 180 // Drawing of bright spot finished, now draw a thin border
221 // around the LED which resembles a shadow with light coming 181 // around the LED which resembles a shadow with light coming
222 // from the upper left. 182 // from the upper left.
223 183
224 // pen.setWidth( 2 * scale + 1 ); // ### shouldn't this value be smaller for smaller LEDs? 184 pen.setWidth(2);
225 pen.setWidth(2 * scale);
226 brush.setStyle(Qt::NoBrush); 185 brush.setStyle(Qt::NoBrush);
227 paint.setBrush(brush); // This avoids filling of the ellipse 186 paint.setBrush(brush); // This avoids filling of the ellipse
228 187
229 // Set the initial color value to colorGroup().light() (bright) and start 188 // Set the initial color value to colorGroup().light() (bright) and start
230 // drawing the shadow border at 45° (45*16 = 720). 189 // drawing the shadow border at 45° (45*16 = 720).
233 color = palette().light().color(); 192 color = palette().light().color();
234 193
235 for (int arc = 120; arc < 2880; arc += 240) { 194 for (int arc = 120; arc < 2880; arc += 240) {
236 pen.setColor(color); 195 pen.setColor(color);
237 paint.setPen(pen); 196 paint.setPen(pen);
238 int w = width - pen.width()/2 - scale + 1; 197 int w = width - pen.width()/2;
239 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);
240 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);
241 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
242 } // end for ( angle = 720; angle < 6480; angle += 160 ) 201 } // end for ( angle = 720; angle < 6480; angle += 160 )
243 202
244 paint.end(); 203 paint.end();
245 //
246 // painting done
247
248 QPixmap *&dest = led_state ? d->on_map : d->off_map;
249
250 if (scale > 1) {
251
252 QImage i = tmpMap->toImage();
253 width /= scale;
254 delete tmpMap;
255 dest = new QPixmap(QPixmap::fromImage
256 (i.scaled(width, width,
257 Qt::KeepAspectRatio,
258 Qt::SmoothTransformation)));
259
260 } else {
261
262 dest = tmpMap;
263 }
264
265 paint.begin(this);
266 paint.drawPixmap(0, 0, *dest);
267 paint.end();
268 } 204 }
269 205
270 bool 206 bool
271 LEDButton::state() const 207 LEDButton::state() const
272 { 208 {
301 LEDButton::setColor(const QColor& col) 237 LEDButton::setColor(const QColor& col)
302 { 238 {
303 if(led_color!=col) { 239 if(led_color!=col) {
304 led_color = col; 240 led_color = col;
305 d->offcolor = col.dark(d->dark_factor); 241 d->offcolor = col.dark(d->dark_factor);
306 delete d->on_map;
307 d->on_map = 0;
308 delete d->off_map;
309 d->off_map = 0;
310 update(); 242 update();
311 } 243 }
312 } 244 }
313 245
314 void 246 void
346 } 278 }
347 279
348 QSize 280 QSize
349 LEDButton::sizeHint() const 281 LEDButton::sizeHint() const
350 { 282 {
351 return QSize(17, 17); 283 return WidgetScale::scaleQSize(QSize(17, 17));
352 } 284 }
353 285
354 QSize 286 QSize
355 LEDButton::minimumSizeHint() const 287 LEDButton::minimumSizeHint() const
356 { 288 {
357 return QSize(17, 17); 289 return WidgetScale::scaleQSize(QSize(17, 17));
358 } 290 }
359 291