tweak flat button animation parameters

This commit is contained in:
laserpants 2016-05-15 12:17:07 +03:00
parent e646cc0332
commit 6dfd0ab83d
7 changed files with 157 additions and 41 deletions

View File

@ -4,7 +4,6 @@
#include <QStylePainter> #include <QStylePainter>
#include <QStyleOption> #include <QStyleOption>
#include <QApplication> #include <QApplication>
#include <QPalette>
#include <QDebug> #include <QDebug>
#include "lib/rippleoverlay.h" #include "lib/rippleoverlay.h"
#include "lib/ripple.h" #include "lib/ripple.h"
@ -74,13 +73,15 @@ void FlatButton::paintEvent(QPaintEvent *event)
painter.drawControl(QStyle::CE_PushButtonLabel, option); painter.drawControl(QStyle::CE_PushButtonLabel, option);
if (isEnabled() && testAttribute(Qt::WA_Hover) && underMouse()) const qreal bgOpacity = d->delegate->backgroundOpacity();
if (isEnabled() && bgOpacity > 0)
{ {
QPainter painter(this); QPainter painter(this);
QBrush brush; QBrush brush;
brush.setStyle(Qt::SolidPattern); brush.setStyle(Qt::SolidPattern);
brush.setColor(d->textColor()); brush.setColor(d->delegate->backgroundColor());
painter.setOpacity(0.1); painter.setOpacity(bgOpacity);
painter.fillRect(rect(), brush); painter.fillRect(rect(), brush);
} }
@ -98,26 +99,14 @@ void FlatButton::mousePressEvent(QMouseEvent *event)
{ {
Q_D(FlatButton); Q_D(FlatButton);
Style &style = Style::instance();
Ripple *ripple = new Ripple(event->pos()); Ripple *ripple = new Ripple(event->pos());
ripple->setRadiusEndValue(100); ripple->setRadiusEndValue(100);
ripple->setOpacityStartValue(0.2); ripple->setOpacityStartValue(0.3);
ripple->setColor(d->textColor()); ripple->setColor(style.themeColor("text"));
//d->ripple->addRipple(ripple); d->ripple->addRipple(ripple);
QPushButton::mousePressEvent(event); QPushButton::mousePressEvent(event);
} }
void FlatButton::enterEvent(QEvent *event)
{
update();
QPushButton::enterEvent(event);
}
void FlatButton::leaveEvent(QEvent *event)
{
update();
QPushButton::leaveEvent(event);
}

View File

@ -22,8 +22,6 @@ protected:
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void enterEvent(QEvent *event) Q_DECL_OVERRIDE;
void leaveEvent(QEvent *event) Q_DECL_OVERRIDE;
const QScopedPointer<FlatButtonPrivate> d_ptr; const QScopedPointer<FlatButtonPrivate> d_ptr;

View File

@ -1 +1,106 @@
#include "flatbutton_internal.h" #include "flatbutton_internal.h"
#include <QAbstractTransition>
#include <QEventTransition>
#include <QSignalTransition>
#include <QPropertyAnimation>
#include "flatbutton.h"
#include "lib/style.h"
FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent)
: QStateMachine(parent),
button(parent)
{
Style &style = Style::instance();
QState *normalState = new QState;
QState *focusedState = new QState;
QState *pressedState = new QState;
addState(normalState);
addState(focusedState);
addState(pressedState);
setInitialState(normalState);
normalState->assignProperty(this, "backgroundOpacity", 0);
normalState->assignProperty(this, "backgroundColor", style.themeColor("text"));
focusedState->assignProperty(this, "backgroundOpacity", 0.15);
focusedState->assignProperty(this, "backgroundColor", style.themeColor("text"));
pressedState->assignProperty(this, "backgroundOpacity", 0.15);
pressedState->assignProperty(this, "backgroundColor", style.themeColor("text"));
QAbstractTransition *transition;
QPropertyAnimation *animation;
//
transition = new QEventTransition(button, QEvent::Enter);
transition->setTargetState(focusedState);
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(140);
transition->addAnimation(animation);
normalState->addTransition(transition);
//
transition = new QEventTransition(button, QEvent::Leave);
transition->setTargetState(normalState);
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(140);
transition->addAnimation(animation);
focusedState->addTransition(transition);
//
transition = new QEventTransition(button, QEvent::MouseButtonPress);
transition->setTargetState(pressedState);
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(140);
transition->addAnimation(animation);
focusedState->addTransition(transition);
//
transition = new QEventTransition(button, QEvent::MouseButtonRelease);
transition->setTargetState(focusedState);
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(500);
transition->addAnimation(animation);
pressedState->addTransition(transition);
//
start();
}
FlatButtonDelegate::~FlatButtonDelegate()
{
}
void FlatButtonDelegate::setBackgroundOpacity(qreal opacity)
{
_backgroundOpacity = opacity;
button->update();
}
qreal FlatButtonDelegate::backgroundOpacity() const
{
return _backgroundOpacity;
}
void FlatButtonDelegate::setBackgroundColor(const QColor &color)
{
_backgroundColor = color;
button->update();
}
QColor FlatButtonDelegate::backgroundColor() const
{
return _backgroundColor;
}

View File

@ -1,4 +1,34 @@
#ifndef FLATBUTTON_INTERNAL_H #ifndef FLATBUTTON_INTERNAL_H
#define FLATBUTTON_INTERNAL_H #define FLATBUTTON_INTERNAL_H
#include <QStateMachine>
#include <QColor>
class FlatButton;
class FlatButtonDelegate : public QStateMachine
{
Q_OBJECT
Q_PROPERTY(qreal backgroundOpacity WRITE setBackgroundOpacity READ backgroundOpacity)
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
public:
FlatButtonDelegate(FlatButton *parent);
~FlatButtonDelegate();
void setBackgroundOpacity(qreal opacity);
qreal backgroundOpacity() const;
void setBackgroundColor(const QColor &color);
QColor backgroundColor() const;
private:
Q_DISABLE_COPY(FlatButtonDelegate)
FlatButton *const button;
qreal _backgroundOpacity;
QColor _backgroundColor;
};
#endif // FLATBUTTON_INTERNAL_H #endif // FLATBUTTON_INTERNAL_H

View File

@ -1,11 +1,11 @@
#ifndef FLATBUTTON_P_H #ifndef FLATBUTTON_P_H
#define FLATBUTTON_P_H #define FLATBUTTON_P_H
#include <QStateMachine>
#include "flatbutton.h" #include "flatbutton.h"
#include "lib/rippleoverlay.h" #include "lib/rippleoverlay.h"
#include "lib/theme.h" #include "lib/theme.h"
#include "lib/style.h" #include "lib/style.h"
#include "flatbutton_internal.h"
class FlatButtonPrivate class FlatButtonPrivate
{ {
@ -18,15 +18,16 @@ public:
QColor textColor() const; QColor textColor() const;
void setTextColor(const QString &themeColor); void setTextColor(const QString &themeColor);
FlatButton *const q_ptr; FlatButton *const q_ptr;
RippleOverlay *const ripple; RippleOverlay *const ripple;
QStateMachine machine; FlatButtonDelegate *const delegate;
Material::Role role; Material::Role role;
}; };
FlatButtonPrivate::FlatButtonPrivate(FlatButton *parent) FlatButtonPrivate::FlatButtonPrivate(FlatButton *parent)
: q_ptr(parent), : q_ptr(parent),
ripple(new RippleOverlay(parent)), ripple(new RippleOverlay(parent)),
delegate(new FlatButtonDelegate(parent)),
role(Material::Default) role(Material::Default)
{ {
Style &style = Style::instance(); Style &style = Style::instance();
@ -45,16 +46,6 @@ FlatButtonPrivate::FlatButtonPrivate(FlatButton *parent)
palette.setColor(QPalette::Disabled, QPalette::ButtonText, palette.setColor(QPalette::Disabled, QPalette::ButtonText,
style.themeColor("disabled")); style.themeColor("disabled"));
parent->setPalette(palette); parent->setPalette(palette);
QState *normalState = new QState;
QState *focusedState = new QState;
QState *pressedState = new QState;
machine.addState(normalState);
machine.addState(focusedState);
machine.addState(pressedState);
machine.setInitialState(normalState);
} }
QColor FlatButtonPrivate::textColor() const QColor FlatButtonPrivate::textColor() const

View File

@ -40,9 +40,12 @@ SliderStateMachine::SliderStateMachine(Slider *parent,
QColor fillColor = style.themeColor("primary1"); QColor fillColor = style.themeColor("primary1");
inactiveState->assignProperty(track, "fillColor", style.themeColor("accent2")); inactiveState->assignProperty(track, "fillColor",
slidingState->assignProperty(track, "fillColor", style.themeColor("accent3")); style.themeColor("accent3").lighter(130));
focusState->assignProperty(track, "fillColor", style.themeColor("accent3")); slidingState->assignProperty(track, "fillColor",
style.themeColor("accent3"));
focusState->assignProperty(track, "fillColor",
style.themeColor("accent3"));
addState(topState); addState(topState);

View File

@ -15,7 +15,7 @@ FlatButtonExamples::FlatButtonExamples(QWidget *parent)
flatButton->setText("Press me!"); flatButton->setText("Press me!");
flatButton->setMinimumSize(200, 42); flatButton->setMinimumSize(200, 42);
flatButton->setRole(Material::Primary); // flatButton->setRole(Material::Primary);
// flatButton->setDisabled(true); // flatButton->setDisabled(true);
ExampleView *view = new ExampleView; ExampleView *view = new ExampleView;