make icon button geometry behave nicely

This commit is contained in:
laserpants 2016-03-19 23:31:22 +03:00
parent 6cb6db5e89
commit e3d64f989a
2 changed files with 28 additions and 20 deletions

View File

@ -8,13 +8,16 @@
IconButton::IconButton(const QIcon &icon, QWidget *parent) IconButton::IconButton(const QIcon &icon, QWidget *parent)
: QAbstractButton(parent), : QAbstractButton(parent),
_overlay(new RippleOverlay(parent)) _overlay(new RippleOverlay(parent)),
_geometryWidget(0)
{ {
setIcon(icon); setIcon(icon);
QSizePolicy policy; QSizePolicy policy;
policy.setWidthForHeight(true); policy.setWidthForHeight(true);
setSizePolicy(policy); setSizePolicy(policy);
setGeometryWidget(this);
} }
IconButton::~IconButton() IconButton::~IconButton()
@ -32,17 +35,13 @@ QSize IconButton::sizeHint() const
expandedTo(QApplication::globalStrut())); expandedTo(QApplication::globalStrut()));
} }
void IconButton::moveEvent(QMoveEvent *event) void IconButton::setGeometryWidget(QWidget *widget)
{ {
Q_UNUSED(event) if (_geometryWidget) {
_geometryWidget->removeEventFilter(this);
updateOverlayGeometry(); }
} _geometryWidget = widget;
widget->installEventFilter(this);
void IconButton::resizeEvent(QResizeEvent *event)
{
Q_UNUSED(event)
updateOverlayGeometry(); updateOverlayGeometry();
} }
@ -62,28 +61,36 @@ void IconButton::mousePressEvent(QMouseEvent *event)
if (!_overlay) if (!_overlay)
return; return;
const QRect &size = geometry(); QPoint p(_overlay->width(), _overlay->height());
_overlay->addRipple(QPoint(size.width(), size.height()), iconSize().width()); _overlay->addRipple(p/2, iconSize().width());
emit clicked(); emit clicked();
} }
bool IconButton::event(QEvent *event) bool IconButton::event(QEvent *event)
{ {
if (QEvent::ParentChange == event->type()) { if (QEvent::ParentChange == event->type() && parentWidget()) {
_overlay->setParent(parentWidget()); _overlay->setParent(parentWidget());
} }
return QAbstractButton::event(event); return QAbstractButton::event(event);
} }
bool IconButton::eventFilter(QObject *obj, QEvent *event)
{
const QEvent::Type type = event->type();
if (QEvent::Resize == type || QEvent::Move == type) {
updateOverlayGeometry();
}
return QAbstractButton::eventFilter(obj, event);
}
void IconButton::updateOverlayGeometry() void IconButton::updateOverlayGeometry()
{ {
if (!_overlay) if (!_overlay || !_geometryWidget)
return; return;
int x, y, w, h; const int s = iconSize().width()/2;
geometry().getRect(&x, &y, &w, &h); _overlay->setGeometry(_geometryWidget->geometry().adjusted(-s, -s, s, s));
_overlay->setGeometry(x-w/2, y-h/2, w*2, h*2);
} }
QStyleOptionButton IconButton::getStyleOption() const QStyleOptionButton IconButton::getStyleOption() const

View File

@ -15,19 +15,20 @@ public:
~IconButton(); ~IconButton();
QSize sizeHint() const Q_DECL_OVERRIDE; QSize sizeHint() const Q_DECL_OVERRIDE;
void setGeometryWidget(QWidget *widget);
protected: protected:
void moveEvent(QMoveEvent *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;
bool event(QEvent *event) Q_DECL_OVERRIDE; bool event(QEvent *event) Q_DECL_OVERRIDE;
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
private: private:
void updateOverlayGeometry(); void updateOverlayGeometry();
QStyleOptionButton getStyleOption() const; QStyleOptionButton getStyleOption() const;
RippleOverlay *const _overlay; RippleOverlay *const _overlay;
QWidget *_geometryWidget;
}; };
#endif // ICONBUTTON_H #endif // ICONBUTTON_H