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 
16 class 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  */
35 class KWIDGETSADDONS_EXPORT KLed : public QWidget
36 {
37     Q_OBJECT
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 
44 public:
45     /**
46      * Status of the light is on/off.
47      * @short LED on/off.
48      */
49     enum State { Off, On };
50     Q_ENUM(State)
51 
52     /**
53      * Shades of the lamp.
54      * @short LED shape
55      */
56     enum Shape { Rectangular, Circular };
57     Q_ENUM(Shape)
58 
59     /**
60      * Displays a flat, round or sunken LED.
61      *
62      * @short LED look.
63      */
64     enum Look {
65         Flat,
66         Raised,
67         Sunken,
68     };
69     Q_ENUM(Look)
70 
71     /**
72      * Constructs a green, round LED widget which will initially
73      * be turned on.
74      *
75      * @param parent The parent widget.
76      */
77     explicit KLed(QWidget *parent = nullptr);
78 
79     /**
80      * Constructs a round LED widget with the supplied color which will
81      * initially be turned on.
82      *
83      * @param color Initial color of the LED.
84      * @param parent The parent widget.
85      * @short Constructor
86      */
87     explicit KLed(const QColor &color, QWidget *parent = nullptr);
88 
89     /**
90      * Constructor with the color, state and look.
91      *
92      * Differs from above only in the parameters, which configure all settings.
93      *
94      * @param color  Initial color of the LED.
95      * @param state  Sets the State.
96      * @param look   Sets the Look.
97      * @param shape  Sets the Shape (rectangular or circular).
98      * @param parent The parent widget.
99      * @short Constructor
100      */
101     KLed(const QColor &color, KLed::State state, KLed::Look look, KLed::Shape shape, QWidget *parent = nullptr);
102 
103     /**
104      * Destroys the LED widget.
105      * @short Destructor
106      */
107     ~KLed() override;
108 
109     /**
110      * Returns the current color of the widget.
111      *
112      * @returns LED color
113      * @see setColor()
114      */
115     QColor color() const;
116 
117     /**
118      * Returns the current state of the widget (on/off).
119      * @returns LED state
120      *
121      * @see State
122      */
123     State state() const;
124 
125     /**
126      * Returns the current look of the widget.
127      * @returns LED look
128      *
129      * @see Look
130      */
131     Look look() const;
132 
133     /**
134      * Returns the current shape of the widget.
135      * @returns LED shape
136      *
137      * @see Shape
138      */
139     Shape shape() const;
140 
141     /**
142      * Returns the factor to darken the LED.
143      * @returns dark factor
144      *
145      * @see setDarkFactor()
146      */
147     int darkFactor() const;
148 
149     /**
150      * Set the color of the widget.
151      *
152      * The LED is shown with @p color when in the KLed::On state
153      * or with the darken color in KLed::Off state.
154      *
155      * The widget calls the update() method, so it will
156      * be updated when entering the main event loop.
157      *
158      * @param color New color of the LED.
159      *
160      * @see color() darkFactor()
161      */
162     void setColor(const QColor &color);
163 
164     /**
165      * Sets the state of the widget to On or Off.
166      *
167      * @param state The LED state: on or off.
168      *
169      * @see on() off() toggle()
170      */
171     void setState(State state);
172 
173     /**
174      * Sets the look of the widget.
175      *
176      * The look may be Flat, Raised or Sunken.
177      *
178      * The widget calls the update() method, so it will
179      * be updated when entering the main event loop.
180      *
181      * @param look New look of the LED.
182      *
183      * @see Look
184      */
185     void setLook(Look look);
186 
187     /**
188      * Set the shape of the LED.
189      *
190      * @param shape The LED shape.
191      * @short Set LED shape.
192      */
193     void setShape(Shape shape);
194 
195     /**
196      * Sets the factor to darken the LED in KLed::Off state.
197      *
198      * The @p darkFactor should be greater than 100, otherwise the LED
199      * becomes lighter in KLed::Off state.
200      *
201      * Defaults to 300.
202      *
203      * @param darkFactor Sets the factor to darken the LED.
204      *
205      * @see setColor
206      */
207     void setDarkFactor(int darkFactor);
208 
209     QSize sizeHint() const override;
210     QSize minimumSizeHint() const override;
211 
212 public Q_SLOTS:
213 
214     /**
215      * Toggles the state of the led from Off to On or vice versa.
216      */
217     void toggle();
218 
219     /**
220      * Sets the state of the widget to On.
221      *
222      * @see off() toggle()  setState()
223      */
224     void on();
225 
226     /**
227      * Sets the state of the widget to Off.
228      *
229      * @see on() toggle()  setState()
230      */
231     void off();
232 
233 protected:
234     void paintEvent(QPaintEvent *) override;
235     void resizeEvent(QResizeEvent *) override;
236 
237     /**
238      * @internal
239      * invalidates caches after property changes and calls update()
240      */
241     void updateCachedPixmap();
242 
243 private:
244     void updateAccessibleName();
245 
246 private:
247     std::unique_ptr<class KLedPrivate> const d;
248 };
249 
250 #endif
251