From 6a27b85924159e90f4fa1f608ae95ab67af73cd7 Mon Sep 17 00:00:00 2001 From: FarmRadio Hangar Date: Tue, 17 May 2016 16:58:46 +0300 Subject: [PATCH] re-implement Raised Button animations as state machine --- components/raisedbutton.cpp | 98 +++++++++++-------------------------- components/raisedbutton.h | 28 ----------- components/raisedbutton_p.h | 2 + 3 files changed, 31 insertions(+), 97 deletions(-) diff --git a/components/raisedbutton.cpp b/components/raisedbutton.cpp index 546664a..6c40768 100644 --- a/components/raisedbutton.cpp +++ b/components/raisedbutton.cpp @@ -1,5 +1,6 @@ #include "raisedbutton.h" #include +#include #include "raisedbutton_p.h" void RaisedButtonPrivate::init() @@ -13,6 +14,34 @@ void RaisedButtonPrivate::init() 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, "blurRadius", 7); + + pressedState->assignProperty(effect, "offset", QPoint(0, 6)); + pressedState->assignProperty(effect, "blurRadius", 20); + + QAbstractTransition *transition; + + transition = new QEventTransition(q, QEvent::MouseButtonPress); + transition->setTargetState(pressedState); + normalState->addTransition(transition); + + transition = new QEventTransition(q, QEvent::MouseButtonRelease); + transition->setTargetState(normalState); + pressedState->addTransition(transition); + + machine.setInitialState(normalState); + + // add animation.. duration: 100 !!! + + machine.start(); } RaisedButton::RaisedButton(QWidget *parent) @@ -26,72 +55,3 @@ RaisedButton::RaisedButton(QWidget *parent) RaisedButton::~RaisedButton() { } - -//#include -//#include -//#include -//#include -//#include -//#include "raisedbutton.h" -// -//RaisedButton::RaisedButton(QWidget *parent) -// : FlatButton(parent) -//{ -// QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect; -// effect->setBlurRadius(7); -// effect->setOffset(QPoint(0, 0)); -// effect->setColor(QColor(0, 0, 0, 100)); -// setGraphicsEffect(effect); -// -// setAutoFillBackground(true); -// -// QPropertyAnimation *animation; -// -// animation = new QPropertyAnimation; -// animation->setTargetObject(effect); -// animation->setPropertyName("offset"); -// animation->setStartValue(QPoint(0, 6)); -// animation->setEndValue(QPoint(0, 0)); -// animation->setDuration(100); -// _group.addAnimation(animation); -// -// animation = new QPropertyAnimation; -// animation->setTargetObject(effect); -// animation->setPropertyName("blurRadius"); -// animation->setStartValue(20); -// animation->setEndValue(7); -// animation->setDuration(100); -// _group.addAnimation(animation); -// -// connect(animation, SIGNAL(valueChanged(QVariant)), this, SLOT(update())); -//} -// -//RaisedButton::~RaisedButton() -//{ -//} -// -//void RaisedButton::mousePressEvent(QMouseEvent *event) -//{ -// _group.setDirection(QAbstractAnimation::Backward); -// _group.start(); -// -// FlatButton::mousePressEvent(event); -//} -// -//void RaisedButton::mouseReleaseEvent(QMouseEvent *event) -//{ -// _group.setDirection(QAbstractAnimation::Forward); -// _group.start(); -// -// FlatButton::mouseReleaseEvent(event); -//} -// -//void RaisedButton::paintEvent(QPaintEvent *event) -//{ -// Q_UNUSED(event) -// -// QStylePainter painter(this); -// -//// painter.drawControl(QStyle::CE_PushButton, getStyleOption()); -//} -// diff --git a/components/raisedbutton.h b/components/raisedbutton.h index 528ea45..d5d827e 100644 --- a/components/raisedbutton.h +++ b/components/raisedbutton.h @@ -19,31 +19,3 @@ private: }; #endif // RAISEDBUTTON_H - -//#ifndef RAISEDBUTTON_H -//#define RAISEDBUTTON_H -// -//#include -//#include "flatbutton.h" -// -//class QPropertyAnimation; -// -//class RaisedButton : public FlatButton -//{ -// Q_OBJECT -// -//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 -// diff --git a/components/raisedbutton_p.h b/components/raisedbutton_p.h index 821d549..cd5c205 100644 --- a/components/raisedbutton_p.h +++ b/components/raisedbutton_p.h @@ -14,6 +14,8 @@ public: {} void init(); + + QStateMachine machine; }; #endif // RAISEDBUTTON_P_H