IconButton => md

This commit is contained in:
Achraf k 2022-02-17 02:23:36 +01:00
parent 8a55c8f1d6
commit 792f3cfa24
3 changed files with 62 additions and 48 deletions

View File

@ -5,25 +5,27 @@
#include "lib/qtmaterialstyle.h" #include "lib/qtmaterialstyle.h"
#include "lib/qtmaterialrippleoverlay.h" #include "lib/qtmaterialrippleoverlay.h"
namespace md
{
/*! /*!
* \class QtMaterialIconButtonPrivate * \class QtMaterialIconButtonPrivate
* \internal * \internal
*/ */
QtMaterialIconButtonPrivate::QtMaterialIconButtonPrivate(QtMaterialIconButton *q) IconButtonPrivate::IconButtonPrivate(IconButton *q)
: q_ptr(q) : q_ptr(q)
{ {
} }
QtMaterialIconButtonPrivate::~QtMaterialIconButtonPrivate() IconButtonPrivate::~IconButtonPrivate()
{ {
} }
void QtMaterialIconButtonPrivate::init() void IconButtonPrivate::init()
{ {
Q_Q(QtMaterialIconButton); Q_Q(IconButton);
rippleOverlay = new QtMaterialRippleOverlay(q->parentWidget()); rippleOverlay = new RippleOverlay(q->parentWidget());
useThemeColors = true; useThemeColors = true;
rippleOverlay->installEventFilter(q); rippleOverlay->installEventFilter(q);
@ -35,9 +37,9 @@ void QtMaterialIconButtonPrivate::init()
q->setSizePolicy(policy); q->setSizePolicy(policy);
} }
void QtMaterialIconButtonPrivate::updateRipple() void IconButtonPrivate::updateRipple()
{ {
Q_Q(QtMaterialIconButton); Q_Q(IconButton);
QRect r(q->rect()); QRect r(q->rect());
r.setSize(QSize(q->width()*2, q->height()*2)); r.setSize(QSize(q->width()*2, q->height()*2));
@ -49,30 +51,30 @@ void QtMaterialIconButtonPrivate::updateRipple()
* \class QtMaterialIconButton * \class QtMaterialIconButton
*/ */
QtMaterialIconButton::QtMaterialIconButton(const QIcon &icon, QWidget *parent) IconButton::IconButton(const QIcon &icon, QWidget *parent)
: QAbstractButton(parent), : QAbstractButton(parent),
d_ptr(new QtMaterialIconButtonPrivate(this)) d_ptr(new IconButtonPrivate(this))
{ {
d_func()->init(); d_func()->init();
setIcon(icon); setIcon(icon);
} }
QtMaterialIconButton::~QtMaterialIconButton() IconButton::~IconButton()
{ {
} }
/*! /*!
* \reimp * \reimp
*/ */
QSize QtMaterialIconButton::sizeHint() const QSize IconButton::sizeHint() const
{ {
return iconSize(); return iconSize();
} }
void QtMaterialIconButton::setUseThemeColors(bool value) void IconButton::setUseThemeColors(bool value)
{ {
Q_D(QtMaterialIconButton); Q_D(IconButton);
if (d->useThemeColors == value) { if (d->useThemeColors == value) {
return; return;
@ -82,16 +84,16 @@ void QtMaterialIconButton::setUseThemeColors(bool value)
update(); update();
} }
bool QtMaterialIconButton::useThemeColors() const bool IconButton::useThemeColors() const
{ {
Q_D(const QtMaterialIconButton); Q_D(const IconButton);
return d->useThemeColors; return d->useThemeColors;
} }
void QtMaterialIconButton::setColor(const QColor &color) void IconButton::setColor(const QColor &color)
{ {
Q_D(QtMaterialIconButton); Q_D(IconButton);
d->color = color; d->color = color;
@ -99,9 +101,9 @@ void QtMaterialIconButton::setColor(const QColor &color)
update(); update();
} }
QColor QtMaterialIconButton::color() const QColor IconButton::color() const
{ {
Q_D(const QtMaterialIconButton); Q_D(const IconButton);
if (d->useThemeColors || !d->color.isValid()) { if (d->useThemeColors || !d->color.isValid()) {
return QtMaterialStyle::instance().themeColor("text"); return QtMaterialStyle::instance().themeColor("text");
@ -109,9 +111,9 @@ QColor QtMaterialIconButton::color() const
return d->color; return d->color;
} }
void QtMaterialIconButton::setDisabledColor(const QColor &color) void IconButton::setDisabledColor(const QColor &color)
{ {
Q_D(QtMaterialIconButton); Q_D(IconButton);
d->disabledColor = color; d->disabledColor = color;
@ -119,9 +121,9 @@ void QtMaterialIconButton::setDisabledColor(const QColor &color)
update(); update();
} }
QColor QtMaterialIconButton::disabledColor() const QColor IconButton::disabledColor() const
{ {
Q_D(const QtMaterialIconButton); Q_D(const IconButton);
if (d->useThemeColors || !d->disabledColor.isValid()) { if (d->useThemeColors || !d->disabledColor.isValid()) {
return QtMaterialStyle::instance().themeColor("disabled"); return QtMaterialStyle::instance().themeColor("disabled");
@ -129,7 +131,7 @@ QColor QtMaterialIconButton::disabledColor() const
return d->disabledColor; return d->disabledColor;
} }
QtMaterialIconButton::QtMaterialIconButton(QtMaterialIconButtonPrivate &d, QWidget *parent) IconButton::IconButton(IconButtonPrivate &d, QWidget *parent)
: QAbstractButton(parent), : QAbstractButton(parent),
d_ptr(&d) d_ptr(&d)
{ {
@ -139,9 +141,9 @@ QtMaterialIconButton::QtMaterialIconButton(QtMaterialIconButtonPrivate &d, QWidg
/*! /*!
* \reimp * \reimp
*/ */
bool QtMaterialIconButton::event(QEvent *event) bool IconButton::event(QEvent *event)
{ {
Q_D(QtMaterialIconButton); Q_D(IconButton);
switch (event->type()) switch (event->type())
{ {
@ -165,11 +167,11 @@ bool QtMaterialIconButton::event(QEvent *event)
/*! /*!
* \reimp * \reimp
*/ */
bool QtMaterialIconButton::eventFilter(QObject *obj, QEvent *event) bool IconButton::eventFilter(QObject *obj, QEvent *event)
{ {
if (QEvent::Resize == event->type()) if (QEvent::Resize == event->type())
{ {
Q_D(QtMaterialIconButton); Q_D(IconButton);
d->updateRipple(); d->updateRipple();
} }
@ -179,9 +181,9 @@ bool QtMaterialIconButton::eventFilter(QObject *obj, QEvent *event)
/*! /*!
* \reimp * \reimp
*/ */
void QtMaterialIconButton::mousePressEvent(QMouseEvent *event) void IconButton::mousePressEvent(QMouseEvent *event)
{ {
Q_D(QtMaterialIconButton); Q_D(IconButton);
d->rippleOverlay->addRipple(QPoint(d->rippleOverlay->width(), d->rippleOverlay->addRipple(QPoint(d->rippleOverlay->width(),
d->rippleOverlay->height())/2, d->rippleOverlay->height())/2,
@ -194,7 +196,7 @@ void QtMaterialIconButton::mousePressEvent(QMouseEvent *event)
/*! /*!
* \reimp * \reimp
*/ */
void QtMaterialIconButton::paintEvent(QPaintEvent *event) void IconButton::paintEvent(QPaintEvent *event)
{ {
Q_UNUSED(event) Q_UNUSED(event)
@ -210,3 +212,5 @@ void QtMaterialIconButton::paintEvent(QPaintEvent *event)
const qreal h = pixmap.height(); const qreal h = pixmap.height();
painter.drawPixmap(QRect((r.width()-w)/2, (r.height()-h)/2, w, h), pixmap); painter.drawPixmap(QRect((r.width()-w)/2, (r.height()-h)/2, w, h), pixmap);
} }
}

View File

@ -3,15 +3,18 @@
#include <QtWidgets/QAbstractButton> #include <QtWidgets/QAbstractButton>
class QtMaterialIconButtonPrivate; namespace md
{
class QtMaterialIconButton : public QAbstractButton class IconButtonPrivate;
class IconButton : public QAbstractButton
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit QtMaterialIconButton(const QIcon &icon, QWidget *parent = 0); explicit IconButton(const QIcon &icon, QWidget *parent = 0);
~QtMaterialIconButton(); ~IconButton();
QSize sizeHint() const Q_DECL_OVERRIDE; QSize sizeHint() const Q_DECL_OVERRIDE;
@ -25,18 +28,20 @@ public:
QColor disabledColor() const; QColor disabledColor() const;
protected: protected:
QtMaterialIconButton(QtMaterialIconButtonPrivate &d, QWidget *parent = 0); IconButton(IconButtonPrivate &d, QWidget *parent = 0);
bool event(QEvent *event) Q_DECL_OVERRIDE; bool event(QEvent *event) Q_DECL_OVERRIDE;
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE; bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE; void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE; void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
const QScopedPointer<QtMaterialIconButtonPrivate> d_ptr; const QScopedPointer<IconButtonPrivate> d_ptr;
private: private:
Q_DISABLE_COPY(QtMaterialIconButton) Q_DISABLE_COPY(IconButton)
Q_DECLARE_PRIVATE(QtMaterialIconButton) Q_DECLARE_PRIVATE(IconButton)
}; };
}
#endif // QTMATERIALICONBUTTON_H #endif // QTMATERIALICONBUTTON_H

View File

@ -3,27 +3,32 @@
#include <QtGlobal> #include <QtGlobal>
class QtMaterialIconButton;
class QtMaterialRippleOverlay; namespace md
{
class IconButton;
class RippleOverlay;
class QColor; class QColor;
class QtMaterialIconButtonPrivate class IconButtonPrivate
{ {
Q_DISABLE_COPY(QtMaterialIconButtonPrivate) Q_DISABLE_COPY(IconButtonPrivate)
Q_DECLARE_PUBLIC(QtMaterialIconButton) Q_DECLARE_PUBLIC(IconButton)
public: public:
QtMaterialIconButtonPrivate(QtMaterialIconButton *q); IconButtonPrivate(IconButton *q);
virtual ~QtMaterialIconButtonPrivate(); virtual ~IconButtonPrivate();
void init(); void init();
void updateRipple(); void updateRipple();
QtMaterialIconButton *const q_ptr; IconButton *const q_ptr;
QtMaterialRippleOverlay *rippleOverlay; RippleOverlay *rippleOverlay;
QColor color; QColor color;
QColor disabledColor; QColor disabledColor;
bool useThemeColors; bool useThemeColors;
}; };
}
#endif // QTMATERIALICONBUTTON_P_H #endif // QTMATERIALICONBUTTON_P_H