pimplify toggle class

This commit is contained in:
laserpants 2016-05-29 00:01:24 +03:00
parent 20db7569f3
commit 023f28610f
4 changed files with 276 additions and 262 deletions

View File

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

View File

@ -1,71 +1,77 @@
#ifndef TOGGLE_H #ifndef TOGGLE_H
#define TOGGLE_H #define TOGGLE_H
#include <QAbstractButton>
class QPropertyAnimation;
class RippleOverlay;
class Toggle;
class Thumb : public QWidget
{
Q_OBJECT
Q_PROPERTY(qreal progress WRITE setProgress READ progress)
public:
explicit Thumb(Toggle *parent);
~Thumb();
void setProgress(qreal progress);
inline qreal progress() const { return _progress; }
inline void setOffset(int offset) { _offset = offset; }
inline int offset() const { return _offset; }
signals:
void clicked();
protected:
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
Toggle *const _toggle;
QPropertyAnimation *const _animation;
qreal _progress;
int _offset;
};
class Toggle : public QAbstractButton
{
Q_OBJECT
friend class Thumb;
void updateOverlayGeometry();
public:
explicit Toggle(QWidget *parent = 0);
~Toggle();
QSize sizeHint() const;
inline Qt::Orientation orientation() const { return _orientation; }
void setOrientation(Qt::Orientation orientation);
protected slots:
void addRipple();
protected:
bool event(QEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
Thumb *const _thumb;
RippleOverlay *const _overlay;
Qt::Orientation _orientation;
};
#endif // TOGGLE_H #endif // TOGGLE_H
//#ifndef TOGGLE_H
//#define TOGGLE_H
//
//#include <QAbstractButton>
//
//class QPropertyAnimation;
//class RippleOverlay;
//class Toggle;
//
//class Thumb : public QWidget
//{
// Q_OBJECT
//
// Q_PROPERTY(qreal progress WRITE setProgress READ progress)
//
//public:
// explicit Thumb(Toggle *parent);
// ~Thumb();
//
// void setProgress(qreal progress);
// inline qreal progress() const { return _progress; }
//
// inline void setOffset(int offset) { _offset = offset; }
// inline int offset() const { return _offset; }
//
//signals:
// void clicked();
//
//protected:
// bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
// void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
// void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
//
//private:
// Toggle *const _toggle;
// QPropertyAnimation *const _animation;
// qreal _progress;
// int _offset;
//};
//
//class Toggle : public QAbstractButton
//{
// Q_OBJECT
//
// friend class Thumb;
//
// void updateOverlayGeometry();
//
//public:
// explicit Toggle(QWidget *parent = 0);
// ~Toggle();
//
// QSize sizeHint() const;
//
// inline Qt::Orientation orientation() const { return _orientation; }
// void setOrientation(Qt::Orientation orientation);
//
//protected slots:
// void addRipple();
//
//protected:
// bool event(QEvent *event) Q_DECL_OVERRIDE;
// void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
//
//private:
// Thumb *const _thumb;
// RippleOverlay *const _overlay;
// Qt::Orientation _orientation;
//};
//
//#endif // TOGGLE_H
//

View File

@ -106,7 +106,8 @@ HEADERS += mainwindow.h \
components/flatbutton_p.h \ components/flatbutton_p.h \
components/slider_internal.h \ components/slider_internal.h \
components/flatbutton_internal.h \ components/flatbutton_internal.h \
components/raisedbutton_p.h components/raisedbutton_p.h \
toggle_p.h
RESOURCES += \ RESOURCES += \
resources.qrc resources.qrc

4
toggle_p.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef TOGGLE_P_H
#define TOGGLE_P_H
#endif // TOGGLE_P_H