• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

KDEUI

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

KDE's Doxygen guidelines are available online.

KDEUI

Skip menu "KDEUI"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Modules
  • Related Pages

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal