implement icon menu zoom effect
This commit is contained in:
parent
8a0daa0c8f
commit
9271838d70
|
@ -3,13 +3,14 @@
|
||||||
#include <QMouseEvent>
|
#include <QMouseEvent>
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include "flatbutton.h"
|
#include "flatbutton.h"
|
||||||
#include "style.h"
|
#include "lib/style.h"
|
||||||
|
|
||||||
FlatButton::FlatButton(QWidget *parent)
|
FlatButton::FlatButton(QWidget *parent)
|
||||||
: QAbstractButton(parent),
|
: QAbstractButton(parent),
|
||||||
_overlay(new RippleOverlay(this))
|
_overlay(new RippleOverlay(this))
|
||||||
{
|
{
|
||||||
setStyle(&Style::instance());
|
setStyle(&Style::instance());
|
||||||
|
setAttribute(Qt::WA_Hover);
|
||||||
}
|
}
|
||||||
|
|
||||||
FlatButton::FlatButton(const QString &text, QWidget *parent)
|
FlatButton::FlatButton(const QString &text, QWidget *parent)
|
||||||
|
@ -18,6 +19,7 @@ FlatButton::FlatButton(const QString &text, QWidget *parent)
|
||||||
{
|
{
|
||||||
setText(text);
|
setText(text);
|
||||||
setStyle(&Style::instance());
|
setStyle(&Style::instance());
|
||||||
|
setAttribute(Qt::WA_Hover);
|
||||||
}
|
}
|
||||||
|
|
||||||
FlatButton::~FlatButton()
|
FlatButton::~FlatButton()
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include "iconmenu.h"
|
#include "iconmenu.h"
|
||||||
#include "menu.h"
|
#include "menu.h"
|
||||||
|
#include "lib/scaleeffect.h"
|
||||||
|
|
||||||
MenuOverlay::MenuOverlay(QWidget *parent)
|
MenuOverlay::MenuOverlay(QWidget *parent)
|
||||||
: QWidget(parent)
|
: QWidget(parent)
|
||||||
|
@ -22,19 +23,23 @@ IconMenu::IconMenu(const QIcon &icon, QWidget *parent)
|
||||||
_menuOverlay(new MenuOverlay),
|
_menuOverlay(new MenuOverlay),
|
||||||
_menu(new Menu(_menuOverlay)),
|
_menu(new Menu(_menuOverlay)),
|
||||||
_animation(new QPropertyAnimation(this)),
|
_animation(new QPropertyAnimation(this)),
|
||||||
|
_effect(new ScaleEffect(this)),
|
||||||
_menuVisible(false),
|
_menuVisible(false),
|
||||||
_progress(0)
|
_progress(1)
|
||||||
{
|
{
|
||||||
setCheckable(true);
|
setCheckable(true);
|
||||||
setOverlayParent(parent);
|
setOverlayParent(parent);
|
||||||
|
|
||||||
_animation->setPropertyName("progress");
|
_animation->setPropertyName("progress");
|
||||||
_animation->setTargetObject(this);
|
_animation->setTargetObject(this);
|
||||||
_animation->setDuration(350);
|
_animation->setDuration(200);
|
||||||
_animation->setStartValue(1);
|
_animation->setStartValue(1);
|
||||||
_animation->setEndValue(0);
|
_animation->setEndValue(0);
|
||||||
|
|
||||||
|
_animation->setEasingCurve(QEasingCurve::InOutCubic);
|
||||||
|
|
||||||
_menu->hide();
|
_menu->hide();
|
||||||
|
_menu->setGraphicsEffect(_effect);
|
||||||
|
|
||||||
_menuOverlay->installEventFilter(this);
|
_menuOverlay->installEventFilter(this);
|
||||||
|
|
||||||
|
@ -57,6 +62,8 @@ void IconMenu::setProgress(qreal progress)
|
||||||
return;
|
return;
|
||||||
_progress = progress;
|
_progress = progress;
|
||||||
|
|
||||||
|
_effect->setScale(0.5*(1 + progress), progress);
|
||||||
|
|
||||||
emit progressChanged(progress);
|
emit progressChanged(progress);
|
||||||
update();
|
update();
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ public:
|
||||||
};
|
};
|
||||||
|
|
||||||
class QPropertyAnimation;
|
class QPropertyAnimation;
|
||||||
|
class ScaleEffect;
|
||||||
class Menu;
|
class Menu;
|
||||||
|
|
||||||
class IconMenu : public IconButton
|
class IconMenu : public IconButton
|
||||||
|
@ -49,6 +50,7 @@ private:
|
||||||
MenuOverlay *const _menuOverlay;
|
MenuOverlay *const _menuOverlay;
|
||||||
Menu *const _menu;
|
Menu *const _menu;
|
||||||
QPropertyAnimation *const _animation;
|
QPropertyAnimation *const _animation;
|
||||||
|
ScaleEffect *const _effect;
|
||||||
QPoint _menuPos;
|
QPoint _menuPos;
|
||||||
bool _menuVisible;
|
bool _menuVisible;
|
||||||
qreal _progress;
|
qreal _progress;
|
||||||
|
|
|
@ -0,0 +1,38 @@
|
||||||
|
#include <QPainter>
|
||||||
|
#include "scaleeffect.h"
|
||||||
|
|
||||||
|
ScaleEffect::ScaleEffect(QObject *parent)
|
||||||
|
: QGraphicsEffect(parent),
|
||||||
|
_scaleX(1),
|
||||||
|
_scaleY(1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScaleEffect::setScale(qreal x, qreal y)
|
||||||
|
{
|
||||||
|
_scaleX = x;
|
||||||
|
_scaleY = y;
|
||||||
|
updateBoundingRect();
|
||||||
|
}
|
||||||
|
|
||||||
|
QSizeF ScaleEffect::scale() const
|
||||||
|
{
|
||||||
|
return QSizeF(_scaleX, _scaleY);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ScaleEffect::draw(QPainter *painter)
|
||||||
|
{
|
||||||
|
QPoint offset;
|
||||||
|
const QPixmap pixmap = sourcePixmap(
|
||||||
|
Qt::DeviceCoordinates,
|
||||||
|
&offset,
|
||||||
|
QGraphicsEffect::PadToEffectiveBoundingRect);
|
||||||
|
|
||||||
|
const QSize &size = pixmap.size();
|
||||||
|
painter->setWorldTransform(QTransform());
|
||||||
|
painter->drawPixmap(offset, pixmap.scaled(
|
||||||
|
size.width()*_scaleX,
|
||||||
|
size.height()*_scaleY,
|
||||||
|
Qt::IgnoreAspectRatio,
|
||||||
|
Qt::SmoothTransformation));
|
||||||
|
}
|
|
@ -0,0 +1,25 @@
|
||||||
|
#ifndef SCALEEFFECT_H
|
||||||
|
#define SCALEEFFECT_H
|
||||||
|
|
||||||
|
#include <QGraphicsEffect>
|
||||||
|
|
||||||
|
class ScaleEffect : public QGraphicsEffect
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
public:
|
||||||
|
explicit ScaleEffect(QObject *parent = 0);
|
||||||
|
|
||||||
|
void setScale(qreal x, qreal y);
|
||||||
|
QSizeF scale() const;
|
||||||
|
inline qreal scaleX() const { return _scaleX; }
|
||||||
|
inline qreal scaleY() const { return _scaleY; }
|
||||||
|
|
||||||
|
void draw(QPainter *painter);
|
||||||
|
|
||||||
|
private:
|
||||||
|
qreal _scaleX;
|
||||||
|
qreal _scaleY;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SCALEEFFECT_H
|
|
@ -50,7 +50,8 @@ SOURCES += main.cpp\
|
||||||
components/menu.cpp \
|
components/menu.cpp \
|
||||||
components/scrollbar.cpp \
|
components/scrollbar.cpp \
|
||||||
lib/style.cpp \
|
lib/style.cpp \
|
||||||
examples/iconmenuexamples.cpp
|
examples/iconmenuexamples.cpp \
|
||||||
|
lib/scaleeffect.cpp
|
||||||
|
|
||||||
HEADERS += mainwindow.h \
|
HEADERS += mainwindow.h \
|
||||||
style.h \
|
style.h \
|
||||||
|
@ -94,4 +95,5 @@ HEADERS += mainwindow.h \
|
||||||
components/menu.h \
|
components/menu.h \
|
||||||
components/scrollbar.h \
|
components/scrollbar.h \
|
||||||
lib/style.h \
|
lib/style.h \
|
||||||
examples/iconmenuexamples.h
|
examples/iconmenuexamples.h \
|
||||||
|
lib/scaleeffect.h
|
||||||
|
|
Loading…
Reference in New Issue