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