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 \
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 +=

View File

@ -1,5 +1,7 @@
#include "bottombuttongroup.h"
#include "opacityhelper.h"
#include <functional>
#include <QPushButton>
@ -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);

View File

@ -4,12 +4,14 @@
#include <QAbstractButton>
#include <QGroupBox>
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

View File

@ -13,7 +13,6 @@
#include <QApplication>
#include <QStyle>
#include <QScreen>
#include <QGraphicsOpacityEffect>
#include <QMenu>
#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<MSG*>(message);
if (msg->message == WM_NCHITTEST) {

View File

@ -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;

View File

@ -1,6 +1,7 @@
#include "navigatorview.h"
#include "graphicsview.h"
#include "opacityhelper.h"
#include <QMouseEvent>
#include <QDebug>
@ -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) {

View File

@ -3,6 +3,7 @@
#include <QGraphicsView>
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

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 "opacityhelper.h"
#include <QPainter>
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
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);
}

View File

@ -3,11 +3,7 @@
#include <QPushButton>
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