idropshadowable.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "idropshadowable.h"
  2. #include "dropshadowmgr.h"
  3. #include "../ImageBlur/imageblur.h"
  4. #include "../PaintHelper/painthelper.h"
  5. #include <QDebug>
  6. IDropShadowable::IDropShadowable(QWidget *selfWidget)
  7. : m_pSelfWidget(selfWidget)
  8. {
  9. selfWidget->installEventFilter(DropShadowMgr::Instance());
  10. }
  11. void IDropShadowable::SetDropShadow(const BoxShadow &boxShadow, const QSize &size)
  12. {
  13. ClearDropShadow();
  14. DropShadowMgr::sm_cacheShadows.clear();
  15. m_listShadows.append(boxShadow);
  16. if(!DropShadowMgr::sm_cacheShadows.contains(boxShadow))
  17. {
  18. int radius = boxShadow.radius;
  19. int imageWidth = size.width()+radius*2;
  20. int imageHeight = size.height()+radius*2;
  21. QImage image(imageWidth, imageHeight, QImage::Format_ARGB32);
  22. image.fill(boxShadow.color);
  23. //边框设为透明
  24. for(int x=0;x<image.width();x++)
  25. {
  26. for(int y=0;y<image.height();y++)
  27. {
  28. if(x>=radius && x<(image.width()-radius) && y>=radius && y<(image.height()-radius)) continue;
  29. image.setPixelColor(QPoint(x,y), Qt::transparent);
  30. }
  31. }
  32. GaussBlur::Blur(image, boxShadow.radius);
  33. BoxShadow shadow_image(boxShadow);
  34. shadow_image.image = image;
  35. DropShadowMgr::sm_cacheShadows.append(shadow_image);
  36. }
  37. m_pSelfWidget->update();
  38. }
  39. void IDropShadowable::SetDropShadows(const QList<BoxShadow> &shadows, const QSize &size)
  40. {
  41. Q_UNUSED(shadows)
  42. Q_UNUSED(size)
  43. //找到最大的radius, x偏移, y偏移
  44. return;
  45. }
  46. void IDropShadowable::ClearDropShadow()
  47. {
  48. m_listShadows.clear();
  49. m_pSelfWidget->update();
  50. }
  51. void IDropShadowable::PaintShadows()
  52. {
  53. PainterEx painter(m_pSelfWidget);
  54. painter.setRenderHint(QPainter::Antialiasing);
  55. for(const BoxShadow &boxShadow: m_listShadows)
  56. {
  57. int index = DropShadowMgr::sm_cacheShadows.indexOf(boxShadow);
  58. if(index == -1) continue;
  59. QImage image = DropShadowMgr::sm_cacheShadows.at(index).image;
  60. painter.drawImage(image.rect(), image, image.rect());
  61. }
  62. }