#include "lts_led_widget.h" #include #include #include namespace LtsWidgets { void LtsLedWidget::resizeEvent(QResizeEvent *event) { switch (m_aspectHint) { case AspectHint::NoHint: { 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; } } 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, minSize, QPointF(width(), height()) / GRADIENT_FOCALPOINT_OFFSET); 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 ¤tcolor) { if (m_currentcolor == currentcolor) return; m_currentcolor = currentcolor; repaint(); } }