From 011e46b70ba36720e2a772a487e8534744d902dc Mon Sep 17 00:00:00 2001 From: Gary Wang Date: Sun, 6 Oct 2019 14:58:01 +0800 Subject: [PATCH] refactor: opacity helper --- PineapplePictures.pro | 2 ++ bottombuttongroup.cpp | 8 ++++++++ bottombuttongroup.h | 5 +++++ mainwindow.cpp | 43 ++++++++++--------------------------------- mainwindow.h | 4 ---- navigatorview.cpp | 7 +++++++ navigatorview.h | 3 +++ opacityhelper.cpp | 27 +++++++++++++++++++++++++++ opacityhelper.h | 23 +++++++++++++++++++++++ toolbutton.cpp | 15 +++++---------- toolbutton.h | 11 +++-------- 11 files changed, 93 insertions(+), 55 deletions(-) create mode 100644 opacityhelper.cpp create mode 100644 opacityhelper.h diff --git a/PineapplePictures.pro b/PineapplePictures.pro index a22e560..3b4a66a 100644 --- a/PineapplePictures.pro +++ b/PineapplePictures.pro @@ -31,6 +31,7 @@ SOURCES += \ bottombuttongroup.cpp \ graphicsscene.cpp \ navigatorview.cpp \ + opacityhelper.cpp \ toolbutton.cpp HEADERS += \ @@ -39,6 +40,7 @@ HEADERS += \ bottombuttongroup.h \ graphicsscene.h \ navigatorview.h \ + opacityhelper.h \ toolbutton.h FORMS += diff --git a/bottombuttongroup.cpp b/bottombuttongroup.cpp index c57f7e3..17f7a12 100644 --- a/bottombuttongroup.cpp +++ b/bottombuttongroup.cpp @@ -1,5 +1,7 @@ #include "bottombuttongroup.h" +#include "opacityhelper.h" + #include #include @@ -8,6 +10,7 @@ BottomButtonGroup::BottomButtonGroup(QWidget *parent) : QGroupBox (parent) + , m_opacityHelper(new OpacityHelper(this)) { QHBoxLayout * mainLayout = new QHBoxLayout(this); mainLayout->setSizeConstraint(QLayout::SetFixedSize); @@ -53,6 +56,11 @@ BottomButtonGroup::BottomButtonGroup(QWidget *parent) })); } +void BottomButtonGroup::setOpacity(qreal opacity, bool animated) +{ + m_opacityHelper->setOpacity(opacity, animated); +} + void BottomButtonGroup::addButton(QAbstractButton *button) { layout()->addWidget(button); diff --git a/bottombuttongroup.h b/bottombuttongroup.h index 1284959..de28b39 100644 --- a/bottombuttongroup.h +++ b/bottombuttongroup.h @@ -4,12 +4,14 @@ #include #include +class OpacityHelper; class BottomButtonGroup : public QGroupBox { Q_OBJECT public: explicit BottomButtonGroup(QWidget *parent = nullptr); + void setOpacity(qreal opacity, bool animated = true); void addButton(QAbstractButton *button); signals: @@ -19,6 +21,9 @@ signals: void zoomOutBtnClicked(); void toggleCheckerboardBtnClicked(); void rotateRightBtnClicked(); + +private: + OpacityHelper * m_opacityHelper; }; #endif // BOTTOMBUTTONGROUP_H diff --git a/mainwindow.cpp b/mainwindow.cpp index 8348f5a..e95cd2d 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -13,7 +13,6 @@ #include #include #include -#include #include #ifdef _WIN32 @@ -97,17 +96,9 @@ MainWindow::MainWindow(QWidget *parent) : m_gv->setVisible(false); }); - m_btnGrpEffect = new QGraphicsOpacityEffect(this); - m_bribViewEffect = new QGraphicsOpacityEffect(this); - m_bottomButtonGroup->setGraphicsEffect(m_btnGrpEffect); - m_gv->setGraphicsEffect(m_bribViewEffect); - m_btnGrpOpacityAnimation = new QPropertyAnimation(m_btnGrpEffect, "opacity"); - m_btnGrpOpacityAnimation->setDuration(300); - m_bribViewOpacityAnimation = new QPropertyAnimation(m_bribViewEffect, "opacity"); - m_bribViewOpacityAnimation->setDuration(300); - - m_btnGrpEffect->setOpacity(0); - m_bribViewEffect->setOpacity(0); + m_bottomButtonGroup->setOpacity(0, false); + m_gv->setOpacity(0, false); + m_closeButton->setOpacity(0, false); centerWindow(); } @@ -159,34 +150,20 @@ void MainWindow::showEvent(QShowEvent *event) void MainWindow::enterEvent(QEvent *event) { - m_btnGrpOpacityAnimation->stop(); - m_btnGrpOpacityAnimation->setStartValue(m_btnGrpEffect->opacity()); - m_btnGrpOpacityAnimation->setEndValue(1); - m_btnGrpOpacityAnimation->start(); + m_bottomButtonGroup->setOpacity(1); + m_gv->setOpacity(1); - m_bribViewOpacityAnimation->stop(); - m_bribViewOpacityAnimation->setStartValue(m_bribViewEffect->opacity()); - m_bribViewOpacityAnimation->setEndValue(1); - m_bribViewOpacityAnimation->start(); - - m_closeButton->setIconOpacity(1); + m_closeButton->setOpacity(1); return QMainWindow::enterEvent(event); } void MainWindow::leaveEvent(QEvent *event) { - m_btnGrpOpacityAnimation->stop(); - m_btnGrpOpacityAnimation->setStartValue(m_btnGrpEffect->opacity()); - m_btnGrpOpacityAnimation->setEndValue(0); - m_btnGrpOpacityAnimation->start(); + m_bottomButtonGroup->setOpacity(0); + m_gv->setOpacity(0); - m_bribViewOpacityAnimation->stop(); - m_bribViewOpacityAnimation->setStartValue(m_bribViewEffect->opacity()); - m_bribViewOpacityAnimation->setEndValue(0); - m_bribViewOpacityAnimation->start(); - - m_closeButton->setIconOpacity(0); + m_closeButton->setOpacity(0); return QMainWindow::leaveEvent(event); } @@ -274,7 +251,7 @@ bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *r #ifdef _WIN32 // https://stackoverflow.com/questions/43505580/qt-windows-resizable-frameless-window // Too lazy to do this now.. just stackoverflow it and did a copy and paste.. - Q_UNUSED(eventType); + Q_UNUSED(eventType) MSG* msg = static_cast(message); if (msg->message == WM_NCHITTEST) { diff --git a/mainwindow.h b/mainwindow.h index 230650c..c4bab33 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -49,10 +49,6 @@ protected slots: private: QPoint m_oldMousePos; - QGraphicsOpacityEffect *m_btnGrpEffect; - QGraphicsOpacityEffect *m_bribViewEffect; - QPropertyAnimation *m_btnGrpOpacityAnimation; - QPropertyAnimation *m_bribViewOpacityAnimation; QPropertyAnimation *m_fadeOutAnimation; QPropertyAnimation *m_floatUpAnimation; QParallelAnimationGroup *m_exitAnimationGroup; diff --git a/navigatorview.cpp b/navigatorview.cpp index c02dee8..3882cdd 100644 --- a/navigatorview.cpp +++ b/navigatorview.cpp @@ -1,6 +1,7 @@ #include "navigatorview.h" #include "graphicsview.h" +#include "opacityhelper.h" #include #include @@ -8,6 +9,7 @@ NavigatorView::NavigatorView(QWidget *parent) : QGraphicsView (parent) , m_viewportRegion(this->rect()) + , m_opacityHelper(new OpacityHelper(this)) { setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); @@ -21,6 +23,11 @@ void NavigatorView::setMainView(GraphicsView *mainView) m_mainView = mainView; } +void NavigatorView::setOpacity(qreal opacity, bool animated) +{ + m_opacityHelper->setOpacity(opacity, animated); +} + void NavigatorView::updateMainViewportRegion() { if (m_mainView != nullptr) { diff --git a/navigatorview.h b/navigatorview.h index 163e1ab..f0cbc07 100644 --- a/navigatorview.h +++ b/navigatorview.h @@ -3,6 +3,7 @@ #include +class OpacityHelper; class GraphicsView; class NavigatorView : public QGraphicsView { @@ -11,6 +12,7 @@ public: NavigatorView(QWidget *parent = nullptr); void setMainView(GraphicsView *mainView); + void setOpacity(qreal opacity, bool animated = true); public slots: void updateMainViewportRegion(); @@ -26,6 +28,7 @@ private: bool m_mouseDown = false; QPolygon m_viewportRegion; QGraphicsView *m_mainView = nullptr; + OpacityHelper *m_opacityHelper = nullptr; }; #endif // NAVIGATORVIEW_H diff --git a/opacityhelper.cpp b/opacityhelper.cpp new file mode 100644 index 0000000..3e5b43e --- /dev/null +++ b/opacityhelper.cpp @@ -0,0 +1,27 @@ +#include "opacityhelper.h" + +#include +#include + +OpacityHelper::OpacityHelper(QWidget *parent) + : QObject(parent) + , m_opacityFx(new QGraphicsOpacityEffect(parent)) + , m_opacityAnimation(new QPropertyAnimation(m_opacityFx, "opacity")) +{ + parent->setGraphicsEffect(m_opacityFx); + + m_opacityAnimation->setDuration(300); +} + +void OpacityHelper::setOpacity(qreal opacity, bool animated) +{ + if (!animated) { + m_opacityFx->setOpacity(opacity); + return; + } + + m_opacityAnimation->stop(); + m_opacityAnimation->setStartValue(m_opacityFx->opacity()); + m_opacityAnimation->setEndValue(opacity); + m_opacityAnimation->start(); +} diff --git a/opacityhelper.h b/opacityhelper.h new file mode 100644 index 0000000..db9871e --- /dev/null +++ b/opacityhelper.h @@ -0,0 +1,23 @@ +#ifndef OPACITYHELPER_H +#define OPACITYHELPER_H + +#include + +QT_BEGIN_NAMESPACE +class QGraphicsOpacityEffect; +class QPropertyAnimation; +QT_END_NAMESPACE + +class OpacityHelper : QObject +{ +public: + OpacityHelper(QWidget * parent); + + void setOpacity(qreal opacity, bool animated = true); + +protected: + QGraphicsOpacityEffect * m_opacityFx; + QPropertyAnimation * m_opacityAnimation; +}; + +#endif // OPACITYHELPER_H diff --git a/toolbutton.cpp b/toolbutton.cpp index 6e66fa8..689b4cb 100644 --- a/toolbutton.cpp +++ b/toolbutton.cpp @@ -1,31 +1,26 @@ #include "toolbutton.h" +#include "opacityhelper.h" + #include #include #include ToolButton::ToolButton(QWidget *parent) : QPushButton(parent) - , m_opacityFx(new QGraphicsOpacityEffect(this)) - , m_opacityAnimation(new QPropertyAnimation(m_opacityFx, "opacity")) + , m_opacityHelper(new OpacityHelper(this)) { setFlat(true); setFixedSize(50, 50); - setGraphicsEffect(m_opacityFx); setStyleSheet("QPushButton {" "background: transparent;" "}" "QPushButton:hover {" "background: red;" "}"); - - m_opacityAnimation->setDuration(300); } -void ToolButton::setIconOpacity(qreal opacity) +void ToolButton::setOpacity(qreal opacity, bool animated) { - m_opacityAnimation->stop(); - m_opacityAnimation->setStartValue(m_opacityFx->opacity()); - m_opacityAnimation->setEndValue(opacity); - m_opacityAnimation->start(); + m_opacityHelper->setOpacity(opacity, animated); } diff --git a/toolbutton.h b/toolbutton.h index 0002273..f6db8f5 100644 --- a/toolbutton.h +++ b/toolbutton.h @@ -3,11 +3,7 @@ #include -QT_BEGIN_NAMESPACE -class QGraphicsOpacityEffect; -class QPropertyAnimation; -QT_END_NAMESPACE - +class OpacityHelper; class ToolButton : public QPushButton { Q_OBJECT @@ -15,11 +11,10 @@ public: ToolButton(QWidget * parent = nullptr); public slots: - void setIconOpacity(qreal opacity); + void setOpacity(qreal opacity, bool animated = true); private: - QGraphicsOpacityEffect * m_opacityFx; - QPropertyAnimation * m_opacityAnimation; + OpacityHelper * m_opacityHelper; }; #endif // TOOLBUTTON_H