Debian Codegraph @

src/qled.cpp

raw symbols SCIP
1 /* LED class adapted from KDE to use under QT
2 *
3 Copyright (C) 1998 Jörg Habenicht ([email protected])
4 Copyright (C) 2010 Christoph Feck <[email protected]>
5 Copyright (C) 2011 Mario Stephan <[email protected]>
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
16
17 You should have received a copy of the GNU Library General Public License
18 along with this library; see the file COPYING.LIB. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA.
21 */
22
23 #include "qled.h"
24
25 #include <QStyle>
26 #include <QStyleOption>
27 #include <QtGui/QImage>
28 #include <QtGui/QPainter>
29
30 class QLed::Private {
31 public:
32 Private()
33 : darkFactor(300)
34 , state(On)
35 , look(Raised)
36 , shape(Circular)
37 {
38 }
39
40 int darkFactor;
41 QColor color;
42 State state;
43 Look look;
44 Shape shape;
45 QPixmap cachedPixmap[2]; // for both states
46 QStyle::ControlElement ce_indicatorLedCircular;
47 QStyle::ControlElement ce_indicatorLedRectangular;
48 };
49
50 QLed::QLed(QWidget* parent)
51 : QWidget(parent)
52 , d(new Private)
53 {
54 setColor(Qt::green);
55 }
56
57 QLed::QLed(const QColor& color, QWidget* parent)
58 : QWidget(parent)
59 , d(new Private)
60 {
61 setColor(color);
62 }
63
64 QLed::QLed(const QColor& color, State state, Look look, Shape shape,
65 QWidget* parent)
66 : QWidget(parent)
67 , d(new Private)
68 {
69 d->state = (state == Off ? Off : On);
70 d->look = look;
71 d->shape = shape;
72 setColor(color);
73 }
74
75 QLed::~QLed()
76 {
77 delete d;
78 }
79
80 void QLed::paintEvent(QPaintEvent*)
81 {
82 switch (d->shape) {
83 case Rectangular:
84 switch (d->look) {
85 case Sunken:
86 paintRectFrame(false);
87 break;
88 case Raised:
89 paintRectFrame(true);
90 break;
91 case Flat:
92 paintRect();
93 break;
94 }
95 break;
96 case Circular:
97 switch (d->look) {
98 case Flat:
99 paintFlat();
100 break;
101 case Raised:
102 paintRaised();
103 break;
104 case Sunken:
105 paintSunken();
106 break;
107 }
108 break;
109 }
110 }
111
112 int QLed::ledWidth() const
113 {
114 // Make sure the LED is round!
115 int size = qMin(width(), height());
116
117 // leave one pixel border
118 size -= 2;
119
120 return qMax(0, size);
121 }
122
123 bool QLed::paintCachedPixmap()
124 {
125 if (d->cachedPixmap[d->state].isNull()) {
126 return false;
127 }
128 QPainter painter(this);
129 painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
130 return true;
131 }
132
133 void QLed::paintFlat()
134 {
135 paintLed(Circular, Flat);
136 }
137
138 void QLed::paintRaised()
139 {
140 paintLed(Circular, Raised);
141 }
142
143 void QLed::paintSunken()
144 {
145 paintLed(Circular, Sunken);
146 }
147
148 void QLed::paintRect()
149 {
150 paintLed(Rectangular, Flat);
151 }
152
153 void QLed::paintRectFrame(bool raised)
154 {
155 paintLed(Rectangular, raised ? Raised : Sunken);
156 }
157
158 QLed::State QLed::state() const
159 {
160 return d->state;
161 }
162
163 QLed::Shape QLed::shape() const
164 {
165 return d->shape;
166 }
167
168 QColor QLed::color() const
169 {
170 return d->color;
171 }
172
173 QLed::Look QLed::look() const
174 {
175 return d->look;
176 }
177
178 void QLed::setState(State state)
179 {
180 if (d->state == state)
181 return;
182
183 d->state = (state == Off ? Off : On);
184 updateCachedPixmap();
185 }
186
187 void QLed::setShape(Shape shape)
188 {
189 if (d->shape == shape)
190 return;
191
192 d->shape = shape;
193 updateCachedPixmap();
194 }
195
196 void QLed::setColor(const QColor& color)
197 {
198 if (d->color == color)
199 return;
200
201 d->color = color;
202 updateCachedPixmap();
203 }
204
205 void QLed::setDarkFactor(int darkFactor)
206 {
207 if (d->darkFactor == darkFactor)
208 return;
209
210 d->darkFactor = darkFactor;
211 updateCachedPixmap();
212 }
213
214 int QLed::darkFactor() const
215 {
216 return d->darkFactor;
217 }
218
219 void QLed::setLook(Look look)
220 {
221 if (d->look == look)
222 return;
223
224 d->look = look;
225 updateCachedPixmap();
226 }
227
228 void QLed::toggle()
229 {
230 d->state = (d->state == On ? Off : On);
231 updateCachedPixmap();
232 }
233
234 void QLed::on()
235 {
236 setState(On);
237 }
238
239 void QLed::off()
240 {
241 setState(Off);
242 }
243
244 void QLed::resizeEvent(QResizeEvent*)
245 {
246 updateCachedPixmap();
247 }
248
249 QSize QLed::sizeHint() const
250 {
251 QStyleOption option;
252 option.initFrom(this);
253 int iconSize = style()->pixelMetric(QStyle::PM_SmallIconSize, &option, this);
254 return QSize(iconSize, iconSize);
255 }
256
257 QSize QLed::minimumSizeHint() const
258 {
259 return QSize(16, 16);
260 }
261
262 void QLed::updateCachedPixmap()
263 {
264 d->cachedPixmap[Off] = QPixmap();
265 d->cachedPixmap[On] = QPixmap();
266 update();
267 }
268
269 void QLed::paintLed(Shape shape, Look look)
270 {
271 if (paintCachedPixmap()) {
272 return;
273 }
274
275 QSize size(width() - 2, height() - 2);
276 if (shape == Circular) {
277 const int width = ledWidth();
278 size = QSize(width, width);
279 }
280 QPointF center(size.width() / 2.0, size.height() / 2.0);
281 const int smallestSize = qMin(size.width(), size.height());
282 QPainter painter;
283 QImage image(size, QImage::Format_ARGB32_Premultiplied);
284 image.fill(0);
285 QRadialGradient fillGradient(center, smallestSize / 2.0, QPointF(center.x(), size.height() / 3.0));
286 const QColor fillColor = d->state != Off ? d->color : d->color.dark(d->darkFactor);
287 fillGradient.setColorAt(0.0, fillColor.light(250));
288 fillGradient.setColorAt(0.5, fillColor.light(130));
289 fillGradient.setColorAt(1.0, fillColor);
290 QConicalGradient borderGradient(center, look == Sunken ? 90 : -90);
291 QColor borderColor = palette().color(QPalette::Dark);
292 if (d->state == On) {
293 QColor glowOverlay = fillColor;
294 glowOverlay.setAlpha(80);
295 borderColor = overlayColors(borderColor, glowOverlay);
296 }
297 borderGradient.setColorAt(0.2, borderColor);
298 borderGradient.setColorAt(0.5, palette().color(QPalette::Light));
299 borderGradient.setColorAt(0.8, borderColor);
300
301 painter.begin(&image);
302 painter.setRenderHint(QPainter::Antialiasing);
303 painter.setBrush(look == Flat ? QBrush(fillColor) : QBrush(fillGradient));
304 const QBrush penBrush = (look == Flat) ? QBrush(borderColor) : QBrush(borderGradient);
305 const qreal penWidth = smallestSize / 8.0;
306 painter.setPen(QPen(penBrush, penWidth));
307 QRectF r(penWidth / 2.0, penWidth / 2.0, size.width() - penWidth, size.height() - penWidth);
308 if (shape == Rectangular) {
309 painter.drawRect(r);
310 } else {
311 painter.drawEllipse(r);
312 }
313 painter.end();
314
315 d->cachedPixmap[d->state] = QPixmap::fromImage(image);
316 painter.begin(this);
317 painter.drawPixmap(1, 1, d->cachedPixmap[d->state]);
318 painter.end();
319 }
320
321 QColor QLed::overlayColors(const QColor& base, const QColor& paint,
322 QPainter::CompositionMode comp)
323 {
324 QImage img(1, 1, QImage::Format_ARGB32_Premultiplied);
325 QPainter p(&img);
326 QColor start = base;
327 start.setAlpha(255); // opaque
328 p.fillRect(0, 0, 1, 1, start);
329 p.setCompositionMode(comp);
330 p.fillRect(0, 0, 1, 1, paint);
331 p.end();
332 return img.pixel(0, 0);
333 }