AcrylicLabel.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "AcrylicLabel.h"
  2. #include "Common/ImageUtils.h"
  3. #include <QPainter>
  4. BlurCoverThread::BlurCoverThread(QObject *parent)
  5. : QThread(parent), m_imagePath(""), m_blurRadius(0), m_maxSize(QSize())
  6. {
  7. }
  8. void BlurCoverThread::blur(const QString &imagePath, int blurRadius, const QSize &maxSize)
  9. {
  10. m_imagePath = imagePath;
  11. m_blurRadius = blurRadius;
  12. m_maxSize = maxSize;
  13. start();
  14. }
  15. void BlurCoverThread::run()
  16. {
  17. if (m_imagePath.isEmpty()) {
  18. return;
  19. }
  20. GaussianBlur gaussBlur(m_blurRadius, 18);
  21. QPixmap pix;
  22. QImage origin(m_imagePath);
  23. if (m_maxSize.isValid()) {
  24. pix = QPixmap::fromImage(
  25. gaussBlur.BlurImage(origin.scaled(m_maxSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation)));
  26. } else {
  27. pix = QPixmap::fromImage(gaussBlur.BlurImage(origin));
  28. }
  29. emit blurFinished(pix);
  30. }
  31. AcrylicTextureLabel::AcrylicTextureLabel(const QColor &tintColor, const QColor &luminosityColor, double noiseOpacity,
  32. QWidget *parent)
  33. : QLabel(parent), m_tintColor(tintColor), m_luminosityColor(luminosityColor), m_noiseOpacity(noiseOpacity)
  34. {
  35. m_noiseImage = QImage(":/qfluentwidgets/images/acrylic/noise.png");
  36. setAttribute(Qt::WA_TranslucentBackground);
  37. }
  38. void AcrylicTextureLabel::setTintColor(const QColor &color)
  39. {
  40. m_tintColor = color;
  41. update();
  42. }
  43. void AcrylicTextureLabel::paintEvent(QPaintEvent * /*event*/)
  44. {
  45. QImage acrylicTexture = QImage(64, 64, QImage::Format_ARGB32_Premultiplied);
  46. // paint luminosity layer
  47. acrylicTexture.fill(m_luminosityColor);
  48. // paint tint color
  49. QPainter painter(&acrylicTexture);
  50. painter.fillRect(acrylicTexture.rect(), m_tintColor);
  51. // paint noise
  52. painter.setOpacity(m_noiseOpacity);
  53. painter.drawImage(acrylicTexture.rect(), m_noiseImage);
  54. QBrush acrylicBrush(acrylicTexture);
  55. QPainter painterThis(this);
  56. painterThis.fillRect(rect(), acrylicBrush);
  57. }
  58. AcrylicLabel::AcrylicLabel(int blurRadius, const QColor &tintColor, const QColor &luminosityColor,
  59. const QSize &maxBlurSize, QWidget *parent)
  60. : QLabel(parent),
  61. m_imagePath(""),
  62. m_blurRadius(blurRadius),
  63. m_maxBlurSize(maxBlurSize),
  64. m_blurPixmap(QPixmap()),
  65. m_acrylicTextureLabel(new AcrylicTextureLabel(tintColor, luminosityColor, 0.03, this)),
  66. m_blurThread(new BlurCoverThread(this))
  67. {
  68. connect(m_blurThread, &BlurCoverThread::blurFinished, this, &AcrylicLabel::onBlurFinished);
  69. }
  70. void AcrylicLabel::setImage(const QString &imagePath)
  71. {
  72. m_imagePath = imagePath;
  73. m_blurThread->blur(m_imagePath, m_blurRadius, m_maxBlurSize);
  74. }
  75. void AcrylicLabel::setTintColor(const QColor &color)
  76. {
  77. m_acrylicTextureLabel->setTintColor(color);
  78. }
  79. void AcrylicLabel::setBlurRadius(int blurRadius)
  80. {
  81. m_blurRadius = blurRadius;
  82. }
  83. void AcrylicLabel::setMaxBlurSize(const QSize &maxBlurSize)
  84. {
  85. m_maxBlurSize = maxBlurSize;
  86. }
  87. void AcrylicLabel::onBlurFinished(const QPixmap &pixmap)
  88. {
  89. m_blurPixmap = pixmap;
  90. setPixmap(m_blurPixmap);
  91. adjustSize();
  92. }
  93. void AcrylicLabel::resizeEvent(QResizeEvent *event)
  94. {
  95. QLabel::resizeEvent(event);
  96. m_acrylicTextureLabel->resize(size());
  97. if ((!m_blurPixmap.isNull()) && (m_blurPixmap.size() != size())) {
  98. setPixmap(m_blurPixmap.scaled(size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation));
  99. }
  100. }