浏览代码

V0.8.13
1、添加了TipWIdget弱提示组件

Apple 1 月之前
父节点
当前提交
98bb10ddab

二进制
common/TipWidget/Tip/Complete2x.png


二进制
common/TipWidget/Tip/Failed2x.png


二进制
common/TipWidget/Tip/Tips2x.png


二进制
common/TipWidget/Tip/Wait2x.png


+ 8 - 0
common/TipWidget/tip.qrc

@@ -0,0 +1,8 @@
+<RCC>
+    <qresource prefix="/">
+        <file>Tip/Complete2x.png</file>
+        <file>Tip/Failed2x.png</file>
+        <file>Tip/Tips2x.png</file>
+        <file>Tip/Wait2x.png</file>
+    </qresource>
+</RCC>

+ 244 - 0
common/TipWidget/tipwidget.cpp

@@ -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);
+}

+ 54 - 0
common/TipWidget/tipwidget.h

@@ -0,0 +1,54 @@
+#ifndef TIPWIDGET_H
+#define TIPWIDGET_H
+
+#include <QWidget>
+
+class QPropertyAnimation;
+namespace Ui {
+class TipWidget;
+}
+
+class TipWidget : public QWidget
+{
+    Q_OBJECT
+public:
+    enum FormType
+    {
+        OPERATOR_OK = 0,        // 操作成功!
+        OPERATOR_FAIL,          // 操作失败!
+        OPERATOR_TIP,           // 普通提示!
+        OPERATOR_WARN,          // 需注意!
+    };
+public:
+    static const int WIDTH = 168;
+    static const int HEIGHT = 56;
+    static void display(FormType type, QWidget* parent = Q_NULLPTR, int nTitleHeight = 48);
+    static void display(FormType type, QString text, QWidget* parent = Q_NULLPTR, int nTitleHeight = 48);
+
+    explicit TipWidget(FormType type, QWidget *parent = 0);
+    TipWidget(FormType type, QString& text, QWidget *parent = 0);
+    ~TipWidget();
+
+    void setEndPos(int x, int y);
+    void run();
+public slots:
+    void onBtnClose();
+    void onFadeOut();
+protected:
+    void paintEvent(QPaintEvent *event) override;
+private:
+    void setFormType(FormType type);
+private:
+    Ui::TipWidget *ui;
+    QColor _backgroundColor{246, 255, 237};
+    QColor _borderColor{183, 235, 143};
+    QRect _endRect;
+    QTimer* _pKillTimer{nullptr};
+    QPropertyAnimation* _animation{nullptr};
+    QPropertyAnimation* _animFadeOut{nullptr};
+
+    QString m_tipText;              /* 提示文本 */
+    bool m_customText;              /* 自定义文本标志 */
+};
+
+#endif // TIPWIDGET_H

+ 84 - 0
common/TipWidget/tipwidget.ui

@@ -0,0 +1,84 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>TipWidget</class>
+ <widget class="QWidget" name="TipWidget">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>168</width>
+    <height>56</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>Form</string>
+  </property>
+  <property name="styleSheet">
+   <string notr="true">QLabel
+{
+background:transparent;
+}
+
+QLabel#label_tipIcon
+{
+font-weight: 500;
+font-size: 16px;
+color: rgba(0,0,0,0.85);
+line-height: 24px;
+text-align: left;
+font-style: normal;
+}
+</string>
+  </property>
+  <layout class="QHBoxLayout" name="horizontalLayout">
+   <item>
+    <widget class="QLabel" name="label_tipIcon">
+     <property name="sizePolicy">
+      <sizepolicy hsizetype="Fixed" vsizetype="Fixed">
+       <horstretch>0</horstretch>
+       <verstretch>0</verstretch>
+      </sizepolicy>
+     </property>
+     <property name="minimumSize">
+      <size>
+       <width>24</width>
+       <height>24</height>
+      </size>
+     </property>
+     <property name="maximumSize">
+      <size>
+       <width>24</width>
+       <height>24</height>
+      </size>
+     </property>
+     <property name="styleSheet">
+      <string notr="true"/>
+     </property>
+     <property name="text">
+      <string/>
+     </property>
+    </widget>
+   </item>
+   <item>
+    <widget class="QLabel" name="label_tipTitle">
+     <property name="styleSheet">
+      <string notr="true">font-family:&quot;Source Han Sans CN&quot;;
+font-size: 16px;
+font-weight: 500;
+color: rgba(0,0,0,0.85);</string>
+     </property>
+     <property name="text">
+      <string>保存成功!</string>
+     </property>
+     <property name="alignment">
+      <set>Qt::AlignCenter</set>
+     </property>
+    </widget>
+   </item>
+  </layout>
+  <zorder>label_tipTitle</zorder>
+  <zorder>label_tipIcon</zorder>
+ </widget>
+ <resources/>
+ <connections/>
+</ui>