repo: 添加 BaseWidgetOpacity 基础类来实现继承者的淡出动画来代替主窗口的实现

BaseWidgetOpacity 是一个提供了淡出/淡入动画的基础类

1. closeEvent 窗口关闭时进行淡出动画
    此前在 MainWindow 中实现的淡出动画将由 BaseWidgetOpacity 来实现。

    此前 MainWindow 原有的 DBlurEffectWidget 父类将移交至 BaseWidgetOpacity 继承。

注意:
    如果 MainWindow 在未来重写 closeEvent 事件时将可能丢失 BaseWidgetOpacity 中的淡出效果
This commit is contained in:
麻木 2022-12-19 02:47:38 +08:00
parent adf9032897
commit c9d0c8b751
7 changed files with 78 additions and 41 deletions

@ -11,7 +11,7 @@
#define AppPageSettings 3
MainWindow::MainWindow(QWidget *parent)
: DBlurEffectWidget(parent)
: BaseWidgetOpacity(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
@ -299,32 +299,3 @@ void MainWindow::on_pushButton_14_clicked()
});
}
}
/// @brief 窗口关闭事件
/// @param event
void MainWindow::closeEvent(QCloseEvent *event)
{
if (!closeWindowAnimation) {
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setEasingCurve(QEasingCurve::OutQuart);
animation->setDuration(500);
animation->setStartValue(1.0);
animation->setEndValue(0.0);
QObject::connect(animation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value){
this->update();
// setWindowTitle(QString("ヾ(⌒∇⌒*)See You♪ - %1%").arg(int(value.toFloat() * 100)));
});
QObject::connect(animation, &QPropertyAnimation::finished, this, [=](){
closeWindowAnimation = true;
this->close();
});
animation->start();
event->ignore();
} else {
event->accept();
}
}

@ -2,7 +2,6 @@
#define MAINWINDOWDTK_H
#include <DMainWindow>
#include <DBlurEffectWidget>
#include <DTitlebar>
#include <DSearchEdit>
#include <QGraphicsDropShadowEffect>
@ -12,6 +11,7 @@
#include <QDir>
#include <QDesktopServices>
#include "widgets/base/basewidgetopacity.h"
#include "widgets/downloadlistwidget.h"
#include "widgets/common/progressbutton.h"
#include "utils/widgetanimation.h"
@ -23,7 +23,7 @@ namespace Ui {
class MainWindow;
}
class MainWindow : public DBlurEffectWidget
class MainWindow : public BaseWidgetOpacity
{
Q_OBJECT
@ -53,10 +53,6 @@ private slots:
void onGetUrl(const QString &url);
void on_pushButton_14_clicked();
// QWidget interface
protected:
bool closeWindowAnimation = false;
void closeEvent(QCloseEvent *event) override;
};
#endif // MAINWINDOWDTK_H

@ -58,7 +58,8 @@ SOURCES += \
backend/downloadworker.cpp \
pages/appintopage.cpp \
widgets/big_image.cpp \
backend/image_show.cpp
backend/image_show.cpp \
widgets/base/basewidgetopacity.cpp
HEADERS += \
dbus/dbussparkstoreservice.h \
@ -79,7 +80,8 @@ HEADERS += \
backend/downloadworker.h \
pages/appintopage.h \
widgets/big_image.h \
backend/image_show.h
backend/image_show.h \
widgets/base/basewidgetopacity.h
FORMS += \
mainwindow-dtk.ui \

@ -25,7 +25,7 @@ void WidgetAnimation::widgetShake(QWidget *pWidget, int nRange)
pAnimation->start(QAbstractAnimation::DeleteWhenStopped);
}
void WidgetAnimation::widgetOpacity(QWidget *pWidget, bool isAppear)
QPropertyAnimation* WidgetAnimation::createWidgetOpacity(QWidget *pWidget, bool isAppear)
{
QPropertyAnimation *animation = new QPropertyAnimation(pWidget, "windowOpacity", pWidget);
//设置动画效果
@ -42,6 +42,11 @@ void WidgetAnimation::widgetOpacity(QWidget *pWidget, bool isAppear)
animation->setKeyValueAt(0, 1);
animation->setKeyValueAt(1, 0);
}
// 开始动画
animation->start();
return animation;
}
void WidgetAnimation::widgetOpacity(QWidget *pWidget, bool isAppear)
{
// 开始动画
createWidgetOpacity(pWidget, isAppear)->start();
}

@ -10,6 +10,8 @@ class WidgetAnimation
public:
WidgetAnimation();
static void widgetShake(QWidget *pWidget, int nRange);
static QPropertyAnimation* createWidgetOpacity(QWidget *pWidget, bool isAppear);
static void widgetOpacity(QWidget *pWidget, bool isAppear);
};

@ -0,0 +1,37 @@
#include "basewidgetopacity.h"
#include <QCloseEvent>
#include <QPropertyAnimation>
BaseWidgetOpacity::BaseWidgetOpacity(QWidget *parent) : DBlurEffectWidget(parent)
{
// WidgetAnimation::widgetOpacity(this,true);
}
/// @brief 窗口关闭事件
/// @param event
void BaseWidgetOpacity::closeEvent(QCloseEvent *event)
{
if (!closeWindowAnimation) {
QPropertyAnimation *animation = new QPropertyAnimation(this, "windowOpacity");
animation->setEasingCurve(QEasingCurve::OutQuart);
animation->setDuration(500);
animation->setStartValue(1.0);
animation->setEndValue(0.0);
QObject::connect(animation, &QPropertyAnimation::valueChanged, this, [=](const QVariant &value){
this->update();
// setWindowTitle(QString("ヾ(⌒∇⌒*)See You♪ - %1%").arg(int(value.toFloat() * 100)));
});
QObject::connect(animation, &QPropertyAnimation::finished, this, [=](){
closeWindowAnimation = true;
this->close();
});
animation->start();
event->ignore();
} else {
event->accept();
}
}

@ -0,0 +1,24 @@
#ifndef BASEWIDGETOPACITY_H
#define BASEWIDGETOPACITY_H
#include <DBlurEffectWidget>
DWIDGET_USE_NAMESPACE
class BaseWidgetOpacity : public DBlurEffectWidget
{
Q_OBJECT
public:
explicit BaseWidgetOpacity(QWidget *parent = nullptr);
signals:
public slots:
// QWidget interface
protected:
bool closeWindowAnimation = false;
void closeEvent(QCloseEvent *event) override;
};
#endif // BASEWIDGETOPACITY_H