animate drop shadow on RaisedButton

This commit is contained in:
laserpants 2016-03-22 12:48:59 +03:00
parent 7a04426900
commit 28b063e591
2 changed files with 62 additions and 0 deletions

View File

@ -1,3 +1,7 @@
#include <QDebug>
#include <QPropertyAnimation>
#include <QStylePainter>
#include <QPaintEvent>
#include "raisedbutton.h"
#include "lib/customshadoweffect.h"
@ -7,8 +11,55 @@ RaisedButton::RaisedButton(QWidget *parent)
CustomShadowEffect *effect = new CustomShadowEffect;
setGraphicsEffect(effect);
setAutoFillBackground(true);
QPropertyAnimation *animation;
animation = new QPropertyAnimation;
animation->setTargetObject(effect);
animation->setPropertyName("distance");
animation->setStartValue(2);
animation->setEndValue(6);
animation->setDuration(100);
_group.addAnimation(animation);
animation = new QPropertyAnimation;
animation->setTargetObject(effect);
animation->setPropertyName("blurRadius");
animation->setStartValue(10);
animation->setEndValue(20);
animation->setDuration(100);
_group.addAnimation(animation);
connect(animation, SIGNAL(valueChanged(QVariant)), this, SLOT(update()));
}
RaisedButton::~RaisedButton()
{
}
void RaisedButton::mousePressEvent(QMouseEvent *event)
{
Q_UNUSED(event)
_group.setDirection(QAbstractAnimation::Forward);
_group.start();
FlatButton::mousePressEvent(event);
}
void RaisedButton::mouseReleaseEvent(QMouseEvent *event)
{
Q_UNUSED(event)
_group.setDirection(QAbstractAnimation::Backward);
_group.start();
FlatButton::mouseReleaseEvent(event);
}
void RaisedButton::paintEvent(QPaintEvent *event)
{
QStylePainter painter(this);
painter.drawControl(QStyle::CE_PushButton, getStyleOption());
}

View File

@ -1,8 +1,11 @@
#ifndef RAISEDBUTTON_H
#define RAISEDBUTTON_H
#include <QParallelAnimationGroup>
#include "flatbutton.h"
class QPropertyAnimation;
class RaisedButton : public FlatButton
{
Q_OBJECT
@ -10,6 +13,14 @@ class RaisedButton : public FlatButton
public:
explicit RaisedButton(QWidget *parent = 0);
~RaisedButton();
protected:
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
QParallelAnimationGroup _group;
};
#endif // RAISEDBUTTON_H