123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- #include "qtmaterialiconbutton.h"
- #include "qtmaterialiconbutton_p.h"
- #include <QPainter>
- #include <QEvent>
- #include "lib/qtmaterialstyle.h"
- #include "lib/qtmaterialrippleoverlay.h"
- /*!
- * \class QtMaterialIconButtonPrivate
- * \internal
- */
- QtMaterialIconButtonPrivate::QtMaterialIconButtonPrivate(QtMaterialIconButton *q)
- : q_ptr(q)
- {
- }
- QtMaterialIconButtonPrivate::~QtMaterialIconButtonPrivate()
- {
- }
- void QtMaterialIconButtonPrivate::init()
- {
- Q_Q(QtMaterialIconButton);
- rippleOverlay = new QtMaterialRippleOverlay(q->parentWidget());
- useThemeColors = true;
- rippleOverlay->installEventFilter(q);
- q->setStyle(&QtMaterialStyle::instance());
- QSizePolicy policy;
- policy.setWidthForHeight(true);
- q->setSizePolicy(policy);
- }
- void QtMaterialIconButtonPrivate::updateRipple()
- {
- Q_Q(QtMaterialIconButton);
- QRect r(q->rect());
- r.setSize(QSize(q->width()*2, q->height()*2));
- r.moveCenter(q->geometry().center());
- rippleOverlay->setGeometry(r);
- }
- /*!
- * \class QtMaterialIconButton
- */
- QtMaterialIconButton::QtMaterialIconButton(const QIcon &icon, QWidget *parent)
- : QAbstractButton(parent),
- d_ptr(new QtMaterialIconButtonPrivate(this))
- {
- d_func()->init();
- setIcon(icon);
- }
- QtMaterialIconButton::~QtMaterialIconButton()
- {
- }
- /*!
- * \reimp
- */
- QSize QtMaterialIconButton::sizeHint() const
- {
- return iconSize();
- }
- void QtMaterialIconButton::setUseThemeColors(bool value)
- {
- Q_D(QtMaterialIconButton);
- if (d->useThemeColors == value) {
- return;
- }
- d->useThemeColors = value;
- update();
- }
- bool QtMaterialIconButton::useThemeColors() const
- {
- Q_D(const QtMaterialIconButton);
- return d->useThemeColors;
- }
- void QtMaterialIconButton::setColor(const QColor &color)
- {
- Q_D(QtMaterialIconButton);
- d->color = color;
- MATERIAL_DISABLE_THEME_COLORS
- update();
- }
- QColor QtMaterialIconButton::color() const
- {
- Q_D(const QtMaterialIconButton);
- if (d->useThemeColors || !d->color.isValid()) {
- return QtMaterialStyle::instance().themeColor("text");
- }
- return d->color;
- }
- void QtMaterialIconButton::setDisabledColor(const QColor &color)
- {
- Q_D(QtMaterialIconButton);
- d->disabledColor = color;
- MATERIAL_DISABLE_THEME_COLORS
- update();
- }
- QColor QtMaterialIconButton::disabledColor() const
- {
- Q_D(const QtMaterialIconButton);
- if (d->useThemeColors || !d->disabledColor.isValid()) {
- return QtMaterialStyle::instance().themeColor("disabled");
- }
- return d->disabledColor;
- }
- QtMaterialIconButton::QtMaterialIconButton(QtMaterialIconButtonPrivate &d, QWidget *parent)
- : QAbstractButton(parent),
- d_ptr(&d)
- {
- d_func()->init();
- }
- /*!
- * \reimp
- */
- bool QtMaterialIconButton::event(QEvent *event)
- {
- Q_D(QtMaterialIconButton);
- switch (event->type())
- {
- case QEvent::Move:
- case QEvent::Resize:
- d->updateRipple();
- break;
- case QEvent::ParentChange: {
- QWidget *widget;
- if ((widget = parentWidget())) {
- d->rippleOverlay->setParent(widget);
- }
- break;
- }
- default:
- break;
- }
- return QAbstractButton::event(event);
- }
- /*!
- * \reimp
- */
- bool QtMaterialIconButton::eventFilter(QObject *obj, QEvent *event)
- {
- if (QEvent::Resize == event->type())
- {
- Q_D(QtMaterialIconButton);
- d->updateRipple();
- }
- return QAbstractButton::eventFilter(obj, event);
- }
- /*!
- * \reimp
- */
- void QtMaterialIconButton::mousePressEvent(QMouseEvent *event)
- {
- Q_D(QtMaterialIconButton);
- d->rippleOverlay->addRipple(QPoint(d->rippleOverlay->width(),
- d->rippleOverlay->height())/2,
- iconSize().width());
- emit clicked();
- QAbstractButton::mousePressEvent(event);
- }
- /*!
- * \reimp
- */
- void QtMaterialIconButton::paintEvent(QPaintEvent *event)
- {
- 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);
- }
|