From b450e57b3bd60d9b334615a644a0efeedc709720 Mon Sep 17 00:00:00 2001 From: laserpants Date: Sat, 11 Jun 2016 19:44:16 +0300 Subject: [PATCH] add showHalo property to Flat Button --- components/flatbutton.cpp | 53 ++++++++++++++++++++++-------- components/flatbutton.h | 5 +++ components/flatbutton_internal.cpp | 8 +++-- components/flatbutton_p.h | 1 + components/tabs.cpp | 26 ++++++++++++++- components/tabs.h | 3 ++ components/tabs_internal.cpp | 12 ++++--- components/tabs_p.h | 1 + 8 files changed, 88 insertions(+), 21 deletions(-) diff --git a/components/flatbutton.cpp b/components/flatbutton.cpp index 9079cb6..431f4bc 100644 --- a/components/flatbutton.cpp +++ b/components/flatbutton.cpp @@ -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(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(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); + } +} diff --git a/components/flatbutton.h b/components/flatbutton.h index 46664ae..a223eaa 100644 --- a/components/flatbutton.h +++ b/components/flatbutton.h @@ -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 d_ptr; private: diff --git a/components/flatbutton_internal.cpp b/components/flatbutton_internal.cpp index 6357e5e..350c076 100644 --- a/components/flatbutton_internal.cpp +++ b/components/flatbutton_internal.cpp @@ -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); diff --git a/components/flatbutton_p.h b/components/flatbutton_p.h index cc93b53..7759bd9 100644 --- a/components/flatbutton_p.h +++ b/components/flatbutton_p.h @@ -31,6 +31,7 @@ public: QColor backgroundColor; QColor disabledTextColor; bool useThemeColors; + bool showHalo; qreal peakOpacity; }; diff --git a/components/tabs.cpp b/components/tabs.cpp index 9461b11..7b3a0e1 100644 --- a/components/tabs.cpp +++ b/components/tabs.cpp @@ -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(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); diff --git a/components/tabs.h b/components/tabs.h index 566625a..7f5b0d6 100644 --- a/components/tabs.h +++ b/components/tabs.h @@ -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; diff --git a/components/tabs_internal.cpp b/components/tabs_internal.cpp index 3e82457..58c0320 100644 --- a/components/tabs_internal.cpp +++ b/components/tabs_internal.cpp @@ -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()) { diff --git a/components/tabs_p.h b/components/tabs_p.h index 0d5b9fc..3b24c08 100644 --- a/components/tabs_p.h +++ b/components/tabs_p.h @@ -26,6 +26,7 @@ public: QColor textColor; int tab; bool useThemeColors; + bool showHalo; }; #endif // TABS_P_H