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