From 18fdf9c03cb86180b00ed0308b1038b79cd011ee Mon Sep 17 00:00:00 2001 From: FarmRadio Hangar Date: Wed, 18 May 2016 15:08:11 +0300 Subject: [PATCH] animate Raised Button --- components/raisedbutton.cpp | 65 +++++++++++++++++++++++++++++++++---- components/raisedbutton.h | 3 ++ 2 files changed, 62 insertions(+), 6 deletions(-) diff --git a/components/raisedbutton.cpp b/components/raisedbutton.cpp index 6c40768..7fd3dce 100644 --- a/components/raisedbutton.cpp +++ b/components/raisedbutton.cpp @@ -1,6 +1,10 @@ #include "raisedbutton.h" #include #include +#include +#include +#include +#include #include "raisedbutton_p.h" void RaisedButtonPrivate::init() @@ -9,37 +13,59 @@ void RaisedButtonPrivate::init() QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect; effect->setBlurRadius(7); - effect->setOffset(QPoint(0, 0)); + effect->setOffset(QPointF(0, 0)); effect->setColor(QColor(0, 0, 0, 100)); q->setGraphicsEffect(effect); - q->setAutoFillBackground(true); - QState *normalState = new QState; QState *pressedState = new QState; machine.addState(normalState); machine.addState(pressedState); - normalState->assignProperty(effect, "offset", QPoint(0, 0)); + normalState->assignProperty(effect, "offset", QPointF(0, 0)); normalState->assignProperty(effect, "blurRadius", 7); - pressedState->assignProperty(effect, "offset", QPoint(0, 6)); + pressedState->assignProperty(effect, "offset", QPointF(0, 6)); pressedState->assignProperty(effect, "blurRadius", 20); QAbstractTransition *transition; + QPropertyAnimation *animation; + + // transition = new QEventTransition(q, QEvent::MouseButtonPress); transition->setTargetState(pressedState); + + animation = new QPropertyAnimation(effect, "offset"); + animation->setDuration(100); + transition->addAnimation(animation); + animation = new QPropertyAnimation(effect, "blurRadius"); + animation->setDuration(100); + transition->addAnimation(animation); + normalState->addTransition(transition); + // + transition = new QEventTransition(q, QEvent::MouseButtonRelease); transition->setTargetState(normalState); + + animation = new QPropertyAnimation(effect, "offset"); + transition->addAnimation(animation); + animation->setDuration(100); + animation = new QPropertyAnimation(effect, "blurRadius"); + animation->setDuration(100); + transition->addAnimation(animation); + pressedState->addTransition(transition); + // + machine.setInitialState(normalState); - // add animation.. duration: 100 !!! + QObject::connect(effect, SIGNAL(blurRadiusChanged(qreal)), q, SLOT(update())); + QObject::connect(effect, SIGNAL(offsetChanged(QPointF)), q, SLOT(update())); machine.start(); } @@ -55,3 +81,30 @@ RaisedButton::RaisedButton(QWidget *parent) RaisedButton::~RaisedButton() { } + +void RaisedButton::paintEvent(QPaintEvent *event) +{ + Q_UNUSED(event) + + QPainter painter(this); + + painter.setRenderHint(QPainter::Antialiasing); + + QBrush brush; + brush.setStyle(Qt::SolidPattern); + brush.setColor(Qt::white); + painter.setBrush(brush); + painter.setPen(Qt::NoPen); + + painter.drawRoundedRect(rect(), 3, 3); + + QStylePainter style(this); + + QStyleOptionButton option; + initStyleOption(&option); + option.features |= QStyleOptionButton::Flat; + + style.drawControl(QStyle::CE_PushButtonLabel, option); + + //FlatButton::paintEvent(event); +} diff --git a/components/raisedbutton.h b/components/raisedbutton.h index d5d827e..3cd94b4 100644 --- a/components/raisedbutton.h +++ b/components/raisedbutton.h @@ -13,6 +13,9 @@ public: explicit RaisedButton(QWidget *parent = 0); ~RaisedButton(); +protected: + void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; + private: Q_DISABLE_COPY(RaisedButton) Q_DECLARE_PRIVATE(RaisedButton)