Files
qt5-legatus-legacy/Lts/Common/LtsWidgets/lts_led_widget.h
T
2026-08-13 18:33:10 +02:00

179 lines
5.2 KiB
C++
Executable File

/**
* @RDE_HEADER@
* LTS LED Widget
* $Id$
* $HeadURL$
* $Author$
*/
#pragma once
#include <QWidget>
#include "lts_widgets_export.h"
namespace LtsWidgets
{
/**
* This class generates a LED with 2D/3D design, customizable colors and animation.
*
* If you need more than two states, simple derive from this class, only use "status=on"
* generate pairs of identifiers and colors and change the colors with appropriate method:
* e.g.:
* DerivedLEDWidget(...) : LtsLedWidget() {}
* enum Status { ST_STATUS1 = 0, ST_STATUS2, ST_STATUS3 };
* static QMap<Status, QColor> cMap; // Fill it respectively
* DerivedLEDWidget::setStatus(Status b) { setOnColor(cMap.value(b, Qt::black)); }
*/
class LTSWIDGETS_EXPORT LtsLedWidget : public QWidget
{
Q_OBJECT
public:
enum class AspectHint
{
WidthIsHeight,
NoHint,
HeightIsWidth
};
Q_ENUM(AspectHint)
Q_PROPERTY(QColor oncolor READ onColor WRITE setOnColor)
Q_PROPERTY(QColor offcolor READ offColor WRITE setOffColor)
Q_PROPERTY(bool status READ status WRITE setStatus)
Q_PROPERTY(bool usesGradient READ usesGradient WRITE setUseGradient)
Q_PROPERTY(int animTime READ animTime WRITE setAnimTime)
Q_PROPERTY(AspectHint aspectHint READ aspectHint WRITE setAspectHint)
Q_PROPERTY(QColor currentcolor READ currentColor WRITE setCurrentColor DESIGNABLE false)
/**
* Constructor
* @param parent The widget's parent. Default value is NULL.
* @param oncolor The color for LED when turned on. Default value is Qt::red, see also setOnColor() for details.
* @param offcolor The color for LED when turned off. Default value is Qt::black (i.e. "Use darker color"), see also setOffColor() for details.
* @param useGradient The 2D/3D design status. Default value is true (3D).
* @param animtime The animation time in msecs. Default value is 0.
*/
explicit LtsLedWidget(QWidget *parent = nullptr, const QColor &oncolor = Qt::red, const QColor &offcolor = Qt::black, bool useGradient = true, int animtime = 0);
void paintEvent(QPaintEvent *pe);
/**
* Set animation time in msec
* Value = 0 shows no animation
* Reasonable value is between 400 and 700 msecs
* @param time time in msecs
*/
void setAnimTime(int time) { m_animTime = time; }
/**
* Get current animation time in msecs
* @return animation time in msecs
*/
int animTime() const { return m_animTime; }
/**
* Get current led status
* @return the LED's status
*/
bool status() const { return m_status; }
/**
* Set 2D/3D design
* Value true set a 3D design of the led widget,
* Value false set a 2D design of the led widget
* @param useGradient
*/
void setUseGradient(bool useGradient);
/**
* Get a state info about the current 2D/3D design
* @return true for 3D and false for 2D design
*/
bool usesGradient() const { return m_gradient; }
/**
* Set the color for the LED when turned "on".
* If status is "on" and the oncolor is changed
* the animation will be run.
* @param oncolor color for LED in state "on"
*/
void setOnColor(const QColor &oncolor);
/**
* Get the current oncolor
* @return the current oncolor
*/
QColor onColor() const { return m_oncolor; }
/**
* Set the color for the LED when turned "off".
* If offcolor is black the wigdet use a darker version of oncolor as offcolor value.
* If status is "off" and the offcolor is changed
* the animation will be run.
* @param offcolor color for LED in state "off"
*/
void setOffColor(const QColor &offcolor);
/**
* Get the current offcolor
* @return the current offcolor
*/
QColor offColor() const { return m_offcolor; }
/**
* Convenience function set oncolor and offcolor in one step
* Default value for offcolor is black.
* If offcolor is black the wigdet use a darker version of oncolor as offcolor value.
* @param oncolor The color for LED when turned on.
* @param offcolor The color for LED when turned off. Default value is Qt::black (i.e. "Use darker color").
*/
void setColor(const QColor &oncolor, const QColor &offcolor = Qt::black);
/**
* Get current color
* @return the currently visible color
*/
QColor currentColor() const { return m_currentcolor; }
void resizeEvent(QResizeEvent *event);
AspectHint aspectHint() const { return m_aspectHint; }
public slots:
/**
* Public slot set current led status
* @param status The LED's status (on/off)
*/
void setStatus(bool status);
void setAspectHint(AspectHint aspectHint)
{
m_aspectHint = aspectHint;
}
private:
/**
* Set current color
* Needed for color animation
* ***This method is private for a reason!***
* @param currentcolor
*/
void setCurrentColor(const QColor &currentcolor);
/**
* Private function to start the color animation
*/
void animate();
bool m_status;
QColor m_oncolor;
QColor m_offcolor;
QColor m_currentcolor;
bool m_gradient;
int m_animTime;
AspectHint m_aspectHint = AspectHint::NoHint;
};
} // namespace LtsWidgets