add showHalo property to Flat Button

This commit is contained in:
laserpants 2016-06-11 19:44:16 +03:00
parent 9ddb4d6fb0
commit b450e57b3b
8 changed files with 88 additions and 21 deletions

View File

@ -15,6 +15,7 @@ FlatButtonPrivate::FlatButtonPrivate(FlatButton *q)
cornerRadius(3),
bgMode(Qt::TransparentMode),
useThemeColors(true),
showHalo(true),
peakOpacity(0.15)
{
}
@ -234,6 +235,21 @@ bool FlatButton::useThemeColors() const
return d->useThemeColors;
}
void FlatButton::setShowHalo(bool state)
{
Q_D(FlatButton);
d->showHalo = state;
update();
}
bool FlatButton::showHalo() const
{
Q_D(const FlatButton);
return d->showHalo;
}
FlatButton::FlatButton(FlatButtonPrivate &d, QWidget *parent)
: QPushButton(parent),
d_ptr(&d)
@ -259,9 +275,6 @@ void FlatButton::paintEvent(QPaintEvent *event)
Q_D(FlatButton);
const qreal bgOpacity = d->delegate->backgroundOpacity();
const qreal haloOpacity = d->delegate->haloOpacity();
const qreal s = d->delegate->haloScaleFactor()*d->delegate->haloSize()*0.7;
const qreal hs = static_cast<qreal>(width())*s;
const qreal cr = d->cornerRadius;
QPainter painter(this);
@ -287,16 +300,7 @@ void FlatButton::paintEvent(QPaintEvent *event)
painter.drawRoundedRect(rect(), cr, cr);
}
if (isEnabled() && haloOpacity > 0) {
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(palette().color(QPalette::Active, QPalette::ButtonText));
painter.setOpacity(haloOpacity);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
QPointF center = rect().center();
painter.drawEllipse(center, hs, hs);
}
paintHalo(&painter);
QStylePainter style(this);
@ -337,3 +341,26 @@ void FlatButton::mousePressEvent(QMouseEvent *event)
QPushButton::mousePressEvent(event);
}
void FlatButton::paintHalo(QPainter *painter)
{
Q_D(FlatButton);
if (!d->showHalo)
return;
const qreal haloOpacity = d->delegate->haloOpacity();
const qreal s = d->delegate->haloScaleFactor()*d->delegate->haloSize()*0.7;
const qreal hs = static_cast<qreal>(width())*s;
if (isEnabled() && haloOpacity > 0) {
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(palette().color(QPalette::Active, QPalette::ButtonText));
painter->setOpacity(haloOpacity);
painter->setBrush(brush);
painter->setPen(Qt::NoPen);
QPointF center = rect().center();
painter->drawEllipse(center, hs, hs);
}
}

View File

@ -31,6 +31,9 @@ public:
void setUseThemeColors(bool value);
bool useThemeColors() const;
void setShowHalo(bool state);
bool showHalo() const;
void setRippleStyle(Material::RippleStyle style);
Material::RippleStyle rippleStyle() const;
@ -59,6 +62,8 @@ protected:
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintHalo(QPainter *painter);
const QScopedPointer<FlatButtonPrivate> d_ptr;
private:

View File

@ -44,9 +44,10 @@ FlatButtonDelegate::FlatButtonDelegate(FlatButton *parent)
addTransition(button, QEvent::Leave, _pressedState, _normalFocusedState);
addTransition(button, QEvent::FocusOut, _pressedState, _hoveredState);
_normalState->assignProperty(this, "haloSize", 0);
_normalFocusedState->assignProperty(this, "haloSize", 0.7);
_normalState->assignProperty(this, "haloSize", 1);
_hoveredState->assignProperty(this, "haloSize", 1);
_hoveredState->assignProperty(this, "haloSize", 0);
_pressedState->assignProperty(this, "haloSize", 4);
_hoveredFocusedState->assignProperty(this, "haloSize", 0.7);
//
@ -213,11 +214,12 @@ void FlatButtonDelegate::addTransition(QAbstractTransition *transition,
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "haloOpacity");
animation->setDuration(350);
animation->setDuration(170);
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "haloSize");
animation->setDuration(350);
animation->setEasingCurve(QEasingCurve::OutCubic);
transition->addAnimation(animation);
fromState->addTransition(transition);

View File

@ -31,6 +31,7 @@ public:
QColor backgroundColor;
QColor disabledTextColor;
bool useThemeColors;
bool showHalo;
qreal peakOpacity;
};

View File

@ -10,7 +10,8 @@
TabsPrivate::TabsPrivate(Tabs *q)
: q_ptr(q),
tab(-1),
useThemeColors(true)
useThemeColors(true),
showHalo(false)
{
}
@ -51,6 +52,28 @@ bool Tabs::useThemeColors() const
return d->useThemeColors;
}
void Tabs::setShowHalo(bool state)
{
Q_D(Tabs);
d->showHalo = state;
Tab *tab;
for (int i = 0; i < d->tabLayout->count(); ++i) {
QLayoutItem *item = d->tabLayout->itemAt(i);
if ((tab = static_cast<Tab *>(item->widget()))) {
tab->setShowHalo(state);
}
}
}
bool Tabs::showHalo() const
{
Q_D(const Tabs);
return d->showHalo;
}
void Tabs::setInkColor(const QColor &color)
{
Q_D(Tabs);
@ -212,6 +235,7 @@ Tab *Tabs::createTab(const QString &text)
tab->setRole(Material::Primary);
tab->setBackgroundMode(Qt::OpaqueMode);
tab->setPeakOpacity(0.25);
tab->setShowHalo(d->showHalo);
d->tabLayout->addWidget(tab);

View File

@ -22,6 +22,9 @@ public:
void setUseThemeColors(bool value);
bool useThemeColors() const;
void setShowHalo(bool state);
bool showHalo() const;
void setInkColor(const QColor &color);
QColor inkColor() const;

View File

@ -87,13 +87,17 @@ void TabsInkBar::paintEvent(QPaintEvent *event)
}
Tab::Tab(QWidget *parent)
: FlatButton(parent)
: FlatButton(parent),
_active(false)
// _showHalo(true)
{
init();
}
Tab::Tab(QString text, QWidget *parent)
: FlatButton(parent)
: FlatButton(parent),
_active(false)
// _showHalo(true)
{
init();
@ -117,8 +121,6 @@ void Tab::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
//FlatButton::paintEvent(event);
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing);
@ -130,6 +132,8 @@ void Tab::paintEvent(QPaintEvent *event)
painter.setPen(Qt::NoPen);
painter.drawRect(rect());
paintHalo(&painter);
QStylePainter style(this);
if (!icon().isNull()) {

View File

@ -26,6 +26,7 @@ public:
QColor textColor;
int tab;
bool useThemeColors;
bool showHalo;
};
#endif // TABS_P_H