refactor: opacity helper

This commit is contained in:
Gary Wang 2019-10-06 14:58:01 +08:00
parent 665385aa3d
commit 011e46b70b
11 changed files with 93 additions and 55 deletions

View File

@ -31,6 +31,7 @@ SOURCES += \
bottombuttongroup.cpp \ bottombuttongroup.cpp \
graphicsscene.cpp \ graphicsscene.cpp \
navigatorview.cpp \ navigatorview.cpp \
opacityhelper.cpp \
toolbutton.cpp toolbutton.cpp
HEADERS += \ HEADERS += \
@ -39,6 +40,7 @@ HEADERS += \
bottombuttongroup.h \ bottombuttongroup.h \
graphicsscene.h \ graphicsscene.h \
navigatorview.h \ navigatorview.h \
opacityhelper.h \
toolbutton.h toolbutton.h
FORMS += FORMS +=

View File

@ -1,5 +1,7 @@
#include "bottombuttongroup.h" #include "bottombuttongroup.h"
#include "opacityhelper.h"
#include <functional> #include <functional>
#include <QPushButton> #include <QPushButton>
@ -8,6 +10,7 @@
BottomButtonGroup::BottomButtonGroup(QWidget *parent) BottomButtonGroup::BottomButtonGroup(QWidget *parent)
: QGroupBox (parent) : QGroupBox (parent)
, m_opacityHelper(new OpacityHelper(this))
{ {
QHBoxLayout * mainLayout = new QHBoxLayout(this); QHBoxLayout * mainLayout = new QHBoxLayout(this);
mainLayout->setSizeConstraint(QLayout::SetFixedSize); 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) void BottomButtonGroup::addButton(QAbstractButton *button)
{ {
layout()->addWidget(button); layout()->addWidget(button);

View File

@ -4,12 +4,14 @@
#include <QAbstractButton> #include <QAbstractButton>
#include <QGroupBox> #include <QGroupBox>
class OpacityHelper;
class BottomButtonGroup : public QGroupBox class BottomButtonGroup : public QGroupBox
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit BottomButtonGroup(QWidget *parent = nullptr); explicit BottomButtonGroup(QWidget *parent = nullptr);
void setOpacity(qreal opacity, bool animated = true);
void addButton(QAbstractButton *button); void addButton(QAbstractButton *button);
signals: signals:
@ -19,6 +21,9 @@ signals:
void zoomOutBtnClicked(); void zoomOutBtnClicked();
void toggleCheckerboardBtnClicked(); void toggleCheckerboardBtnClicked();
void rotateRightBtnClicked(); void rotateRightBtnClicked();
private:
OpacityHelper * m_opacityHelper;
}; };
#endif // BOTTOMBUTTONGROUP_H #endif // BOTTOMBUTTONGROUP_H

View File

@ -13,7 +13,6 @@
#include <QApplication> #include <QApplication>
#include <QStyle> #include <QStyle>
#include <QScreen> #include <QScreen>
#include <QGraphicsOpacityEffect>
#include <QMenu> #include <QMenu>
#ifdef _WIN32 #ifdef _WIN32
@ -97,17 +96,9 @@ MainWindow::MainWindow(QWidget *parent) :
m_gv->setVisible(false); m_gv->setVisible(false);
}); });
m_btnGrpEffect = new QGraphicsOpacityEffect(this); m_bottomButtonGroup->setOpacity(0, false);
m_bribViewEffect = new QGraphicsOpacityEffect(this); m_gv->setOpacity(0, false);
m_bottomButtonGroup->setGraphicsEffect(m_btnGrpEffect); m_closeButton->setOpacity(0, false);
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);
centerWindow(); centerWindow();
} }
@ -159,34 +150,20 @@ void MainWindow::showEvent(QShowEvent *event)
void MainWindow::enterEvent(QEvent *event) void MainWindow::enterEvent(QEvent *event)
{ {
m_btnGrpOpacityAnimation->stop(); m_bottomButtonGroup->setOpacity(1);
m_btnGrpOpacityAnimation->setStartValue(m_btnGrpEffect->opacity()); m_gv->setOpacity(1);
m_btnGrpOpacityAnimation->setEndValue(1);
m_btnGrpOpacityAnimation->start();
m_bribViewOpacityAnimation->stop(); m_closeButton->setOpacity(1);
m_bribViewOpacityAnimation->setStartValue(m_bribViewEffect->opacity());
m_bribViewOpacityAnimation->setEndValue(1);
m_bribViewOpacityAnimation->start();
m_closeButton->setIconOpacity(1);
return QMainWindow::enterEvent(event); return QMainWindow::enterEvent(event);
} }
void MainWindow::leaveEvent(QEvent *event) void MainWindow::leaveEvent(QEvent *event)
{ {
m_btnGrpOpacityAnimation->stop(); m_bottomButtonGroup->setOpacity(0);
m_btnGrpOpacityAnimation->setStartValue(m_btnGrpEffect->opacity()); m_gv->setOpacity(0);
m_btnGrpOpacityAnimation->setEndValue(0);
m_btnGrpOpacityAnimation->start();
m_bribViewOpacityAnimation->stop(); m_closeButton->setOpacity(0);
m_bribViewOpacityAnimation->setStartValue(m_bribViewEffect->opacity());
m_bribViewOpacityAnimation->setEndValue(0);
m_bribViewOpacityAnimation->start();
m_closeButton->setIconOpacity(0);
return QMainWindow::leaveEvent(event); return QMainWindow::leaveEvent(event);
} }
@ -274,7 +251,7 @@ bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
#ifdef _WIN32 #ifdef _WIN32
// https://stackoverflow.com/questions/43505580/qt-windows-resizable-frameless-window // 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.. // 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<MSG*>(message); MSG* msg = static_cast<MSG*>(message);
if (msg->message == WM_NCHITTEST) { if (msg->message == WM_NCHITTEST) {

View File

@ -49,10 +49,6 @@ protected slots:
private: private:
QPoint m_oldMousePos; QPoint m_oldMousePos;
QGraphicsOpacityEffect *m_btnGrpEffect;
QGraphicsOpacityEffect *m_bribViewEffect;
QPropertyAnimation *m_btnGrpOpacityAnimation;
QPropertyAnimation *m_bribViewOpacityAnimation;
QPropertyAnimation *m_fadeOutAnimation; QPropertyAnimation *m_fadeOutAnimation;
QPropertyAnimation *m_floatUpAnimation; QPropertyAnimation *m_floatUpAnimation;
QParallelAnimationGroup *m_exitAnimationGroup; QParallelAnimationGroup *m_exitAnimationGroup;

View File

@ -1,6 +1,7 @@
#include "navigatorview.h" #include "navigatorview.h"
#include "graphicsview.h" #include "graphicsview.h"
#include "opacityhelper.h"
#include <QMouseEvent> #include <QMouseEvent>
#include <QDebug> #include <QDebug>
@ -8,6 +9,7 @@
NavigatorView::NavigatorView(QWidget *parent) NavigatorView::NavigatorView(QWidget *parent)
: QGraphicsView (parent) : QGraphicsView (parent)
, m_viewportRegion(this->rect()) , m_viewportRegion(this->rect())
, m_opacityHelper(new OpacityHelper(this))
{ {
setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
@ -21,6 +23,11 @@ void NavigatorView::setMainView(GraphicsView *mainView)
m_mainView = mainView; m_mainView = mainView;
} }
void NavigatorView::setOpacity(qreal opacity, bool animated)
{
m_opacityHelper->setOpacity(opacity, animated);
}
void NavigatorView::updateMainViewportRegion() void NavigatorView::updateMainViewportRegion()
{ {
if (m_mainView != nullptr) { if (m_mainView != nullptr) {

View File

@ -3,6 +3,7 @@
#include <QGraphicsView> #include <QGraphicsView>
class OpacityHelper;
class GraphicsView; class GraphicsView;
class NavigatorView : public QGraphicsView class NavigatorView : public QGraphicsView
{ {
@ -11,6 +12,7 @@ public:
NavigatorView(QWidget *parent = nullptr); NavigatorView(QWidget *parent = nullptr);
void setMainView(GraphicsView *mainView); void setMainView(GraphicsView *mainView);
void setOpacity(qreal opacity, bool animated = true);
public slots: public slots:
void updateMainViewportRegion(); void updateMainViewportRegion();
@ -26,6 +28,7 @@ private:
bool m_mouseDown = false; bool m_mouseDown = false;
QPolygon m_viewportRegion; QPolygon m_viewportRegion;
QGraphicsView *m_mainView = nullptr; QGraphicsView *m_mainView = nullptr;
OpacityHelper *m_opacityHelper = nullptr;
}; };
#endif // NAVIGATORVIEW_H #endif // NAVIGATORVIEW_H

27
opacityhelper.cpp Normal file
View File

@ -0,0 +1,27 @@
#include "opacityhelper.h"
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
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();
}

23
opacityhelper.h Normal file
View File

@ -0,0 +1,23 @@
#ifndef OPACITYHELPER_H
#define OPACITYHELPER_H
#include <QWidget>
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

View File

@ -1,31 +1,26 @@
#include "toolbutton.h" #include "toolbutton.h"
#include "opacityhelper.h"
#include <QPainter> #include <QPainter>
#include <QGraphicsOpacityEffect> #include <QGraphicsOpacityEffect>
#include <QPropertyAnimation> #include <QPropertyAnimation>
ToolButton::ToolButton(QWidget *parent) ToolButton::ToolButton(QWidget *parent)
: QPushButton(parent) : QPushButton(parent)
, m_opacityFx(new QGraphicsOpacityEffect(this)) , m_opacityHelper(new OpacityHelper(this))
, m_opacityAnimation(new QPropertyAnimation(m_opacityFx, "opacity"))
{ {
setFlat(true); setFlat(true);
setFixedSize(50, 50); setFixedSize(50, 50);
setGraphicsEffect(m_opacityFx);
setStyleSheet("QPushButton {" setStyleSheet("QPushButton {"
"background: transparent;" "background: transparent;"
"}" "}"
"QPushButton:hover {" "QPushButton:hover {"
"background: red;" "background: red;"
"}"); "}");
m_opacityAnimation->setDuration(300);
} }
void ToolButton::setIconOpacity(qreal opacity) void ToolButton::setOpacity(qreal opacity, bool animated)
{ {
m_opacityAnimation->stop(); m_opacityHelper->setOpacity(opacity, animated);
m_opacityAnimation->setStartValue(m_opacityFx->opacity());
m_opacityAnimation->setEndValue(opacity);
m_opacityAnimation->start();
} }

View File

@ -3,11 +3,7 @@
#include <QPushButton> #include <QPushButton>
QT_BEGIN_NAMESPACE class OpacityHelper;
class QGraphicsOpacityEffect;
class QPropertyAnimation;
QT_END_NAMESPACE
class ToolButton : public QPushButton class ToolButton : public QPushButton
{ {
Q_OBJECT Q_OBJECT
@ -15,11 +11,10 @@ public:
ToolButton(QWidget * parent = nullptr); ToolButton(QWidget * parent = nullptr);
public slots: public slots:
void setIconOpacity(qreal opacity); void setOpacity(qreal opacity, bool animated = true);
private: private:
QGraphicsOpacityEffect * m_opacityFx; OpacityHelper * m_opacityHelper;
QPropertyAnimation * m_opacityAnimation;
}; };
#endif // TOOLBUTTON_H #endif // TOOLBUTTON_H