KWidgetsAddons

kled.h
1/*
2 This file is part of the KDE libraries
3 SPDX-FileCopyrightText: 1998 Jörg Habenicht <j.habenicht@europemail.com>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#ifndef KLED_H
9#define KLED_H
10
11#include <kwidgetsaddons_export.h>
12
13#include <QWidget>
14#include <memory>
15
16class QColor;
17
18/**
19 * @class KLed kled.h KLed
20 *
21 * @short An LED widget.
22 *
23 * Displays a round or rectangular light emitting diode.
24 *
25 * It is configurable to arbitrary colors, the two on/off states and three
26 * styles (or "looks");
27 *
28 * It may display itself in a performant flat view, a round view with
29 * light spot or a round view sunken in the screen.
30 *
31 * \image html kled.png "KLed Widget"
32 *
33 * @author Joerg Habenicht, Richard J. Moore (rich@kde.org) 1998, 1999
34 */
35class KWIDGETSADDONS_EXPORT KLed : public QWidget
36{
38 Q_PROPERTY(State state READ state WRITE setState)
39 Q_PROPERTY(Shape shape READ shape WRITE setShape)
40 Q_PROPERTY(Look look READ look WRITE setLook)
41 Q_PROPERTY(QColor color READ color WRITE setColor)
42 Q_PROPERTY(int darkFactor READ darkFactor WRITE setDarkFactor)
43
44public:
45 /**
46 * Status of the light is on/off.
47 * @short LED on/off.
48 */
49 enum State {
50 Off,
51 On
52 };
54
55 /**
56 * Shades of the lamp.
57 * @short LED shape
58 */
59 enum Shape {
60 Rectangular,
61 Circular
62 };
64
65 /**
66 * Displays a flat, round or sunken LED.
67 *
68 * @short LED look.
69 */
70 enum Look {
71 Flat,
72 Raised,
73 Sunken,
74 };
75 Q_ENUM(Look)
76
77 /**
78 * Constructs a green, round LED widget which will initially
79 * be turned on.
80 *
81 * @param parent The parent widget.
82 */
83 explicit KLed(QWidget *parent = nullptr);
84
85 /**
86 * Constructs a round LED widget with the supplied color which will
87 * initially be turned on.
88 *
89 * @param color Initial color of the LED.
90 * @param parent The parent widget.
91 * @short Constructor
92 */
93 explicit KLed(const QColor &color, QWidget *parent = nullptr);
94
95 /**
96 * Constructor with the color, state and look.
97 *
98 * Differs from above only in the parameters, which configure all settings.
99 *
100 * @param color Initial color of the LED.
101 * @param state Sets the State.
102 * @param look Sets the Look.
103 * @param shape Sets the Shape (rectangular or circular).
104 * @param parent The parent widget.
105 * @short Constructor
106 */
107 KLed(const QColor &color, KLed::State state, KLed::Look look, KLed::Shape shape, QWidget *parent = nullptr);
108
109 /**
110 * Destroys the LED widget.
111 * @short Destructor
112 */
113 ~KLed() override;
114
115 /**
116 * Returns the current color of the widget.
117 *
118 * @returns LED color
119 * @see setColor()
120 */
121 QColor color() const;
122
123 /**
124 * Returns the current state of the widget (on/off).
125 * @returns LED state
126 *
127 * @see State
128 */
129 State state() const;
130
131 /**
132 * Returns the current look of the widget.
133 * @returns LED look
134 *
135 * @see Look
136 */
137 Look look() const;
138
139 /**
140 * Returns the current shape of the widget.
141 * @returns LED shape
142 *
143 * @see Shape
144 */
145 Shape shape() const;
146
147 /**
148 * Returns the factor to darken the LED.
149 * @returns dark factor
150 *
151 * @see setDarkFactor()
152 */
153 int darkFactor() const;
154
155 /**
156 * Set the color of the widget.
157 *
158 * The LED is shown with @p color when in the KLed::On state
159 * or with the darken color in KLed::Off state.
160 *
161 * The widget calls the update() method, so it will
162 * be updated when entering the main event loop.
163 *
164 * @param color New color of the LED.
165 *
166 * @see color() darkFactor()
167 */
168 void setColor(const QColor &color);
169
170 /**
171 * Sets the state of the widget to On or Off.
172 *
173 * @param state The LED state: on or off.
174 *
175 * @see on() off() toggle()
176 */
177 void setState(State state);
178
179 /**
180 * Sets the look of the widget.
181 *
182 * The look may be Flat, Raised or Sunken.
183 *
184 * The widget calls the update() method, so it will
185 * be updated when entering the main event loop.
186 *
187 * @param look New look of the LED.
188 *
189 * @see Look
190 */
191 void setLook(Look look);
192
193 /**
194 * Set the shape of the LED.
195 *
196 * @param shape The LED shape.
197 * @short Set LED shape.
198 */
199 void setShape(Shape shape);
200
201 /**
202 * Sets the factor to darken the LED in KLed::Off state.
203 *
204 * The @p darkFactor should be greater than 100, otherwise the LED
205 * becomes lighter in KLed::Off state.
206 *
207 * Defaults to 300.
208 *
209 * @param darkFactor Sets the factor to darken the LED.
210 *
211 * @see setColor
212 */
213 void setDarkFactor(int darkFactor);
214
215 QSize sizeHint() const override;
216 QSize minimumSizeHint() const override;
217
218public Q_SLOTS:
219
220 /**
221 * Toggles the state of the led from Off to On or vice versa.
222 */
223 void toggle();
224
225 /**
226 * Sets the state of the widget to On.
227 *
228 * @see off() toggle() setState()
229 */
230 void on();
231
232 /**
233 * Sets the state of the widget to Off.
234 *
235 * @see on() toggle() setState()
236 */
237 void off();
238
239protected:
240 void paintEvent(QPaintEvent *) override;
241 void resizeEvent(QResizeEvent *) override;
242
243private:
244 /**
245 * @internal
246 * invalidates caches after property changes and calls update()
247 */
248 KWIDGETSADDONS_NO_EXPORT void updateCachedPixmap();
249
250 KWIDGETSADDONS_NO_EXPORT void updateAccessibleName();
251
252private:
253 std::unique_ptr<class KLedPrivate> const d;
254};
255
256#endif
An LED widget.
Definition kled.h:36
void on()
Sets the state of the widget to On.
Definition kled.cpp:141
void setColor(const QColor &color)
Set the color of the widget.
Definition kled.cpp:99
State
Status of the light is on/off.
Definition kled.h:49
Shape
Shades of the lamp.
Definition kled.h:59
void setDarkFactor(int darkFactor)
Sets the factor to darken the LED in KLed::Off state.
Definition kled.cpp:109
~KLed() override
Destroys the LED widget.
Look
Displays a flat, round or sunken LED.
Definition kled.h:70
void setShape(Shape shape)
Set the shape of the LED.
Definition kled.cpp:89
KLed(QWidget *parent=nullptr)
Constructs a green, round LED widget which will initially be turned on.
Definition kled.cpp:28
void off()
Sets the state of the widget to Off.
Definition kled.cpp:146
void setState(State state)
Sets the state of the widget to On or Off.
Definition kled.cpp:78
KLed(const QColor &color, KLed::State state, KLed::Look look, KLed::Shape shape, QWidget *parent=nullptr)
Constructor with the color, state and look.
void toggle()
Toggles the state of the led from Off to On or vice versa.
Definition kled.cpp:134
void setLook(Look look)
Sets the look of the widget.
Definition kled.cpp:124
Q_ENUM(...)
Q_OBJECTQ_OBJECT
Q_PROPERTY(...)
Q_SLOTSQ_SLOTS
QObject * parent() const const
QWidget(QWidget *parent, Qt::WindowFlags f)
virtual void paintEvent(QPaintEvent *event)
virtual void resizeEvent(QResizeEvent *event)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 28 2025 12:02:04 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.