fix raised button focus halo animation

This commit is contained in:
laserpants 2016-05-28 15:59:44 +03:00
parent 921bc2aff4
commit 7a70648dda
4 changed files with 31 additions and 21 deletions

View File

@ -123,7 +123,7 @@ void FlatButton::paintEvent(QPaintEvent *event)
const qreal bgOpacity = d->delegate->backgroundOpacity();
const qreal haloOpacity = d->delegate->focusHaloOpacity();
const int hs = d->delegate->focusHaloSize();
const int hs = (width()/2)*d->delegate->focusHaloSize();
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);

View File

@ -15,7 +15,7 @@ FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent)
_hoveredState(new QState(this)),
_hoveredFocusedState(new QState(this)),
_pressedState(new QState(this)),
_focusHaloSize(85)
_focusHaloSize(0.8)
{
setInitialState(_normalState);
@ -37,15 +37,15 @@ FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent)
grow->setTargetObject(this);
grow->setPropertyName("focusHaloSize");
grow->setStartValue(85);
grow->setEndValue(100);
grow->setStartValue(0.8);
grow->setEndValue(0.9);
grow->setEasingCurve(QEasingCurve::InOutSine);
grow->setDuration(840);
shrink->setTargetObject(this);
shrink->setPropertyName("focusHaloSize");
shrink->setStartValue(100);
shrink->setEndValue(85);
shrink->setStartValue(0.9);
shrink->setEndValue(0.8);
shrink->setEasingCurve(QEasingCurve::InOutSine);
shrink->setDuration(840);
@ -94,13 +94,13 @@ qreal FlatButtonDelegate::focusHaloOpacity() const
return _focusHaloOpacity;
}
void FlatButtonDelegate::setFocusHaloSize(int size)
void FlatButtonDelegate::setFocusHaloSize(qreal size)
{
_focusHaloSize = size;
button->update();
}
int FlatButtonDelegate::focusHaloSize() const
qreal FlatButtonDelegate::focusHaloSize() const
{
return _focusHaloSize;
}
@ -116,7 +116,7 @@ void FlatButtonDelegate::assignProperties()
_normalFocusedState->assignProperty(this, "backgroundOpacity", 0);
_normalFocusedState->assignProperty(this, "backgroundColor", textColor);
_normalFocusedState->assignProperty(this, "focusHaloOpacity", 0.12);
_normalFocusedState->assignProperty(this, "focusHaloOpacity", 0.15);
_hoveredState->assignProperty(this, "backgroundOpacity", 0.15);
_hoveredState->assignProperty(this, "backgroundColor", textColor);
@ -124,7 +124,7 @@ void FlatButtonDelegate::assignProperties()
_hoveredFocusedState->assignProperty(this, "backgroundOpacity", 0.15);
_hoveredFocusedState->assignProperty(this, "backgroundColor", textColor);
_normalFocusedState->assignProperty(this, "focusHaloOpacity", 0.12);
_normalFocusedState->assignProperty(this, "focusHaloOpacity", 0.15);
_pressedState->assignProperty(this, "backgroundOpacity", 0.15);
_pressedState->assignProperty(this, "backgroundColor", textColor);

View File

@ -14,7 +14,7 @@ class FlatButtonDelegate : public QStateMachine
Q_PROPERTY(QColor backgroundColor WRITE setBackgroundColor READ backgroundColor)
Q_PROPERTY(qreal backgroundOpacity WRITE setBackgroundOpacity READ backgroundOpacity)
Q_PROPERTY(qreal focusHaloOpacity WRITE setFocusHaloOpacity READ focusHaloOpacity)
Q_PROPERTY(int focusHaloSize WRITE setFocusHaloSize READ focusHaloSize)
Q_PROPERTY(qreal focusHaloSize WRITE setFocusHaloSize READ focusHaloSize)
public:
FlatButtonDelegate(FlatButton *parent);
@ -29,8 +29,8 @@ public:
void setFocusHaloOpacity(qreal opacity);
qreal focusHaloOpacity() const;
void setFocusHaloSize(int size);
int focusHaloSize() const;
void setFocusHaloSize(qreal size);
qreal focusHaloSize() const;
void assignProperties();
@ -47,7 +47,7 @@ private:
QState *const _pressedState;
qreal _backgroundOpacity;
qreal _focusHaloOpacity;
int _focusHaloSize;
qreal _focusHaloSize;
QColor _backgroundColor;
};

View File

@ -131,21 +131,31 @@ void RaisedButton::paintEvent(QPaintEvent *event)
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
painter.save();
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(palette().color(QPalette::Active, QPalette::Background));
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.drawRoundedRect(rect(), 3, 3);
FlatButton::paintEvent(event);
const int hs = (width()/2)*d->delegate->focusHaloSize();
const qreal haloOpacity = d->delegate->focusHaloOpacity();
//QStylePainter style(this);
brush.setColor(palette().color(QPalette::Active, QPalette::ButtonText));
painter.setBrush(brush);
painter.setOpacity(haloOpacity);
painter.setPen(Qt::NoPen);
painter.drawEllipse(rect().center(), hs, hs);
//QStyleOptionButton option;
//initStyleOption(&option);
//option.features |= QStyleOptionButton::Flat;
painter.restore();
//style.drawControl(QStyle::CE_PushButtonLabel, option);
QStylePainter style(this);
QStyleOptionButton option;
initStyleOption(&option);
option.features |= QStyleOptionButton::Flat;
style.drawControl(QStyle::CE_PushButtonLabel, option);
}