#pragma once
#include <QLabel>
#include <QTimer>
#include <atomic>
using namespace std;
enum EnType
{
enLabelOk =1,
enLabelError
};
class YxCloseAnimationLabel:public QLabel
{
Q_OBJECT
public:
YxCloseAnimationLabel(QString _strText, EnType _type,QWidget *parent = Q_NULLPTR );
~YxCloseAnimationLabel();
private slots:
void closeAnimation();
protected:
void paintEvent(QPaintEvent *event);
private:
QTimer m_timer;
QFont m_font;
static atomic_short m_iCount;
EnType m_type;
};
#include "YxCloseAnimationLabel.h"
#include <QPainter>
#include <QPropertyAnimation>
atomic_short YxCloseAnimationLabel::m_iCount = 0;
YxCloseAnimationLabel::YxCloseAnimationLabel(QString _strText, EnType _type, QWidget *parent /*= Q_NULLPTR */):QLabel(parent)
,m_type(_type)
{
if (m_iCount > 4) {
m_iCount = 0;
}
setWindowFlags(Qt::Window | Qt::FramelessWindowHint);
//setAttribute(Qt::WA_TranslucentBackground);
setAttribute(Qt::WA_QuitOnClose);
setAttribute(Qt::WA_DeleteOnClose);
m_font.setPointSize(10);
m_font.setFamily("msyh");
QFontMetrics metrics = QFontMetrics(m_font);
setFixedSize(metrics.width(_strText) + 45,
metrics.height() + 22);
QLabel::setText(_strText);
connect(&m_timer, SIGNAL(timeout()), SLOT(closeAnimation()));
m_timer.setInterval(1200);
m_timer.start();
m_iCount++;
}
YxCloseAnimationLabel::~YxCloseAnimationLabel()
{
}
void YxCloseAnimationLabel::closeAnimation()
{
m_timer.stop();
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity", this);
animation->setDuration(1300);
animation->setStartValue(1);
animation->setEndValue(0);
animation->start();
connect(animation, SIGNAL(finished()), SLOT(close()));
}
void YxCloseAnimationLabel::paintEvent(QPaintEvent *event) {
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.setPen(Qt::NoPen);
painter.setBrush(QColor(255, 255, 255));
painter.drawRoundRect(rect(), 6, 6);
painter.setFont(m_font);
painter.setPen(QColor(89, 89, 89));
QRect textRect;
textRect.setRect(rect().x()+26, rect().y(), rect().width()- 26,rect().height());
painter.drawText(textRect, Qt::AlignCenter, text());
QRect windowsRect = geometry();
int x = 10;
int y = (windowsRect.height() - 16) / 2;
QPoint imagePoint(x, y);
QImage image_;
if (m_type == enLabelOk)
{
image_.load(":/YxStreamClient/Resources/ok_ico.png");
}
else {
image_.load(":/YxStreamClient/Resources/error_ico.png");
}
painter.drawImage(imagePoint, image_);
painter.end();
windowsRect.moveTop((windowsRect.height()*m_iCount + 10* m_iCount));
setGeometry(windowsRect);
}