From d4ed0ea51a63c1208602fc6f040352e2fa2e63f8 Mon Sep 17 00:00:00 2001 From: laserpants Date: Sun, 15 May 2016 22:51:01 +0300 Subject: [PATCH] align flat button background colors with button role --- components/flatbutton.cpp | 25 +++++++------ components/flatbutton_internal.cpp | 57 +++++++++++++++--------------- components/flatbutton_internal.h | 12 +++++-- examples/flatbuttonexamples.cpp | 2 +- 4 files changed, 53 insertions(+), 43 deletions(-) diff --git a/components/flatbutton.cpp b/components/flatbutton.cpp index 1269bf7..0766754 100644 --- a/components/flatbutton.cpp +++ b/components/flatbutton.cpp @@ -37,12 +37,15 @@ void FlatButton::setRole(Material::Role role) { case Material::Primary: d->setTextColor("primary1"); + d->delegate->assignProperties(); break; case Material::Secondary: d->setTextColor("accent1"); + d->delegate->assignProperties(); break; default: d->setTextColor("text"); + d->delegate->assignProperties(); break; } update(); @@ -65,14 +68,6 @@ void FlatButton::paintEvent(QPaintEvent *event) Q_D(FlatButton); - QStylePainter painter(this); - - QStyleOptionButton option; - initStyleOption(&option); - option.features |= QStyleOptionButton::Flat; - - painter.drawControl(QStyle::CE_PushButtonLabel, option); - const qreal bgOpacity = d->delegate->backgroundOpacity(); if (isEnabled() && bgOpacity > 0) @@ -87,6 +82,14 @@ void FlatButton::paintEvent(QPaintEvent *event) painter.drawRoundedRect(rect(), 3, 3); } + QStylePainter painter(this); + + QStyleOptionButton option; + initStyleOption(&option); + option.features |= QStyleOptionButton::Flat; + + painter.drawControl(QStyle::CE_PushButtonLabel, option); + #ifdef DEBUG_LAYOUT QPainter debug(this); QPen pen; @@ -101,12 +104,12 @@ void FlatButton::mousePressEvent(QMouseEvent *event) { Q_D(FlatButton); - Style &style = Style::instance(); + QColor color = palette().color(QPalette::Active, QPalette::ButtonText); Ripple *ripple = new Ripple(event->pos()); ripple->setRadiusEndValue(100); - ripple->setOpacityStartValue(0.3); - ripple->setColor(style.themeColor("text")); + ripple->setOpacityStartValue(0.4); + ripple->setColor(color); d->ripple->addRipple(ripple); diff --git a/components/flatbutton_internal.cpp b/components/flatbutton_internal.cpp index 3c40df6..c9a9ed4 100644 --- a/components/flatbutton_internal.cpp +++ b/components/flatbutton_internal.cpp @@ -8,28 +8,14 @@ FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent) : QStateMachine(parent), - button(parent) + button(parent), + _normalState(new QState(this)), + _focusedState(new QState(this)), + _pressedState(new QState(this)) { - Style &style = Style::instance(); + setInitialState(_normalState); - 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")); + assignProperties(); QAbstractTransition *transition; QPropertyAnimation *animation; @@ -37,42 +23,42 @@ FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent) // transition = new QEventTransition(button, QEvent::Enter); - transition->setTargetState(focusedState); + transition->setTargetState(_focusedState); animation = new QPropertyAnimation(this, "backgroundOpacity"); animation->setDuration(140); transition->addAnimation(animation); - normalState->addTransition(transition); + _normalState->addTransition(transition); // transition = new QEventTransition(button, QEvent::Leave); - transition->setTargetState(normalState); + transition->setTargetState(_normalState); animation = new QPropertyAnimation(this, "backgroundOpacity"); animation->setDuration(140); transition->addAnimation(animation); - focusedState->addTransition(transition); + _focusedState->addTransition(transition); // transition = new QEventTransition(button, QEvent::MouseButtonPress); - transition->setTargetState(pressedState); + transition->setTargetState(_pressedState); animation = new QPropertyAnimation(this, "backgroundOpacity"); animation->setDuration(140); transition->addAnimation(animation); - focusedState->addTransition(transition); + _focusedState->addTransition(transition); // transition = new QEventTransition(button, QEvent::MouseButtonRelease); - transition->setTargetState(focusedState); + transition->setTargetState(_focusedState); animation = new QPropertyAnimation(this, "backgroundOpacity"); animation->setDuration(500); transition->addAnimation(animation); - pressedState->addTransition(transition); + _pressedState->addTransition(transition); // @@ -104,3 +90,18 @@ QColor FlatButtonDelegate::backgroundColor() const { return _backgroundColor; } + +void FlatButtonDelegate::assignProperties() +{ + QColor textColor = button->palette().color(QPalette::Active, + QPalette::ButtonText); + + _normalState->assignProperty(this, "backgroundOpacity", 0); + _normalState->assignProperty(this, "backgroundColor", textColor); + + _focusedState->assignProperty(this, "backgroundOpacity", 0.15); + _focusedState->assignProperty(this, "backgroundColor", textColor); + + _pressedState->assignProperty(this, "backgroundOpacity", 0.15); + _pressedState->assignProperty(this, "backgroundColor", textColor); +} diff --git a/components/flatbutton_internal.h b/components/flatbutton_internal.h index 513eeea..ec74478 100644 --- a/components/flatbutton_internal.h +++ b/components/flatbutton_internal.h @@ -23,12 +23,18 @@ public: void setBackgroundColor(const QColor &color); QColor backgroundColor() const; + void assignProperties(); + private: Q_DISABLE_COPY(FlatButtonDelegate) - FlatButton *const button; - qreal _backgroundOpacity; - QColor _backgroundColor; + FlatButton *const button; + QState *const _normalState; + QState *const _focusedState; + QState *const _pressedState; + qreal _backgroundOpacity; + QColor _backgroundColor; + }; #endif // FLATBUTTON_INTERNAL_H diff --git a/examples/flatbuttonexamples.cpp b/examples/flatbuttonexamples.cpp index adb405a..1de3902 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::Secondary); // flatButton->setDisabled(true); ExampleView *view = new ExampleView;