From 37b4c3c53180711fcacac273db3d38bee43b1c35 Mon Sep 17 00:00:00 2001 From: laserpants Date: Wed, 25 May 2016 00:13:01 +0300 Subject: [PATCH] adjust RaisedButton colors --- components/flatbutton.cpp | 15 ++++----- components/flatbutton.h | 2 +- components/flatbutton_p.h | 6 ++-- components/raisedbutton.cpp | 54 ++++++++++++++++++++++++++----- components/raisedbutton.h | 2 ++ examples/examplelist.cpp | 8 ++--- examples/flatbuttonexamples.cpp | 2 +- examples/frame.cpp | 2 +- examples/raisedbuttonexamples.cpp | 47 +++++++++++++++++++-------- 9 files changed, 98 insertions(+), 40 deletions(-) diff --git a/components/flatbutton.cpp b/components/flatbutton.cpp index 64b14fb..0724c76 100644 --- a/components/flatbutton.cpp +++ b/components/flatbutton.cpp @@ -34,15 +34,14 @@ void FlatButtonPrivate::init() q->setPalette(palette); } -void FlatButtonPrivate::setTextColor(const QString &themeColor) +void FlatButtonPrivate::setPaletteColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QString &themeColor) { Q_Q(FlatButton); QPalette palette(q->palette()); Style &style = Style::instance(); - palette.setColor(QPalette::Active, QPalette::ButtonText, - style.themeColor(themeColor)); + palette.setColor(group, role, style.themeColor(themeColor)); q->setPalette(palette); } @@ -76,18 +75,16 @@ void FlatButton::setRole(Material::Role role) switch (role) { case Material::Primary: - d->setTextColor("primary1"); - d->delegate->assignProperties(); + d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "primary1"); break; case Material::Secondary: - d->setTextColor("accent1"); - d->delegate->assignProperties(); + d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "accent1"); break; default: - d->setTextColor("text"); - d->delegate->assignProperties(); + d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "text"); break; } + d->delegate->assignProperties(); update(); } diff --git a/components/flatbutton.h b/components/flatbutton.h index b9e753f..0de44eb 100644 --- a/components/flatbutton.h +++ b/components/flatbutton.h @@ -16,7 +16,7 @@ public: explicit FlatButton(const QString &text, QWidget *parent = 0); ~FlatButton(); - void setRole(Material::Role role); + virtual void setRole(Material::Role role); protected: FlatButton(FlatButtonPrivate &d, QWidget *parent = 0); diff --git a/components/flatbutton_p.h b/components/flatbutton_p.h index e1df510..ccc7750 100644 --- a/components/flatbutton_p.h +++ b/components/flatbutton_p.h @@ -1,12 +1,14 @@ #ifndef FLATBUTTON_P_H #define FLATBUTTON_P_H -#include "flatbutton.h" +#include #include "lib/rippleoverlay.h" #include "lib/theme.h" #include "lib/style.h" #include "flatbutton_internal.h" +class FlatButton; + class FlatButtonPrivate { Q_DISABLE_COPY(FlatButtonPrivate) @@ -19,7 +21,7 @@ public: {} void init(); - void setTextColor(const QString &themeColor); + void setPaletteColor(QPalette::ColorGroup group, QPalette::ColorRole role, const QString &themeColor); FlatButton *const q_ptr; RippleOverlay *ripple; diff --git a/components/raisedbutton.cpp b/components/raisedbutton.cpp index 7fd3dce..b28929b 100644 --- a/components/raisedbutton.cpp +++ b/components/raisedbutton.cpp @@ -1,10 +1,13 @@ #include "raisedbutton.h" #include #include +#include #include #include #include #include +#include +#include #include "raisedbutton_p.h" void RaisedButtonPrivate::init() @@ -14,7 +17,7 @@ void RaisedButtonPrivate::init() QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect; effect->setBlurRadius(7); effect->setOffset(QPointF(0, 0)); - effect->setColor(QColor(0, 0, 0, 100)); + effect->setColor(QColor(0, 0, 0, 60)); q->setGraphicsEffect(effect); QState *normalState = new QState; @@ -48,6 +51,20 @@ void RaisedButtonPrivate::init() // + transition = new QEventTransition(q, QEvent::MouseButtonDblClick); + transition->setTargetState(pressedState); + + animation = new QPropertyAnimation(effect, "offset"); + animation->setDuration(100); + transition->addAnimation(animation); + animation = new QPropertyAnimation(effect, "blurRadius"); + animation->setDuration(100); + transition->addAnimation(animation); + + normalState->addTransition(transition); + + // + transition = new QEventTransition(q, QEvent::MouseButtonRelease); transition->setTargetState(normalState); @@ -63,10 +80,6 @@ void RaisedButtonPrivate::init() // machine.setInitialState(normalState); - - QObject::connect(effect, SIGNAL(blurRadiusChanged(qreal)), q, SLOT(update())); - QObject::connect(effect, SIGNAL(offsetChanged(QPointF)), q, SLOT(update())); - machine.start(); } @@ -82,17 +95,44 @@ RaisedButton::~RaisedButton() { } +void RaisedButton::setRole(Material::Role role) +{ + Q_D(FlatButton); + + d->role = role; + + switch (role) + { + case Material::Primary: + d->setPaletteColor(QPalette::Active, QPalette::Background, "primary1"); + d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "alternateText"); + break; + case Material::Secondary: + d->setPaletteColor(QPalette::Active, QPalette::Background, "accent1"); + d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "alternateText"); + break; + default: + d->setPaletteColor(QPalette::Active, QPalette::Background, "canvas"); + d->setPaletteColor(QPalette::Active, QPalette::ButtonText, "text"); + break; + } + d->delegate->assignProperties(); + update(); +} + void RaisedButton::paintEvent(QPaintEvent *event) { Q_UNUSED(event) + Q_D(RaisedButton); + QPainter painter(this); painter.setRenderHint(QPainter::Antialiasing); QBrush brush; brush.setStyle(Qt::SolidPattern); - brush.setColor(Qt::white); + brush.setColor(palette().color(QPalette::Active, QPalette::Background)); painter.setBrush(brush); painter.setPen(Qt::NoPen); @@ -105,6 +145,4 @@ void RaisedButton::paintEvent(QPaintEvent *event) option.features |= QStyleOptionButton::Flat; style.drawControl(QStyle::CE_PushButtonLabel, option); - - //FlatButton::paintEvent(event); } diff --git a/components/raisedbutton.h b/components/raisedbutton.h index 3cd94b4..27bf13c 100644 --- a/components/raisedbutton.h +++ b/components/raisedbutton.h @@ -13,6 +13,8 @@ public: explicit RaisedButton(QWidget *parent = 0); ~RaisedButton(); + void setRole(Material::Role role) Q_DECL_OVERRIDE; + protected: void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; diff --git a/examples/examplelist.cpp b/examples/examplelist.cpp index 858cef3..11ff798 100644 --- a/examples/examplelist.cpp +++ b/examples/examplelist.cpp @@ -10,10 +10,10 @@ ExampleList::ExampleList(QWidget *parent) widget->setLayout(layout); - QSizePolicy policy; - policy.setHorizontalPolicy(QSizePolicy::Expanding); - policy.setVerticalPolicy(QSizePolicy::Maximum); - widget->setSizePolicy(policy); + //QSizePolicy policy; + //policy.setHorizontalPolicy(QSizePolicy::Expanding); + //policy.setVerticalPolicy(QSizePolicy::Maximum); + //widget->setSizePolicy(policy); setWidget(widget); setWidgetResizable(true); diff --git a/examples/flatbuttonexamples.cpp b/examples/flatbuttonexamples.cpp index 1de3902..23af460 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::Secondary); + flatButton->setRole(Material::Primary); // flatButton->setDisabled(true); ExampleView *view = new ExampleView; diff --git a/examples/frame.cpp b/examples/frame.cpp index fbd3dd9..40b73fe 100644 --- a/examples/frame.cpp +++ b/examples/frame.cpp @@ -23,7 +23,7 @@ Frame::Frame(QWidget *parent) _layout->setStretch(0, 1); _layout->setStretch(1, 1); - setMinimumHeight(300); + //setMinimumHeight(300); } Frame::~Frame() diff --git a/examples/raisedbuttonexamples.cpp b/examples/raisedbuttonexamples.cpp index bab1fa5..e8592ab 100644 --- a/examples/raisedbuttonexamples.cpp +++ b/examples/raisedbuttonexamples.cpp @@ -9,23 +9,42 @@ RaisedButtonExamples::RaisedButtonExamples(QWidget *parent) { QLayout *layout = widget()->layout(); - { - RaisedButton *raisedButton = new RaisedButton; - raisedButton->setText("Press me!"); - raisedButton->setMinimumSize(200, 42); + RaisedButton *raisedButton = new RaisedButton; + raisedButton->setRole(Material::Primary); + raisedButton->setText("Press me!"); - ExampleView *view = new ExampleView; - view->setWidget(raisedButton); + //raisedButton->setFixedSize(400, 50); - Frame *frame = new Frame; - frame->setCodeSnippet( - "RaisedButton *raisedButton = new RaisedButton;\n" - "raisedButton->setText(\"Press me!\");" - ); - frame->setWidget(view); + layout->addWidget(raisedButton); + //layout->setAlignment(raisedButton, Qt::AlignHCenter); - layout->addWidget(frame); - } + + QPushButton *pb = new QPushButton; + pb->setText("Press me!"); + + layout->addWidget(pb); + + //QLayout *layout = widget()->layout(); + + //{ + // RaisedButton *raisedButton = new RaisedButton; + // raisedButton->setText("Press me!"); + // raisedButton->setMinimumSize(200, 42); + + // raisedButton->setRole(Material::Primary); + + // ExampleView *view = new ExampleView; + // view->setWidget(raisedButton); + + // Frame *frame = new Frame; + // frame->setCodeSnippet( + // "RaisedButton *raisedButton = new RaisedButton;\n" + // "raisedButton->setText(\"Press me!\");" + // ); + // frame->setWidget(view); + + // layout->addWidget(frame); + //} } RaisedButtonExamples::~RaisedButtonExamples()