tweak Toggle animation
This commit is contained in:
parent
bc1d95bfe4
commit
19a090fd15
|
@ -1,5 +1,6 @@
|
||||||
#include <QAbstractButton>
|
#include <QAbstractButton>
|
||||||
#include <QPropertyAnimation>
|
#include <QPropertyAnimation>
|
||||||
|
#include <QMouseEvent>
|
||||||
#include <QEvent>
|
#include <QEvent>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
|
@ -11,7 +12,8 @@ Thumb::Thumb(Toggle *parent)
|
||||||
: QWidget(parent),
|
: QWidget(parent),
|
||||||
_toggle(parent),
|
_toggle(parent),
|
||||||
_animation(new QPropertyAnimation(this)),
|
_animation(new QPropertyAnimation(this)),
|
||||||
_progress(0)
|
_progress(0),
|
||||||
|
_offset(0)
|
||||||
{
|
{
|
||||||
parent->installEventFilter(this);
|
parent->installEventFilter(this);
|
||||||
|
|
||||||
|
@ -26,6 +28,19 @@ Thumb::~Thumb()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Thumb::setProgress(qreal p)
|
||||||
|
{
|
||||||
|
if (_progress == p)
|
||||||
|
return;
|
||||||
|
|
||||||
|
_progress = p;
|
||||||
|
_offset = p*(static_cast<qreal>(width()-qMin(width(), height())));
|
||||||
|
|
||||||
|
emit progressChanged(p);
|
||||||
|
|
||||||
|
update();
|
||||||
|
}
|
||||||
|
|
||||||
bool Thumb::eventFilter(QObject *obj, QEvent *event)
|
bool Thumb::eventFilter(QObject *obj, QEvent *event)
|
||||||
{
|
{
|
||||||
const QEvent::Type type = event->type();
|
const QEvent::Type type = event->type();
|
||||||
|
@ -35,24 +50,27 @@ bool Thumb::eventFilter(QObject *obj, QEvent *event)
|
||||||
return QWidget::eventFilter(obj, event);
|
return QWidget::eventFilter(obj, event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Thumb::mousePressEvent(QMouseEvent *event)
|
||||||
|
{
|
||||||
|
Q_UNUSED(event)
|
||||||
|
}
|
||||||
|
|
||||||
void Thumb::mouseReleaseEvent(QMouseEvent *event)
|
void Thumb::mouseReleaseEvent(QMouseEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
|
|
||||||
emit clicked();
|
const bool checked = _toggle->isChecked();
|
||||||
|
_animation->setDirection(checked
|
||||||
if (_toggle->isChecked()) {
|
? QAbstractAnimation::Forward
|
||||||
_animation->setDirection(QAbstractAnimation::Forward);
|
: QAbstractAnimation::Backward);
|
||||||
if (QAbstractAnimation::Running != _animation->state()) {
|
if (QAbstractAnimation::Running != _animation->state()) {
|
||||||
_animation->setEasingCurve(QEasingCurve::OutCubic);
|
_animation->setEasingCurve(checked
|
||||||
}
|
? QEasingCurve::OutCubic
|
||||||
} else {
|
: QEasingCurve::InCubic);
|
||||||
_animation->setDirection(QAbstractAnimation::Backward);
|
|
||||||
if (QAbstractAnimation::Running != _animation->state()) {
|
|
||||||
_animation->setEasingCurve(QEasingCurve::InCubic);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
_animation->start();
|
_animation->start();
|
||||||
|
|
||||||
|
emit clicked();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Thumb::paintEvent(QPaintEvent *event)
|
void Thumb::paintEvent(QPaintEvent *event)
|
||||||
|
@ -73,37 +91,69 @@ void Thumb::paintEvent(QPaintEvent *event)
|
||||||
|
|
||||||
QBrush brush;
|
QBrush brush;
|
||||||
brush.setStyle(Qt::SolidPattern);
|
brush.setStyle(Qt::SolidPattern);
|
||||||
brush.setColor(QColor(120, 120, 120));
|
brush.setColor(Qt::white);
|
||||||
|
|
||||||
painter.setBrush(brush);
|
painter.setBrush(brush);
|
||||||
painter.setPen(Qt::NoPen);
|
painter.setPen(Qt::NoPen);
|
||||||
|
|
||||||
|
// painter.drawEllipse(5 + _progress*(static_cast<qreal>(width()-d)), 5, d-10, d-10);
|
||||||
|
|
||||||
const int d = qMin(width(), height());
|
const int d = qMin(width(), height());
|
||||||
painter.drawEllipse(5 + _progress*(static_cast<qreal>(width()-d)), 5, d-10, d-10);
|
painter.drawEllipse(5 + _offset, 5, d-10, d-10);
|
||||||
}
|
}
|
||||||
|
|
||||||
Toggle::Toggle(QWidget *parent)
|
Toggle::Toggle(QWidget *parent)
|
||||||
: QAbstractButton(parent),
|
: QAbstractButton(parent),
|
||||||
_overlay(new RippleOverlay(this)),
|
_thumb(new Thumb(this)),
|
||||||
_thumb(new Thumb(this))
|
_overlay(new RippleOverlay(parent))
|
||||||
{
|
{
|
||||||
|
setFixedSize(64, 48);
|
||||||
setCheckable(true);
|
setCheckable(true);
|
||||||
|
|
||||||
CustomShadowEffect *effect = new CustomShadowEffect;
|
CustomShadowEffect *effect = new CustomShadowEffect;
|
||||||
effect->setDistance(0);
|
effect->setDistance(0);
|
||||||
effect->setBlurRadius(6);
|
effect->setBlurRadius(6);
|
||||||
effect->setColor(QColor(60, 60, 60));
|
effect->setColor(QColor(100, 100, 100));
|
||||||
|
|
||||||
_thumb->setGraphicsEffect(effect);
|
_thumb->setGraphicsEffect(effect);
|
||||||
_thumb->installEventFilter(this);
|
_thumb->installEventFilter(this);
|
||||||
|
|
||||||
connect(_thumb, SIGNAL(clicked()), this, SLOT(toggle()));
|
connect(_thumb, SIGNAL(clicked()), this, SLOT(toggle()));
|
||||||
|
connect(_thumb, SIGNAL(clicked()), this, SLOT(xx()));
|
||||||
|
connect(_thumb, SIGNAL(progressChanged(qreal)), this, SLOT(yy()));
|
||||||
}
|
}
|
||||||
|
|
||||||
Toggle::~Toggle()
|
Toggle::~Toggle()
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Toggle::xx()
|
||||||
|
{
|
||||||
|
const int d = height()/2; // ???
|
||||||
|
_overlay->addRipple(QPoint(10+d, 20+d), 35);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Toggle::yy()
|
||||||
|
{
|
||||||
|
//const int d = progress*(static_cast<qreal>(width()-d));
|
||||||
|
|
||||||
|
//const int r = qMin(_thumb->width(), _thumb->height());
|
||||||
|
const int d = _thumb->offset();
|
||||||
|
|
||||||
|
_overlay->setGeometry(geometry().adjusted(-10+d, -20, 10+d, 20));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
void Toggle::paintEvent(QPaintEvent *event)
|
void Toggle::paintEvent(QPaintEvent *event)
|
||||||
{
|
{
|
||||||
Q_UNUSED(event)
|
Q_UNUSED(event)
|
||||||
|
@ -111,6 +161,8 @@ void Toggle::paintEvent(QPaintEvent *event)
|
||||||
QPainter painter(this);
|
QPainter painter(this);
|
||||||
painter.setRenderHint(QPainter::Antialiasing);
|
painter.setRenderHint(QPainter::Antialiasing);
|
||||||
|
|
||||||
|
painter.drawRect(rect());
|
||||||
|
|
||||||
const int h = height()/2;
|
const int h = height()/2;
|
||||||
|
|
||||||
QBrush brush;
|
QBrush brush;
|
||||||
|
|
|
@ -11,20 +11,24 @@ class Thumb : public QWidget
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
Q_PROPERTY(qreal progress WRITE setProgress READ progress)
|
Q_PROPERTY(qreal progress WRITE setProgress READ progress NOTIFY progressChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Thumb(Toggle *parent);
|
explicit Thumb(Toggle *parent);
|
||||||
~Thumb();
|
~Thumb();
|
||||||
|
|
||||||
inline void setProgress(qreal p) { _progress = p; update(); }
|
void setProgress(qreal p);
|
||||||
inline qreal progress() const { return _progress; }
|
inline qreal progress() const { return _progress; }
|
||||||
|
|
||||||
|
inline int offset() const { return _offset; }
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void clicked();
|
void clicked();
|
||||||
|
void progressChanged(qreal);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||||
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
|
@ -32,6 +36,7 @@ private:
|
||||||
Toggle *const _toggle;
|
Toggle *const _toggle;
|
||||||
QPropertyAnimation *const _animation;
|
QPropertyAnimation *const _animation;
|
||||||
qreal _progress;
|
qreal _progress;
|
||||||
|
int _offset;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Toggle : public QAbstractButton
|
class Toggle : public QAbstractButton
|
||||||
|
@ -42,14 +47,19 @@ public:
|
||||||
explicit Toggle(QWidget *parent = 0);
|
explicit Toggle(QWidget *parent = 0);
|
||||||
~Toggle();
|
~Toggle();
|
||||||
|
|
||||||
QSize sizeHint() const { return QSize(132, 64); }
|
QSize sizeHint() const { return QSize(64, 48); }
|
||||||
|
|
||||||
|
protected slots:
|
||||||
|
void xx();
|
||||||
|
void yy();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RippleOverlay *const _overlay;
|
|
||||||
Thumb *const _thumb;
|
Thumb *const _thumb;
|
||||||
|
RippleOverlay *const _overlay;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TOGGLE_H
|
#endif // TOGGLE_H
|
||||||
|
|
Loading…
Reference in New Issue