qt-material-widgets/components/flatbutton_internal.cpp

132 lines
3.6 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>
#include "flatbutton.h"
#include "lib/style.h"
FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent)
: QStateMachine(parent),
button(parent),
_normalState(new QState(this)),
2016-05-27 14:12:16 +00:00
_hoveredState(new QState(this)),
_focusedState2(new QState(this)),
_pressedState(new QState(this))
2016-05-15 09:17:07 +00:00
{
setInitialState(_normalState);
2016-05-15 09:17:07 +00:00
assignProperties();
2016-05-15 09:17:07 +00:00
QAbstractTransition *transition;
QPropertyAnimation *animation;
//
transition = new QEventTransition(button, QEvent::Enter);
2016-05-27 14:12:16 +00:00
transition->setTargetState(_hoveredState);
2016-05-15 09:17:07 +00:00
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(140);
transition->addAnimation(animation);
_normalState->addTransition(transition);
2016-05-15 09:17:07 +00:00
//
2016-05-27 14:12:16 +00:00
//transition = new QEventTransition(button, QEvent::FocusIn);
//transition->setTargetState(_focusedState2);
////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);
_focusedState2->addTransition(transition);
//
2016-05-15 09:17:07 +00:00
transition = new QEventTransition(button, QEvent::Leave);
transition->setTargetState(_normalState);
2016-05-15 09:17:07 +00:00
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(140);
transition->addAnimation(animation);
2016-05-27 14:12:16 +00:00
_hoveredState->addTransition(transition);
2016-05-15 09:17:07 +00:00
//
transition = new QEventTransition(button, QEvent::MouseButtonPress);
transition->setTargetState(_pressedState);
2016-05-15 09:17:07 +00:00
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(140);
transition->addAnimation(animation);
2016-05-27 14:12:16 +00:00
_hoveredState->addTransition(transition);
2016-05-15 09:17:07 +00:00
//
transition = new QEventTransition(button, QEvent::MouseButtonRelease);
2016-05-27 14:12:16 +00:00
transition->setTargetState(_focusedState2);
2016-05-15 09:17:07 +00:00
animation = new QPropertyAnimation(this, "backgroundOpacity");
animation->setDuration(500);
transition->addAnimation(animation);
_pressedState->addTransition(transition);
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;
}
void FlatButtonDelegate::assignProperties()
{
QColor textColor = button->palette().color(QPalette::Active,
QPalette::ButtonText);
_normalState->assignProperty(this, "backgroundOpacity", 0);
_normalState->assignProperty(this, "backgroundColor", textColor);
2016-05-27 14:12:16 +00:00
_hoveredState->assignProperty(this, "backgroundOpacity", 0.15);
_hoveredState->assignProperty(this, "backgroundColor", textColor);
_focusedState2->assignProperty(this, "backgroundOpacity", 0.15);
_focusedState2->assignProperty(this, "backgroundColor", textColor);
_pressedState->assignProperty(this, "backgroundOpacity", 0.15);
_pressedState->assignProperty(this, "backgroundColor", textColor);
}