align flat button background colors with button role

This commit is contained in:
laserpants 2016-05-15 22:51:01 +03:00
parent 2870ca75a3
commit d4ed0ea51a
4 changed files with 53 additions and 43 deletions

View File

@ -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);

View File

@ -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);
}

View File

@ -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

View File

@ -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;