64 lines
1.6 KiB
C++
64 lines
1.6 KiB
C++
|
|
#include "QBreathingLight.h"
|
||
|
|
#include <QPainter>
|
||
|
|
#include <QColor>
|
||
|
|
#include <algorithm>
|
||
|
|
QBreathingLight::QBreathingLight(QWidget *parent)
|
||
|
|
: QWidget(parent)
|
||
|
|
, m_opacity(1.0f)
|
||
|
|
, m_isFlashing(false)
|
||
|
|
, m_flashColor(Qt::red)
|
||
|
|
, m_flashDuration(3000)
|
||
|
|
{
|
||
|
|
setFixedSize(40, 40);
|
||
|
|
connect(&m_animationTimer, &QTimer::timeout, this, &QBreathingLight::updateAnimation);
|
||
|
|
}
|
||
|
|
void QBreathingLight::setFlashDuration(int milliseconds)
|
||
|
|
{
|
||
|
|
m_flashDuration = milliseconds;
|
||
|
|
}
|
||
|
|
void QBreathingLight::setFlashColor(const QColor &color)
|
||
|
|
{
|
||
|
|
m_flashColor = color;
|
||
|
|
}
|
||
|
|
void QBreathingLight::triggerSignal()
|
||
|
|
{
|
||
|
|
if (!m_isFlashing) {
|
||
|
|
m_isFlashing = true;
|
||
|
|
m_flashStartTime = QTime::currentTime();
|
||
|
|
m_animationTimer.start(30);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
void QBreathingLight::paintEvent(QPaintEvent *event)
|
||
|
|
{
|
||
|
|
Q_UNUSED(event);
|
||
|
|
QPainter painter(this);
|
||
|
|
painter.setRenderHint(QPainter::Antialiasing);
|
||
|
|
painter.fillRect(rect(), QBrush(QColor(240, 240, 240)));
|
||
|
|
QPoint center = rect().center();
|
||
|
|
int radius = qMin(width(), height()) / 3;
|
||
|
|
QColor color;
|
||
|
|
if (m_isFlashing) {
|
||
|
|
color = m_flashColor;
|
||
|
|
color.setAlphaF(m_opacity);
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
color = Qt::green;
|
||
|
|
}
|
||
|
|
painter.setBrush(QBrush(color));
|
||
|
|
painter.setPen(Qt::NoPen);
|
||
|
|
painter.drawEllipse(center, radius, radius);
|
||
|
|
}
|
||
|
|
void QBreathingLight::updateAnimation()
|
||
|
|
{
|
||
|
|
int elapsed = m_flashStartTime.msecsTo(QTime::currentTime());
|
||
|
|
if (elapsed >= m_flashDuration) {
|
||
|
|
m_isFlashing = false;
|
||
|
|
m_opacity = 1.0f;
|
||
|
|
m_animationTimer.stop();
|
||
|
|
update();
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
float progress = static_cast<float>(elapsed) / m_flashDuration;
|
||
|
|
m_opacity = 0.5f * (1.0f + sin(progress * 2 * 3.14 * 5));
|
||
|
|
update();
|
||
|
|
}
|