1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QGRAPHICSEFFECT_H
5#define QGRAPHICSEFFECT_H
6
7#include <QtWidgets/qtwidgetsglobal.h>
8#include <QtCore/qobject.h>
9#include <QtCore/qpoint.h>
10#include <QtCore/qrect.h>
11#include <QtGui/qcolor.h>
12#include <QtGui/qbrush.h>
13
14QT_REQUIRE_CONFIG(graphicseffect);
15
16QT_BEGIN_NAMESPACE
17
18class QGraphicsItem;
19class QStyleOption;
20class QPainter;
21class QPixmap;
22
23class QGraphicsEffectSource;
24
25class QGraphicsEffectPrivate;
26class Q_WIDGETS_EXPORT QGraphicsEffect : public QObject
27{
28 Q_OBJECT
29 Q_PROPERTY(bool enabled READ isEnabled WRITE setEnabled NOTIFY enabledChanged)
30public:
31 enum ChangeFlag {
32 SourceAttached = 0x1,
33 SourceDetached = 0x2,
34 SourceBoundingRectChanged = 0x4,
35 SourceInvalidated = 0x8
36 };
37 Q_DECLARE_FLAGS(ChangeFlags, ChangeFlag)
38 Q_FLAG(ChangeFlags)
39
40 enum PixmapPadMode {
41 NoPad,
42 PadToTransparentBorder,
43 PadToEffectiveBoundingRect
44 };
45
46 QGraphicsEffect(QObject *parent = nullptr);
47 virtual ~QGraphicsEffect();
48
49 virtual QRectF boundingRectFor(const QRectF &sourceRect) const;
50 QRectF boundingRect() const;
51
52 bool isEnabled() const;
53
54public Q_SLOTS:
55 void setEnabled(bool enable);
56 void update();
57
58Q_SIGNALS:
59 void enabledChanged(bool enabled);
60
61protected:
62 QGraphicsEffect(QGraphicsEffectPrivate &d, QObject *parent = nullptr);
63 virtual void draw(QPainter *painter) = 0;
64 virtual void sourceChanged(ChangeFlags flags);
65 void updateBoundingRect();
66
67 bool sourceIsPixmap() const;
68 QRectF sourceBoundingRect(Qt::CoordinateSystem system = Qt::LogicalCoordinates) const;
69 void drawSource(QPainter *painter);
70 QPixmap sourcePixmap(Qt::CoordinateSystem system = Qt::LogicalCoordinates,
71 QPoint *offset = nullptr,
72 PixmapPadMode mode = PadToEffectiveBoundingRect) const;
73
74private:
75 Q_DECLARE_PRIVATE(QGraphicsEffect)
76 Q_DISABLE_COPY(QGraphicsEffect)
77 friend class QGraphicsItem;
78 friend class QGraphicsItemPrivate;
79 friend class QGraphicsScenePrivate;
80 friend class QWidget;
81 friend class QWidgetPrivate;
82
83public:
84 QGraphicsEffectSource *source() const; // internal
85
86};
87Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsEffect::ChangeFlags)
88
89class QGraphicsColorizeEffectPrivate;
90class Q_WIDGETS_EXPORT QGraphicsColorizeEffect: public QGraphicsEffect
91{
92 Q_OBJECT
93 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
94 Q_PROPERTY(qreal strength READ strength WRITE setStrength NOTIFY strengthChanged)
95public:
96 QGraphicsColorizeEffect(QObject *parent = nullptr);
97 ~QGraphicsColorizeEffect();
98
99 QColor color() const;
100 qreal strength() const;
101
102public Q_SLOTS:
103 void setColor(const QColor &c);
104 void setStrength(qreal strength);
105
106Q_SIGNALS:
107 void colorChanged(const QColor &color);
108 void strengthChanged(qreal strength);
109
110protected:
111 void draw(QPainter *painter) override;
112
113private:
114 Q_DECLARE_PRIVATE(QGraphicsColorizeEffect)
115 Q_DISABLE_COPY(QGraphicsColorizeEffect)
116};
117
118class QGraphicsBlurEffectPrivate;
119class Q_WIDGETS_EXPORT QGraphicsBlurEffect: public QGraphicsEffect
120{
121 Q_OBJECT
122 Q_PROPERTY(qreal blurRadius READ blurRadius WRITE setBlurRadius NOTIFY blurRadiusChanged)
123 Q_PROPERTY(BlurHints blurHints READ blurHints WRITE setBlurHints NOTIFY blurHintsChanged)
124public:
125 enum BlurHint {
126 PerformanceHint = 0x00,
127 QualityHint = 0x01,
128 AnimationHint = 0x02
129 };
130 Q_ENUM(BlurHint)
131 Q_DECLARE_FLAGS(BlurHints, BlurHint)
132 Q_FLAG(BlurHints)
133
134 QGraphicsBlurEffect(QObject *parent = nullptr);
135 ~QGraphicsBlurEffect();
136
137 QRectF boundingRectFor(const QRectF &rect) const override;
138 qreal blurRadius() const;
139 BlurHints blurHints() const;
140
141public Q_SLOTS:
142 void setBlurRadius(qreal blurRadius);
143 void setBlurHints(BlurHints hints);
144
145Q_SIGNALS:
146 void blurRadiusChanged(qreal blurRadius);
147 void blurHintsChanged(BlurHints hints);
148
149protected:
150 void draw(QPainter *painter) override;
151
152private:
153 Q_DECLARE_PRIVATE(QGraphicsBlurEffect)
154 Q_DISABLE_COPY(QGraphicsBlurEffect)
155};
156
157Q_DECLARE_OPERATORS_FOR_FLAGS(QGraphicsBlurEffect::BlurHints)
158
159class QGraphicsDropShadowEffectPrivate;
160class Q_WIDGETS_EXPORT QGraphicsDropShadowEffect: public QGraphicsEffect
161{
162 Q_OBJECT
163 Q_PROPERTY(QPointF offset READ offset WRITE setOffset NOTIFY offsetChanged)
164 Q_PROPERTY(qreal xOffset READ xOffset WRITE setXOffset NOTIFY offsetChanged)
165 Q_PROPERTY(qreal yOffset READ yOffset WRITE setYOffset NOTIFY offsetChanged)
166 Q_PROPERTY(qreal blurRadius READ blurRadius WRITE setBlurRadius NOTIFY blurRadiusChanged)
167 Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
168public:
169 QGraphicsDropShadowEffect(QObject *parent = nullptr);
170 ~QGraphicsDropShadowEffect();
171
172 QRectF boundingRectFor(const QRectF &rect) const override;
173 QPointF offset() const;
174
175 inline qreal xOffset() const
176 { return offset().x(); }
177
178 inline qreal yOffset() const
179 { return offset().y(); }
180
181 qreal blurRadius() const;
182 QColor color() const;
183
184public Q_SLOTS:
185 void setOffset(const QPointF &ofs);
186
187 inline void setOffset(qreal dx, qreal dy)
188 { setOffset(QPointF(dx, dy)); }
189
190 inline void setOffset(qreal d)
191 { setOffset(QPointF(d, d)); }
192
193 inline void setXOffset(qreal dx)
194 { setOffset(QPointF(dx, yOffset())); }
195
196 inline void setYOffset(qreal dy)
197 { setOffset(QPointF(xOffset(), dy)); }
198
199 void setBlurRadius(qreal blurRadius);
200 void setColor(const QColor &color);
201
202Q_SIGNALS:
203 void offsetChanged(const QPointF &offset);
204 void blurRadiusChanged(qreal blurRadius);
205 void colorChanged(const QColor &color);
206
207protected:
208 void draw(QPainter *painter) override;
209
210private:
211 Q_DECLARE_PRIVATE(QGraphicsDropShadowEffect)
212 Q_DISABLE_COPY(QGraphicsDropShadowEffect)
213};
214
215class QGraphicsOpacityEffectPrivate;
216class Q_WIDGETS_EXPORT QGraphicsOpacityEffect: public QGraphicsEffect
217{
218 Q_OBJECT
219 Q_PROPERTY(qreal opacity READ opacity WRITE setOpacity NOTIFY opacityChanged)
220 Q_PROPERTY(QBrush opacityMask READ opacityMask WRITE setOpacityMask NOTIFY opacityMaskChanged)
221public:
222 QGraphicsOpacityEffect(QObject *parent = nullptr);
223 ~QGraphicsOpacityEffect();
224
225 qreal opacity() const;
226 QBrush opacityMask() const;
227
228public Q_SLOTS:
229 void setOpacity(qreal opacity);
230 void setOpacityMask(const QBrush &mask);
231
232Q_SIGNALS:
233 void opacityChanged(qreal opacity);
234 void opacityMaskChanged(const QBrush &mask);
235
236protected:
237 void draw(QPainter *painter) override;
238
239private:
240 Q_DECLARE_PRIVATE(QGraphicsOpacityEffect)
241 Q_DISABLE_COPY(QGraphicsOpacityEffect)
242};
243
244QT_END_NAMESPACE
245
246#endif // QGRAPHICSEFFECT_H
247
248

source code of qtbase/src/widgets/effects/qgraphicseffect.h