diff --git a/components/flatbutton.cpp b/components/flatbutton.cpp index 40c4c66..2714397 100644 --- a/components/flatbutton.cpp +++ b/components/flatbutton.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "lib/rippleoverlay.h" #include "lib/ripple.h" @@ -74,13 +73,15 @@ void FlatButton::paintEvent(QPaintEvent *event) painter.drawControl(QStyle::CE_PushButtonLabel, option); - if (isEnabled() && testAttribute(Qt::WA_Hover) && underMouse()) + const qreal bgOpacity = d->delegate->backgroundOpacity(); + + if (isEnabled() && bgOpacity > 0) { QPainter painter(this); QBrush brush; brush.setStyle(Qt::SolidPattern); - brush.setColor(d->textColor()); - painter.setOpacity(0.1); + brush.setColor(d->delegate->backgroundColor()); + painter.setOpacity(bgOpacity); painter.fillRect(rect(), brush); } @@ -98,26 +99,14 @@ void FlatButton::mousePressEvent(QMouseEvent *event) { Q_D(FlatButton); + Style &style = Style::instance(); + Ripple *ripple = new Ripple(event->pos()); ripple->setRadiusEndValue(100); - ripple->setOpacityStartValue(0.2); - ripple->setColor(d->textColor()); + ripple->setOpacityStartValue(0.3); + ripple->setColor(style.themeColor("text")); - //d->ripple->addRipple(ripple); + d->ripple->addRipple(ripple); QPushButton::mousePressEvent(event); } - -void FlatButton::enterEvent(QEvent *event) -{ - update(); - - QPushButton::enterEvent(event); -} - -void FlatButton::leaveEvent(QEvent *event) -{ - update(); - - QPushButton::leaveEvent(event); -} diff --git a/components/flatbutton.h b/components/flatbutton.h index f787f4c..804cce9 100644 --- a/components/flatbutton.h +++ b/components/flatbutton.h @@ -22,8 +22,6 @@ protected: void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; - void enterEvent(QEvent *event) Q_DECL_OVERRIDE; - void leaveEvent(QEvent *event) Q_DECL_OVERRIDE; const QScopedPointer d_ptr; diff --git a/components/flatbutton_internal.cpp b/components/flatbutton_internal.cpp index 895ec9e..3c40df6 100644 --- a/components/flatbutton_internal.cpp +++ b/components/flatbutton_internal.cpp @@ -1 +1,106 @@ #include "flatbutton_internal.h" +#include +#include +#include +#include +#include "flatbutton.h" +#include "lib/style.h" + +FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent) + : QStateMachine(parent), + button(parent) +{ + Style &style = Style::instance(); + + QState *normalState = new QState; + QState *focusedState = new QState; + QState *pressedState = new QState; + + addState(normalState); + addState(focusedState); + addState(pressedState); + + setInitialState(normalState); + + normalState->assignProperty(this, "backgroundOpacity", 0); + normalState->assignProperty(this, "backgroundColor", style.themeColor("text")); + + focusedState->assignProperty(this, "backgroundOpacity", 0.15); + focusedState->assignProperty(this, "backgroundColor", style.themeColor("text")); + + pressedState->assignProperty(this, "backgroundOpacity", 0.15); + pressedState->assignProperty(this, "backgroundColor", style.themeColor("text")); + + QAbstractTransition *transition; + QPropertyAnimation *animation; + + // + + transition = new QEventTransition(button, QEvent::Enter); + transition->setTargetState(focusedState); + + animation = new QPropertyAnimation(this, "backgroundOpacity"); + animation->setDuration(140); + transition->addAnimation(animation); + normalState->addTransition(transition); + + // + + transition = new QEventTransition(button, QEvent::Leave); + transition->setTargetState(normalState); + + animation = new QPropertyAnimation(this, "backgroundOpacity"); + animation->setDuration(140); + transition->addAnimation(animation); + focusedState->addTransition(transition); + + // + + transition = new QEventTransition(button, QEvent::MouseButtonPress); + transition->setTargetState(pressedState); + + animation = new QPropertyAnimation(this, "backgroundOpacity"); + animation->setDuration(140); + transition->addAnimation(animation); + focusedState->addTransition(transition); + + // + + transition = new QEventTransition(button, QEvent::MouseButtonRelease); + transition->setTargetState(focusedState); + + animation = new QPropertyAnimation(this, "backgroundOpacity"); + animation->setDuration(500); + transition->addAnimation(animation); + pressedState->addTransition(transition); + + // + + start(); +} + +FlatButtonDelegate::~FlatButtonDelegate() +{ +} + +void FlatButtonDelegate::setBackgroundOpacity(qreal opacity) +{ + _backgroundOpacity = opacity; + button->update(); +} + +qreal FlatButtonDelegate::backgroundOpacity() const +{ + return _backgroundOpacity; +} + +void FlatButtonDelegate::setBackgroundColor(const QColor &color) +{ + _backgroundColor = color; + button->update(); +} + +QColor FlatButtonDelegate::backgroundColor() const +{ + return _backgroundColor; +} diff --git a/components/flatbutton_internal.h b/components/flatbutton_internal.h index c6e3302..513eeea 100644 --- a/components/flatbutton_internal.h +++ b/components/flatbutton_internal.h @@ -1,4 +1,34 @@ #ifndef FLATBUTTON_INTERNAL_H #define FLATBUTTON_INTERNAL_H +#include +#include + +class FlatButton; + +class FlatButtonDelegate : public QStateMachine +{ + Q_OBJECT + + Q_PROPERTY(qreal backgroundOpacity WRITE setBackgroundOpacity READ backgroundOpacity) + Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor) + +public: + FlatButtonDelegate(FlatButton *parent); + ~FlatButtonDelegate(); + + void setBackgroundOpacity(qreal opacity); + qreal backgroundOpacity() const; + + void setBackgroundColor(const QColor &color); + QColor backgroundColor() const; + +private: + Q_DISABLE_COPY(FlatButtonDelegate) + + FlatButton *const button; + qreal _backgroundOpacity; + QColor _backgroundColor; +}; + #endif // FLATBUTTON_INTERNAL_H diff --git a/components/flatbutton_p.h b/components/flatbutton_p.h index f3e20bf..eb311ab 100644 --- a/components/flatbutton_p.h +++ b/components/flatbutton_p.h @@ -1,11 +1,11 @@ #ifndef FLATBUTTON_P_H #define FLATBUTTON_P_H -#include #include "flatbutton.h" #include "lib/rippleoverlay.h" #include "lib/theme.h" #include "lib/style.h" +#include "flatbutton_internal.h" class FlatButtonPrivate { @@ -18,15 +18,16 @@ public: QColor textColor() const; void setTextColor(const QString &themeColor); - FlatButton *const q_ptr; - RippleOverlay *const ripple; - QStateMachine machine; + FlatButton *const q_ptr; + RippleOverlay *const ripple; + FlatButtonDelegate *const delegate; Material::Role role; }; FlatButtonPrivate::FlatButtonPrivate(FlatButton *parent) : q_ptr(parent), ripple(new RippleOverlay(parent)), + delegate(new FlatButtonDelegate(parent)), role(Material::Default) { Style &style = Style::instance(); @@ -45,16 +46,6 @@ FlatButtonPrivate::FlatButtonPrivate(FlatButton *parent) palette.setColor(QPalette::Disabled, QPalette::ButtonText, style.themeColor("disabled")); parent->setPalette(palette); - - QState *normalState = new QState; - QState *focusedState = new QState; - QState *pressedState = new QState; - - machine.addState(normalState); - machine.addState(focusedState); - machine.addState(pressedState); - - machine.setInitialState(normalState); } QColor FlatButtonPrivate::textColor() const diff --git a/components/slider_internal.cpp b/components/slider_internal.cpp index 5a465d1..8678ace 100644 --- a/components/slider_internal.cpp +++ b/components/slider_internal.cpp @@ -40,9 +40,12 @@ SliderStateMachine::SliderStateMachine(Slider *parent, QColor fillColor = style.themeColor("primary1"); - inactiveState->assignProperty(track, "fillColor", style.themeColor("accent2")); - slidingState->assignProperty(track, "fillColor", style.themeColor("accent3")); - focusState->assignProperty(track, "fillColor", style.themeColor("accent3")); + inactiveState->assignProperty(track, "fillColor", + style.themeColor("accent3").lighter(130)); + slidingState->assignProperty(track, "fillColor", + style.themeColor("accent3")); + focusState->assignProperty(track, "fillColor", + style.themeColor("accent3")); addState(topState); diff --git a/examples/flatbuttonexamples.cpp b/examples/flatbuttonexamples.cpp index 23af460..adb405a 100644 --- a/examples/flatbuttonexamples.cpp +++ b/examples/flatbuttonexamples.cpp @@ -15,7 +15,7 @@ FlatButtonExamples::FlatButtonExamples(QWidget *parent) flatButton->setText("Press me!"); flatButton->setMinimumSize(200, 42); - flatButton->setRole(Material::Primary); +// flatButton->setRole(Material::Primary); // flatButton->setDisabled(true); ExampleView *view = new ExampleView;