comparison widgets/LEDButton.cpp @ 34:c43f2c4f66f2

* As previous commit
author Chris Cannam
date Fri, 17 Feb 2006 18:11:08 +0000
parents 651e4e868bcc
children 01ab51f72e84
comparison
equal deleted inserted replaced
33:651e4e868bcc 34:c43f2c4f66f2
22 #include <QPainter> 22 #include <QPainter>
23 #include <QImage> 23 #include <QImage>
24 #include <QColor> 24 #include <QColor>
25 #include <QMouseEvent> 25 #include <QMouseEvent>
26 26
27 #include <iostream>
28
27 29
28 class LEDButton::LEDButtonPrivate 30 class LEDButton::LEDButtonPrivate
29 { 31 {
30 friend class LEDButton; 32 friend class LEDButton;
31 33
36 }; 38 };
37 39
38 40
39 LEDButton::LEDButton(QWidget *parent) : 41 LEDButton::LEDButton(QWidget *parent) :
40 QWidget(parent), 42 QWidget(parent),
41 led_state(On) 43 led_state(true)
42 { 44 {
43 QColor col(Qt::green); 45 QColor col(Qt::green);
44 d = new LEDButton::LEDButtonPrivate; 46 d = new LEDButton::LEDButtonPrivate;
45 d->dark_factor = 300; 47 d->dark_factor = 300;
46 d->offcolor = col.dark(300); 48 d->offcolor = col.dark(300);
51 } 53 }
52 54
53 55
54 LEDButton::LEDButton(const QColor& col, QWidget *parent) : 56 LEDButton::LEDButton(const QColor& col, QWidget *parent) :
55 QWidget(parent), 57 QWidget(parent),
56 led_state(On) 58 led_state(true)
57 { 59 {
58 d = new LEDButton::LEDButtonPrivate; 60 d = new LEDButton::LEDButtonPrivate;
59 d->dark_factor = 300; 61 d->dark_factor = 300;
60 d->offcolor = col.dark(300); 62 d->offcolor = col.dark(300);
61 d->off_map = 0; 63 d->off_map = 0;
62 d->on_map = 0; 64 d->on_map = 0;
63 65
64 setColor(col); 66 setColor(col);
65 } 67 }
66 68
67 LEDButton::LEDButton(const QColor& col, LEDButton::State state, QWidget *parent) : 69 LEDButton::LEDButton(const QColor& col, bool state, QWidget *parent) :
68 QWidget(parent), 70 QWidget(parent),
69 led_state(state) 71 led_state(state)
70 { 72 {
71 d = new LEDButton::LEDButtonPrivate; 73 d = new LEDButton::LEDButtonPrivate;
72 d->dark_factor = 300; 74 d->dark_factor = 300;
85 } 87 }
86 88
87 void 89 void
88 LEDButton::mousePressEvent(QMouseEvent *e) 90 LEDButton::mousePressEvent(QMouseEvent *e)
89 { 91 {
92 std::cerr << "LEDButton(" << this << ")::mousePressEvent" << std::endl;
93
90 if (e->buttons() & Qt::LeftButton) { 94 if (e->buttons() & Qt::LeftButton) {
91 toggle(); 95 toggle();
92 emit stateChanged(state()); 96 bool newState = state();
97 std::cerr << "emitting new state " << newState << std::endl;
98 emit stateChanged(newState);
93 } 99 }
94 } 100 }
95 101
96 void 102 void
97 LEDButton::paintEvent(QPaintEvent *) 103 LEDButton::paintEvent(QPaintEvent *)
110 width = this->height(); 116 width = this->height();
111 width -= 2; // leave one pixel border 117 width -= 2; // leave one pixel border
112 if (width < 0) 118 if (width < 0)
113 width = 0; 119 width = 0;
114 120
115 // maybe we could stop HERE, if width <=0 ? 121 QPixmap *tmpMap = 0;
122
123 if (led_state) {
124 if (d->on_map) {
125 paint.begin(this);
126 paint.drawPixmap(0, 0, *d->on_map);
127 paint.end();
128 return;
129 }
130 } else {
131 if (d->off_map) {
132 paint.begin(this);
133 paint.drawPixmap(0, 0, *d->off_map);
134 paint.end();
135 return;
136 }
137 }
116 138
117 int scale = 1; 139 int scale = 1;
118 QPixmap *tmpMap = 0; 140 width *= scale;
119 bool smooth = true; 141
120 142 tmpMap = new QPixmap(width, width);
121 if (smooth) { 143 tmpMap->fill(palette().background().color());
122 if (led_state) { 144 paint.begin(tmpMap);
123 if (d->on_map) {
124 paint.begin(this);
125 paint.drawPixmap(0, 0, *d->on_map);
126 paint.end();
127 return;
128 }
129 } else {
130 if (d->off_map) {
131 paint.begin(this);
132 paint.drawPixmap(0, 0, *d->off_map);
133 paint.end();
134 return;
135 }
136 }
137
138 scale = 1;
139 width *= scale;
140
141 tmpMap = new QPixmap(width, width);
142 tmpMap->fill(palette().background().color());
143 paint.begin(tmpMap);
144
145 } else {
146 paint.begin(this);
147 }
148 145
149 paint.setRenderHint(QPainter::Antialiasing, true); 146 paint.setRenderHint(QPainter::Antialiasing, true);
150 147
151 // Set the color of the LED according to given parameters 148 // Set the color of the LED according to given parameters
152 color = (led_state) ? led_color : d->offcolor; 149 color = (led_state) ? led_color : d->offcolor;
219 216
220 paint.end(); 217 paint.end();
221 // 218 //
222 // painting done 219 // painting done
223 220
224 if (smooth) { 221 QPixmap *&dest = led_state ? d->on_map : d->off_map;
225 QPixmap *&dest = led_state ? d->on_map : d->off_map; 222
223 if (scale > 1) {
224
226 QImage i = tmpMap->toImage(); 225 QImage i = tmpMap->toImage();
227 width /= scale; 226 width /= scale;
228 delete tmpMap; 227 delete tmpMap;
229 dest = new QPixmap(QPixmap::fromImage 228 dest = new QPixmap(QPixmap::fromImage
230 (i.scaled(width, width, 229 (i.scaled(width, width,
231 Qt::KeepAspectRatio, 230 Qt::KeepAspectRatio,
232 Qt::SmoothTransformation))); 231 Qt::SmoothTransformation)));
233 paint.begin(this); 232
234 paint.drawPixmap(0, 0, *dest); 233 } else {
235 paint.end(); 234
236 } 235 dest = tmpMap;
237 } 236 }
238 237
239 LEDButton::State 238 paint.begin(this);
239 paint.drawPixmap(0, 0, *dest);
240 paint.end();
241 }
242
243 bool
240 LEDButton::state() const 244 LEDButton::state() const
241 { 245 {
242 return led_state; 246 return led_state;
243 } 247 }
244 248
247 { 251 {
248 return led_color; 252 return led_color;
249 } 253 }
250 254
251 void 255 void
252 LEDButton::setState( State state ) 256 LEDButton::setState( bool state )
253 { 257 {
254 if (led_state != state) 258 if (led_state != state)
255 { 259 {
256 led_state = state; 260 led_state = state;
257 update(); 261 update();
259 } 263 }
260 264
261 void 265 void
262 LEDButton::toggleState() 266 LEDButton::toggleState()
263 { 267 {
264 led_state = (led_state == On) ? Off : On; 268 led_state = (led_state == true) ? false : true;
265 // setColor(led_color); 269 // setColor(led_color);
266 update(); 270 update();
267 } 271 }
268 272
269 void 273 void
303 } 307 }
304 308
305 void 309 void
306 LEDButton::on() 310 LEDButton::on()
307 { 311 {
308 setState(On); 312 setState(true);
309 } 313 }
310 314
311 void 315 void
312 LEDButton::off() 316 LEDButton::off()
313 { 317 {
314 setState(Off); 318 setState(false);
315 } 319 }
316 320
317 QSize 321 QSize
318 LEDButton::sizeHint() const 322 LEDButton::sizeHint() const
319 { 323 {