HAL
shadow_effect.cpp
Go to the documentation of this file.
2 
3 #include <QPainter>
4 
5 QT_BEGIN_NAMESPACE
6 extern Q_WIDGETS_EXPORT void qt_blurImage(QPainter* p, QImage& blurImage, qreal radius, bool quality, bool alphaOnly, int transposed = 0);
7 QT_END_NAMESPACE
8 
9 namespace hal
10 {
11  ShadowEffect::ShadowEffect(QObject* parent) : QGraphicsEffect(parent), mDistance(4.0f), mBlurRadius(10.0f), mColor(0, 0, 0, 80)
12  {
13  }
14 
16  {
17  // if nothing to show outside the item, just draw source
18  if ((blurRadius() + distance()) <= 0)
19  {
20  drawSource(painter);
21  return;
22  }
23 
25  QPoint offset;
26  const QPixmap px = sourcePixmap(Qt::DeviceCoordinates, &offset, mode);
27 
28  // return if no source
29  if (px.isNull())
30  return;
31 
32  // save world transform
33  QTransform restoreTransform = painter->worldTransform();
34  painter->setWorldTransform(QTransform());
35 
36  // Calculate size for the background image
37  QSize szi(px.size().width() + 2 * distance(), px.size().height() + 2 * distance());
38 
40  QPixmap scaled = px.scaled(szi);
41  tmp.fill(0);
42  QPainter tmpPainter(&tmp);
44  tmpPainter.drawPixmap(QPointF(-distance(), -distance()), scaled);
45  tmpPainter.end();
46 
47  // blur the alpha channel
49  blurred.fill(0);
50  QPainter blurPainter(&blurred);
51  qt_blurImage(&blurPainter, tmp, blurRadius(), false, true);
52  blurPainter.end();
53 
54  tmp = blurred;
55 
56  // blacken the image...
57  tmpPainter.begin(&tmp);
59  tmpPainter.fillRect(tmp.rect(), color());
60  tmpPainter.end();
61 
62  // draw the blurred shadow...
63  painter->drawImage(offset, tmp);
64 
65  // draw the actual pixmap...
66  painter->drawPixmap(offset, px, QRectF());
67 
68  // restore world transform
69  painter->setWorldTransform(restoreTransform);
70  }
71 
73  {
74  qreal delta = blurRadius() + distance();
75  return rect.united(rect.adjusted(-delta, -delta, delta, delta));
76  }
77 }
QColor color() const
void draw(QPainter *painter)
qreal distance() const
Definition: shadow_effect.h:84
ShadowEffect(QObject *parent=nullptr)
qreal blurRadius() const
QRectF boundingRectFor(const QRectF &rect) const
void drawSource(QPainter *painter)
QPixmap sourcePixmap(Qt::CoordinateSystem system, QPoint *offset, QGraphicsEffect::PixmapPadMode mode) const const
Format_ARGB32_Premultiplied
void fill(uint pixelValue)
QRect rect() const const
QSize size() const const
CompositionMode_Source
bool begin(QPaintDevice *device)
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
bool end()
void fillRect(const QRectF &rectangle, const QBrush &brush)
void setCompositionMode(QPainter::CompositionMode mode)
void setWorldTransform(const QTransform &matrix, bool combine)
const QTransform & worldTransform() const const
bool isNull() const const
QPixmap scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode, Qt::TransformationMode transformMode) const const
QSize size() const const
QRectF adjusted(qreal dx1, qreal dy1, qreal dx2, qreal dy2) const const
QRectF united(const QRectF &rectangle) const const
int height() const const
int width() const const
DeviceCoordinates
QT_BEGIN_NAMESPACE Q_WIDGETS_EXPORT void qt_blurImage(QPainter *p, QImage &blurImage, qreal radius, bool quality, bool alphaOnly, int transposed=0)