代码拉取完成,页面将自动刷新
#include "qtmaterialflatbutton_internal.h"
#include "lib/qtmaterialstatetransition.h"
#include "qtmaterialflatbutton.h"
#include <QEventTransition>
#include <QFocusEvent>
#include <QPropertyAnimation>
#include <QSequentialAnimationGroup>
/*!
* \class QtMaterialFlatButtonStateMachine
* \internal
*/
QtMaterialFlatButtonStateMachine::QtMaterialFlatButtonStateMachine(QtMaterialFlatButton *parent)
: QStateMachine(parent),
m_button(parent),
m_topLevelState(new QState(QState::ParallelStates)),
m_configState(new QState(m_topLevelState)),
m_checkableState(new QState(m_topLevelState)),
m_checkedState(new QState(m_checkableState)),
m_uncheckedState(new QState(m_checkableState)),
m_neutralState(new QState(m_configState)),
m_neutralFocusedState(new QState(m_configState)),
m_hoveredState(new QState(m_configState)),
m_hoveredFocusedState(new QState(m_configState)),
m_pressedState(new QState(m_configState)),
m_haloAnimation(new QSequentialAnimationGroup(this)),
m_overlayOpacity(0),
m_checkedOverlayProgress(parent->isChecked() ? 1 : 0),
m_haloOpacity(0),
m_haloSize(0.8),
m_haloScaleFactor(1),
m_wasChecked(false)
{
Q_ASSERT(parent);
parent->installEventFilter(this);
m_configState->setInitialState(m_neutralState);
addState(m_topLevelState);
setInitialState(m_topLevelState);
m_checkableState->setInitialState(parent->isChecked() ? m_checkedState : m_uncheckedState);
QtMaterialStateTransition *transition;
QPropertyAnimation *animation;
transition = new QtMaterialStateTransition(FlatButtonCheckedTransition);
transition->setTargetState(m_checkedState);
m_uncheckedState->addTransition(transition);
animation = new QPropertyAnimation(this, "checkedOverlayProgress", this);
animation->setDuration(200);
transition->addAnimation(animation);
transition = new QtMaterialStateTransition(FlatButtonUncheckedTransition);
transition->setTargetState(m_uncheckedState);
m_checkedState->addTransition(transition);
animation = new QPropertyAnimation(this, "checkedOverlayProgress", this);
animation->setDuration(200);
transition->addAnimation(animation);
addTransition(m_button, QEvent::FocusIn, m_neutralState, m_neutralFocusedState);
addTransition(m_button, QEvent::FocusOut, m_neutralFocusedState, m_neutralState);
addTransition(m_button, QEvent::Enter, m_neutralState, m_hoveredState);
addTransition(m_button, QEvent::Leave, m_hoveredState, m_neutralState);
addTransition(m_button, QEvent::Enter, m_neutralFocusedState, m_hoveredFocusedState);
addTransition(m_button, QEvent::Leave, m_hoveredFocusedState, m_neutralFocusedState);
addTransition(m_button, QEvent::FocusIn, m_hoveredState, m_hoveredFocusedState);
addTransition(m_button, QEvent::FocusOut, m_hoveredFocusedState, m_hoveredState);
transition = new QtMaterialStateTransition(FlatButtonPressedTransition);
transition->setTargetState(m_pressedState);
m_hoveredState->addTransition(transition);
addTransition(m_button, QEvent::Leave, m_pressedState, m_neutralFocusedState);
addTransition(m_button, QEvent::FocusOut, m_pressedState, m_hoveredState);
m_neutralState->assignProperty(this, "haloSize", 0);
m_neutralFocusedState->assignProperty(this, "haloSize", 0.7);
m_hoveredState->assignProperty(this, "haloSize", 0);
m_pressedState->assignProperty(this, "haloSize", 4);
m_hoveredFocusedState->assignProperty(this, "haloSize", 0.7);
QPropertyAnimation *grow = new QPropertyAnimation(this);
grow->setTargetObject(this);
grow->setPropertyName("haloScaleFactor");
grow->setStartValue(0.56);
grow->setEndValue(0.63);
grow->setEasingCurve(QEasingCurve::InOutSine);
grow->setDuration(840);
QPropertyAnimation *shrink = new QPropertyAnimation(this);
shrink->setTargetObject(this);
shrink->setPropertyName("haloScaleFactor");
shrink->setStartValue(0.63);
shrink->setEndValue(0.56);
shrink->setEasingCurve(QEasingCurve::InOutSine);
shrink->setDuration(840);
m_haloAnimation->addAnimation(grow);
m_haloAnimation->addAnimation(shrink);
m_haloAnimation->setLoopCount(-1);
}
QtMaterialFlatButtonStateMachine::~QtMaterialFlatButtonStateMachine()
{
}
void QtMaterialFlatButtonStateMachine::setOverlayOpacity(qreal opacity)
{
bool changed = (m_overlayOpacity != opacity);
m_overlayOpacity = opacity;
if (changed)
{
if (m_button->overlayStyle() != Material::NoOverlay)
{
m_button->update();
}
}
}
void QtMaterialFlatButtonStateMachine::setCheckedOverlayProgress(qreal progress)
{
bool changed = (m_checkedOverlayProgress != progress);
m_checkedOverlayProgress = progress;
if (changed)
{
m_button->update();
}
}
void QtMaterialFlatButtonStateMachine::setHaloOpacity(qreal opacity)
{
bool changed = (m_haloOpacity != opacity);
m_haloOpacity = opacity;
if (changed)
{
m_button->update();
}
}
void QtMaterialFlatButtonStateMachine::setHaloSize(qreal size)
{
bool changed = (m_haloSize != size);
m_haloSize = size;
if (changed)
{
m_button->update();
}
}
void QtMaterialFlatButtonStateMachine::setHaloScaleFactor(qreal factor)
{
bool changed = (m_haloScaleFactor != factor);
m_haloScaleFactor = factor;
if (changed)
{
if (m_button->isHaloVisible() && m_button->hasFocus())
{
m_button->update();
}
}
}
void QtMaterialFlatButtonStateMachine::startAnimations()
{
m_haloAnimation->start();
start();
}
void QtMaterialFlatButtonStateMachine::setupProperties()
{
const qreal baseOpacity = m_button->baseOpacity();
m_neutralState->assignProperty(this, "overlayOpacity", 0);
m_neutralState->assignProperty(this, "haloOpacity", 0);
m_neutralFocusedState->assignProperty(this, "overlayOpacity", 0);
m_neutralFocusedState->assignProperty(this, "haloOpacity", baseOpacity);
m_hoveredState->assignProperty(this, "overlayOpacity", baseOpacity);
m_hoveredState->assignProperty(this, "haloOpacity", 0);
m_hoveredFocusedState->assignProperty(this, "overlayOpacity", baseOpacity);
m_hoveredFocusedState->assignProperty(this, "haloOpacity", baseOpacity);
m_pressedState->assignProperty(this, "overlayOpacity", baseOpacity);
m_pressedState->assignProperty(this, "haloOpacity", 0);
m_checkedState->assignProperty(this, "checkedOverlayProgress", 1);
m_uncheckedState->assignProperty(this, "checkedOverlayProgress", 0);
m_button->update();
}
void QtMaterialFlatButtonStateMachine::updateCheckedStatus()
{
const bool checked = m_button->isChecked();
if (m_wasChecked != checked)
{
m_wasChecked = checked;
if (checked)
{
postEvent(new QtMaterialStateTransitionEvent(FlatButtonCheckedTransition));
}
else
{
postEvent(new QtMaterialStateTransitionEvent(FlatButtonUncheckedTransition));
}
}
}
bool QtMaterialFlatButtonStateMachine::eventFilter(QObject *watched, QEvent *event)
{
if (QEvent::FocusIn == event->type())
{
QFocusEvent *focusEvent = static_cast<QFocusEvent *>(event);
if ((focusEvent != nullptr) && focusEvent->reason() == Qt::MouseFocusReason)
{
postEvent(new QtMaterialStateTransitionEvent(FlatButtonPressedTransition));
return true;
}
}
return QStateMachine::eventFilter(watched, event);
}
void QtMaterialFlatButtonStateMachine::addTransition(QObject *object, QEvent::Type eventType, QState *fromState, QState *toState)
{
addTransition(new QEventTransition(object, eventType), fromState, toState);
}
void QtMaterialFlatButtonStateMachine::addTransition(QAbstractTransition *transition, QState *fromState, QState *toState)
{
transition->setTargetState(toState);
QPropertyAnimation *animation;
animation = new QPropertyAnimation(this, "overlayOpacity", this);
animation->setDuration(150);
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "haloOpacity", this);
animation->setDuration(170);
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "haloSize", this);
animation->setDuration(350);
animation->setEasingCurve(QEasingCurve::OutCubic);
transition->addAnimation(animation);
fromState->addTransition(transition);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。