1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include "idropshadowable.h"
- #include "dropshadowmgr.h"
- #include "../ImageBlur/imageblur.h"
- #include "../PaintHelper/painthelper.h"
- #include <QDebug>
- IDropShadowable::IDropShadowable(QWidget *selfWidget)
- : m_pSelfWidget(selfWidget)
- {
- selfWidget->installEventFilter(DropShadowMgr::Instance());
- }
- void IDropShadowable::SetDropShadow(const BoxShadow &boxShadow, const QSize &size)
- {
- ClearDropShadow();
- DropShadowMgr::sm_cacheShadows.clear();
- m_listShadows.append(boxShadow);
- if(!DropShadowMgr::sm_cacheShadows.contains(boxShadow))
- {
- int radius = boxShadow.radius;
- int imageWidth = size.width()+radius*2;
- int imageHeight = size.height()+radius*2;
- QImage image(imageWidth, imageHeight, QImage::Format_ARGB32);
- image.fill(boxShadow.color);
- //边框设为透明
- for(int x=0;x<image.width();x++)
- {
- for(int y=0;y<image.height();y++)
- {
- if(x>=radius && x<(image.width()-radius) && y>=radius && y<(image.height()-radius)) continue;
- image.setPixelColor(QPoint(x,y), Qt::transparent);
- }
- }
- GaussBlur::Blur(image, boxShadow.radius);
-
- BoxShadow shadow_image(boxShadow);
- shadow_image.image = image;
- DropShadowMgr::sm_cacheShadows.append(shadow_image);
- }
- m_pSelfWidget->update();
- }
- void IDropShadowable::SetDropShadows(const QList<BoxShadow> &shadows, const QSize &size)
- {
- Q_UNUSED(shadows)
- Q_UNUSED(size)
- //找到最大的radius, x偏移, y偏移
- return;
- }
- void IDropShadowable::ClearDropShadow()
- {
- m_listShadows.clear();
- m_pSelfWidget->update();
- }
- void IDropShadowable::PaintShadows()
- {
- PainterEx painter(m_pSelfWidget);
- painter.setRenderHint(QPainter::Antialiasing);
- for(const BoxShadow &boxShadow: m_listShadows)
- {
- int index = DropShadowMgr::sm_cacheShadows.indexOf(boxShadow);
- if(index == -1) continue;
- QImage image = DropShadowMgr::sm_cacheShadows.at(index).image;
- painter.drawImage(image.rect(), image, image.rect());
- }
- }
|