qt-material-widgets/components/raisedbutton.cpp

169 lines
4.2 KiB
C++
Raw Normal View History

2016-03-22 08:50:48 +00:00
#include "raisedbutton.h"
#include <QGraphicsDropShadowEffect>
#include <QEventTransition>
2016-05-24 21:13:01 +00:00
#include <QSignalTransition>
2016-05-18 12:08:11 +00:00
#include <QPropertyAnimation>
#include <QPainter>
#include <QStylePainter>
#include <QStyleOption>
2016-05-24 21:13:01 +00:00
#include <QMouseEvent>
#include <QDebug>
#include "raisedbutton_p.h"
2016-05-30 23:08:52 +00:00
RaisedButtonPrivate::RaisedButtonPrivate(RaisedButton *q)
: FlatButtonPrivate(q)
{
}
RaisedButtonPrivate::~RaisedButtonPrivate()
{
}
void RaisedButtonPrivate::init()
{
Q_Q(RaisedButton);
2016-06-05 12:07:56 +00:00
q->setTextColor(Qt::white);
2016-05-30 23:08:52 +00:00
q->setPeakOpacity(0.25);
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
effect->setBlurRadius(7);
2016-05-18 12:08:11 +00:00
effect->setOffset(QPointF(0, 0));
2016-05-24 21:13:01 +00:00
effect->setColor(QColor(0, 0, 0, 60));
q->setGraphicsEffect(effect);
QState *normalState = new QState;
QState *pressedState = new QState;
machine.addState(normalState);
machine.addState(pressedState);
2016-05-18 12:08:11 +00:00
normalState->assignProperty(effect, "offset", QPointF(0, 0));
normalState->assignProperty(effect, "blurRadius", 7);
2016-05-18 12:08:11 +00:00
pressedState->assignProperty(effect, "offset", QPointF(0, 6));
pressedState->assignProperty(effect, "blurRadius", 20);
QAbstractTransition *transition;
2016-05-18 12:08:11 +00:00
QPropertyAnimation *animation;
//
transition = new QEventTransition(q, QEvent::MouseButtonPress);
transition->setTargetState(pressedState);
2016-05-18 12:08:11 +00:00
animation = new QPropertyAnimation(effect, "offset");
animation->setDuration(100);
transition->addAnimation(animation);
animation = new QPropertyAnimation(effect, "blurRadius");
animation->setDuration(100);
transition->addAnimation(animation);
normalState->addTransition(transition);
2016-05-18 12:08:11 +00:00
//
2016-05-24 21:13:01 +00:00
transition = new QEventTransition(q, QEvent::MouseButtonDblClick);
transition->setTargetState(pressedState);
animation = new QPropertyAnimation(effect, "offset");
animation->setDuration(100);
transition->addAnimation(animation);
animation = new QPropertyAnimation(effect, "blurRadius");
animation->setDuration(100);
transition->addAnimation(animation);
normalState->addTransition(transition);
//
transition = new QEventTransition(q, QEvent::MouseButtonRelease);
transition->setTargetState(normalState);
2016-05-18 12:08:11 +00:00
animation = new QPropertyAnimation(effect, "offset");
transition->addAnimation(animation);
animation->setDuration(100);
animation = new QPropertyAnimation(effect, "blurRadius");
animation->setDuration(100);
transition->addAnimation(animation);
pressedState->addTransition(transition);
2016-05-18 12:08:11 +00:00
//
machine.setInitialState(normalState);
machine.start();
}
2016-03-22 08:50:48 +00:00
RaisedButton::RaisedButton(QWidget *parent)
: FlatButton(*new RaisedButtonPrivate(this), parent)
2016-03-22 08:50:48 +00:00
{
Q_D(RaisedButton);
d->init();
2016-05-27 14:12:16 +00:00
setMinimumHeight(42);
2016-03-22 08:50:48 +00:00
}
RaisedButton::~RaisedButton()
{
}
2016-05-18 12:08:11 +00:00
2016-05-28 14:08:05 +00:00
bool RaisedButton::event(QEvent *event)
{
Q_D(RaisedButton);
if (QEvent::EnabledChange == event->type()) {
if (isEnabled()) {
d->machine.start();
} else {
d->machine.stop();
}
}
return FlatButton::event(event);
}
2016-05-18 12:08:11 +00:00
void RaisedButton::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
2016-05-24 21:13:01 +00:00
Q_D(RaisedButton);
2016-05-18 12:08:11 +00:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
2016-05-28 12:59:44 +00:00
painter.save();
const qreal cr = d->cornerRadius;
2016-05-30 23:08:52 +00:00
QBrush brush;
2016-05-28 14:08:05 +00:00
brush.setStyle(Qt::SolidPattern);
brush.setColor(isEnabled()
2016-06-05 12:07:56 +00:00
? backgroundColor() : palette().color(QPalette::Disabled, QPalette::Background));
2016-05-28 14:08:05 +00:00
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.drawRoundedRect(rect(), cr, cr);
if (isEnabled()) {
const qreal hs = static_cast<qreal>(width())*d->delegate->focusHaloSize()/2;
const qreal haloOpacity = d->delegate->focusHaloOpacity();
brush.setColor(palette().color(QPalette::Active, QPalette::ButtonText));
painter.setBrush(brush);
painter.setOpacity(haloOpacity);
painter.setPen(Qt::NoPen);
QPointF center = rect().center();
painter.drawEllipse(center, hs, hs);
}
2016-05-28 12:59:44 +00:00
painter.restore();
2016-05-28 07:52:03 +00:00
2016-05-28 12:59:44 +00:00
QStylePainter style(this);
2016-05-18 12:08:11 +00:00
2016-05-28 12:59:44 +00:00
QStyleOptionButton option;
initStyleOption(&option);
option.features |= QStyleOptionButton::Flat;
2016-05-18 12:08:11 +00:00
2016-05-28 12:59:44 +00:00
style.drawControl(QStyle::CE_PushButtonLabel, option);
2016-05-18 12:08:11 +00:00
}