qt-material-widgets/components/drawer_internal.cpp

173 lines
4.6 KiB
C++
Raw Normal View History

2022-02-17 15:17:19 +00:00
#include "drawer_internal.h"
2017-09-30 00:20:12 +00:00
#include <QState>
#include <QPainter>
#include <QtWidgets/QLayout>
2017-09-30 00:20:12 +00:00
#include <QSignalTransition>
#include <QPropertyAnimation>
2022-02-17 15:17:19 +00:00
#include "drawer.h"
2022-02-17 02:22:22 +00:00
namespace md
{
/*!
* \class QtMaterialDrawerStateMachine
* \internal
*/
2022-02-17 02:22:22 +00:00
DrawerStateMachine::DrawerStateMachine(DrawerWidget *drawer, Drawer *parent)
: QStateMachine(parent),
2017-09-30 00:20:12 +00:00
m_drawer(drawer),
m_main(parent),
m_openingState(new QState),
m_openedState(new QState),
m_closingState(new QState),
2017-09-30 00:20:12 +00:00
m_closedState(new QState),
m_opacity(0)
2017-09-30 00:20:12 +00:00
{
addState(m_openingState);
addState(m_openedState);
addState(m_closingState);
2017-09-30 00:20:12 +00:00
addState(m_closedState);
setInitialState(m_closedState);
QSignalTransition *transition;
QPropertyAnimation *animation;
transition = new QSignalTransition(this, SIGNAL(signalOpen()));
transition->setTargetState(m_openingState);
2017-09-30 00:20:12 +00:00
m_closedState->addTransition(transition);
animation = new QPropertyAnimation(drawer, "offset", this);
2017-09-30 00:20:12 +00:00
animation->setDuration(220);
animation->setEasingCurve(QEasingCurve::OutCirc);
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "opacity", this);
animation->setDuration(220);
transition->addAnimation(animation);
transition = new QSignalTransition(animation, SIGNAL(finished()));
transition->setTargetState(m_openedState);
m_openingState->addTransition(transition);
transition = new QSignalTransition(this, SIGNAL(signalClose()));
transition->setTargetState(m_closingState);
m_openingState->addTransition(transition);
animation = new QPropertyAnimation(this, "opacity", this);
animation->setDuration(220);
transition->addAnimation(animation);
animation = new QPropertyAnimation(drawer, "offset", this);
animation->setDuration(220);
animation->setEasingCurve(QEasingCurve::InCirc);
transition->addAnimation(animation);
transition = new QSignalTransition(animation, SIGNAL(finished()));
2017-09-30 00:20:12 +00:00
transition->setTargetState(m_closedState);
m_closingState->addTransition(transition);
transition = new QSignalTransition(this, SIGNAL(signalClose()));
transition->setTargetState(m_closingState);
m_openedState->addTransition(transition);
2017-09-30 00:20:12 +00:00
animation = new QPropertyAnimation(drawer, "offset", this);
2017-09-30 00:20:12 +00:00
animation->setDuration(220);
animation->setEasingCurve(QEasingCurve::InCirc);
transition->addAnimation(animation);
animation = new QPropertyAnimation(this, "opacity", this);
animation->setDuration(220);
transition->addAnimation(animation);
transition = new QSignalTransition(animation, SIGNAL(finished()));
transition->setTargetState(m_closedState);
m_closingState->addTransition(transition);
2017-09-30 00:20:12 +00:00
updatePropertyAssignments();
}
2022-02-17 02:22:22 +00:00
DrawerStateMachine::~DrawerStateMachine()
2017-09-30 00:20:12 +00:00
{
}
2022-02-17 02:22:22 +00:00
void DrawerStateMachine::setOpacity(qreal opacity)
{
m_opacity = opacity;
m_main->update();
}
2022-02-17 02:22:22 +00:00
bool DrawerStateMachine::isInClosedState() const
{
return m_closedState->active();
}
2022-02-17 02:22:22 +00:00
void DrawerStateMachine::updatePropertyAssignments()
{
const qreal closedOffset = -(m_drawer->width()+32);
m_closingState->assignProperty(m_drawer, "offset", closedOffset);
m_closedState->assignProperty(m_drawer, "offset", closedOffset);
m_closingState->assignProperty(this, "opacity", 0);
m_closedState->assignProperty(this, "opacity", 0);
m_openingState->assignProperty(m_drawer, "offset", 0);
m_openingState->assignProperty(this, "opacity", 0.4);
}
/*!
* \class QtMaterialDrawerWidget
* \internal
*/
2022-02-17 02:22:22 +00:00
DrawerWidget::DrawerWidget(QWidget *parent)
: OverlayWidget(parent),
m_offset(0)
{
}
2022-02-17 02:22:22 +00:00
DrawerWidget::~DrawerWidget()
{
}
2022-02-17 02:22:22 +00:00
void DrawerWidget::setOffset(int offset)
2017-09-30 00:20:12 +00:00
{
m_offset = offset;
QWidget *widget = parentWidget();
2017-09-30 00:20:12 +00:00
if (widget) {
setGeometry(widget->rect().translated(offset, 0));
2017-09-30 00:20:12 +00:00
}
update();
2017-09-30 00:20:12 +00:00
}
2022-02-17 02:22:22 +00:00
void DrawerWidget::paintEvent(QPaintEvent *event)
{
Q_UNUSED(event)
QPainter painter(this);
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::white);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.drawRect(rect().adjusted(0, 0, -16, 0));
QLinearGradient gradient(QPointF(width()-16, 0), QPointF(width(), 0));
gradient.setColorAt(0, QColor(0, 0, 0, 80));
gradient.setColorAt(0.5, QColor(0, 0, 0, 20));
gradient.setColorAt(1, QColor(0, 0, 0, 0));
painter.setBrush(QBrush(gradient));
painter.drawRect(width()-16, 0, 16, height());
}
2022-02-17 02:22:22 +00:00
QRect DrawerWidget::overlayGeometry() const
2017-09-30 00:20:12 +00:00
{
2022-02-17 02:22:22 +00:00
return OverlayWidget::overlayGeometry().translated(m_offset, 0);
}
2017-09-30 00:20:12 +00:00
}