|
@@ -0,0 +1,244 @@
|
|
|
+#include "tipwidget.h"
|
|
|
+#include "ui_tipwidget.h"
|
|
|
+#include <QPainter>
|
|
|
+#include <QTimer>
|
|
|
+#include <QPropertyAnimation>
|
|
|
+#include <QGraphicsOpacityEffect>
|
|
|
+#include <QPixmap>
|
|
|
+#include <QElapsedTimer>
|
|
|
+#include <QTime>
|
|
|
+#include <QDebug>
|
|
|
+
|
|
|
+static QList<TipWidget*> sl_tipWdgs{Q_NULLPTR, Q_NULLPTR, Q_NULLPTR, Q_NULLPTR};
|
|
|
+
|
|
|
+void TipWidget::display(FormType type, QWidget* parent, int nTitleHeight)
|
|
|
+{
|
|
|
+ if (Q_NULLPTR == parent || sl_tipWdgs.count() > 4)
|
|
|
+ return;
|
|
|
+ static QElapsedTimer s_timer;
|
|
|
+ if (s_timer.elapsed() > 5 && s_timer.restart() < 500)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int nPosX = (parent->width() - WIDTH) / 2;
|
|
|
+ int nPosY = nTitleHeight;
|
|
|
+ TipWidget* obj = Q_NULLPTR;
|
|
|
+ for (int i = 0; i < sl_tipWdgs.count(); ++i)
|
|
|
+ {
|
|
|
+ if (sl_tipWdgs.at(i) == Q_NULLPTR)
|
|
|
+ {
|
|
|
+ obj = new TipWidget(type, parent);
|
|
|
+ obj->setEndPos(nPosX, nPosY + i * HEIGHT + 10);
|
|
|
+ if (!obj->isVisible())
|
|
|
+ obj->show();
|
|
|
+ obj->run();
|
|
|
+ sl_tipWdgs[i] = obj;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+/* 这个是上面的重载函数,可以自定义文本 */
|
|
|
+void TipWidget::display(FormType type, QString text, QWidget *parent, int nTitleHeight)
|
|
|
+{
|
|
|
+ if (Q_NULLPTR == parent || sl_tipWdgs.count() > 4)
|
|
|
+ return;
|
|
|
+ static QElapsedTimer s_timer;
|
|
|
+ if (s_timer.elapsed() > 5 && s_timer.restart() < 500)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ int nPosX = (parent->width() - WIDTH) / 2;
|
|
|
+ int nPosY = nTitleHeight;
|
|
|
+ TipWidget* obj = Q_NULLPTR;
|
|
|
+ for (int i = 0; i < sl_tipWdgs.count(); ++i)
|
|
|
+ {
|
|
|
+ if (sl_tipWdgs.at(i) == Q_NULLPTR)
|
|
|
+ {
|
|
|
+ obj = new TipWidget(type, text, parent);
|
|
|
+
|
|
|
+ obj->setEndPos(nPosX, nPosY + i * HEIGHT + 10);
|
|
|
+ if (!obj->isVisible())
|
|
|
+ obj->show();
|
|
|
+ obj->run();
|
|
|
+ sl_tipWdgs[i] = obj;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+TipWidget::TipWidget(FormType type, QWidget *parent) :
|
|
|
+ QWidget(parent),
|
|
|
+ ui(new Ui::TipWidget)
|
|
|
+{
|
|
|
+ ui->setupUi(this);
|
|
|
+ setWindowFlag(Qt::FramelessWindowHint);
|
|
|
+ _endRect = rect();
|
|
|
+ setFormType(type);
|
|
|
+// ui->label_tipIcon->hide();
|
|
|
+ m_customText = false;
|
|
|
+}
|
|
|
+
|
|
|
+TipWidget::TipWidget(FormType type, QString& text, QWidget *parent) :
|
|
|
+ QWidget(parent),
|
|
|
+ ui(new Ui::TipWidget),
|
|
|
+ m_tipText(text)
|
|
|
+{
|
|
|
+ ui->setupUi(this);
|
|
|
+ setWindowFlag(Qt::FramelessWindowHint);
|
|
|
+ _endRect = rect();
|
|
|
+ m_customText = true;
|
|
|
+ setFormType(type);
|
|
|
+ /* 设置字体居中 */
|
|
|
+ ui->label_tipTitle->setAlignment(Qt::AlignCenter);
|
|
|
+ if(text.count() > 5)
|
|
|
+ {
|
|
|
+ QFontMetrics fm(ui->label_tipTitle->font());
|
|
|
+ auto textWidth = fm.width(text);
|
|
|
+ int width = textWidth + this->width() - ui->label_tipTitle->width();
|
|
|
+ this->setFixedWidth(width);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+TipWidget::~TipWidget()
|
|
|
+{
|
|
|
+ delete ui;
|
|
|
+}
|
|
|
+
|
|
|
+void TipWidget::setFormType(FormType type)
|
|
|
+{
|
|
|
+ QPixmap img;
|
|
|
+ switch(type)
|
|
|
+ {
|
|
|
+ case OPERATOR_OK:
|
|
|
+ _backgroundColor = QColor(246, 255, 237);
|
|
|
+ _borderColor = QColor(183, 235, 143);
|
|
|
+ img.load(R"(:/Tip/Complete2x.png)");
|
|
|
+ if(m_customText)
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText(m_tipText);
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText("操作成功!");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case OPERATOR_FAIL:
|
|
|
+ _backgroundColor = QColor(255, 241, 240);
|
|
|
+ _borderColor = QColor(255, 163, 158);
|
|
|
+ img.load(":/Tip/Failed2x.png");
|
|
|
+ if(m_customText)
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText(m_tipText);
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText("操作失败!");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case OPERATOR_TIP:
|
|
|
+ _backgroundColor = QColor(236, 238, 254);
|
|
|
+ _borderColor = QColor(68, 88, 254);
|
|
|
+ img.load(":/Tip/Wait2x.png");
|
|
|
+ if(m_customText)
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText(m_tipText);
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText("普通提示!");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case OPERATOR_WARN:
|
|
|
+ _backgroundColor = QColor(255, 251, 230);
|
|
|
+ _borderColor = QColor(255, 229, 143);
|
|
|
+ img.load(":/Tip/Tips2x.png");
|
|
|
+ if(m_customText)
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText(m_tipText);
|
|
|
+ }else
|
|
|
+ {
|
|
|
+ ui->label_tipTitle->setText("需注意!");
|
|
|
+ }
|
|
|
+ break;
|
|
|
+
|
|
|
+ default:
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ img = img.scaled(ui->label_tipIcon->width(), ui->label_tipIcon->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
|
|
|
+ ui->label_tipIcon->setPixmap(img);
|
|
|
+}
|
|
|
+
|
|
|
+void TipWidget::setEndPos(int x, int y)
|
|
|
+{
|
|
|
+ _endRect.moveTo(x, y);
|
|
|
+}
|
|
|
+
|
|
|
+void TipWidget::run()
|
|
|
+{
|
|
|
+ if (nullptr == _animation)
|
|
|
+ _animation = new QPropertyAnimation(this, "geometry");
|
|
|
+ _animation->setDuration(1 * 1000);
|
|
|
+ _animation->setStartValue(QRect(_endRect.x(), _endRect.y() + 150, _endRect.width(), _endRect.height()));
|
|
|
+ _animation->setEndValue(_endRect);
|
|
|
+ connect(_animation, &QPropertyAnimation::finished, this, [this]{
|
|
|
+ if (nullptr == _pKillTimer)
|
|
|
+ _pKillTimer = new QTimer(this);
|
|
|
+ _pKillTimer->start(1 * 1000);
|
|
|
+ connect(_pKillTimer, &QTimer::timeout, this, &TipWidget::onFadeOut);
|
|
|
+ });
|
|
|
+ _animation->start();
|
|
|
+}
|
|
|
+
|
|
|
+void TipWidget::onFadeOut()
|
|
|
+{
|
|
|
+ if (nullptr != _pKillTimer && _pKillTimer->isActive())
|
|
|
+ {
|
|
|
+ _pKillTimer->stop();
|
|
|
+ }
|
|
|
+ QGraphicsOpacityEffect* effect = new QGraphicsOpacityEffect(this);
|
|
|
+ this->setGraphicsEffect(effect);
|
|
|
+ if (nullptr == _animFadeOut)
|
|
|
+ _animFadeOut = new QPropertyAnimation(effect, "opacity");
|
|
|
+ _animFadeOut->setDuration(1 * 1000);
|
|
|
+ _animFadeOut->setStartValue(1.0);
|
|
|
+ _animFadeOut->setEndValue(0.0);
|
|
|
+ connect(_animFadeOut, &QPropertyAnimation::finished, this, [this]{
|
|
|
+ onBtnClose();
|
|
|
+ });
|
|
|
+ _animFadeOut->start();
|
|
|
+}
|
|
|
+
|
|
|
+void TipWidget::onBtnClose()
|
|
|
+{
|
|
|
+ close();
|
|
|
+ for (int i = 0; i < sl_tipWdgs.count(); ++i)
|
|
|
+ {
|
|
|
+ if (sl_tipWdgs.at(i) == this)
|
|
|
+ {
|
|
|
+ sl_tipWdgs[i] = Q_NULLPTR;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ deleteLater();
|
|
|
+}
|
|
|
+
|
|
|
+void TipWidget::paintEvent(QPaintEvent *event)
|
|
|
+{
|
|
|
+ QRect rc(rect());
|
|
|
+ rc.adjust(1, 1, -2, -2);
|
|
|
+ QPainter painter(this);
|
|
|
+ painter.setRenderHint(QPainter::Antialiasing, true);
|
|
|
+ painter.save();
|
|
|
+ QBrush brush(_backgroundColor);
|
|
|
+ painter.setBrush(brush);
|
|
|
+ painter.drawRoundedRect(rc, 2.0, 2.0);
|
|
|
+ painter.restore();
|
|
|
+
|
|
|
+ painter.save();
|
|
|
+ QPen pen;
|
|
|
+ pen.setWidth(2);
|
|
|
+ pen.setColor(_borderColor);
|
|
|
+ painter.setPen(pen);
|
|
|
+ painter.setBrush(Qt::transparent);
|
|
|
+ painter.drawRoundedRect(rc, 2.0, 2.0);
|
|
|
+ painter.restore();
|
|
|
+ QWidget::paintEvent(event);
|
|
|
+}
|