From e8d4374bd38fa574167f2b720e4d48c6d64731b3 Mon Sep 17 00:00:00 2001 From: laserpants Date: Sun, 12 Jun 2016 03:56:37 +0300 Subject: [PATCH] tweak ripple animation parameters --- components/flatbutton.cpp | 6 +- components/tabs.cpp | 40 ++-- components/tabs.h | 5 +- components/tabs_p.h | 2 + components/tmp_slider.cpp | 304 ------------------------------ components/tmp_slider.h | 94 --------- components/toggle.cpp | 2 +- components/toggle.h | 5 + components/toggle_internal.cpp | 9 + components/toggle_internal.h | 6 +- examples/raisedbuttonexamples.cpp | 1 + lib/ripple.h | 12 ++ lib/rippleoverlay.cpp | 7 + lib/rippleoverlay.h | 2 + 14 files changed, 70 insertions(+), 425 deletions(-) delete mode 100644 components/tmp_slider.cpp delete mode 100644 components/tmp_slider.h diff --git a/components/flatbutton.cpp b/components/flatbutton.cpp index 431f4bc..79003ab 100644 --- a/components/flatbutton.cpp +++ b/components/flatbutton.cpp @@ -332,10 +332,10 @@ void FlatButton::mousePressEvent(QMouseEvent *event) ? rect().center() : event->pos()); - ripple->setRadiusEndValue(width()*0.45); - ripple->setOpacityStartValue(0.4); + ripple->setRadiusEndValue(width()/2); + ripple->setOpacityStartValue(0.35); ripple->setColor(palette().color(QPalette::Active, QPalette::ButtonText)); - ripple->setDuration(500); + ripple->setDuration(600, 1300); d->ripple->addRipple(ripple); diff --git a/components/tabs.cpp b/components/tabs.cpp index 7b3a0e1..605169d 100644 --- a/components/tabs.cpp +++ b/components/tabs.cpp @@ -11,7 +11,8 @@ TabsPrivate::TabsPrivate(Tabs *q) : q_ptr(q), tab(-1), useThemeColors(true), - showHalo(false) + showHalo(false), + rippleStyle(Material::PositionedRipple) { } @@ -74,6 +75,28 @@ bool Tabs::showHalo() const return d->showHalo; } +void Tabs::setRippleStyle(Material::RippleStyle style) +{ + Q_D(Tabs); + + d->rippleStyle = style; + + Tab *tab; + for (int i = 0; i < d->tabLayout->count(); ++i) { + QLayoutItem *item = d->tabLayout->itemAt(i); + if ((tab = static_cast(item->widget()))) { + tab->setRippleStyle(style); + } + } +} + +Material::RippleStyle Tabs::rippleStyle() const +{ + Q_D(const Tabs); + + return d->rippleStyle; +} + void Tabs::setInkColor(const QColor &color) { Q_D(Tabs); @@ -161,20 +184,6 @@ void Tabs::addTab(const QString &text, const QIcon &icon) tab->setIconSize(QSize(22, 22)); } -void Tabs::setRippleStyle(Material::RippleStyle style) -{ - Q_D(Tabs); - - Tab *tab; - for (int i = 0; i < d->tabLayout->count(); ++i) { - QLayoutItem *item = d->tabLayout->itemAt(i); - if ((tab = static_cast(item->widget()))) { - tab->setRippleStyle(style); - } - } - -} - const QLayout *Tabs::tabLayout() const { Q_D(const Tabs); @@ -231,7 +240,6 @@ Tab *Tabs::createTab(const QString &text) Tab *tab = new Tab(text); tab->setCornerRadius(0); - tab->setRippleStyle(Material::CenteredRipple); tab->setRole(Material::Primary); tab->setBackgroundMode(Qt::OpaqueMode); tab->setPeakOpacity(0.25); diff --git a/components/tabs.h b/components/tabs.h index 7f5b0d6..7cc6dda 100644 --- a/components/tabs.h +++ b/components/tabs.h @@ -25,6 +25,9 @@ public: void setShowHalo(bool state); bool showHalo() const; + void setRippleStyle(Material::RippleStyle style); + Material::RippleStyle rippleStyle() const; + void setInkColor(const QColor &color); QColor inkColor() const; @@ -37,8 +40,6 @@ public: void addTab(const QString &text); void addTab(const QString &text, const QIcon &icon); - void setRippleStyle(Material::RippleStyle style); - const QLayout *tabLayout() const; int currentIndex() const; diff --git a/components/tabs_p.h b/components/tabs_p.h index 3b24c08..3cf8f33 100644 --- a/components/tabs_p.h +++ b/components/tabs_p.h @@ -3,6 +3,7 @@ #include #include +#include "lib/theme.h" class QHBoxLayout; class Tabs; @@ -27,6 +28,7 @@ public: int tab; bool useThemeColors; bool showHalo; + Material::RippleStyle rippleStyle; }; #endif // TABS_P_H diff --git a/components/tmp_slider.cpp b/components/tmp_slider.cpp deleted file mode 100644 index e4201d9..0000000 --- a/components/tmp_slider.cpp +++ /dev/null @@ -1,304 +0,0 @@ -#include -#include -#include -#include -#include "slider.h" - -Handle::Handle(Slider *slider) - : QWidget(slider), - _slider(slider), - _knobSize(12), - _haloSize(0), - _phase(0) -{ - setAttribute(Qt::WA_TransparentForMouseEvents); -} - -Handle::~Handle() -{ -} - -void Handle::refreshGeometry() -{ - const QSize handle = sizeHint(); - setGeometry(QRect(_slider->orientation() == Qt::Horizontal - ? QPoint(qBound(0, _position.x(), _slider->width()-handle.width()), _slider->height()/2-handle.height()/2) - : QPoint(_slider->width()/2-handle.width()/2, qBound(0, _position.y(), _slider->height()-handle.height())), handle)); - update(); -} - -void Handle::paintEvent(QPaintEvent *event) -{ - QPainter painter(this); - painter.setRenderHint(QPainter::Antialiasing); - - QBrush brush; - brush.setColor(QColor(0, 0, 0)); - brush.setStyle(Qt::SolidPattern); - painter.setBrush(brush); - painter.setPen(Qt::NoPen); - - // Paint halo - if (_haloSize > 12) { - painter.save(); - painter.setOpacity(0.1); - painter.drawEllipse(QRectF((width()-_haloSize)/2, (height()-_haloSize)/2, _haloSize, _haloSize)); - painter.restore(); - } - - const QRectF rect((width()-_knobSize)/2, (height()-_knobSize)/2, _knobSize, _knobSize); - - // Paint default knob - painter.drawEllipse(rect); - - // Hollow knob (indicates that value == minimum or slider is disabled) - if (_phase < 1) - { - QPen pen; - pen.setColor(QColor(0, 0, 0, 80)); - pen.setWidth(2); - painter.setPen(pen); - - brush.setColor(Qt::white); - painter.setBrush(brush); - - painter.setOpacity(1-_phase); - - painter.drawEllipse(rect); - } - - QWidget::paintEvent(event); -} - -Slider::Slider(QWidget *parent) - : QAbstractSlider(parent), - _knobAnimation(new QPropertyAnimation(this)), - _haloAnimation(new QPropertyAnimation(this)), - _phaseAnimation(new QPropertyAnimation(this)), - _handle(new Handle(this)), - _drag(false), - _hover(false), - _orientation(Qt::Horizontal) -{ - _knobAnimation->setPropertyName("knobSize"); - _knobAnimation->setTargetObject(_handle); - _knobAnimation->setStartValue(12); - _knobAnimation->setEndValue(20); - _knobAnimation->setDuration(100); - - _haloAnimation->setPropertyName("haloSize"); - _haloAnimation->setTargetObject(_handle); - _haloAnimation->setStartValue(12); - _haloAnimation->setEndValue(30); - _haloAnimation->setDuration(220); - - _phaseAnimation->setPropertyName("phase"); - _phaseAnimation->setTargetObject(_handle); - _phaseAnimation->setStartValue(0); - _phaseAnimation->setEndValue(1); - _phaseAnimation->setDuration(500); - - setMouseTracking(true); - setFocusPolicy(Qt::StrongFocus); -} - -Slider::~Slider() -{ -} - -void Slider::setOrientation(Qt::Orientation orientation) -{ - if (_orientation == orientation) - return; - _orientation = orientation; - _handle->refreshGeometry(); - update(); -} - -void Slider::paintEvent(QPaintEvent *event) -{ - QPainter painter(this); - - if (hasFocus()) { - painter.drawRect(rect()); - } - - QRect rect = Qt::Vertical == _orientation - ? QRect(width()/2-1, 0, 2, height()) - : QRect(0, height()/2-1, width(), 2); - - const QSize handle = _handle->sizeHint(); - - QBrush brush; - brush.setStyle(Qt::SolidPattern); - brush.setColor(_hover ? QColor(0, 0, 0, 80) : QColor(0, 0, 0, 40)); - - if (Qt::Horizontal == _orientation) { - rect.adjust(handle.width()/2, 0, -handle.width()/2, 0); - } else { - rect.adjust(0, handle.height()/2, 0, -handle.height()/2); - } - painter.fillRect(rect, brush); - - painter.save(); - brush.setColor(QColor(0, 0, 0)); - const QPoint range = Qt::Vertical == _orientation - ? QPoint(width(), _handle->y()+handle.height()/2) - : QPoint(_handle->x()+handle.width()/2, height()); - painter.fillRect(rect.intersected(QRect(QPoint(0, 0), range)), brush); - painter.restore(); - - QAbstractSlider::paintEvent(event); -} - -void Slider::mousePressEvent(QMouseEvent *event) -{ - const QPoint pos = event->pos(); - const bool touchesHandle = overHandle(pos); - - if (touchesHandle || overTrack(pos)) { - const QSize handle = _handle->sizeHint(); - _handle->setOffset((pos - QPoint(handle.width(), handle.height())/2) - event->globalPos()); - _handle->setRelativePosition(event->globalPos()); - _drag = true; - _knobAnimation->setDirection(QAbstractAnimation::Forward); - _knobAnimation->start(); - - if (touchesHandle) { - _haloAnimation->setDirection(QAbstractAnimation::Backward); - _haloAnimation->start(); - } else { - _haloAnimation->stop(); - _handle->setHaloSize(0); - } - updateValue(); - } - QAbstractSlider::mousePressEvent(event); -} - -void Slider::mouseReleaseEvent(QMouseEvent *event) -{ - Q_UNUSED(event) - - if (_drag) { - _drag = false; - _hover = false; - _knobAnimation->setDirection(QAbstractAnimation::Backward); - _knobAnimation->start(); - updateHoverState(mapFromGlobal(QCursor::pos())); - } -} - -void Slider::mouseMoveEvent(QMouseEvent *event) -{ - if (_drag) { - _handle->setRelativePosition(event->globalPos()); - updateValue(); - } else { - updateHoverState(event->pos()); - } - QAbstractSlider::mouseMoveEvent(event); -} - -void Slider::enterEvent(QEvent *event) -{ - Q_UNUSED(event) - - updateHoverState(mapFromGlobal(QCursor::pos())); -} - -void Slider::leaveEvent(QEvent *event) -{ - Q_UNUSED(event) - - endHover(); -} - -void Slider::resizeEvent(QResizeEvent *event) -{ - _handle->refreshGeometry(); - QAbstractSlider::resizeEvent(event); -} - -bool Slider::overTrack(const QPoint &pos) const -{ - if (Qt::Horizontal == _orientation) { - const int handleW = _handle->width(); - return QRect(handleW/2, height()/2-4, width()-handleW, 8).contains(pos); - } else { - const int handleH = _handle->height(); - return QRect(width()/2-4, handleH/2, 8, height()-handleH).contains(pos); - } -} - -bool Slider::overHandle(const QPoint &pos) const -{ - const int knob = _handle->knobSize(); - const int hl = _handle->x() + (20-knob)/2; // @TODO: 20 should not be hard coded - const int ht = _handle->y() + (20-knob)/2; // @TODO: 20 should not be hard coded - - return (pos.x() > hl - 10 && pos.x() < hl+knob + 10 - && pos.y() > ht - 10 && pos.y() < ht+knob + 10); -} - -void Slider::updateValue() -{ - const qreal tot = Qt::Horizontal == _orientation - ? geometry().width()-_handle->width() - : geometry().height()-_handle->height(); - - const qreal r = Qt::Horizontal == _orientation - ? _handle->geometry().left() / tot - : _handle->geometry().top() / tot; - - // @TODO: use QStyle::sliderValueFromPosition() - - const int oldValue = value(); - const int newValue = (1-r)*minimum()+r*maximum(); - - if (oldValue == newValue) { - return; - } - - setValue(newValue); - - if (oldValue == 0 && newValue != 0) { - _phaseAnimation->setDirection(QAbstractAnimation::Forward); - _phaseAnimation->start(); - } else if (newValue == 0 && oldValue != 0) { - _phaseAnimation->setDirection(QAbstractAnimation::Backward); - _phaseAnimation->start(); - } - - update(); -} - -void Slider::updateHoverState(const QPoint &pos) -{ - if (overTrack(pos) || (overHandle(pos))) { - beginHover(); - } else { - endHover(); - } -} - -void Slider::beginHover() { - if (!_hover) { - _hover = true; - _haloAnimation->setDirection(QAbstractAnimation::Forward); - _haloAnimation->start(); - update(); - } -} - -void Slider::endHover() -{ - if (_hover) { - _hover = false; - if (_handle->haloSize() > 12) { - _haloAnimation->setDirection(QAbstractAnimation::Backward); - _haloAnimation->start(); - } - } - update(); -} diff --git a/components/tmp_slider.h b/components/tmp_slider.h deleted file mode 100644 index a9a5775..0000000 --- a/components/tmp_slider.h +++ /dev/null @@ -1,94 +0,0 @@ -#ifndef SLIDER_H -#define SLIDER_H - -#include -#include - -class QPropertyAnimation; -class Slider; - -class Handle : public QWidget -{ - Q_OBJECT - - Q_PROPERTY(qreal knobSize WRITE setKnobSize READ knobSize) - Q_PROPERTY(qreal haloSize WRITE setHaloSize READ haloSize) - Q_PROPERTY(qreal phase WRITE setPhase READ phase) - -public: - explicit Handle(Slider *slider); - ~Handle(); - - inline QSize sizeHint() const Q_DECL_OVERRIDE { return QSize(30, 30); } - - inline void setRelativePosition(const QPoint &pos) { setPosition(_offset + pos); } - - inline void setPosition(const QPoint &pos) { _position = pos; refreshGeometry(); } - inline const QPoint &position() const { return _position; } - - inline void setOffset(const QPoint &offset) { _offset = offset; update(); } - inline const QPoint &offset() const { return _offset; } - - inline void setKnobSize (qreal size) { _knobSize = size; refreshGeometry(); } - inline qreal knobSize() const { return _knobSize; } - - inline void setHaloSize (qreal size) { _haloSize = size; update(); } - inline qreal haloSize() const { return _haloSize; } - - inline void setPhase (qreal phase) { _phase = phase; update(); } - inline qreal phase() const { return _phase; } - - void refreshGeometry(); - -protected: - void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; - -private: - Slider *const _slider; - QPoint _position; - QPoint _offset; - qreal _knobSize; - qreal _haloSize; - qreal _phase; -}; - -class Slider : public QAbstractSlider -{ - Q_OBJECT - -public: - explicit Slider(QWidget *parent = 0); - ~Slider(); - - void setOrientation(Qt::Orientation orientation); - inline Qt::Orientation orientation() const { return _orientation; } - -protected: - void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; - void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; - void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE; - void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE; - void enterEvent(QEvent *event) Q_DECL_OVERRIDE; - void leaveEvent(QEvent *event) Q_DECL_OVERRIDE; - void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; - - bool overTrack(const QPoint &pos) const; - bool overHandle(const QPoint &pos) const; - - void updateValue(); - void updateHoverState(const QPoint &pos); - - void beginHover(); - void endHover(); - -private: - QPropertyAnimation *const _knobAnimation; - QPropertyAnimation *const _haloAnimation; - QPropertyAnimation *const _phaseAnimation; - Handle *const _handle; - bool _drag; - bool _hover; - Qt::Orientation _orientation; -}; - -#endif // SLIDER_H diff --git a/components/toggle.cpp b/components/toggle.cpp index 54ad8b4..01c050e 100644 --- a/components/toggle.cpp +++ b/components/toggle.cpp @@ -275,7 +275,7 @@ void Toggle::addRipple() } Ripple *ripple = new Ripple(QPoint(10+t, 20+t)); - ripple->setColor(isChecked() ? activeColor() : trackColor()); + ripple->setColor(d->track->trackColor()); ripple->setRadiusEndValue(w); ripple->setOpacityStartValue(0.4); diff --git a/components/toggle.h b/components/toggle.h index 1587e6a..11fe4fc 100644 --- a/components/toggle.h +++ b/components/toggle.h @@ -4,6 +4,8 @@ #include class TogglePrivate; +class ToggleTrack; +class ToggleThumb; class Toggle : public QAbstractButton { @@ -52,6 +54,9 @@ protected: private: Q_DISABLE_COPY(Toggle) Q_DECLARE_PRIVATE(Toggle) + + friend class ToggleTrack; + friend class ToggleThumb; }; #endif // TOGGLE_H diff --git a/components/toggle_internal.cpp b/components/toggle_internal.cpp index 8616d28..d051ad4 100644 --- a/components/toggle_internal.cpp +++ b/components/toggle_internal.cpp @@ -2,8 +2,10 @@ #include #include #include "lib/style.h" +#include "lib/rippleoverlay.h" #include "toggle_internal.h" #include "toggle.h" +#include "toggle_p.h" ToggleThumb::ToggleThumb(Toggle *parent) : QWidget(parent), @@ -100,6 +102,13 @@ ToggleTrack::~ToggleTrack() { } +void ToggleTrack::setTrackColor(const QColor &color) +{ + _trackColor = color; + _toggle->d_func()->ripple->setColor(color); + update(); +} + bool ToggleTrack::eventFilter(QObject *obj, QEvent *event) { const QEvent::Type type = event->type(); diff --git a/components/toggle_internal.h b/components/toggle_internal.h index 516e423..0a22158 100644 --- a/components/toggle_internal.h +++ b/components/toggle_internal.h @@ -57,11 +57,7 @@ public: ToggleTrack(Toggle *parent); ~ToggleTrack(); - inline void setTrackColor(const QColor &color) - { - _trackColor = color; - update(); - } + void setTrackColor(const QColor &color); inline QColor trackColor() const { diff --git a/examples/raisedbuttonexamples.cpp b/examples/raisedbuttonexamples.cpp index 032a96d..3a1af00 100644 --- a/examples/raisedbuttonexamples.cpp +++ b/examples/raisedbuttonexamples.cpp @@ -13,6 +13,7 @@ RaisedButtonExamples::RaisedButtonExamples(QWidget *parent) RaisedButton *raisedButton = new RaisedButton; raisedButton->setRole(Material::Primary); raisedButton->setText("Press me!"); + raisedButton->setMaximumWidth(408); //raisedButton->setDisabled(true); diff --git a/lib/ripple.h b/lib/ripple.h index 13c431d..702b8e1 100644 --- a/lib/ripple.h +++ b/lib/ripple.h @@ -32,12 +32,24 @@ public: inline const QPoint ¢er() const { return _center; } + inline void setDuration(int radius, int opacity) + { + _radiusAnimation->setDuration(radius); + _opacityAnimation->setDuration(opacity); + } + inline void setDuration(int duration) { _radiusAnimation->setDuration(duration); _opacityAnimation->setDuration(duration); } + inline void setEasingCurve(QEasingCurve radius, QEasingCurve opacity) + { + _radiusAnimation->setEasingCurve(radius); + _opacityAnimation->setEasingCurve(opacity); + } + inline void setEasingCurve(QEasingCurve easing) { _radiusAnimation->setEasingCurve(easing); diff --git a/lib/rippleoverlay.cpp b/lib/rippleoverlay.cpp index 7212c43..c52e0a3 100644 --- a/lib/rippleoverlay.cpp +++ b/lib/rippleoverlay.cpp @@ -29,6 +29,13 @@ void RippleOverlay::addRipple(Ripple *ripple) ripple->startAnimation(); } +void RippleOverlay::setColor(const QColor &color) +{ + QList::const_iterator i; + for (i = ripples.begin(); i != ripples.end(); ++i) + (*i)->setColor(color); +} + void RippleOverlay::paintEvent(QPaintEvent *event) { Q_UNUSED(event) diff --git a/lib/rippleoverlay.h b/lib/rippleoverlay.h index 4c262b1..d0a9ef5 100644 --- a/lib/rippleoverlay.h +++ b/lib/rippleoverlay.h @@ -16,6 +16,8 @@ public: void addRipple(const QPoint &position, qreal radius = 300); void addRipple(Ripple *ripple); + void setColor(const QColor &color); + protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;