qt-material-widgets/components/flatbutton_internal.cpp

175 lines
5.3 KiB
C++
Raw Normal View History

2016-05-15 07:08:23 +00:00
#include "flatbutton_internal.h"
2016-05-15 09:17:07 +00:00
#include <QAbstractTransition>
#include <QEventTransition>
#include <QSignalTransition>
#include <QPropertyAnimation>
2016-05-28 07:52:03 +00:00
#include <QSequentialAnimationGroup>
2016-05-15 09:17:07 +00:00
#include "flatbutton.h"
#include "lib/style.h"
FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent)
: QStateMachine(parent),
button(parent),
_normalState(new QState(this)),
2016-05-28 07:52:03 +00:00
_normalFocusedState(new QState(this)),
2016-05-27 14:12:16 +00:00
_hoveredState(new QState(this)),
2016-05-28 07:52:03 +00:00
_hoveredFocusedState(new QState(this)),
_pressedState(new QState(this)),
2016-05-28 12:59:44 +00:00
_focusHaloSize(0.8)
2016-05-15 09:17:07 +00:00
{
setInitialState(_normalState);
2016-05-15 09:17:07 +00:00
2016-05-28 07:52:03 +00:00
addTransition(QEvent::Enter, _normalFocusedState, _hoveredFocusedState);
addTransition(QEvent::FocusIn, _normalState, _normalFocusedState);
addTransition(QEvent::FocusOut, _normalFocusedState, _normalState);
addTransition(QEvent::Enter, _normalState, _hoveredState);
addTransition(QEvent::Leave, _hoveredFocusedState, _normalFocusedState);
addTransition(QEvent::Leave, _hoveredState, _normalState);
addTransition(QEvent::MouseButtonPress, _hoveredState, _pressedState);
addTransition(QEvent::MouseButtonRelease, _pressedState, _hoveredFocusedState);
QSequentialAnimationGroup *group = new QSequentialAnimationGroup(this);
QPropertyAnimation *grow = new QPropertyAnimation;
QPropertyAnimation *shrink = new QPropertyAnimation;
grow->setTargetObject(this);
grow->setPropertyName("focusHaloSize");
2016-05-28 12:59:44 +00:00
grow->setStartValue(0.8);
grow->setEndValue(0.9);
2016-05-28 07:52:03 +00:00
grow->setEasingCurve(QEasingCurve::InOutSine);
grow->setDuration(840);
shrink->setTargetObject(this);
shrink->setPropertyName("focusHaloSize");
2016-05-28 12:59:44 +00:00
shrink->setStartValue(0.9);
shrink->setEndValue(0.8);
2016-05-28 07:52:03 +00:00
shrink->setEasingCurve(QEasingCurve::InOutSine);
shrink->setDuration(840);
group->addAnimation(grow);
group->addAnimation(shrink);
group->setLoopCount(-1);
group->start();
2016-05-15 09:17:07 +00:00
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;
}
2016-05-28 07:52:03 +00:00
void FlatButtonDelegate::setFocusHaloOpacity(qreal opacity)
{
_focusHaloOpacity = opacity;
button->update();
}
qreal FlatButtonDelegate::focusHaloOpacity() const
{
return _focusHaloOpacity;
}
2016-05-28 12:59:44 +00:00
void FlatButtonDelegate::setFocusHaloSize(qreal size)
2016-05-28 07:52:03 +00:00
{
_focusHaloSize = size;
button->update();
}
2016-05-28 12:59:44 +00:00
qreal FlatButtonDelegate::focusHaloSize() const
2016-05-28 07:52:03 +00:00
{
return _focusHaloSize;
}
2016-05-30 16:03:41 +00:00
void FlatButtonDelegate::updatePalette()
{
2016-05-30 16:03:41 +00:00
QColor color;
switch (button->role())
{
case Material::Primary:
color = button->primaryTextColor();
break;
case Material::Secondary:
color = button->secondaryTextColor();
break;
case Material::Default:
default:
color = button->defaultTextColor();
}
QPalette palette(button->palette());
palette.setColor(QPalette::Active, QPalette::ButtonText, color);
2016-05-30 23:08:52 +00:00
palette.setColor(QPalette::Inactive, QPalette::ButtonText, color);
2016-05-30 16:03:41 +00:00
palette.setColor(QPalette::Disabled, QPalette::ButtonText, button->disabledTextColor());
button->setPalette(palette);
_normalState->assignProperty(this, "backgroundOpacity", 0);
2016-05-30 16:03:41 +00:00
_normalState->assignProperty(this, "backgroundColor", color);
2016-05-28 07:52:03 +00:00
_normalState->assignProperty(this, "focusHaloOpacity", 0);
_normalFocusedState->assignProperty(this, "backgroundOpacity", 0);
2016-05-30 16:03:41 +00:00
_normalFocusedState->assignProperty(this, "backgroundColor", color);
2016-05-30 23:08:52 +00:00
_normalFocusedState->assignProperty(this, "focusHaloOpacity", button->peakOpacity());
2016-05-30 23:08:52 +00:00
_hoveredState->assignProperty(this, "backgroundOpacity", button->peakOpacity());
2016-05-30 16:03:41 +00:00
_hoveredState->assignProperty(this, "backgroundColor", color);
2016-05-28 07:52:03 +00:00
_hoveredState->assignProperty(this, "focusHaloOpacity", 0);
2016-05-27 14:12:16 +00:00
2016-05-30 23:08:52 +00:00
_hoveredFocusedState->assignProperty(this, "backgroundOpacity", button->peakOpacity());
2016-05-30 16:03:41 +00:00
_hoveredFocusedState->assignProperty(this, "backgroundColor", color);
2016-05-30 23:08:52 +00:00
_normalFocusedState->assignProperty(this, "focusHaloOpacity", button->peakOpacity());
2016-05-30 23:08:52 +00:00
_pressedState->assignProperty(this, "backgroundOpacity", button->peakOpacity());
2016-05-30 16:03:41 +00:00
_pressedState->assignProperty(this, "backgroundColor", color);
2016-05-28 07:52:03 +00:00
_pressedState->assignProperty(this, "focusHaloOpacity", 0);
2016-05-30 16:03:41 +00:00
button->update();
2016-05-28 07:52:03 +00:00
}
void FlatButtonDelegate::addTransition(QEvent::Type eventType,
QState *fromState,
QState *toState)
{
QAbstractTransition *transition = new QEventTransition(button, eventType);
transition->setTargetState(toState);
QPropertyAnimation *animation;
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(200);
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "backgroundColor");
animation->setDuration(200);
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "focusHaloOpacity");
animation->setDuration(350);
transition->addAnimation(animation);
fromState->addTransition(transition);
}