qt-material-widgets/source/component/qtmaterialdialog.cpp

178 lines
4.4 KiB
C++
Raw Normal View History

2023-01-31 17:37:40 +00:00
#include <QPainter>
#include <QPropertyAnimation>
2017-09-29 23:15:08 +00:00
#include <QState>
2023-01-31 17:37:40 +00:00
#include <QStateMachine>
#include "qtmaterialdialog.h"
2017-09-29 23:40:00 +00:00
#include <QtWidgets/QApplication>
2023-01-31 17:37:40 +00:00
#include <QtWidgets/QGraphicsDropShadowEffect>
#include <QtWidgets/QStackedLayout>
2017-09-29 23:40:00 +00:00
#include "lib/qtmaterialstatetransition.h"
2023-01-31 17:37:40 +00:00
#include "qtmaterialdialog_internal.h"
#include "qtmaterialdialog_p.h"
2017-09-29 23:15:08 +00:00
/*!
* \class QtMaterialDialogPrivate
* \internal
*/
2023-01-31 17:37:40 +00:00
QtMaterialDialogPrivate::QtMaterialDialogPrivate(QtMaterialDialog* q)
2017-09-29 23:15:08 +00:00
: q_ptr(q)
{
}
2023-01-31 17:37:40 +00:00
QtMaterialDialogPrivate::~QtMaterialDialogPrivate() {}
2017-09-29 23:15:08 +00:00
void QtMaterialDialogPrivate::init()
{
2023-01-31 17:37:40 +00:00
Q_Q(QtMaterialDialog);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
dialogWindow = new QtMaterialDialogWindow(q);
proxyStack = new QStackedLayout;
stateMachine = new QStateMachine(q);
proxy = new QtMaterialDialogProxy(dialogWindow, proxyStack, q);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QVBoxLayout* layout = new QVBoxLayout;
q->setLayout(layout);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QWidget* widget = new QWidget;
widget->setLayout(proxyStack);
widget->setMinimumWidth(400);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QGraphicsDropShadowEffect* effect = new QGraphicsDropShadowEffect;
effect->setColor(QColor(0, 0, 0, 200));
effect->setBlurRadius(64);
effect->setOffset(0, 13);
widget->setGraphicsEffect(effect);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
layout->addWidget(widget);
layout->setAlignment(widget, Qt::AlignCenter);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
proxyStack->addWidget(dialogWindow);
proxyStack->addWidget(proxy);
proxyStack->setCurrentIndex(1);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
q->setAttribute(Qt::WA_TransparentForMouseEvents);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QState* hiddenState = new QState;
QState* visibleState = new QState;
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
stateMachine->addState(hiddenState);
stateMachine->addState(visibleState);
stateMachine->setInitialState(hiddenState);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QtMaterialStateTransition* transition;
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
transition = new QtMaterialStateTransition(DialogShowTransition);
transition->setTargetState(visibleState);
hiddenState->addTransition(transition);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
transition = new QtMaterialStateTransition(DialogHideTransition);
transition->setTargetState(hiddenState);
visibleState->addTransition(transition);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
visibleState->assignProperty(proxy, "opacity", 1);
visibleState->assignProperty(effect, "color", QColor(0, 0, 0, 200));
visibleState->assignProperty(dialogWindow, "offset", 0);
hiddenState->assignProperty(proxy, "opacity", 0);
hiddenState->assignProperty(effect, "color", QColor(0, 0, 0, 0));
hiddenState->assignProperty(dialogWindow, "offset", 200);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QPropertyAnimation* animation;
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
animation = new QPropertyAnimation(proxy, "opacity", q);
animation->setDuration(280);
stateMachine->addDefaultAnimation(animation);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
animation = new QPropertyAnimation(effect, "color", q);
animation->setDuration(280);
stateMachine->addDefaultAnimation(animation);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
animation = new QPropertyAnimation(dialogWindow, "offset", q);
animation->setDuration(280);
animation->setEasingCurve(QEasingCurve::OutCirc);
stateMachine->addDefaultAnimation(animation);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QObject::connect(
visibleState, SIGNAL(propertiesAssigned()), proxy, SLOT(makeOpaque()));
QObject::connect(hiddenState,
SIGNAL(propertiesAssigned()),
proxy,
SLOT(makeTransparent()));
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
stateMachine->start();
QCoreApplication::processEvents();
2017-09-29 23:15:08 +00:00
}
/*!
* \class QtMaterialDialog
*/
2023-01-31 17:37:40 +00:00
QtMaterialDialog::QtMaterialDialog(QWidget* parent)
: QtMaterialOverlayWidget(parent)
, d_ptr(new QtMaterialDialogPrivate(this))
2017-09-29 23:15:08 +00:00
{
2023-01-31 17:37:40 +00:00
d_func()->init();
2017-09-29 23:15:08 +00:00
}
2023-01-31 17:37:40 +00:00
QtMaterialDialog::~QtMaterialDialog() {}
QLayout* QtMaterialDialog::windowLayout() const
2017-09-29 23:15:08 +00:00
{
2023-01-31 17:37:40 +00:00
Q_D(const QtMaterialDialog);
return d->dialogWindow->layout();
2017-09-29 23:15:08 +00:00
}
2023-01-31 17:37:40 +00:00
QWidget* QtMaterialDialog::getContentWidget() const
2017-09-29 23:15:08 +00:00
{
2023-01-31 17:37:40 +00:00
Q_D(const QtMaterialDialog);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
return d->dialogWindow;
2017-09-29 23:15:08 +00:00
}
2023-01-31 17:37:40 +00:00
void QtMaterialDialog::setWindowLayout(QLayout* layout)
2017-09-29 23:15:08 +00:00
{
2023-01-31 17:37:40 +00:00
Q_D(QtMaterialDialog);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
d->dialogWindow->setLayout(layout);
2017-09-29 23:15:08 +00:00
}
void QtMaterialDialog::showDialog()
{
2023-01-31 17:37:40 +00:00
Q_D(QtMaterialDialog);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
d->stateMachine->postEvent(
new QtMaterialStateTransitionEvent(DialogShowTransition));
raise();
2017-09-29 23:15:08 +00:00
}
void QtMaterialDialog::hideDialog()
{
2023-01-31 17:37:40 +00:00
Q_D(QtMaterialDialog);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
d->stateMachine->postEvent(
new QtMaterialStateTransitionEvent(DialogHideTransition));
setAttribute(Qt::WA_TransparentForMouseEvents);
d->proxyStack->setCurrentIndex(1);
2017-09-29 23:15:08 +00:00
}
2023-01-31 17:37:40 +00:00
void QtMaterialDialog::paintEvent(QPaintEvent* event)
2017-09-29 23:15:08 +00:00
{
2023-01-31 17:37:40 +00:00
Q_UNUSED(event)
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
Q_D(QtMaterialDialog);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QPainter painter(this);
2017-09-29 23:15:08 +00:00
2023-01-31 17:37:40 +00:00
QBrush brush;
brush.setStyle(Qt::SolidPattern);
brush.setColor(Qt::black);
painter.setBrush(brush);
painter.setPen(Qt::NoPen);
painter.setOpacity(d->proxy->opacity() / 2.4);
painter.drawRect(rect());
2017-09-29 23:15:08 +00:00
}