Make QRegion not need to be friends with QVector
[profile/ivi/qtbase.git] / src / gui / painting / qpainter_p.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 QPAINTER_P_H
43 #define QPAINTER_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "QtGui/qbrush.h"
57 #include "QtGui/qfont.h"
58 #include "QtGui/qpen.h"
59 #include "QtGui/qregion.h"
60 #include "QtGui/qmatrix.h"
61 #include "QtGui/qpainter.h"
62 #include "QtGui/qpainterpath.h"
63 #include "QtGui/qpaintengine.h"
64 #include <QtCore/qhash.h>
65
66 #include <private/qpen_p.h>
67
68 QT_BEGIN_NAMESPACE
69
70 class QPaintEngine;
71 class QEmulationPaintEngine;
72 class QPaintEngineEx;
73 struct QFixedPoint;
74
75 struct QTLWExtra;
76
77 struct DataPtrContainer {
78     void *ptr;
79 };
80
81 inline void *data_ptr(const QTransform &t) { return (DataPtrContainer *) &t; }
82 inline bool qtransform_fast_equals(const QTransform &a, const QTransform &b) { return data_ptr(a) == data_ptr(b); }
83
84 // QPen inline functions...
85 inline QPen::DataPtr &data_ptr(const QPen &p) { return const_cast<QPen &>(p).data_ptr(); }
86 inline bool qpen_fast_equals(const QPen &a, const QPen &b) { return data_ptr(a) == data_ptr(b); }
87 inline QBrush qpen_brush(const QPen &p) { return data_ptr(p)->brush; }
88 inline qreal qpen_widthf(const QPen &p) { return data_ptr(p)->width; }
89 inline Qt::PenStyle qpen_style(const QPen &p) { return data_ptr(p)->style; }
90 inline Qt::PenCapStyle qpen_capStyle(const QPen &p) { return data_ptr(p)->capStyle; }
91 inline Qt::PenJoinStyle qpen_joinStyle(const QPen &p) { return data_ptr(p)->joinStyle; }
92
93 // QBrush inline functions...
94 inline QBrush::DataPtr &data_ptr(const QBrush &p) { return const_cast<QBrush &>(p).data_ptr(); }
95 inline bool qbrush_fast_equals(const QBrush &a, const QBrush &b) { return data_ptr(a) == data_ptr(b); }
96 inline Qt::BrushStyle qbrush_style(const QBrush &b) { return data_ptr(b)->style; }
97 inline const QColor &qbrush_color(const QBrush &b) { return data_ptr(b)->color; }
98 inline bool qbrush_has_transform(const QBrush &b) { return data_ptr(b)->transform.type() > QTransform::TxNone; }
99
100 class QPainterClipInfo
101 {
102 public:
103     enum ClipType { RegionClip, PathClip, RectClip, RectFClip };
104
105     QPainterClipInfo(const QPainterPath &p, Qt::ClipOperation op, const QTransform &m) :
106         clipType(PathClip), matrix(m), operation(op), path(p) { }
107
108     QPainterClipInfo(const QRegion &r, Qt::ClipOperation op, const QTransform &m) :
109         clipType(RegionClip), matrix(m), operation(op), region(r) { }
110
111     QPainterClipInfo(const QRect &r, Qt::ClipOperation op, const QTransform &m) :
112         clipType(RectClip), matrix(m), operation(op), rect(r) { }
113
114     QPainterClipInfo(const QRectF &r, Qt::ClipOperation op, const QTransform &m) :
115         clipType(RectFClip), matrix(m), operation(op), rectf(r) { }
116
117     ClipType clipType;
118     QTransform matrix;
119     Qt::ClipOperation operation;
120     QPainterPath path;
121     QRegion region;
122     QRect rect;
123     QRectF rectf;
124
125     // ###
126 //     union {
127 //         QRegionData *d;
128 //         QPainterPathPrivate *pathData;
129
130 //         struct {
131 //             int x, y, w, h;
132 //         } rectData;
133 //         struct {
134 //             qreal x, y, w, h;
135 //         } rectFData;
136 //     };
137
138 };
139
140
141 class Q_GUI_EXPORT QPainterState : public QPaintEngineState
142 {
143 public:
144     QPainterState();
145     QPainterState(const QPainterState *s);
146     virtual ~QPainterState();
147     void init(QPainter *p);
148
149     QPointF brushOrigin;
150     QFont font;
151     QFont deviceFont;
152     QPen pen;
153     QBrush brush;
154     QBrush bgBrush;             // background brush
155     QRegion clipRegion;
156     QPainterPath clipPath;
157     Qt::ClipOperation clipOperation;
158     QPainter::RenderHints renderHints;
159     QList<QPainterClipInfo> clipInfo; // ### Make me smaller and faster to copy around...
160     QTransform worldMatrix;       // World transformation matrix, not window and viewport
161     QTransform matrix;            // Complete transformation matrix,
162     QTransform redirectionMatrix;
163     int wx, wy, ww, wh;         // window rectangle
164     int vx, vy, vw, vh;         // viewport rectangle
165     qreal opacity;
166
167     uint WxF:1;                 // World transformation
168     uint VxF:1;                 // View transformation
169     uint clipEnabled:1;
170
171     Qt::BGMode bgMode;
172     QPainter *painter;
173     Qt::LayoutDirection layoutDirection;
174     QPainter::CompositionMode composition_mode;
175     uint emulationSpecifier;
176     uint changeFlags;
177 };
178
179 struct QPainterDummyState
180 {
181     QFont font;
182     QPen pen;
183     QBrush brush;
184     QTransform transform;
185 };
186
187 class QRawFont;
188 class QPainterPrivate
189 {
190     Q_DECLARE_PUBLIC(QPainter)
191 public:
192     QPainterPrivate(QPainter *painter)
193     : q_ptr(painter), d_ptrs(0), state(0), dummyState(0), txinv(0), inDestructor(false), d_ptrs_size(0),
194         refcount(1), device(0), original_device(0), helper_device(0), engine(0), emulationEngine(0),
195         extended(0)
196     {
197     }
198
199     ~QPainterPrivate();
200
201     QPainter *q_ptr;
202     QPainterPrivate **d_ptrs;
203
204     QPainterState *state;
205     QVector<QPainterState*> states;
206
207     mutable QPainterDummyState *dummyState;
208
209     QTransform invMatrix;
210     uint txinv:1;
211     uint inDestructor : 1;
212     uint d_ptrs_size;
213     uint refcount;
214
215     enum DrawOperation { StrokeDraw        = 0x1,
216                          FillDraw          = 0x2,
217                          StrokeAndFillDraw = 0x3
218     };
219
220     QPainterDummyState *fakeState() const {
221         if (!dummyState)
222             dummyState = new QPainterDummyState();
223         return dummyState;
224     }
225
226     void updateEmulationSpecifier(QPainterState *s);
227     void updateStateImpl(QPainterState *state);
228     void updateState(QPainterState *state);
229
230     void draw_helper(const QPainterPath &path, DrawOperation operation = StrokeAndFillDraw);
231     void drawStretchedGradient(const QPainterPath &path, DrawOperation operation);
232     void drawOpaqueBackground(const QPainterPath &path, DrawOperation operation);
233     void drawTextItem(const QPointF &p, const QTextItem &_ti, QTextEngine *textEngine);
234
235 #if !defined(QT_NO_RAWFONT)
236     void drawGlyphs(const quint32 *glyphArray, QFixedPoint *positionArray, int glyphCount,
237                     const QRawFont &font, bool overline = false, bool underline = false,
238                     bool strikeOut = false);
239 #endif
240
241     void updateMatrix();
242     void updateInvMatrix();
243
244     int rectSubtraction() const {
245         return state->pen.style() != Qt::NoPen && state->pen.width() == 0 ? 1 : 0;
246     }
247
248     void checkEmulation();
249
250     static QPainterPrivate *get(QPainter *painter)
251     {
252         return painter->d_ptr.data();
253     }
254
255     QTransform viewTransform() const;
256     static bool attachPainterPrivate(QPainter *q, QPaintDevice *pdev);
257     void detachPainterPrivate(QPainter *q);
258
259     QPaintDevice *device;
260     QPaintDevice *original_device;
261     QPaintDevice *helper_device;
262     QPaintEngine *engine;
263     QEmulationPaintEngine *emulationEngine;
264     QPaintEngineEx *extended;
265     QBrush colorBrush;          // for fill with solid color
266 };
267
268 Q_GUI_EXPORT void qt_draw_helper(QPainterPrivate *p, const QPainterPath &path, QPainterPrivate::DrawOperation operation);
269
270 QString qt_generate_brush_key(const QBrush &brush);
271
272 QT_END_NAMESPACE
273
274 #endif // QPAINTER_P_H