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 <QStyleOption>
#include <QApplication>
#include <QPalette>
#include <QDebug>
#include "lib/rippleoverlay.h"
#include "lib/ripple.h"
@ -74,13 +73,15 @@ void FlatButton::paintEvent(QPaintEvent *event)
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);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(d->textColor());
painter.setOpacity(0.1);
brush.setColor(d->delegate->backgroundColor());
painter.setOpacity(bgOpacity);
painter.fillRect(rect(), brush);
}
@ -98,26 +99,14 @@ void FlatButton::mousePressEvent(QMouseEvent *event)
{
Q_D(FlatButton);
Style &style = Style::instance();
Ripple *ripple = new Ripple(event->pos());
ripple->setRadiusEndValue(100);
ripple->setOpacityStartValue(0.2);
ripple->setColor(d->textColor());
ripple->setOpacityStartValue(0.3);
ripple->setColor(style.themeColor("text"));
//d->ripple->addRipple(ripple);
d->ripple->addRipple(ripple);
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 paintEvent(QPaintEvent *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;

View File

@ -1 +1,106 @@
#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
#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

View File

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

View File

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

View File

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