matrixion/src/ui/RaisedButton.cpp

90 lines
2.7 KiB
C++
Raw Normal View History

2017-04-06 02:06:42 +03:00
#include <QEventTransition>
#include <QPropertyAnimation>
#include "RaisedButton.h"
2017-08-20 13:47:22 +03:00
void
RaisedButton::init()
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
shadow_state_machine_ = new QStateMachine(this);
normal_state_ = new QState;
pressed_state_ = new QState;
effect_ = new QGraphicsDropShadowEffect;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
effect_->setBlurRadius(7);
effect_->setOffset(QPointF(0, 2));
effect_->setColor(QColor(0, 0, 0, 75));
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
setBackgroundMode(Qt::OpaqueMode);
setMinimumHeight(42);
setGraphicsEffect(effect_);
setBaseOpacity(0.3);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
shadow_state_machine_->addState(normal_state_);
shadow_state_machine_->addState(pressed_state_);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
normal_state_->assignProperty(effect_, "offset", QPointF(0, 2));
normal_state_->assignProperty(effect_, "blurRadius", 7);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
pressed_state_->assignProperty(effect_, "offset", QPointF(0, 5));
pressed_state_->assignProperty(effect_, "blurRadius", 29);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QAbstractTransition *transition;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
transition = new QEventTransition(this, QEvent::MouseButtonPress);
transition->setTargetState(pressed_state_);
normal_state_->addTransition(transition);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
transition = new QEventTransition(this, QEvent::MouseButtonDblClick);
transition->setTargetState(pressed_state_);
normal_state_->addTransition(transition);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
transition = new QEventTransition(this, QEvent::MouseButtonRelease);
transition->setTargetState(normal_state_);
pressed_state_->addTransition(transition);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
QPropertyAnimation *animation;
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
animation = new QPropertyAnimation(effect_, "offset", this);
animation->setDuration(100);
shadow_state_machine_->addDefaultAnimation(animation);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
animation = new QPropertyAnimation(effect_, "blurRadius", this);
animation->setDuration(100);
shadow_state_machine_->addDefaultAnimation(animation);
2017-04-06 02:06:42 +03:00
2017-09-10 12:59:21 +03:00
shadow_state_machine_->setInitialState(normal_state_);
shadow_state_machine_->start();
2017-04-06 02:06:42 +03:00
}
RaisedButton::RaisedButton(QWidget *parent)
2017-08-20 13:47:22 +03:00
: FlatButton(parent)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
init();
2017-04-06 02:06:42 +03:00
}
RaisedButton::RaisedButton(const QString &text, QWidget *parent)
2017-08-20 13:47:22 +03:00
: FlatButton(parent)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
init();
setText(text);
2017-04-06 02:06:42 +03:00
}
2017-10-01 12:11:33 +03:00
RaisedButton::~RaisedButton() {}
2017-04-06 02:06:42 +03:00
2017-08-20 13:47:22 +03:00
bool
RaisedButton::event(QEvent *event)
2017-04-06 02:06:42 +03:00
{
2017-09-10 12:59:21 +03:00
if (QEvent::EnabledChange == event->type()) {
if (isEnabled()) {
shadow_state_machine_->start();
effect_->setEnabled(true);
} else {
shadow_state_machine_->stop();
effect_->setEnabled(false);
}
}
return FlatButton::event(event);
2017-04-06 02:06:42 +03:00
}