libplasma
shadowitem.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "shadowitem_p.h"
00021
00022 #include "effects/blur.cpp"
00023
00024 #include <QPainter>
00025 #include <QImage>
00026 #include <QDebug>
00027
00028 namespace Plasma
00029 {
00030
00031 ShadowItem::ShadowItem(QGraphicsItem *item)
00032 {
00033 setZValue(20);
00034 m_shadowParent = 0;
00035 m_offset = QPointF(12, 12);
00036 setShadowParent(item);
00037 }
00038
00039 void ShadowItem::setShadowParent(QGraphicsItem *item)
00040 {
00041 m_shadowParent = item;
00042 adjustPosition();
00043 generate();
00044 }
00045
00046
00047 QGraphicsItem * ShadowItem::shadowParent() const
00048 {
00049 return m_shadowParent;
00050 }
00051
00052 QSize ShadowItem::shadowedSize() const
00053 {
00054 QSize s = boundingRect().size().toSize();
00055 return s - QSize(32, 32);
00056 }
00057
00058 void ShadowItem::generate()
00059 {
00060 if (!m_shadowParent) {
00061 return;
00062 }
00063
00064 QPainterPath path = m_shadowParent->shape();
00065 QRectF rect = path.boundingRect();
00066 QSize s = rect.size().toSize() + QSize(30, 30);
00067 QImage img(s, QImage::Format_ARGB32_Premultiplied);
00068 img.fill(0);
00069 QPainter p(&img);
00070 p.translate(15, 15);
00071 p.setRenderHint(QPainter::Antialiasing);
00072 p.setCompositionMode(QPainter::CompositionMode_Source);
00073 p.fillPath(path, Qt::gray);
00074 p.end();
00075 #ifdef DO_GLOW
00076 QImage blurred = img;
00077 expblur<16, 7>(img, 7);
00078 p.begin(&img);
00079 p.setCompositionMode(QPainter::CompositionMode_Plus);
00080 p.drawImage(0, 0, blurred);
00081 p.end();
00082 #else
00083 expblur<16, 7>(img, 7);
00084 #endif
00085
00086 setPixmap(QPixmap::fromImage(img));
00087 }
00088
00089
00090 void ShadowItem::adjustPosition()
00091 {
00092 if (!m_shadowParent) {
00093 return;
00094 }
00095
00096 setPos(m_shadowParent->pos() - m_offset);
00097 }
00098
00099 void ShadowItem::setOffset(const QPointF &offset)
00100 {
00101 m_offset = offset;
00102 }
00103
00104 QPointF ShadowItem::offset() const
00105 {
00106 return m_offset;
00107 }
00108
00109 }