implement basic FAB style and functionality
This commit is contained in:
parent
4e3a17cbda
commit
49f9937545
|
@ -1,11 +1,12 @@
|
|||
#include "fab.h"
|
||||
#include <QPainter>
|
||||
#include <QDebug>
|
||||
#include <QGraphicsDropShadowEffect>
|
||||
#include "fab_p.h"
|
||||
|
||||
FloatingActionButtonPrivate::FloatingActionButtonPrivate(FloatingActionButton *q)
|
||||
: RaisedButtonPrivate(q),
|
||||
mini(false)
|
||||
mini(false),
|
||||
corner(Qt::BottomRightCorner)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -18,12 +19,73 @@ void FloatingActionButtonPrivate::init()
|
|||
Q_Q(FloatingActionButton);
|
||||
|
||||
q->setRole(Material::Primary);
|
||||
q->setFixedSize(56, 56);
|
||||
q->setGeometry(fabGeometry());
|
||||
|
||||
assignAnimationProperties();
|
||||
}
|
||||
|
||||
FloatingActionButton::FloatingActionButton(QWidget *parent)
|
||||
: RaisedButton(parent)
|
||||
QRect FloatingActionButtonPrivate::fabGeometry() const
|
||||
{
|
||||
Q_Q(const FloatingActionButton);
|
||||
|
||||
QWidget *parent = q->parentWidget();
|
||||
if (!parent)
|
||||
return QRect();
|
||||
|
||||
const int offset = mini ? 74 : 90;
|
||||
const int s = mini ? 40 : 56;
|
||||
|
||||
switch (corner)
|
||||
{
|
||||
case Qt::TopLeftCorner:
|
||||
return QRect(offset - s,
|
||||
offset - s,
|
||||
s, s);
|
||||
case Qt::TopRightCorner:
|
||||
return QRect(parent->width() - offset,
|
||||
offset - s,
|
||||
s, s);
|
||||
case Qt::BottomLeftCorner:
|
||||
return QRect(offset -s,
|
||||
parent->height() - offset,
|
||||
s, s);
|
||||
case Qt::BottomRightCorner:
|
||||
default:
|
||||
return QRect(parent->width() - offset,
|
||||
parent->height() - offset,
|
||||
s, s);
|
||||
}
|
||||
}
|
||||
|
||||
void FloatingActionButtonPrivate::assignAnimationProperties()
|
||||
{
|
||||
if (mini)
|
||||
{
|
||||
effect->setColor(QColor(0, 0, 0, 80));
|
||||
|
||||
normalState->assignProperty(effect, "offset", QPointF(0, 3));
|
||||
normalState->assignProperty(effect, "blurRadius", 13);
|
||||
pressedState->assignProperty(effect, "offset", QPointF(0, 7));
|
||||
pressedState->assignProperty(effect, "blurRadius", 20);
|
||||
}
|
||||
else
|
||||
{
|
||||
effect->setColor(QColor(0, 0, 0, 110));
|
||||
|
||||
normalState->assignProperty(effect, "offset", QPointF(0, 6));
|
||||
normalState->assignProperty(effect, "blurRadius", 16);
|
||||
pressedState->assignProperty(effect, "offset", QPointF(0, 11));
|
||||
pressedState->assignProperty(effect, "blurRadius", 28);
|
||||
}
|
||||
}
|
||||
|
||||
FloatingActionButton::FloatingActionButton(const QIcon &icon, QWidget *parent)
|
||||
: RaisedButton(*new FloatingActionButtonPrivate(this), parent)
|
||||
{
|
||||
d_func()->init();
|
||||
|
||||
setIcon(icon);
|
||||
}
|
||||
|
||||
FloatingActionButton::~FloatingActionButton()
|
||||
|
@ -44,7 +106,18 @@ void FloatingActionButton::setMini(bool state)
|
|||
{
|
||||
Q_D(FloatingActionButton);
|
||||
|
||||
if (d->mini == state)
|
||||
return;
|
||||
|
||||
d->mini = state;
|
||||
|
||||
if (state) {
|
||||
setFixedSize(40, 40);
|
||||
} else {
|
||||
setFixedSize(56, 56);
|
||||
}
|
||||
d->assignAnimationProperties();
|
||||
update();
|
||||
}
|
||||
|
||||
bool FloatingActionButton::isMini() const
|
||||
|
@ -54,11 +127,63 @@ bool FloatingActionButton::isMini() const
|
|||
return d->mini;
|
||||
}
|
||||
|
||||
void FloatingActionButton::setCorner(Qt::Corner corner)
|
||||
{
|
||||
Q_D(FloatingActionButton);
|
||||
|
||||
if (d->corner == corner)
|
||||
return;
|
||||
|
||||
d->corner = corner;
|
||||
update();
|
||||
}
|
||||
|
||||
Qt::Corner FloatingActionButton::corner() const
|
||||
{
|
||||
Q_D(const FloatingActionButton);
|
||||
|
||||
return d->corner;
|
||||
}
|
||||
|
||||
bool FloatingActionButton::event(QEvent *event)
|
||||
{
|
||||
QEvent::Type type = event->type();
|
||||
|
||||
if (QEvent::ParentChange == type && parentWidget())
|
||||
{
|
||||
Q_D(FloatingActionButton);
|
||||
|
||||
parent()->installEventFilter(this);
|
||||
setGeometry(d->fabGeometry());
|
||||
}
|
||||
else if (QEvent::ParentAboutToChange == type && parentWidget())
|
||||
{
|
||||
parent()->removeEventFilter(this);
|
||||
}
|
||||
return RaisedButton::event(event);
|
||||
}
|
||||
|
||||
bool FloatingActionButton::eventFilter(QObject *obj, QEvent *event)
|
||||
{
|
||||
QEvent::Type type = event->type();
|
||||
|
||||
if (QEvent::Move == type || QEvent::Resize == type)
|
||||
{
|
||||
Q_D(FloatingActionButton);
|
||||
|
||||
setGeometry(d->fabGeometry());
|
||||
}
|
||||
return RaisedButton::eventFilter(obj, event);
|
||||
}
|
||||
|
||||
void FloatingActionButton::paintEvent(QPaintEvent *event)
|
||||
{
|
||||
Q_UNUSED(event)
|
||||
|
||||
int s = qMin(width(), height());
|
||||
Q_D(FloatingActionButton);
|
||||
|
||||
int s = d->mini ? 40 : 56;
|
||||
|
||||
QRect square = QRect(0, 0, s, s);
|
||||
square.moveCenter(rect().center());
|
||||
|
||||
|
@ -73,4 +198,11 @@ void FloatingActionButton::paintEvent(QPaintEvent *event)
|
|||
painter.setPen(Qt::NoPen);
|
||||
|
||||
painter.drawEllipse(square);
|
||||
|
||||
const int sz = d->mini ? 18 : 24;
|
||||
|
||||
QRect iconRect(0, 0, sz, sz);
|
||||
iconRect.moveCenter(square.center());
|
||||
|
||||
icon().paint(&painter, iconRect, Qt::AlignCenter, QIcon::Normal);
|
||||
}
|
||||
|
|
|
@ -10,7 +10,7 @@ class FloatingActionButton : public RaisedButton
|
|||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit FloatingActionButton(QWidget *parent = 0);
|
||||
explicit FloatingActionButton(const QIcon &icon, QWidget *parent = 0);
|
||||
~FloatingActionButton();
|
||||
|
||||
QSize sizeHint() const Q_DECL_OVERRIDE;
|
||||
|
@ -18,7 +18,12 @@ public:
|
|||
void setMini(bool state);
|
||||
bool isMini() const;
|
||||
|
||||
void setCorner(Qt::Corner corner);
|
||||
Qt::Corner corner() const;
|
||||
|
||||
protected:
|
||||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||
bool eventFilter(QObject *obj, QEvent *event) Q_DECL_OVERRIDE;
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
private:
|
||||
|
|
|
@ -14,7 +14,12 @@ public:
|
|||
|
||||
void init();
|
||||
|
||||
QRect fabGeometry() const;
|
||||
|
||||
void assignAnimationProperties();
|
||||
|
||||
bool mini;
|
||||
Qt::Corner corner;
|
||||
};
|
||||
|
||||
#endif // FAB_P_H
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <QStylePainter>
|
||||
#include <QStyleOption>
|
||||
#include <QApplication>
|
||||
#include <QDebug>
|
||||
#include "lib/rippleoverlay.h"
|
||||
#include "lib/ripple.h"
|
||||
#include "flatbutton_p.h"
|
||||
|
|
|
@ -26,14 +26,14 @@ void RaisedButtonPrivate::init()
|
|||
q->setTextColor(Qt::white);
|
||||
q->setPeakOpacity(0.25);
|
||||
|
||||
QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
|
||||
effect = new QGraphicsDropShadowEffect;
|
||||
effect->setBlurRadius(7);
|
||||
effect->setOffset(QPointF(0, 0));
|
||||
effect->setColor(QColor(0, 0, 0, 60));
|
||||
q->setGraphicsEffect(effect);
|
||||
|
||||
QState *normalState = new QState;
|
||||
QState *pressedState = new QState;
|
||||
normalState = new QState;
|
||||
pressedState = new QState;
|
||||
|
||||
machine.addState(normalState);
|
||||
machine.addState(pressedState);
|
||||
|
@ -109,6 +109,12 @@ RaisedButton::~RaisedButton()
|
|||
{
|
||||
}
|
||||
|
||||
RaisedButton::RaisedButton(RaisedButtonPrivate &d, QWidget *parent)
|
||||
: FlatButton(d, parent)
|
||||
{
|
||||
d_func()->init();
|
||||
}
|
||||
|
||||
bool RaisedButton::event(QEvent *event)
|
||||
{
|
||||
Q_D(RaisedButton);
|
||||
|
|
|
@ -14,6 +14,8 @@ public:
|
|||
~RaisedButton();
|
||||
|
||||
protected:
|
||||
RaisedButton(RaisedButtonPrivate &d, QWidget *parent = 0);
|
||||
|
||||
bool event(QEvent *event) Q_DECL_OVERRIDE;
|
||||
void paintEvent(QPaintEvent *event) Q_DECL_OVERRIDE;
|
||||
|
||||
|
|
|
@ -3,6 +3,8 @@
|
|||
|
||||
#include "flatbutton_p.h"
|
||||
|
||||
class QGraphicsDropShadowEffect;
|
||||
|
||||
class RaisedButtonPrivate : public FlatButtonPrivate
|
||||
{
|
||||
Q_DISABLE_COPY(RaisedButtonPrivate)
|
||||
|
@ -15,6 +17,9 @@ public:
|
|||
void init();
|
||||
|
||||
QStateMachine machine;
|
||||
QState *normalState;
|
||||
QState *pressedState;
|
||||
QGraphicsDropShadowEffect *effect;
|
||||
};
|
||||
|
||||
#endif // RAISEDBUTTON_P_H
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <QLayout>
|
||||
#include <QPushButton>
|
||||
#include <QDebug>
|
||||
#include <QApplication>
|
||||
#include "flatbuttonexamples.h"
|
||||
#include "lib/style.h"
|
||||
#include "components/flatbutton.h"
|
||||
|
@ -206,18 +207,20 @@ FlatButtonExamples::FlatButtonExamples(QWidget *parent)
|
|||
//flatButton->setMinimumSize(200, 50);
|
||||
//flatButton->setCheckable(true);
|
||||
|
||||
FloatingActionButton *button = new FloatingActionButton;
|
||||
FloatingActionButton *button = new FloatingActionButton(QIcon("../qt-material-widgets/ic_local_dining_white_24px.svg"));
|
||||
|
||||
ExampleView *view = new ExampleView;
|
||||
view->setWidget(button);
|
||||
//qDebug() << QGuiApplication::allWindows();
|
||||
|
||||
Frame *frame = new Frame;
|
||||
frame->setCodeSnippet(
|
||||
""
|
||||
);
|
||||
frame->setWidget(view);
|
||||
//ExampleView *view = new ExampleView;
|
||||
//view->setWidget(button);
|
||||
|
||||
layout->addWidget(frame);
|
||||
//Frame *frame = new Frame;
|
||||
//frame->setCodeSnippet(
|
||||
// ""
|
||||
//);
|
||||
//frame->setWidget(view);
|
||||
|
||||
//layout->addWidget(frame);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ RaisedButtonExamples::RaisedButtonExamples(QWidget *parent)
|
|||
raisedButton->setText("Press me!");
|
||||
raisedButton->setMaximumWidth(408);
|
||||
|
||||
raisedButton->setDisabled(true);
|
||||
|
||||
//raisedButton->setDisabled(true);
|
||||
|
||||
//raisedButton->setFixedSize(400, 50);
|
||||
|
|
|
@ -20,6 +20,7 @@
|
|||
#include "examples/avatarexamples.h"
|
||||
#include "examples/menuexamples.h"
|
||||
#include "examples/iconmenuexamples.h"
|
||||
#include "components/fab.h"
|
||||
|
||||
MainWindow::MainWindow(QWidget *parent)
|
||||
: QMainWindow(parent),
|
||||
|
@ -43,6 +44,19 @@ MainWindow::MainWindow(QWidget *parent)
|
|||
{
|
||||
_initWidget();
|
||||
_initMenu();
|
||||
|
||||
FloatingActionButton *button = new FloatingActionButton(QIcon("../qt-material-widgets/ic_local_dining_white_24px.svg"));
|
||||
button->setParent(this);
|
||||
|
||||
button->setCorner(Qt::TopLeftCorner);
|
||||
button->setMini(true);
|
||||
|
||||
//
|
||||
|
||||
FloatingActionButton *button2 = new FloatingActionButton(QIcon("../qt-material-widgets/ic_message_white_24px.svg"));
|
||||
button2->setParent(this);
|
||||
|
||||
button2->setDisabled(true);
|
||||
}
|
||||
|
||||
MainWindow::~MainWindow()
|
||||
|
|
Loading…
Reference in New Issue