qt-material-widgets/components/toggle.cpp

195 lines
4.9 KiB
C++
Raw Normal View History

2016-03-26 10:37:48 +00:00
#include <QAbstractButton>
2016-03-27 14:06:22 +00:00
#include <QPropertyAnimation>
2016-03-27 19:48:32 +00:00
#include <QMouseEvent>
#include <QEvent>
#include <QMouseEvent>
#include <QPainter>
2016-03-25 21:57:51 +00:00
#include "toggle.h"
2016-03-26 10:37:48 +00:00
#include "../lib/rippleoverlay.h"
2016-03-26 12:58:01 +00:00
#include "../lib/customshadoweffect.h"
2016-03-26 10:37:48 +00:00
Thumb::Thumb(Toggle *parent)
2016-03-27 14:06:22 +00:00
: QWidget(parent),
_toggle(parent),
_animation(new QPropertyAnimation(this)),
2016-03-28 08:51:24 +00:00
_progress(1),
2016-03-27 19:48:32 +00:00
_offset(0)
2016-03-26 12:42:21 +00:00
{
parent->installEventFilter(this);
2016-03-27 14:06:22 +00:00
_animation->setPropertyName("progress");
_animation->setTargetObject(this);
_animation->setDuration(350);
2016-03-28 08:51:24 +00:00
_animation->setStartValue(1);
_animation->setEndValue(0);
2016-03-26 12:42:21 +00:00
}
Thumb::~Thumb()
{
}
2016-03-28 08:51:24 +00:00
void Thumb::setProgress(qreal progress)
2016-03-27 19:48:32 +00:00
{
2016-03-28 08:51:24 +00:00
if (_progress == progress)
2016-03-27 19:48:32 +00:00
return;
2016-03-28 08:51:24 +00:00
_progress = progress;
2016-03-27 19:48:32 +00:00
2016-03-29 20:28:40 +00:00
const QSize s(Qt::Horizontal == _toggle->orientation()
? size() : size().transposed());
setOffset(progress*static_cast<qreal>(s.width()-s.height()));
2016-03-27 19:48:32 +00:00
2016-03-29 20:28:40 +00:00
emit progressChanged(progress);
2016-03-27 19:48:32 +00:00
update();
}
bool Thumb::eventFilter(QObject *obj, QEvent *event)
{
const QEvent::Type type = event->type();
if (QEvent::Resize == type || QEvent::Move == type) {
2016-03-28 08:51:24 +00:00
setGeometry(parentWidget()->rect().adjusted(8, 8, -8, -8));
} else if (QEvent::MouseButtonRelease == type) {
return true;
}
return QWidget::eventFilter(obj, event);
}
2016-03-27 19:48:32 +00:00
void Thumb::mouseReleaseEvent(QMouseEvent *event)
{
const bool checked = _toggle->isChecked();
_toggle->setChecked(!checked);
if (QAbstractAnimation::Running != _animation->state()) {
_animation->setEasingCurve(checked
? QEasingCurve::OutCubic
: QEasingCurve::InCubic);
}
2016-03-27 19:48:32 +00:00
_animation->setDirection(checked
? QAbstractAnimation::Forward
: QAbstractAnimation::Backward);
2016-03-27 14:06:22 +00:00
_animation->start();
2016-03-27 19:48:32 +00:00
emit clicked();
QWidget::mouseReleaseEvent(event);
2016-03-27 14:06:22 +00:00
}
void Thumb::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
2016-03-26 12:58:01 +00:00
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
2016-03-26 12:58:01 +00:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
2016-03-27 19:48:32 +00:00
brush.setColor(Qt::white);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
2016-03-26 12:58:01 +00:00
2016-03-29 20:28:40 +00:00
if (Qt::Horizontal == _toggle->orientation()) {
const int s = height()-10;
painter.drawEllipse(5+_offset, 5, s, s);
} else {
const int s = width()-10;
painter.drawEllipse(5, 5+_offset, s, s);
}
2016-03-26 12:58:01 +00:00
}
2016-03-26 10:37:48 +00:00
Toggle::Toggle(QWidget *parent)
: QAbstractButton(parent),
2016-03-27 19:48:32 +00:00
_thumb(new Thumb(this)),
2016-03-28 08:51:24 +00:00
_overlay(new RippleOverlay(parent)),
_orientation(Qt::Horizontal)
2016-03-26 10:37:48 +00:00
{
2016-03-27 14:06:22 +00:00
setCheckable(true);
2016-03-29 20:28:40 +00:00
setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
2016-03-28 08:51:24 +00:00
2016-03-27 08:02:58 +00:00
CustomShadowEffect *effect = new CustomShadowEffect;
effect->setDistance(0);
effect->setBlurRadius(6);
2016-03-27 19:48:32 +00:00
effect->setColor(QColor(100, 100, 100));
2016-03-27 14:06:22 +00:00
2016-03-27 08:02:58 +00:00
_thumb->setGraphicsEffect(effect);
2016-03-27 14:06:22 +00:00
_thumb->installEventFilter(this);
2016-03-29 20:46:50 +00:00
connect(_thumb, SIGNAL(clicked()), this, SLOT(addRipple()));
connect(_thumb, SIGNAL(progressChanged(qreal)), this, SLOT(updateOverlayGeometry()));
2016-03-26 10:37:48 +00:00
}
Toggle::~Toggle()
{
}
2016-03-28 08:51:24 +00:00
QSize Toggle::sizeHint() const
{
return Qt::Horizontal == _orientation
? QSize(64, 48)
: QSize(48, 64);
}
void Toggle::setOrientation(Qt::Orientation orientation)
{
if (_orientation == orientation)
return;
_orientation = orientation;
}
2016-03-29 20:46:50 +00:00
void Toggle::addRipple()
2016-03-27 19:48:32 +00:00
{
2016-03-29 20:28:40 +00:00
if (Qt::Horizontal == _orientation) {
const int d = height()/2;
const int w = _thumb->height()/2+10;
_overlay->addRipple(QPoint(10+d, 20+d), w);
} else {
const int d = width()/2;
const int w = _thumb->width()/2+10;
_overlay->addRipple(QPoint(10+d, 20+d), w);
}
2016-03-27 19:48:32 +00:00
}
2016-03-29 20:46:50 +00:00
void Toggle::updateOverlayGeometry()
2016-03-27 19:48:32 +00:00
{
const int d = _thumb->offset();
2016-03-29 20:28:40 +00:00
if (Qt::Horizontal == _orientation) {
_overlay->setGeometry(geometry().adjusted(-10+d, -20, 10+d, 20));
} else {
_overlay->setGeometry(geometry().adjusted(-10, -20+d, 10, 20+d));
}
2016-03-27 19:48:32 +00:00
}
bool Toggle::event(QEvent *event)
{
const QEvent::Type type = event->type();
if (QEvent::ParentChange == type && parentWidget()) {
_overlay->setParent(parentWidget());
} else if (QEvent::Resize == type || QEvent::Move == type) {
_overlay->setGeometry(geometry().adjusted(-10, -20, 10, 20));
}
return QAbstractButton::event(event);
}
2016-03-26 10:37:48 +00:00
void Toggle::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
2016-03-26 12:38:24 +00:00
painter.setRenderHint(QPainter::Antialiasing);
2016-03-26 12:38:24 +00:00
QBrush brush;
brush.setColor(QColor(180, 180, 180));
brush.setStyle(Qt::SolidPattern);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
2016-03-28 08:51:24 +00:00
if (Qt::Horizontal == _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);
}
}