qtmaterialdialog.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. #include "qtmaterialdialog.h"
  2. #include "qtmaterialdialog_p.h"
  3. #include <QtWidgets/QStackedLayout>
  4. #include <QtWidgets/QGraphicsDropShadowEffect>
  5. #include <QStateMachine>
  6. #include <QState>
  7. #include <QtWidgets/QApplication>
  8. #include <QPropertyAnimation>
  9. #include <QPainter>
  10. #include "qtmaterialdialog_internal.h"
  11. #include "lib/qtmaterialstatetransition.h"
  12. /*!
  13. * \class QtMaterialDialogPrivate
  14. * \internal
  15. */
  16. QtMaterialDialogPrivate::QtMaterialDialogPrivate(QtMaterialDialog *q)
  17. : q_ptr(q)
  18. {
  19. }
  20. QtMaterialDialogPrivate::~QtMaterialDialogPrivate()
  21. {
  22. }
  23. void QtMaterialDialogPrivate::init()
  24. {
  25. Q_Q(QtMaterialDialog);
  26. dialogWindow = new QtMaterialDialogWindow(q);
  27. proxyStack = new QStackedLayout;
  28. stateMachine = new QStateMachine(q);
  29. proxy = new QtMaterialDialogProxy(dialogWindow, proxyStack, q);
  30. QVBoxLayout *layout = new QVBoxLayout;
  31. q->setLayout(layout);
  32. QWidget *widget = new QWidget;
  33. widget->setLayout(proxyStack);
  34. widget->setMinimumWidth(400);
  35. QGraphicsDropShadowEffect *effect = new QGraphicsDropShadowEffect;
  36. effect->setColor(QColor(0, 0, 0, 200));
  37. effect->setBlurRadius(64);
  38. effect->setOffset(0, 13);
  39. widget->setGraphicsEffect(effect);
  40. layout->addWidget(widget);
  41. layout->setAlignment(widget, Qt::AlignCenter);
  42. proxyStack->addWidget(dialogWindow);
  43. proxyStack->addWidget(proxy);
  44. proxyStack->setCurrentIndex(1);
  45. q->setAttribute(Qt::WA_TransparentForMouseEvents);
  46. QState *hiddenState = new QState;
  47. QState *visibleState = new QState;
  48. stateMachine->addState(hiddenState);
  49. stateMachine->addState(visibleState);
  50. stateMachine->setInitialState(hiddenState);
  51. QtMaterialStateTransition *transition;
  52. transition = new QtMaterialStateTransition(DialogShowTransition);
  53. transition->setTargetState(visibleState);
  54. hiddenState->addTransition(transition);
  55. transition = new QtMaterialStateTransition(DialogHideTransition);
  56. transition->setTargetState(hiddenState);
  57. visibleState->addTransition(transition);
  58. visibleState->assignProperty(proxy, "opacity", 1);
  59. visibleState->assignProperty(effect, "color", QColor(0, 0, 0, 200));
  60. visibleState->assignProperty(dialogWindow, "offset", 0);
  61. hiddenState->assignProperty(proxy, "opacity", 0);
  62. hiddenState->assignProperty(effect, "color", QColor(0, 0, 0, 0));
  63. hiddenState->assignProperty(dialogWindow, "offset", 200);
  64. QPropertyAnimation *animation;
  65. animation = new QPropertyAnimation(proxy, "opacity", q);
  66. animation->setDuration(280);
  67. stateMachine->addDefaultAnimation(animation);
  68. animation = new QPropertyAnimation(effect, "color", q);
  69. animation->setDuration(280);
  70. stateMachine->addDefaultAnimation(animation);
  71. animation = new QPropertyAnimation(dialogWindow, "offset", q);
  72. animation->setDuration(280);
  73. animation->setEasingCurve(QEasingCurve::OutCirc);
  74. stateMachine->addDefaultAnimation(animation);
  75. QObject::connect(visibleState, SIGNAL(propertiesAssigned()),
  76. proxy, SLOT(makeOpaque()));
  77. QObject::connect(hiddenState, SIGNAL(propertiesAssigned()),
  78. proxy, SLOT(makeTransparent()));
  79. stateMachine->start();
  80. QCoreApplication::processEvents();
  81. }
  82. /*!
  83. * \class QtMaterialDialog
  84. */
  85. QtMaterialDialog::QtMaterialDialog(QWidget *parent)
  86. : QtMaterialOverlayWidget(parent),
  87. d_ptr(new QtMaterialDialogPrivate(this))
  88. {
  89. d_func()->init();
  90. }
  91. QtMaterialDialog::~QtMaterialDialog()
  92. {
  93. }
  94. QLayout *QtMaterialDialog::windowLayout() const
  95. {
  96. Q_D(const QtMaterialDialog);
  97. return d->dialogWindow->layout();
  98. }
  99. void QtMaterialDialog::setWindowLayout(QLayout *layout)
  100. {
  101. Q_D(QtMaterialDialog);
  102. d->dialogWindow->setLayout(layout);
  103. }
  104. void QtMaterialDialog::showDialog()
  105. {
  106. Q_D(QtMaterialDialog);
  107. d->stateMachine->postEvent(new QtMaterialStateTransitionEvent(DialogShowTransition));
  108. raise();
  109. }
  110. void QtMaterialDialog::hideDialog()
  111. {
  112. Q_D(QtMaterialDialog);
  113. d->stateMachine->postEvent(new QtMaterialStateTransitionEvent(DialogHideTransition));
  114. setAttribute(Qt::WA_TransparentForMouseEvents);
  115. d->proxyStack->setCurrentIndex(1);
  116. }
  117. void QtMaterialDialog::paintEvent(QPaintEvent *event)
  118. {
  119. Q_UNUSED(event)
  120. Q_D(QtMaterialDialog);
  121. QPainter painter(this);
  122. QBrush brush;
  123. brush.setStyle(Qt::SolidPattern);
  124. brush.setColor(Qt::black);
  125. painter.setBrush(brush);
  126. painter.setPen(Qt::NoPen);
  127. painter.setOpacity(d->proxy->opacity()/2.4);
  128. painter.drawRect(rect());
  129. }