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

155 lines
4.1 KiB
C++
Executable File

/**
* @RDE_HEADER@
* Class of Led Widget
* $Id$
* $HeadURL$
* $Author$
*/
#include "lts_led_widget.h"
#include <QPainter>
#include <QPropertyAnimation>
#include <QResizeEvent>
namespace LtsWidgets
{
void LtsLedWidget::resizeEvent(QResizeEvent *event)
{
switch (m_aspectHint) {
case AspectHint::NoHint:
{
// NOTE: the next 4 lines would overwrite set properties from ui files
// setMinimumWidth(0);
// setMaximumWidth(65000);
// setMinimumHeight(0);
// setMaximumHeight(65000);
QWidget::resizeEvent(event);
break;
}
case AspectHint::HeightIsWidth:
setMinimumHeight(event->size().width());
setMaximumHeight(event->size().width());
setMinimumWidth(0);
setMaximumWidth(65000);
break;
case AspectHint::WidthIsHeight:
setMinimumHeight(0);
setMaximumHeight(65000);
setMinimumWidth(event->size().height());
setMaximumWidth(event->size().height());
break;
}
}
/**
* The following values were determined by trial and error:
*/
static constexpr double GRADIENT_KEY_LOWER = 0.3;
static constexpr double GRADIENT_KEY_UPPER = 0.9;
static constexpr int GRADIENT_VALUE_LOWER = 125;
static constexpr int GRADIENT_VALUE_UPPER = 165;
static constexpr double GRADIENT_CENTER_OFFSET = 5.0;
static constexpr double GRADIENT_FOCALPOINT_OFFSET = 2.7;
static constexpr double PERCENTAGE_OF_BORDER = 0.1;
static constexpr int DARK_FACTOR = 200;
LtsLedWidget::LtsLedWidget(QWidget *parent, const QColor &oncolor, const QColor &offcolor, bool useGradient, int animtime)
: QWidget(parent)
, m_status(true)
, m_oncolor(oncolor)
, m_offcolor(offcolor)
, m_currentcolor(oncolor)
, m_gradient(useGradient)
, m_animTime(animtime)
{
setObjectName(QString::fromUtf8("LtsLedWidget"));
}
void LtsLedWidget::paintEvent(QPaintEvent *pe)
{
Q_UNUSED(pe);
QPainter p;
p.begin(this);
p.setOpacity(QWidget::isEnabled() ? 1.00 : 0.5);
int minSize = height() > width() ? width() : height();
if (m_gradient)
{
QRadialGradient grad(QPointF(width(), height()) / GRADIENT_CENTER_OFFSET, // Center
minSize, // Radius
QPointF(width(), height()) / GRADIENT_FOCALPOINT_OFFSET); // FocalPoint
grad.setColorAt(GRADIENT_KEY_LOWER, m_currentcolor.lighter(GRADIENT_VALUE_LOWER));
grad.setColorAt(GRADIENT_KEY_UPPER, m_currentcolor.darker(GRADIENT_VALUE_UPPER));
p.setBrush(grad);
}
else
p.setBrush(QBrush(m_currentcolor, Qt::SolidPattern));
p.setRenderHint(QPainter::Antialiasing);
p.setPen(QPen(Qt::black, PERCENTAGE_OF_BORDER * minSize, Qt::SolidLine, Qt::SquareCap));
p.drawEllipse(QPointF(width(), height()) / 2.0,
width() / (2.0 * (1.00 + PERCENTAGE_OF_BORDER)),
height() / (2.0 * (1.00 + PERCENTAGE_OF_BORDER)));
p.end();
}
void LtsLedWidget::animate()
{
QPropertyAnimation *anim = new QPropertyAnimation(this, "currentcolor");
anim->setDuration(m_animTime);
anim->setEasingCurve(QEasingCurve::OutBack);
anim->setStartValue(m_currentcolor);
anim->setEndValue(m_status ? m_oncolor : m_offcolor == Qt::black ? m_oncolor.darker(DARK_FACTOR) : m_offcolor);
anim->start(QPropertyAnimation::DeleteWhenStopped);
}
void LtsLedWidget::setStatus(bool status)
{
if (m_status == status)
return;
m_status = status;
animate();
}
void LtsLedWidget::setUseGradient(bool useGradient)
{
if (m_gradient == useGradient)
return;
m_gradient = useGradient;
repaint();
}
void LtsLedWidget::setColor(const QColor &oncolor, const QColor &offcolor)
{
setOnColor(oncolor);
setOffColor(offcolor);
}
void LtsLedWidget::setOnColor(const QColor &oncolor)
{
if (m_oncolor == oncolor)
return;
m_oncolor = oncolor;
if (m_status)
animate();
}
void LtsLedWidget::setOffColor(const QColor &offcolor)
{
if (m_offcolor == offcolor)
return;
m_offcolor = offcolor;
if (!m_status)
animate();
}
void LtsLedWidget::setCurrentColor(const QColor &currentcolor)
{
if (m_currentcolor == currentcolor)
return;
m_currentcolor = currentcolor;
repaint();
}
} // namespace namespace LtsWidgets