qt-material-widgets/components/qtmaterialiconbutton.cpp

217 lines
3.9 KiB
C++
Raw Normal View History

2017-09-29 10:48:30 +00:00
#include "qtmaterialiconbutton.h"
#include "qtmaterialiconbutton_p.h"
2017-09-29 09:56:51 +00:00
#include <QPainter>
#include <QEvent>
2017-09-29 10:48:30 +00:00
#include "lib/qtmaterialstyle.h"
#include "lib/qtmaterialrippleoverlay.h"
2017-09-29 09:56:51 +00:00
2022-02-17 01:23:36 +00:00
namespace md
{
2017-09-29 09:56:51 +00:00
/*!
* \class QtMaterialIconButtonPrivate
* \internal
*/
2022-02-17 01:23:36 +00:00
IconButtonPrivate::IconButtonPrivate(IconButton *q)
2017-09-29 09:56:51 +00:00
: q_ptr(q)
{
}
2022-02-17 01:23:36 +00:00
IconButtonPrivate::~IconButtonPrivate()
2017-09-29 09:56:51 +00:00
{
}
2022-02-17 01:23:36 +00:00
void IconButtonPrivate::init()
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_Q(IconButton);
2017-09-29 09:56:51 +00:00
2022-02-17 01:23:36 +00:00
rippleOverlay = new RippleOverlay(q->parentWidget());
2017-09-29 09:56:51 +00:00
useThemeColors = true;
2017-10-01 19:34:55 +00:00
rippleOverlay->installEventFilter(q);
2017-09-29 09:56:51 +00:00
q->setStyle(&QtMaterialStyle::instance());
QSizePolicy policy;
policy.setWidthForHeight(true);
q->setSizePolicy(policy);
}
2022-02-17 01:23:36 +00:00
void IconButtonPrivate::updateRipple()
2017-10-02 13:29:31 +00:00
{
2022-02-17 01:23:36 +00:00
Q_Q(IconButton);
2017-10-02 13:29:31 +00:00
QRect r(q->rect());
r.setSize(QSize(q->width()*2, q->height()*2));
r.moveCenter(q->geometry().center());
rippleOverlay->setGeometry(r);
}
2017-09-29 09:56:51 +00:00
/*!
* \class QtMaterialIconButton
*/
2022-02-17 01:23:36 +00:00
IconButton::IconButton(const QIcon &icon, QWidget *parent)
2017-09-29 09:56:51 +00:00
: QAbstractButton(parent),
2022-02-17 01:23:36 +00:00
d_ptr(new IconButtonPrivate(this))
2017-09-29 09:56:51 +00:00
{
d_func()->init();
setIcon(icon);
}
2022-02-17 01:23:36 +00:00
IconButton::~IconButton()
2017-09-29 09:56:51 +00:00
{
}
/*!
* \reimp
*/
2022-02-17 01:23:36 +00:00
QSize IconButton::sizeHint() const
2017-09-29 09:56:51 +00:00
{
return iconSize();
}
2022-02-17 01:23:36 +00:00
void IconButton::setUseThemeColors(bool value)
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(IconButton);
2017-09-29 09:56:51 +00:00
if (d->useThemeColors == value) {
return;
}
d->useThemeColors = value;
update();
}
2022-02-17 01:23:36 +00:00
bool IconButton::useThemeColors() const
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(const IconButton);
2017-09-29 09:56:51 +00:00
return d->useThemeColors;
}
2022-02-17 01:23:36 +00:00
void IconButton::setColor(const QColor &color)
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(IconButton);
2017-09-29 09:56:51 +00:00
d->color = color;
2017-10-11 20:16:13 +00:00
MATERIAL_DISABLE_THEME_COLORS
2017-09-29 09:56:51 +00:00
update();
}
2022-02-17 01:23:36 +00:00
QColor IconButton::color() const
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(const IconButton);
2017-09-29 09:56:51 +00:00
if (d->useThemeColors || !d->color.isValid()) {
return QtMaterialStyle::instance().themeColor("text");
}
return d->color;
}
2022-02-17 01:23:36 +00:00
void IconButton::setDisabledColor(const QColor &color)
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(IconButton);
2017-09-29 09:56:51 +00:00
d->disabledColor = color;
2017-10-11 20:16:13 +00:00
MATERIAL_DISABLE_THEME_COLORS
2017-09-29 09:56:51 +00:00
update();
}
2022-02-17 01:23:36 +00:00
QColor IconButton::disabledColor() const
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(const IconButton);
2017-09-29 09:56:51 +00:00
if (d->useThemeColors || !d->disabledColor.isValid()) {
return QtMaterialStyle::instance().themeColor("disabled");
}
return d->disabledColor;
}
2022-02-17 01:23:36 +00:00
IconButton::IconButton(IconButtonPrivate &d, QWidget *parent)
2017-09-29 09:56:51 +00:00
: QAbstractButton(parent),
d_ptr(&d)
{
d_func()->init();
}
/*!
* \reimp
*/
2022-02-17 01:23:36 +00:00
bool IconButton::event(QEvent *event)
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(IconButton);
2017-09-29 09:56:51 +00:00
switch (event->type())
{
2017-10-02 13:29:31 +00:00
case QEvent::Move:
case QEvent::Resize:
d->updateRipple();
break;
2017-09-29 09:56:51 +00:00
case QEvent::ParentChange: {
QWidget *widget;
if ((widget = parentWidget())) {
d->rippleOverlay->setParent(widget);
}
break;
}
default:
break;
}
return QAbstractButton::event(event);
}
2017-10-01 19:34:55 +00:00
/*!
* \reimp
*/
2022-02-17 01:23:36 +00:00
bool IconButton::eventFilter(QObject *obj, QEvent *event)
2017-10-01 19:34:55 +00:00
{
if (QEvent::Resize == event->type())
{
2022-02-17 01:23:36 +00:00
Q_D(IconButton);
2017-10-01 19:34:55 +00:00
2017-10-02 13:29:31 +00:00
d->updateRipple();
2017-10-01 19:34:55 +00:00
}
return QAbstractButton::eventFilter(obj, event);
}
2017-09-29 09:56:51 +00:00
/*!
* \reimp
*/
2022-02-17 01:23:36 +00:00
void IconButton::mousePressEvent(QMouseEvent *event)
2017-09-29 09:56:51 +00:00
{
2022-02-17 01:23:36 +00:00
Q_D(IconButton);
2017-09-29 09:56:51 +00:00
d->rippleOverlay->addRipple(QPoint(d->rippleOverlay->width(),
d->rippleOverlay->height())/2,
iconSize().width());
emit clicked();
QAbstractButton::mousePressEvent(event);
}
/*!
* \reimp
*/
2022-02-17 01:23:36 +00:00
void IconButton::paintEvent(QPaintEvent *event)
2017-09-29 09:56:51 +00:00
{
Q_UNUSED(event)
QPainter painter(this);
QPixmap pixmap = icon().pixmap(iconSize());
QPainter icon(&pixmap);
icon.setCompositionMode(QPainter::CompositionMode_SourceIn);
icon.fillRect(pixmap.rect(), isEnabled() ? color() : disabledColor());
QRect r(rect());
const qreal w = pixmap.width();
const qreal h = pixmap.height();
painter.drawPixmap(QRect((r.width()-w)/2, (r.height()-h)/2, w, h), pixmap);
2017-09-29 09:56:51 +00:00
}
2022-02-17 01:23:36 +00:00
}