add QbreathingLight Class
This commit is contained in:
86
QMainwindow/QBreathingLight.cpp
Normal file
86
QMainwindow/QBreathingLight.cpp
Normal file
@@ -0,0 +1,86 @@
|
||||
#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) // Ĭ<><C4AC><EFBFBD><EFBFBD>˸3<CBB8><33>
|
||||
{
|
||||
// <20><><EFBFBD>ù̶<C3B9><CCB6><EFBFBD>С
|
||||
setFixedSize(40, 40);
|
||||
|
||||
// <20><><EFBFBD>Ӷ<EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD>źŲ<C5BA>
|
||||
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); // 30ms<6D><73><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>
|
||||
}
|
||||
}
|
||||
|
||||
void QBreathingLight::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event);
|
||||
|
||||
QPainter painter(this);
|
||||
painter.setRenderHint(QPainter::Antialiasing);
|
||||
|
||||
// <20><><EFBFBD>Ʊ<EFBFBD><C6B1><EFBFBD>
|
||||
painter.fillRect(rect(), QBrush(QColor(240, 240, 240)));
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĵ<EFBFBD>
|
||||
QPoint center = rect().center();
|
||||
int radius = qMin(width(), height()) / 3;
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>״̬<D7B4><CCAC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɫ
|
||||
QColor color;
|
||||
if (m_isFlashing) {
|
||||
color = m_flashColor;
|
||||
color.setAlphaF(m_opacity);
|
||||
}
|
||||
else {
|
||||
color = Qt::green; // δ<><CEB4><EFBFBD><EFBFBD>״̬<D7B4><CCAC>ʾ<EFBFBD><CABE>ɫ
|
||||
}
|
||||
|
||||
// <20><><EFBFBD>ƺ<EFBFBD><C6BA><EFBFBD><EFBFBD><EFBFBD>
|
||||
painter.setBrush(QBrush(color));
|
||||
painter.setPen(Qt::NoPen);
|
||||
painter.drawEllipse(center, radius, radius);
|
||||
}
|
||||
|
||||
void QBreathingLight::updateAnimation()
|
||||
{
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD>Ƿ<C7B7><F1B3ACB9><EFBFBD>˸<EFBFBD><CBB8><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
||||
int elapsed = m_flashStartTime.msecsTo(QTime::currentTime());
|
||||
if (elapsed >= m_flashDuration) {
|
||||
m_isFlashing = false;
|
||||
m_opacity = 1.0f;
|
||||
m_animationTimer.stop();
|
||||
update();
|
||||
return;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7> (ʹ<><CAB9><EFBFBD><EFBFBD><EFBFBD>Һ<EFBFBD><D2BA><EFBFBD>ʵ<EFBFBD><CAB5>ƽ<EFBFBD><C6BD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7>)
|
||||
float progress = static_cast<float>(elapsed) / m_flashDuration;
|
||||
m_opacity = 0.5f * (1.0f + sin(progress * 2 * 3.14 * 5)); // ÿ<><C3BF><EFBFBD><EFBFBD>˸2<CBB8><32>
|
||||
|
||||
update(); // <20><><EFBFBD><EFBFBD><EFBFBD>ػ<EFBFBD>
|
||||
}
|
||||
Reference in New Issue
Block a user