qt-material-widgets/components/toggle.cpp

221 lines
5.4 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 19:12:02 +00:00
#include "lib/ripple.h"
#include "lib/style.h"
2016-05-29 10:08:55 +00:00
#include "toggle_p.h"
2016-05-29 19:12:02 +00:00
#include "toggle_internal.h"
2016-05-29 10:08:55 +00:00
TogglePrivate::TogglePrivate(Toggle *q)
2016-05-29 10:25:14 +00:00
: q_ptr(q),
2016-05-29 19:12:02 +00:00
track(new ToggleTrack(q)),
thumb(new ToggleThumb(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 19:12:02 +00:00
transition = new QSignalTransition(thumb, SIGNAL(clicked(bool)));
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 19:12:02 +00:00
animation->setDuration(200);
animation->setEasingCurve(QEasingCurve::OutQuad);
transition->addAnimation(animation);
animation = new QPropertyAnimation;
animation->setPropertyName("trackColor");
animation->setTargetObject(track);
animation->setDuration(150);
transition->addAnimation(animation);
animation = new QPropertyAnimation;
animation->setPropertyName("thumbColor");
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 19:12:02 +00:00
transition = new QSignalTransition(thumb, SIGNAL(clicked(bool)));
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 19:12:02 +00:00
animation->setDuration(200);
animation->setEasingCurve(QEasingCurve::OutQuad);
transition->addAnimation(animation);
animation = new QPropertyAnimation;
animation->setPropertyName("trackColor");
animation->setTargetObject(track);
animation->setDuration(150);
transition->addAnimation(animation);
animation = new QPropertyAnimation;
animation->setPropertyName("thumbColor");
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 19:12:02 +00:00
const Style &style = Style::instance();
2016-05-29 11:58:20 +00:00
offState->assignProperty(thumb, "shift", 0);
onState->assignProperty(thumb, "shift", 1);
2016-05-29 19:12:02 +00:00
QColor trackOnColor = style.themeColor("primary1");
trackOnColor.setAlpha(100);
QColor trackOffColor = style.themeColor("accent3");
trackOffColor.setAlpha(170);
offState->assignProperty(track, "trackColor", trackOffColor);
onState->assignProperty(track, "trackColor", trackOnColor);
offState->assignProperty(thumb, "thumbColor", style.themeColor("canvas"));
onState->assignProperty(thumb, "thumbColor", style.themeColor("primary1"));
2016-05-29 11:58:20 +00:00
machine.start();
2016-05-29 13:42:13 +00:00
2016-05-29 19:12:02 +00:00
QObject::connect(thumb, SIGNAL(clicked(bool)), q, SLOT(addRipple(bool)));
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));
}
}
2016-05-29 19:12:02 +00:00
void Toggle::addRipple(bool checked)
2016-05-29 13:42:13 +00:00
{
Q_D(Toggle);
2016-05-29 19:12:02 +00:00
int t, w;
2016-05-29 13:42:13 +00:00
if (Qt::Horizontal == d->orientation) {
2016-05-29 19:12:02 +00:00
t = height()/2;
w = d->thumb->height()/2+10;
2016-05-29 13:42:13 +00:00
} else {
2016-05-29 19:12:02 +00:00
t = width()/2;
w = d->thumb->width()/2+10;
2016-05-29 13:42:13 +00:00
}
2016-05-29 19:12:02 +00:00
Ripple *ripple = new Ripple(QPoint(10+t, 20+t));
ripple->setColor(Style::instance().themeColor(checked
? "primary2"
: "accent3"));
ripple->setRadiusEndValue(w);
ripple->setOpacityStartValue(0.4);
d->ripple->addRipple(ripple);
2016-05-29 13:42:13 +00:00
}
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)
2016-05-29 19:12:02 +00:00
#ifdef DEBUG_LAYOUT
2016-05-29 10:25:14 +00:00
QPainter painter(this);
2016-05-29 19:12:02 +00:00
QPen pen;
pen.setColor(Qt::red);
pen.setWidth(1);
painter.setOpacity(1);
painter.setPen(pen);
painter.setBrush(Qt::NoBrush);
painter.drawRect(rect().adjusted(0, 0, -1, -1));
#endif
2016-05-29 08:05:08 +00:00
}