Make QRegion not need to be friends with QVector
[profile/ivi/qtbase.git] / src / gui / painting / qbrush.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtGui module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QBRUSH_H
43 #define QBRUSH_H
44
45 #include <QtCore/qpair.h>
46 #include <QtCore/qpoint.h>
47 #include <QtCore/qvector.h>
48 #include <QtCore/qscopedpointer.h>
49 #include <QtGui/qcolor.h>
50 #include <QtGui/qmatrix.h>
51 #include <QtGui/qtransform.h>
52 #include <QtGui/qimage.h>
53 #include <QtGui/qpixmap.h>
54
55 #if defined(Q_OS_VXWORKS)
56 #  if defined(m_data)
57 #    undef m_data
58 #  endif
59 #  if defined(m_type)
60 #    undef m_type
61 #  endif
62 #endif
63
64 QT_BEGIN_HEADER
65
66 QT_BEGIN_NAMESPACE
67
68
69 struct QBrushData;
70 class QPixmap;
71 class QGradient;
72 class QVariant;
73 struct QBrushDataPointerDeleter;
74
75 class Q_GUI_EXPORT QBrush
76 {
77 public:
78     QBrush();
79     QBrush(Qt::BrushStyle bs);
80     QBrush(const QColor &color, Qt::BrushStyle bs=Qt::SolidPattern);
81     QBrush(Qt::GlobalColor color, Qt::BrushStyle bs=Qt::SolidPattern);
82
83     QBrush(const QColor &color, const QPixmap &pixmap);
84     QBrush(Qt::GlobalColor color, const QPixmap &pixmap);
85     QBrush(const QPixmap &pixmap);
86     QBrush(const QImage &image);
87
88     QBrush(const QBrush &brush);
89
90     QBrush(const QGradient &gradient);
91
92     ~QBrush();
93     QBrush &operator=(const QBrush &brush);
94 #ifdef Q_COMPILER_RVALUE_REFS
95     inline QBrush &operator=(QBrush &&other)
96     { qSwap(d, other.d); return *this; }
97 #endif
98     inline void swap(QBrush &other) { qSwap(d, other.d); }
99
100     operator QVariant() const;
101
102     inline Qt::BrushStyle style() const;
103     void setStyle(Qt::BrushStyle);
104
105     inline const QMatrix &matrix() const;
106     void setMatrix(const QMatrix &mat);
107
108     inline QTransform transform() const;
109     void setTransform(const QTransform &);
110
111     QPixmap texture() const;
112     void setTexture(const QPixmap &pixmap);
113
114     QImage textureImage() const;
115     void setTextureImage(const QImage &image);
116
117     inline const QColor &color() const;
118     void setColor(const QColor &color);
119     inline void setColor(Qt::GlobalColor color);
120
121     const QGradient *gradient() const;
122
123     bool isOpaque() const;
124
125     bool operator==(const QBrush &b) const;
126     inline bool operator!=(const QBrush &b) const { return !(operator==(b)); }
127
128 private:
129     friend class QRasterPaintEngine;
130     friend class QRasterPaintEnginePrivate;
131     friend struct QSpanData;
132     friend class QPainter;
133     friend bool Q_GUI_EXPORT qHasPixmapTexture(const QBrush& brush);
134     void detach(Qt::BrushStyle newStyle);
135     void init(const QColor &color, Qt::BrushStyle bs);
136     QScopedPointer<QBrushData, QBrushDataPointerDeleter> d;
137     void cleanUp(QBrushData *x);
138
139 public:
140     inline bool isDetached() const;
141     typedef QScopedPointer<QBrushData, QBrushDataPointerDeleter> DataPtr;
142     inline DataPtr &data_ptr() { return d; }
143 };
144
145 inline void QBrush::setColor(Qt::GlobalColor acolor)
146 { setColor(QColor(acolor)); }
147
148 Q_DECLARE_TYPEINFO(QBrush, Q_MOVABLE_TYPE);
149 Q_DECLARE_SHARED(QBrush)
150
151 /*****************************************************************************
152   QBrush stream functions
153  *****************************************************************************/
154
155 #ifndef QT_NO_DATASTREAM
156 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QBrush &);
157 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QBrush &);
158 #endif
159
160 #ifndef QT_NO_DEBUG_STREAM
161 Q_GUI_EXPORT QDebug operator<<(QDebug, const QBrush &);
162 #endif
163
164 struct QBrushData
165 {
166     QAtomicInt ref;
167     Qt::BrushStyle style;
168     QColor color;
169     QTransform transform;
170 };
171
172 inline Qt::BrushStyle QBrush::style() const { return d->style; }
173 inline const QColor &QBrush::color() const { return d->color; }
174 inline const QMatrix &QBrush::matrix() const { return d->transform.toAffine(); }
175 inline QTransform QBrush::transform() const { return d->transform; }
176 inline bool QBrush::isDetached() const { return d->ref.load() == 1; }
177
178
179 /*******************************************************************************
180  * QGradients
181  */
182 class QGradientPrivate;
183
184 typedef QPair<qreal, QColor> QGradientStop;
185 typedef QVector<QGradientStop> QGradientStops;
186
187 class Q_GUI_EXPORT QGradient
188 {
189     Q_GADGET
190     Q_ENUMS(Type Spread CoordinateMode)
191 public:
192     enum Type {
193         LinearGradient,
194         RadialGradient,
195         ConicalGradient,
196         NoGradient
197     };
198
199     enum Spread {
200         PadSpread,
201         ReflectSpread,
202         RepeatSpread
203     };
204
205     enum CoordinateMode {
206         LogicalMode,
207         StretchToDeviceMode,
208         ObjectBoundingMode
209     };
210
211     enum InterpolationMode {
212         ColorInterpolation,
213         ComponentInterpolation
214     };
215
216     QGradient();
217
218     Type type() const { return m_type; }
219
220     inline void setSpread(Spread spread);
221     Spread spread() const { return m_spread; }
222
223     void setColorAt(qreal pos, const QColor &color);
224
225     void setStops(const QGradientStops &stops);
226     QGradientStops stops() const;
227
228     CoordinateMode coordinateMode() const;
229     void setCoordinateMode(CoordinateMode mode);
230
231     InterpolationMode interpolationMode() const;
232     void setInterpolationMode(InterpolationMode mode);
233
234     bool operator==(const QGradient &gradient) const;
235     inline bool operator!=(const QGradient &other) const
236     { return !operator==(other); }
237
238 private:
239     friend class QLinearGradient;
240     friend class QRadialGradient;
241     friend class QConicalGradient;
242     friend class QBrush;
243
244     Type m_type;
245     Spread m_spread;
246     QGradientStops m_stops;
247     union {
248         struct {
249             qreal x1, y1, x2, y2;
250         } linear;
251         struct {
252             qreal cx, cy, fx, fy, cradius;
253         } radial;
254         struct {
255             qreal cx, cy, angle;
256         } conical;
257     } m_data;
258     void *dummy;
259 };
260
261 inline void QGradient::setSpread(Spread aspread)
262 { m_spread = aspread; }
263
264 class Q_GUI_EXPORT QLinearGradient : public QGradient
265 {
266 public:
267     QLinearGradient();
268     QLinearGradient(const QPointF &start, const QPointF &finalStop);
269     QLinearGradient(qreal xStart, qreal yStart, qreal xFinalStop, qreal yFinalStop);
270
271     QPointF start() const;
272     void setStart(const QPointF &start);
273     inline void setStart(qreal x, qreal y) { setStart(QPointF(x, y)); }
274
275     QPointF finalStop() const;
276     void setFinalStop(const QPointF &stop);
277     inline void setFinalStop(qreal x, qreal y) { setFinalStop(QPointF(x, y)); }
278 };
279
280
281 class Q_GUI_EXPORT QRadialGradient : public QGradient
282 {
283 public:
284     QRadialGradient();
285     QRadialGradient(const QPointF &center, qreal radius, const QPointF &focalPoint);
286     QRadialGradient(qreal cx, qreal cy, qreal radius, qreal fx, qreal fy);
287
288     QRadialGradient(const QPointF &center, qreal radius);
289     QRadialGradient(qreal cx, qreal cy, qreal radius);
290
291     QRadialGradient(const QPointF &center, qreal centerRadius, const QPointF &focalPoint, qreal focalRadius);
292     QRadialGradient(qreal cx, qreal cy, qreal centerRadius, qreal fx, qreal fy, qreal focalRadius);
293
294     QPointF center() const;
295     void setCenter(const QPointF &center);
296     inline void setCenter(qreal x, qreal y) { setCenter(QPointF(x, y)); }
297
298     QPointF focalPoint() const;
299     void setFocalPoint(const QPointF &focalPoint);
300     inline void setFocalPoint(qreal x, qreal y) { setFocalPoint(QPointF(x, y)); }
301
302     qreal radius() const;
303     void setRadius(qreal radius);
304
305     qreal centerRadius() const;
306     void setCenterRadius(qreal radius);
307
308     qreal focalRadius() const;
309     void setFocalRadius(qreal radius);
310 };
311
312
313 class Q_GUI_EXPORT QConicalGradient : public QGradient
314 {
315 public:
316     QConicalGradient();
317     QConicalGradient(const QPointF &center, qreal startAngle);
318     QConicalGradient(qreal cx, qreal cy, qreal startAngle);
319
320     QPointF center() const;
321     void setCenter(const QPointF &center);
322     inline void setCenter(qreal x, qreal y) { setCenter(QPointF(x, y)); }
323
324     qreal angle() const;
325     void setAngle(qreal angle);
326 };
327
328 QT_END_NAMESPACE
329
330 QT_END_HEADER
331
332 #endif // QBRUSH_H