File indexing completed on 2024-04-28 15:32:04

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1998 Jörg Habenicht <j.habenicht@europemail.com>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 
0008 #ifndef KLED_H
0009 #define KLED_H
0010 
0011 #include <kwidgetsaddons_export.h>
0012 
0013 #include <QWidget>
0014 #include <memory>
0015 
0016 class QColor;
0017 
0018 /**
0019  * @class KLed kled.h KLed
0020  *
0021  * @short An LED widget.
0022  *
0023  * Displays a round or rectangular light emitting diode.
0024  *
0025  * It is configurable to arbitrary colors, the two on/off states and three
0026  * styles (or "looks");
0027  *
0028  * It may display itself in a performant flat view, a round view with
0029  * light spot or a round view sunken in the screen.
0030  *
0031  * \image html kled.png "KLed Widget"
0032  *
0033  * @author Joerg Habenicht, Richard J. Moore (rich@kde.org) 1998, 1999
0034  */
0035 class KWIDGETSADDONS_EXPORT KLed : public QWidget
0036 {
0037     Q_OBJECT
0038     Q_PROPERTY(State state READ state WRITE setState)
0039     Q_PROPERTY(Shape shape READ shape WRITE setShape)
0040     Q_PROPERTY(Look look READ look WRITE setLook)
0041     Q_PROPERTY(QColor color READ color WRITE setColor)
0042     Q_PROPERTY(int darkFactor READ darkFactor WRITE setDarkFactor)
0043 
0044 public:
0045     /**
0046      * Status of the light is on/off.
0047      * @short LED on/off.
0048      */
0049     enum State { Off, On };
0050     Q_ENUM(State)
0051 
0052     /**
0053      * Shades of the lamp.
0054      * @short LED shape
0055      */
0056     enum Shape { Rectangular, Circular };
0057     Q_ENUM(Shape)
0058 
0059     /**
0060      * Displays a flat, round or sunken LED.
0061      *
0062      * @short LED look.
0063      */
0064     enum Look {
0065         Flat,
0066         Raised,
0067         Sunken,
0068     };
0069     Q_ENUM(Look)
0070 
0071     /**
0072      * Constructs a green, round LED widget which will initially
0073      * be turned on.
0074      *
0075      * @param parent The parent widget.
0076      */
0077     explicit KLed(QWidget *parent = nullptr);
0078 
0079     /**
0080      * Constructs a round LED widget with the supplied color which will
0081      * initially be turned on.
0082      *
0083      * @param color Initial color of the LED.
0084      * @param parent The parent widget.
0085      * @short Constructor
0086      */
0087     explicit KLed(const QColor &color, QWidget *parent = nullptr);
0088 
0089     /**
0090      * Constructor with the color, state and look.
0091      *
0092      * Differs from above only in the parameters, which configure all settings.
0093      *
0094      * @param color  Initial color of the LED.
0095      * @param state  Sets the State.
0096      * @param look   Sets the Look.
0097      * @param shape  Sets the Shape (rectangular or circular).
0098      * @param parent The parent widget.
0099      * @short Constructor
0100      */
0101     KLed(const QColor &color, KLed::State state, KLed::Look look, KLed::Shape shape, QWidget *parent = nullptr);
0102 
0103     /**
0104      * Destroys the LED widget.
0105      * @short Destructor
0106      */
0107     ~KLed() override;
0108 
0109     /**
0110      * Returns the current color of the widget.
0111      *
0112      * @returns LED color
0113      * @see setColor()
0114      */
0115     QColor color() const;
0116 
0117     /**
0118      * Returns the current state of the widget (on/off).
0119      * @returns LED state
0120      *
0121      * @see State
0122      */
0123     State state() const;
0124 
0125     /**
0126      * Returns the current look of the widget.
0127      * @returns LED look
0128      *
0129      * @see Look
0130      */
0131     Look look() const;
0132 
0133     /**
0134      * Returns the current shape of the widget.
0135      * @returns LED shape
0136      *
0137      * @see Shape
0138      */
0139     Shape shape() const;
0140 
0141     /**
0142      * Returns the factor to darken the LED.
0143      * @returns dark factor
0144      *
0145      * @see setDarkFactor()
0146      */
0147     int darkFactor() const;
0148 
0149     /**
0150      * Set the color of the widget.
0151      *
0152      * The LED is shown with @p color when in the KLed::On state
0153      * or with the darken color in KLed::Off state.
0154      *
0155      * The widget calls the update() method, so it will
0156      * be updated when entering the main event loop.
0157      *
0158      * @param color New color of the LED.
0159      *
0160      * @see color() darkFactor()
0161      */
0162     void setColor(const QColor &color);
0163 
0164     /**
0165      * Sets the state of the widget to On or Off.
0166      *
0167      * @param state The LED state: on or off.
0168      *
0169      * @see on() off() toggle()
0170      */
0171     void setState(State state);
0172 
0173     /**
0174      * Sets the look of the widget.
0175      *
0176      * The look may be Flat, Raised or Sunken.
0177      *
0178      * The widget calls the update() method, so it will
0179      * be updated when entering the main event loop.
0180      *
0181      * @param look New look of the LED.
0182      *
0183      * @see Look
0184      */
0185     void setLook(Look look);
0186 
0187     /**
0188      * Set the shape of the LED.
0189      *
0190      * @param shape The LED shape.
0191      * @short Set LED shape.
0192      */
0193     void setShape(Shape shape);
0194 
0195     /**
0196      * Sets the factor to darken the LED in KLed::Off state.
0197      *
0198      * The @p darkFactor should be greater than 100, otherwise the LED
0199      * becomes lighter in KLed::Off state.
0200      *
0201      * Defaults to 300.
0202      *
0203      * @param darkFactor Sets the factor to darken the LED.
0204      *
0205      * @see setColor
0206      */
0207     void setDarkFactor(int darkFactor);
0208 
0209     QSize sizeHint() const override;
0210     QSize minimumSizeHint() const override;
0211 
0212 public Q_SLOTS:
0213 
0214     /**
0215      * Toggles the state of the led from Off to On or vice versa.
0216      */
0217     void toggle();
0218 
0219     /**
0220      * Sets the state of the widget to On.
0221      *
0222      * @see off() toggle()  setState()
0223      */
0224     void on();
0225 
0226     /**
0227      * Sets the state of the widget to Off.
0228      *
0229      * @see on() toggle()  setState()
0230      */
0231     void off();
0232 
0233 protected:
0234     void paintEvent(QPaintEvent *) override;
0235     void resizeEvent(QResizeEvent *) override;
0236 
0237 private:
0238     /**
0239      * @internal
0240      * invalidates caches after property changes and calls update()
0241      */
0242     KWIDGETSADDONS_NO_EXPORT void updateCachedPixmap();
0243 
0244     KWIDGETSADDONS_NO_EXPORT void updateAccessibleName();
0245 
0246 private:
0247     std::unique_ptr<class KLedPrivate> const d;
0248 };
0249 
0250 #endif