re-implement icon button as pimpl class
This commit is contained in:
parent
4bfa031091
commit
0ec35cbd70
|
@ -8,9 +8,9 @@
|
|||
- [x] Flat Button
|
||||
- [x] Raised Button
|
||||
- [x] Toggle
|
||||
- [x] Icon Button
|
||||
- [ ] App Bar
|
||||
- [ ] Avatar
|
||||
- [ ] Icon Button
|
||||
- [ ] Icon Menu
|
||||
- [ ] Menu
|
||||
- [ ] Dialog
|
||||
|
|
|
@ -1,22 +1,33 @@
|
|||
#include <QEvent>
|
||||
#include <QPainter>
|
||||
#include <QDebug>
|
||||
#include "iconbutton.h"
|
||||
#include "../lib/rippleoverlay.h"
|
||||
#include "style.h"
|
||||
#include <QPainter>
|
||||
#include <QEvent>
|
||||
#include "iconbutton_p.h"
|
||||
#include "lib/style.h"
|
||||
#include "lib/rippleoverlay.h"
|
||||
|
||||
IconButton::IconButton(const QIcon &icon, QWidget *parent)
|
||||
: QAbstractButton(parent),
|
||||
_overlay(new RippleOverlay(parent)),
|
||||
_geometryWidget(0)
|
||||
IconButtonPrivate::IconButtonPrivate(IconButton *q)
|
||||
: q_ptr(q)
|
||||
{
|
||||
setIcon(icon);
|
||||
}
|
||||
|
||||
void IconButtonPrivate::init()
|
||||
{
|
||||
Q_Q(IconButton);
|
||||
|
||||
ripple = new RippleOverlay(q);
|
||||
|
||||
QSizePolicy policy;
|
||||
policy.setWidthForHeight(true);
|
||||
setSizePolicy(policy);
|
||||
q->setSizePolicy(policy);
|
||||
}
|
||||
|
||||
setGeometryWidget(this);
|
||||
IconButton::IconButton(const QIcon &icon, QWidget *parent)
|
||||
: QAbstractButton(parent),
|
||||
d_ptr(new IconButtonPrivate(this))
|
||||
{
|
||||
d_func()->init();
|
||||
|
||||
setIcon(icon);
|
||||
}
|
||||
|
||||
IconButton::~IconButton()
|
||||
|
@ -28,14 +39,35 @@ QSize IconButton::sizeHint() const
|
|||
return iconSize();
|
||||
}
|
||||
|
||||
void IconButton::setGeometryWidget(QWidget *widget)
|
||||
bool IconButton::event(QEvent *event)
|
||||
{
|
||||
if (_geometryWidget) {
|
||||
_geometryWidget->removeEventFilter(this);
|
||||
const QEvent::Type type = event->type();
|
||||
if (QEvent::Resize == type || QEvent::Move == type)
|
||||
{
|
||||
Q_D(IconButton);
|
||||
|
||||
const int s = iconSize().width()/2;
|
||||
d->ripple->setGeometry(geometry().adjusted(-s, -s, s, s));
|
||||
}
|
||||
_geometryWidget = widget;
|
||||
widget->installEventFilter(this);
|
||||
updateOverlayGeometry();
|
||||
else if (QEvent::ParentChange == type && parentWidget())
|
||||
{
|
||||
Q_D(IconButton);
|
||||
|
||||
d->ripple->setParent(parentWidget());
|
||||
}
|
||||
return QAbstractButton::event(event);
|
||||
}
|
||||
|
||||
void IconButton::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
Q_D(IconButton);
|
||||
|
||||
const QPoint p(d->ripple->width(), d->ripple->height());
|
||||
d->ripple->addRipple(p/2, iconSize().width());
|
||||
|
||||
emit clicked();
|
||||
|
||||
QAbstractButton::mousePressEvent(event);
|
||||
}
|
||||
|
||||
void IconButton::paintEvent(QPaintEvent *event)
|
||||
|
@ -43,46 +75,10 @@ void IconButton::paintEvent(QPaintEvent *event)
|
|||
Q_UNUSED(event)
|
||||
|
||||
QPainter painter(this);
|
||||
|
||||
const QSize &size = iconSize();
|
||||
QPoint pos(width()/2-size.width()/2, height()/2-size.height()/2);
|
||||
icon().paint(&painter, QRect(pos, size));
|
||||
}
|
||||
|
||||
void IconButton::mousePressEvent(QMouseEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
if (!_overlay)
|
||||
return;
|
||||
|
||||
const QPoint p(_overlay->width(), _overlay->height());
|
||||
_overlay->addRipple(p/2, iconSize().width());
|
||||
|
||||
emit clicked();
|
||||
}
|
||||
|
||||
bool IconButton::event(QEvent *event)
|
||||
{
|
||||
if (QEvent::ParentChange == event->type() && parentWidget()) {
|
||||
_overlay->setParent(parentWidget());
|
||||
}
|
||||
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()
|
||||
{
|
||||
if (!_overlay || !_geometryWidget)
|
||||
return;
|
||||
|
||||
const int s = iconSize().width()/2;
|
||||
_overlay->setGeometry(_geometryWidget->geometry().adjusted(-s, -s, s, s));
|
||||
icon().paint(&painter, QRect(pos, size), Qt::AlignCenter,
|
||||
isEnabled() ? QIcon::Normal
|
||||
: QIcon::Disabled);
|
||||
}
|
||||
|
|
|
@ -3,7 +3,7 @@
|
|||
|
||||
#include <QAbstractButton>
|
||||
|
||||
class RippleOverlay;
|
||||
class IconButtonPrivate;
|
||||
|
||||
class IconButton : public QAbstractButton
|
||||
{
|
||||
|
@ -14,19 +14,17 @@ public:
|
|||
~IconButton();
|
||||
|
||||
QSize sizeHint() const Q_DECL_OVERRIDE;
|
||||
void setGeometryWidget(QWidget *widget);
|
||||
|
||||
protected:
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||
void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
const QScopedPointer<IconButtonPrivate> d_ptr;
|
||||
|
||||
private:
|
||||
void updateOverlayGeometry();
|
||||
|
||||
RippleOverlay *const _overlay;
|
||||
QWidget *_geometryWidget;
|
||||
Q_DISABLE_COPY(IconButton)
|
||||
Q_DECLARE_PRIVATE(IconButton)
|
||||
};
|
||||
|
||||
#endif // ICONBUTTON_H
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
#ifndef ICONBUTTON_P_H
|
||||
#define ICONBUTTON_P_H
|
||||
|
||||
class IconButton;
|
||||
class RippleOverlay;
|
||||
|
||||
class IconButtonPrivate
|
||||
{
|
||||
Q_DISABLE_COPY(IconButtonPrivate)
|
||||
Q_DECLARE_PUBLIC(IconButton)
|
||||
|
||||
public:
|
||||
IconButtonPrivate(IconButton *q);
|
||||
|
||||
void init();
|
||||
|
||||
IconButton *const q_ptr;
|
||||
RippleOverlay *ripple;
|
||||
};
|
||||
|
||||
#endif // ICONBUTTON_P_H
|
|
@ -3,6 +3,7 @@
|
|||
#include <QState>
|
||||
#include <QSignalTransition>
|
||||
#include <QPropertyAnimation>
|
||||
#include <QDebug>
|
||||
#include "lib/rippleoverlay.h"
|
||||
#include "lib/ripple.h"
|
||||
#include "lib/style.h"
|
||||
|
@ -13,7 +14,6 @@ TogglePrivate::TogglePrivate(Toggle *q)
|
|||
: q_ptr(q),
|
||||
track(new ToggleTrack(q)),
|
||||
thumb(new ToggleThumb(q)),
|
||||
ripple(new RippleOverlay(q->parentWidget())),
|
||||
orientation(Qt::Horizontal)
|
||||
{
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ void TogglePrivate::init()
|
|||
{
|
||||
Q_Q(Toggle);
|
||||
|
||||
ripple = new RippleOverlay(q->parentWidget());
|
||||
|
||||
q->setCheckable(true);
|
||||
q->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Maximum);
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ public:
|
|||
Toggle *const q_ptr;
|
||||
ToggleTrack *const track;
|
||||
ToggleThumb *const thumb;
|
||||
RippleOverlay *const ripple;
|
||||
RippleOverlay *ripple;
|
||||
QStateMachine machine;
|
||||
Qt::Orientation orientation;
|
||||
};
|
||||
|
|
|
@ -44,6 +44,8 @@ IconButtonExamples::IconButtonExamples(QWidget *parent)
|
|||
IconButton *iconButton = new IconButton(QIcon("../qt-material-widgets/face.svg"));
|
||||
iconButton->setIconSize(QSize(128, 128));
|
||||
|
||||
iconButton->setDisabled(true);
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(iconButton);
|
||||
|
||||
|
|
|
@ -109,7 +109,8 @@ HEADERS += mainwindow.h \
|
|||
components/flatbutton_internal.h \
|
||||
components/raisedbutton_p.h \
|
||||
components/toggle_p.h \
|
||||
components/toggle_internal.h
|
||||
components/toggle_internal.h \
|
||||
components/iconbutton_p.h
|
||||
|
||||
RESOURCES += \
|
||||
resources.qrc
|
||||
|
|
Loading…
Reference in New Issue