qt-material-widgets/components/toggle.cpp

178 lines
4.1 KiB
C++
Raw Normal View History

2016-03-25 21:57:51 +00:00
#include "toggle.h"
2016-05-29 10:25:14 +00:00
#include <QPainter>
2016-05-29 11:58:20 +00:00
#include <QState>
2016-05-29 13:59:44 +00:00
#include <QSignalTransition>
2016-05-29 13:42:13 +00:00
#include <QPropertyAnimation>
#include "lib/rippleoverlay.h"
2016-05-29 10:08:55 +00:00
#include "toggle_p.h"
TogglePrivate::TogglePrivate(Toggle *q)
2016-05-29 10:25:14 +00:00
: q_ptr(q),
2016-05-29 11:58:20 +00:00
thumb(new Thumb(q)),
2016-05-29 13:42:13 +00:00
ripple(new RippleOverlay(q->parentWidget())),
2016-05-29 10:25:14 +00:00
orientation(Qt::Horizontal)
2016-05-29 10:08:55 +00:00
{
}
void TogglePrivate::init()
{
2016-05-29 10:25:14 +00:00
Q_Q(Toggle);
2016-05-29 13:59:44 +00:00
q->setCheckable(true);
2016-05-29 10:25:14 +00:00
q->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
2016-05-29 11:58:20 +00:00
QState *offState = new QState;
QState *onState = new QState;
machine.addState(offState);
machine.addState(onState);
machine.setInitialState(offState);
2016-05-29 13:59:44 +00:00
QSignalTransition *transition;
2016-05-29 13:42:13 +00:00
QPropertyAnimation *animation;
//
2016-05-29 11:58:20 +00:00
2016-05-29 13:59:44 +00:00
transition = new QSignalTransition(thumb, SIGNAL(clicked()));
2016-05-29 11:58:20 +00:00
transition->setTargetState(onState);
offState->addTransition(transition);
2016-05-29 13:42:13 +00:00
animation = new QPropertyAnimation;
animation->setPropertyName("shift");
animation->setTargetObject(thumb);
2016-05-29 13:59:44 +00:00
animation->setDuration(150);
2016-05-29 13:42:13 +00:00
transition->addAnimation(animation);
//
2016-05-29 13:59:44 +00:00
transition = new QSignalTransition(thumb, SIGNAL(clicked()));
2016-05-29 11:58:20 +00:00
transition->setTargetState(offState);
onState->addTransition(transition);
2016-05-29 13:42:13 +00:00
animation = new QPropertyAnimation;
animation->setPropertyName("shift");
animation->setTargetObject(thumb);
2016-05-29 13:59:44 +00:00
animation->setDuration(150);
2016-05-29 13:42:13 +00:00
transition->addAnimation(animation);
//
2016-05-29 11:58:20 +00:00
offState->assignProperty(thumb, "shift", 0);
onState->assignProperty(thumb, "shift", 1);
machine.start();
2016-05-29 13:42:13 +00:00
QObject::connect(thumb, SIGNAL(clicked()), q, SLOT(addRipple()));
2016-05-29 10:08:55 +00:00
}
2016-03-26 10:37:48 +00:00
2016-05-29 08:05:08 +00:00
Toggle::Toggle(QWidget *parent)
2016-05-29 10:08:55 +00:00
: QAbstractButton(parent),
d_ptr(new TogglePrivate(this))
2016-05-29 08:05:08 +00:00
{
2016-05-29 10:08:55 +00:00
d_func()->init();
2016-05-29 08:05:08 +00:00
}
Toggle::~Toggle()
{
}
2016-05-29 10:25:14 +00:00
QSize Toggle::sizeHint() const
{
Q_D(const Toggle);
return Qt::Horizontal == d->orientation
? QSize(64, 48)
: QSize(48, 64);
}
2016-05-29 11:58:20 +00:00
Qt::Orientation Toggle::orientation() const
{
Q_D(const Toggle);
return d->orientation;
}
2016-05-29 08:05:08 +00:00
void Toggle::setOrientation(Qt::Orientation orientation)
{
2016-05-29 10:25:14 +00:00
Q_D(Toggle);
if (d->orientation == orientation)
return;
d->orientation = orientation;
2016-05-29 08:05:08 +00:00
}
2016-05-29 13:42:13 +00:00
void Toggle::updateOverlayGeometry()
{
Q_D(Toggle);
const qreal offset = d->thumb->offset();
if (Qt::Horizontal == d->orientation) {
d->ripple->setGeometry(geometry().adjusted(-10+offset, -20, 10+offset, 20));
} else {
d->ripple->setGeometry(geometry().adjusted(-10, -20+offset, 10, 20+offset));
}
}
void Toggle::addRipple()
{
Q_D(Toggle);
if (Qt::Horizontal == d->orientation) {
const int t = height()/2;
const int w = d->thumb->height()/2+10;
d->ripple->addRipple(QPoint(10+t, 20+t), w);
} else {
const int t = width()/2;
const int w = d->thumb->width()/2+10;
d->ripple->addRipple(QPoint(10+t, 20+t), w);
}
}
bool Toggle::event(QEvent *event)
{
Q_D(Toggle);
const QEvent::Type type = event->type();
if (QEvent::EnabledChange == type) {
if (isEnabled()) {
d->machine.start();
} else {
d->machine.stop();
}
} else if (QEvent::ParentChange == type && parentWidget()) {
d->ripple->setParent(parentWidget());
} else if (QEvent::Resize == type || QEvent::Move == type) {
d->ripple->setGeometry(geometry().adjusted(-10, -20, 10, 20));
}
return QAbstractButton::event(event);
}
2016-05-29 08:05:08 +00:00
void Toggle::paintEvent(QPaintEvent *event)
{
2016-05-29 10:25:14 +00:00
Q_UNUSED(event)
Q_D(Toggle);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
QBrush brush;
brush.setColor(QColor(180, 180, 180));
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
if (Qt::Horizontal == d->orientation) {
const int h = height()/2;
const QRect r(0, h/2, width(), h);
painter.drawRoundedRect(r.adjusted(14, 4, -14, -4), h/2-4, h/2-4);
} else {
const int w = width()/2;
const QRect r(w/2, 0, w, height());
painter.drawRoundedRect(r.adjusted(4, 14, -4, -14), w/2-4, w/2-4);
}
2016-05-29 08:05:08 +00:00
}