Rename QPaintDevice::init() to initPainter()
[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_SHARED(QBrush)
149
150 /*****************************************************************************
151   QBrush stream functions
152  *****************************************************************************/
153
154 #ifndef QT_NO_DATASTREAM
155 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QBrush &);
156 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QBrush &);
157 #endif
158
159 #ifndef QT_NO_DEBUG_STREAM
160 Q_GUI_EXPORT QDebug operator<<(QDebug, const QBrush &);
161 #endif
162
163 struct QBrushData
164 {
165     QAtomicInt ref;
166     Qt::BrushStyle style;
167     QColor color;
168     QTransform transform;
169 };
170
171 inline Qt::BrushStyle QBrush::style() const { return d->style; }
172 inline const QColor &QBrush::color() const { return d->color; }
173 inline const QMatrix &QBrush::matrix() const { return d->transform.toAffine(); }
174 inline QTransform QBrush::transform() const { return d->transform; }
175 inline bool QBrush::isDetached() const { return d->ref.load() == 1; }
176
177
178 /*******************************************************************************
179  * QGradients
180  */
181 class QGradientPrivate;
182
183 typedef QPair<qreal, QColor> QGradientStop;
184 typedef QVector<QGradientStop> QGradientStops;
185
186 class Q_GUI_EXPORT QGradient
187 {
188     Q_GADGET
189     Q_ENUMS(Type Spread CoordinateMode)
190 public:
191     enum Type {
192         LinearGradient,
193         RadialGradient,
194         ConicalGradient,
195         NoGradient
196     };
197
198     enum Spread {
199         PadSpread,
200         ReflectSpread,
201         RepeatSpread
202     };
203
204     enum CoordinateMode {
205         LogicalMode,
206         StretchToDeviceMode,
207         ObjectBoundingMode
208     };
209
210     enum InterpolationMode {
211         ColorInterpolation,
212         ComponentInterpolation
213     };
214
215     QGradient();
216
217     Type type() const { return m_type; }
218
219     inline void setSpread(Spread spread);
220     Spread spread() const { return m_spread; }
221
222     void setColorAt(qreal pos, const QColor &color);
223
224     void setStops(const QGradientStops &stops);
225     QGradientStops stops() const;
226
227     CoordinateMode coordinateMode() const;
228     void setCoordinateMode(CoordinateMode mode);
229
230     InterpolationMode interpolationMode() const;
231     void setInterpolationMode(InterpolationMode mode);
232
233     bool operator==(const QGradient &gradient) const;
234     inline bool operator!=(const QGradient &other) const
235     { return !operator==(other); }
236
237 private:
238     friend class QLinearGradient;
239     friend class QRadialGradient;
240     friend class QConicalGradient;
241     friend class QBrush;
242
243     Type m_type;
244     Spread m_spread;
245     QGradientStops m_stops;
246     union {
247         struct {
248             qreal x1, y1, x2, y2;
249         } linear;
250         struct {
251             qreal cx, cy, fx, fy, cradius;
252         } radial;
253         struct {
254             qreal cx, cy, angle;
255         } conical;
256     } m_data;
257     void *dummy;
258 };
259
260 inline void QGradient::setSpread(Spread aspread)
261 { m_spread = aspread; }
262
263 class Q_GUI_EXPORT QLinearGradient : public QGradient
264 {
265 public:
266     QLinearGradient();
267     QLinearGradient(const QPointF &start, const QPointF &finalStop);
268     QLinearGradient(qreal xStart, qreal yStart, qreal xFinalStop, qreal yFinalStop);
269
270     QPointF start() const;
271     void setStart(const QPointF &start);
272     inline void setStart(qreal x, qreal y) { setStart(QPointF(x, y)); }
273
274     QPointF finalStop() const;
275     void setFinalStop(const QPointF &stop);
276     inline void setFinalStop(qreal x, qreal y) { setFinalStop(QPointF(x, y)); }
277 };
278
279
280 class Q_GUI_EXPORT QRadialGradient : public QGradient
281 {
282 public:
283     QRadialGradient();
284     QRadialGradient(const QPointF &center, qreal radius, const QPointF &focalPoint);
285     QRadialGradient(qreal cx, qreal cy, qreal radius, qreal fx, qreal fy);
286
287     QRadialGradient(const QPointF &center, qreal radius);
288     QRadialGradient(qreal cx, qreal cy, qreal radius);
289
290     QRadialGradient(const QPointF &center, qreal centerRadius, const QPointF &focalPoint, qreal focalRadius);
291     QRadialGradient(qreal cx, qreal cy, qreal centerRadius, qreal fx, qreal fy, qreal focalRadius);
292
293     QPointF center() const;
294     void setCenter(const QPointF &center);
295     inline void setCenter(qreal x, qreal y) { setCenter(QPointF(x, y)); }
296
297     QPointF focalPoint() const;
298     void setFocalPoint(const QPointF &focalPoint);
299     inline void setFocalPoint(qreal x, qreal y) { setFocalPoint(QPointF(x, y)); }
300
301     qreal radius() const;
302     void setRadius(qreal radius);
303
304     qreal centerRadius() const;
305     void setCenterRadius(qreal radius);
306
307     qreal focalRadius() const;
308     void setFocalRadius(qreal radius);
309 };
310
311
312 class Q_GUI_EXPORT QConicalGradient : public QGradient
313 {
314 public:
315     QConicalGradient();
316     QConicalGradient(const QPointF &center, qreal startAngle);
317     QConicalGradient(qreal cx, qreal cy, qreal startAngle);
318
319     QPointF center() const;
320     void setCenter(const QPointF &center);
321     inline void setCenter(qreal x, qreal y) { setCenter(QPointF(x, y)); }
322
323     qreal angle() const;
324     void setAngle(qreal angle);
325 };
326
327 QT_END_NAMESPACE
328
329 QT_END_HEADER
330
331 #endif // QBRUSH_H