MaskDialogBase.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "MaskDialogBase.h"
  2. #include "QFluentWidgets.h"
  3. #include <QGraphicsDropShadowEffect>
  4. #include <QMouseEvent>
  5. #include <QPropertyAnimation>
  6. #include <QDebug>
  7. MaskDialogBase::MaskDialogBase(QWidget *parent) : QDialog(parent)
  8. {
  9. m_hBoxLayout = new QHBoxLayout(this);
  10. windowMask = new QWidget(this);
  11. // dialog box in the center of mask, all widgets take it as parent
  12. widget = new QFrame(this);
  13. widget->setObjectName("centerWidget");
  14. this->setWindowFlags(Qt::FramelessWindowHint);
  15. this->setAttribute(Qt::WA_TranslucentBackground);
  16. if (parent) {
  17. this->setGeometry(0, 0, parent->width(), parent->height());
  18. } else {
  19. this->setGeometry(0, 0, this->window()->width(), this->window()->height());
  20. }
  21. int c = QFWIns.isDarkTheme() ? 0 : 255;
  22. windowMask->resize(this->size());
  23. windowMask->setStyleSheet(QString("background:rgba({%1}, {%2}, {%3}, 0.6)").arg(c).arg(c).arg(c));
  24. m_hBoxLayout->addWidget(widget);
  25. setLayout(m_hBoxLayout);
  26. this->setShadowEffect();
  27. this->window()->installEventFilter(this);
  28. }
  29. void MaskDialogBase::setShadowEffect(float blurRadius, const QPointF &offset, const QColor &color)
  30. {
  31. QGraphicsDropShadowEffect *shadowEffect = new QGraphicsDropShadowEffect(widget);
  32. shadowEffect->setBlurRadius(blurRadius);
  33. shadowEffect->setOffset(offset);
  34. shadowEffect->setColor(color);
  35. widget->setGraphicsEffect(nullptr);
  36. widget->setGraphicsEffect(shadowEffect);
  37. }
  38. void MaskDialogBase::setMaskColor(const QColor &color)
  39. {
  40. windowMask->setStyleSheet(QString("background: rgba(%1, %2, %3, %4)")
  41. .arg(color.red())
  42. .arg(color.green())
  43. .arg(color.blue())
  44. .arg(color.alpha()));
  45. }
  46. bool MaskDialogBase::eventFilter(QObject *watched, QEvent *event)
  47. {
  48. if (watched == this->window()) {
  49. if (event->type() == QEvent::Resize) {
  50. QResizeEvent *re = static_cast<QResizeEvent *>(event);
  51. this->resize(re->size());
  52. }
  53. }
  54. return QDialog::eventFilter(watched, event);
  55. }
  56. void MaskDialogBase::resizeEvent(QResizeEvent * /*event*/)
  57. {
  58. windowMask->resize(this->size());
  59. }
  60. /// fade out
  61. void MaskDialogBase::closeEvent(QCloseEvent *event)
  62. {
  63. widget->setGraphicsEffect(nullptr);
  64. QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect(this);
  65. this->setGraphicsEffect(opacityEffect);
  66. QPropertyAnimation *opacityAni = new QPropertyAnimation(opacityEffect, "opacity", this);
  67. opacityAni->setStartValue(1);
  68. opacityAni->setEndValue(0);
  69. opacityAni->setDuration(100);
  70. opacityAni->setEasingCurve(QEasingCurve::OutCubic);
  71. connect(opacityAni, &QPropertyAnimation::finished, opacityAni, &QPropertyAnimation::deleteLater);
  72. opacityAni->start();
  73. event->accept(); //原作是ignore,有问题
  74. }
  75. /// fade in
  76. void MaskDialogBase::showEvent(QShowEvent *event)
  77. {
  78. QGraphicsOpacityEffect *opacityEffect = new QGraphicsOpacityEffect(this);
  79. this->setGraphicsEffect(opacityEffect);
  80. QPropertyAnimation *opacityAni = new QPropertyAnimation(opacityEffect, "opacity", this);
  81. opacityAni->setStartValue(0);
  82. opacityAni->setEndValue(1);
  83. opacityAni->setDuration(200);
  84. opacityAni->setEasingCurve(QEasingCurve::InSine);
  85. connect(opacityAni, &QPropertyAnimation::finished, opacityEffect, &QGraphicsOpacityEffect::deleteLater);
  86. opacityAni->start();
  87. QDialog::showEvent(event);
  88. }
  89. QHBoxLayout *MaskDialogBase::hBoxLayout() const
  90. {
  91. return m_hBoxLayout;
  92. }