fix pimpl inheritance for Raised Button class

This commit is contained in:
FarmRadio Hangar 2016-05-17 15:26:50 +03:00
parent c2b1b33f35
commit 0f4020d193
9 changed files with 194 additions and 125 deletions

View File

@ -10,16 +10,57 @@
#include "flatbutton_p.h" #include "flatbutton_p.h"
void FlatButtonPrivate::init()
{
Q_Q(FlatButton);
ripple = new RippleOverlay(q);
delegate = new FlatButtonDelegate(q);
Style &style = Style::instance();
q->setStyle(&style);
q->setAttribute(Qt::WA_Hover);
q->setMouseTracking(true);
QFont font(q->font());
font.setCapitalization(QFont::AllUppercase);
font.setPointSizeF(10.5);
font.setStyleName("Medium");
q->setFont(font);
QPalette palette;
palette.setColor(QPalette::Disabled, QPalette::ButtonText,
style.themeColor("disabled"));
q->setPalette(palette);
}
void FlatButtonPrivate::setTextColor(const QString &themeColor)
{
Q_Q(FlatButton);
QPalette palette(q->palette());
Style &style = Style::instance();
palette.setColor(QPalette::Active, QPalette::ButtonText,
style.themeColor(themeColor));
q->setPalette(palette);
}
FlatButton::FlatButton(QWidget *parent) FlatButton::FlatButton(QWidget *parent)
: QPushButton(parent), : QPushButton(parent),
d_ptr(new FlatButtonPrivate(this)) d_ptr(new FlatButtonPrivate(this))
{ {
d_func()->init();
} }
FlatButton::FlatButton(const QString &text, QWidget *parent) FlatButton::FlatButton(const QString &text, QWidget *parent)
: QPushButton(parent), : QPushButton(parent),
d_ptr(new FlatButtonPrivate(this)) d_ptr(new FlatButtonPrivate(this))
{ {
d_func()->init();
setText(text); setText(text);
} }
@ -51,6 +92,13 @@ void FlatButton::setRole(Material::Role role)
update(); update();
} }
FlatButton::FlatButton(FlatButtonPrivate &d, QWidget *parent)
: QPushButton(parent),
d_ptr(&d)
{
d_func()->init();
}
void FlatButton::resizeEvent(QResizeEvent *event) void FlatButton::resizeEvent(QResizeEvent *event)
{ {
Q_D(FlatButton); Q_D(FlatButton);
@ -115,3 +163,4 @@ void FlatButton::mousePressEvent(QMouseEvent *event)
QPushButton::mousePressEvent(event); QPushButton::mousePressEvent(event);
} }

View File

@ -19,6 +19,8 @@ public:
void setRole(Material::Role role); void setRole(Material::Role role);
protected: protected:
FlatButton(FlatButtonPrivate &d, QWidget *parent = 0);
void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE; void resizeEvent(QResizeEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;

View File

@ -13,51 +13,19 @@ class FlatButtonPrivate
Q_DECLARE_PUBLIC(FlatButton) Q_DECLARE_PUBLIC(FlatButton)
public: public:
FlatButtonPrivate(FlatButton *parent); FlatButtonPrivate(FlatButton *q)
: q_ptr(q),
role(Material::Default)
{
}
void init();
void setTextColor(const QString &themeColor); void setTextColor(const QString &themeColor);
FlatButton *const q_ptr; FlatButton *const q_ptr;
RippleOverlay *const ripple; RippleOverlay *ripple;
FlatButtonDelegate *const delegate; FlatButtonDelegate *delegate;
Material::Role role; Material::Role role;
}; };
FlatButtonPrivate::FlatButtonPrivate(FlatButton *parent)
: q_ptr(parent),
ripple(new RippleOverlay(parent)),
delegate(new FlatButtonDelegate(parent)),
role(Material::Default)
{
Style &style = Style::instance();
parent->setStyle(&style);
parent->setAttribute(Qt::WA_Hover);
parent->setMouseTracking(true);
QFont font(parent->font());
font.setCapitalization(QFont::AllUppercase);
font.setPointSizeF(10.5);
font.setStyleName("Medium");
parent->setFont(font);
QPalette palette;
palette.setColor(QPalette::Disabled, QPalette::ButtonText,
style.themeColor("disabled"));
parent->setPalette(palette);
}
void FlatButtonPrivate::setTextColor(const QString &themeColor)
{
Q_Q(FlatButton);
QPalette palette(q->palette());
Style &style = Style::instance();
palette.setColor(QPalette::Active, QPalette::ButtonText,
style.themeColor(themeColor));
q->setPalette(palette);
}
#endif // FLATBUTTON_P_H #endif // FLATBUTTON_P_H

View File

@ -1,67 +1,81 @@
#include <QDebug>
#include <QPropertyAnimation>
#include <QGraphicsDropShadowEffect>
#include <QStylePainter>
#include <QPaintEvent>
#include "raisedbutton.h" #include "raisedbutton.h"
#include "raisedbutton_p.h"
RaisedButton::RaisedButton(QWidget *parent) RaisedButton::RaisedButton(QWidget *parent)
: FlatButton(parent) : FlatButton(*new RaisedButtonPrivate(this), parent)
{ {
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
effect->setBlurRadius(7);
effect->setOffset(QPoint(0, 0));
effect->setColor(QColor(0, 0, 0, 100));
setGraphicsEffect(effect);
setAutoFillBackground(true);
QPropertyAnimation *animation;
animation = new QPropertyAnimation;
animation->setTargetObject(effect);
animation->setPropertyName("offset");
animation->setStartValue(QPoint(0, 6));
animation->setEndValue(QPoint(0, 0));
animation->setDuration(100);
_group.addAnimation(animation);
animation = new QPropertyAnimation;
animation->setTargetObject(effect);
animation->setPropertyName("blurRadius");
animation->setStartValue(20);
animation->setEndValue(7);
animation->setDuration(100);
_group.addAnimation(animation);
connect(animation, SIGNAL(valueChanged(QVariant)), this, SLOT(update()));
} }
RaisedButton::~RaisedButton() RaisedButton::~RaisedButton()
{ {
} }
void RaisedButton::mousePressEvent(QMouseEvent *event) //#include <QDebug>
{ //#include <QPropertyAnimation>
_group.setDirection(QAbstractAnimation::Backward); //#include <QGraphicsDropShadowEffect>
_group.start(); //#include <QStylePainter>
//#include <QPaintEvent>
FlatButton::mousePressEvent(event); //#include "raisedbutton.h"
} //
//RaisedButton::RaisedButton(QWidget *parent)
void RaisedButton::mouseReleaseEvent(QMouseEvent *event) // : FlatButton(parent)
{ //{
_group.setDirection(QAbstractAnimation::Forward); // QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
_group.start(); // effect->setBlurRadius(7);
// effect->setOffset(QPoint(0, 0));
FlatButton::mouseReleaseEvent(event); // effect->setColor(QColor(0, 0, 0, 100));
} // setGraphicsEffect(effect);
//
void RaisedButton::paintEvent(QPaintEvent *event) // setAutoFillBackground(true);
{ //
Q_UNUSED(event) // QPropertyAnimation *animation;
//
QStylePainter painter(this); // animation = new QPropertyAnimation;
// animation->setTargetObject(effect);
// painter.drawControl(QStyle::CE_PushButton, getStyleOption()); // animation->setPropertyName("offset");
} // animation->setStartValue(QPoint(0, 6));
// animation->setEndValue(QPoint(0, 0));
// animation->setDuration(100);
// _group.addAnimation(animation);
//
// animation = new QPropertyAnimation;
// animation->setTargetObject(effect);
// animation->setPropertyName("blurRadius");
// animation->setStartValue(20);
// animation->setEndValue(7);
// animation->setDuration(100);
// _group.addAnimation(animation);
//
// connect(animation, SIGNAL(valueChanged(QVariant)), this, SLOT(update()));
//}
//
//RaisedButton::~RaisedButton()
//{
//}
//
//void RaisedButton::mousePressEvent(QMouseEvent *event)
//{
// _group.setDirection(QAbstractAnimation::Backward);
// _group.start();
//
// FlatButton::mousePressEvent(event);
//}
//
//void RaisedButton::mouseReleaseEvent(QMouseEvent *event)
//{
// _group.setDirection(QAbstractAnimation::Forward);
// _group.start();
//
// FlatButton::mouseReleaseEvent(event);
//}
//
//void RaisedButton::paintEvent(QPaintEvent *event)
//{
// Q_UNUSED(event)
//
// QStylePainter painter(this);
//
//// painter.drawControl(QStyle::CE_PushButton, getStyleOption());
//}
//

View File

@ -1,11 +1,8 @@
#ifndef RAISEDBUTTON_H #ifndef RAISEDBUTTON_H
#define RAISEDBUTTON_H #define RAISEDBUTTON_H
#include <QParallelAnimationGroup>
#include "flatbutton.h" #include "flatbutton.h"
class QPropertyAnimation;
class RaisedButton : public FlatButton class RaisedButton : public FlatButton
{ {
Q_OBJECT Q_OBJECT
@ -13,14 +10,34 @@ class RaisedButton : public FlatButton
public: public:
explicit RaisedButton(QWidget *parent = 0); explicit RaisedButton(QWidget *parent = 0);
~RaisedButton(); ~RaisedButton();
protected:
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
private:
QParallelAnimationGroup _group;
}; };
#endif // RAISEDBUTTON_H #endif // RAISEDBUTTON_H
//#ifndef RAISEDBUTTON_H
//#define RAISEDBUTTON_H
//
//#include <QParallelAnimationGroup>
//#include "flatbutton.h"
//
//class QPropertyAnimation;
//
//class RaisedButton : public FlatButton
//{
// Q_OBJECT
//
//public:
// explicit RaisedButton(QWidget *parent = 0);
// ~RaisedButton();
//
//protected:
// void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
// void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
// void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
//
//private:
// QParallelAnimationGroup _group;
//};
//
//#endif // RAISEDBUTTON_H
//

View File

@ -0,0 +1,18 @@
#ifndef RAISEDBUTTON_P_H
#define RAISEDBUTTON_P_H
#include "flatbutton_p.h"
class RaisedButtonPrivate : public FlatButtonPrivate
{
Q_DISABLE_COPY(RaisedButtonPrivate)
Q_DECLARE_PUBLIC(RaisedButton)
public:
RaisedButtonPrivate(RaisedButton *q)
: FlatButtonPrivate(q)
{
}
};
#endif // RAISEDBUTTON_P_H

View File

@ -38,29 +38,29 @@ public:
int trackWidth; int trackWidth;
}; };
SliderPrivate::SliderPrivate(Slider *parent) SliderPrivate::SliderPrivate(Slider *q)
: q_ptr(parent), : q_ptr(q),
thumb(new SliderThumb(parent)), thumb(new SliderThumb(q)),
track(new SliderTrack(parent)), track(new SliderTrack(q)),
machine(new SliderStateMachine(parent, thumb, track)), machine(new SliderStateMachine(q, thumb, track)),
hoverTrack(false), hoverTrack(false),
hoverThumb(false), hoverThumb(false),
hover(false), hover(false),
step(false), step(false),
pageStepMode(true), pageStepMode(true),
stepTo(0), stepTo(0),
oldValue(parent->value()), oldValue(q->value()),
trackWidth(2) trackWidth(2)
{ {
parent->setMouseTracking(true); q->setMouseTracking(true);
parent->setFocusPolicy(Qt::StrongFocus); q->setFocusPolicy(Qt::StrongFocus);
parent->setPageStep(1); q->setPageStep(1);
QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed); QSizePolicy sp(QSizePolicy::Expanding, QSizePolicy::Fixed);
if (parent->orientation() == Qt::Vertical) if (q->orientation() == Qt::Vertical)
sp.transpose(); sp.transpose();
parent->setSizePolicy(sp); q->setSizePolicy(sp);
parent->setAttribute(Qt::WA_WState_OwnSizePolicy, false); q->setAttribute(Qt::WA_WState_OwnSizePolicy, false);
machine->start(); machine->start();

View File

@ -9,7 +9,7 @@ class ThemePrivate
Q_DECLARE_PUBLIC(Theme) Q_DECLARE_PUBLIC(Theme)
public: public:
ThemePrivate(Theme *parent); ThemePrivate(Theme *q);
QColor rgba(int r, int g, int b, qreal a) const; QColor rgba(int r, int g, int b, qreal a) const;
@ -18,8 +18,8 @@ public:
QHash<QString, QColor> colors; QHash<QString, QColor> colors;
}; };
ThemePrivate::ThemePrivate(Theme *parent) ThemePrivate::ThemePrivate(Theme *q)
: q_ptr(parent) : q_ptr(q)
{ {
} }

View File

@ -105,7 +105,8 @@ HEADERS += mainwindow.h \
lib/theme_p.h \ lib/theme_p.h \
components/flatbutton_p.h \ components/flatbutton_p.h \
components/slider_internal.h \ components/slider_internal.h \
components/flatbutton_internal.h components/flatbutton_internal.h \
components/raisedbutton_p.h
RESOURCES += \ RESOURCES += \
resources.qrc resources.qrc