1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** Contact: Qt Software Information ([email protected])
|
---|
5 | **
|
---|
6 | ** This file is part of the examples of the Qt Toolkit.
|
---|
7 | **
|
---|
8 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
9 | ** Commercial Usage
|
---|
10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
11 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
12 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
13 | ** a written agreement between you and Nokia.
|
---|
14 | **
|
---|
15 | ** GNU Lesser General Public License Usage
|
---|
16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
17 | ** General Public License version 2.1 as published by the Free Software
|
---|
18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
19 | ** packaging of this file. Please review the following information to
|
---|
20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
22 | **
|
---|
23 | ** In addition, as a special exception, Nokia gives you certain
|
---|
24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
---|
25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
---|
26 | ** package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you are unsure which license is appropriate for your use, please
|
---|
37 | ** contact the sales department at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #ifndef CONTEXT2D_H
|
---|
43 | #define CONTEXT2D_H
|
---|
44 |
|
---|
45 | #include "domimage.h"
|
---|
46 |
|
---|
47 | #include <QPainter>
|
---|
48 | #include <QPainterPath>
|
---|
49 | #include <QString>
|
---|
50 | #include <QStack>
|
---|
51 | #include <QMetaType>
|
---|
52 | #include <QTimerEvent>
|
---|
53 |
|
---|
54 | // [3]
|
---|
55 | class CanvasGradient
|
---|
56 | {
|
---|
57 | public:
|
---|
58 | CanvasGradient(const QGradient &v)
|
---|
59 | : value(v) {}
|
---|
60 | CanvasGradient() {}
|
---|
61 |
|
---|
62 | QGradient value;
|
---|
63 | };
|
---|
64 | // [3]
|
---|
65 |
|
---|
66 | Q_DECLARE_METATYPE(CanvasGradient)
|
---|
67 | Q_DECLARE_METATYPE(CanvasGradient*)
|
---|
68 |
|
---|
69 | class ImageData {
|
---|
70 | };
|
---|
71 |
|
---|
72 | class QContext2DCanvas;
|
---|
73 |
|
---|
74 | //! [0]
|
---|
75 | class Context2D : public QObject
|
---|
76 | {
|
---|
77 | Q_OBJECT
|
---|
78 | // compositing
|
---|
79 | Q_PROPERTY(qreal globalAlpha READ globalAlpha WRITE setGlobalAlpha)
|
---|
80 | Q_PROPERTY(QString globalCompositeOperation READ globalCompositeOperation WRITE setGlobalCompositeOperation)
|
---|
81 | Q_PROPERTY(QVariant strokeStyle READ strokeStyle WRITE setStrokeStyle)
|
---|
82 | Q_PROPERTY(QVariant fillStyle READ fillStyle WRITE setFillStyle)
|
---|
83 | // line caps/joins
|
---|
84 | Q_PROPERTY(qreal lineWidth READ lineWidth WRITE setLineWidth)
|
---|
85 | Q_PROPERTY(QString lineCap READ lineCap WRITE setLineCap)
|
---|
86 | Q_PROPERTY(QString lineJoin READ lineJoin WRITE setLineJoin)
|
---|
87 | Q_PROPERTY(qreal miterLimit READ miterLimit WRITE setMiterLimit)
|
---|
88 | // shadows
|
---|
89 | Q_PROPERTY(qreal shadowOffsetX READ shadowOffsetX WRITE setShadowOffsetX)
|
---|
90 | Q_PROPERTY(qreal shadowOffsetY READ shadowOffsetY WRITE setShadowOffsetY)
|
---|
91 | Q_PROPERTY(qreal shadowBlur READ shadowBlur WRITE setShadowBlur)
|
---|
92 | Q_PROPERTY(QString shadowColor READ shadowColor WRITE setShadowColor)
|
---|
93 | //! [0]
|
---|
94 |
|
---|
95 | public:
|
---|
96 | Context2D(QObject *parent = 0);
|
---|
97 | void setSize(int width, int height);
|
---|
98 | void setSize(const QSize &size);
|
---|
99 | QSize size() const;
|
---|
100 |
|
---|
101 | void clear();
|
---|
102 | void reset();
|
---|
103 |
|
---|
104 | // compositing
|
---|
105 | qreal globalAlpha() const; // (default 1.0)
|
---|
106 | QString globalCompositeOperation() const; // (default over)
|
---|
107 | QVariant strokeStyle() const; // (default black)
|
---|
108 | QVariant fillStyle() const; // (default black)
|
---|
109 |
|
---|
110 | void setGlobalAlpha(qreal alpha);
|
---|
111 | void setGlobalCompositeOperation(const QString &op);
|
---|
112 | void setStrokeStyle(const QVariant &style);
|
---|
113 | void setFillStyle(const QVariant &style);
|
---|
114 |
|
---|
115 | // line caps/joins
|
---|
116 | qreal lineWidth() const; // (default 1)
|
---|
117 | QString lineCap() const; // "butt", "round", "square" (default "butt")
|
---|
118 | QString lineJoin() const; // "round", "bevel", "miter" (default "miter")
|
---|
119 | qreal miterLimit() const; // (default 10)
|
---|
120 |
|
---|
121 | void setLineWidth(qreal w);
|
---|
122 | void setLineCap(const QString &s);
|
---|
123 | void setLineJoin(const QString &s);
|
---|
124 | void setMiterLimit(qreal m);
|
---|
125 |
|
---|
126 | // shadows
|
---|
127 | qreal shadowOffsetX() const; // (default 0)
|
---|
128 | qreal shadowOffsetY() const; // (default 0)
|
---|
129 | qreal shadowBlur() const; // (default 0)
|
---|
130 | QString shadowColor() const; // (default black)
|
---|
131 |
|
---|
132 | void setShadowOffsetX(qreal x);
|
---|
133 | void setShadowOffsetY(qreal y);
|
---|
134 | void setShadowBlur(qreal b);
|
---|
135 | void setShadowColor(const QString &str);
|
---|
136 |
|
---|
137 | //! [1]
|
---|
138 | public slots:
|
---|
139 | void save(); // push state on state stack
|
---|
140 | void restore(); // pop state stack and restore state
|
---|
141 |
|
---|
142 | void scale(qreal x, qreal y);
|
---|
143 | void rotate(qreal angle);
|
---|
144 | void translate(qreal x, qreal y);
|
---|
145 | void transform(qreal m11, qreal m12, qreal m21, qreal m22,
|
---|
146 | qreal dx, qreal dy);
|
---|
147 | void setTransform(qreal m11, qreal m12, qreal m21, qreal m22,
|
---|
148 | qreal dx, qreal dy);
|
---|
149 |
|
---|
150 | CanvasGradient createLinearGradient(qreal x0, qreal y0,
|
---|
151 | qreal x1, qreal y1);
|
---|
152 | CanvasGradient createRadialGradient(qreal x0, qreal y0,
|
---|
153 | qreal r0, qreal x1,
|
---|
154 | qreal y1, qreal r1);
|
---|
155 |
|
---|
156 | // rects
|
---|
157 | void clearRect(qreal x, qreal y, qreal w, qreal h);
|
---|
158 | void fillRect(qreal x, qreal y, qreal w, qreal h);
|
---|
159 | void strokeRect(qreal x, qreal y, qreal w, qreal h);
|
---|
160 |
|
---|
161 | // path API
|
---|
162 | void beginPath();
|
---|
163 | void closePath();
|
---|
164 | void moveTo(qreal x, qreal y);
|
---|
165 | void lineTo(qreal x, qreal y);
|
---|
166 | void quadraticCurveTo(qreal cpx, qreal cpy, qreal x, qreal y);
|
---|
167 | void bezierCurveTo(qreal cp1x, qreal cp1y,
|
---|
168 | qreal cp2x, qreal cp2y, qreal x, qreal y);
|
---|
169 | void arcTo(qreal x1, qreal y1, qreal x2, qreal y2, qreal radius);
|
---|
170 | void rect(qreal x, qreal y, qreal w, qreal h);
|
---|
171 | void arc(qreal x, qreal y, qreal radius,
|
---|
172 | qreal startAngle, qreal endAngle,
|
---|
173 | bool anticlockwise);
|
---|
174 | void fill();
|
---|
175 | void stroke();
|
---|
176 | void clip();
|
---|
177 | bool isPointInPath(qreal x, qreal y) const;
|
---|
178 | //! [1]
|
---|
179 |
|
---|
180 | // drawing images
|
---|
181 | void drawImage(DomImage *image, qreal dx, qreal dy);
|
---|
182 | void drawImage(DomImage *image, qreal dx, qreal dy,
|
---|
183 | qreal dw, qreal dh);
|
---|
184 | void drawImage(DomImage *image, qreal sx, qreal sy,
|
---|
185 | qreal sw, qreal sh, qreal dx, qreal dy,
|
---|
186 | qreal dw, qreal dh);
|
---|
187 |
|
---|
188 | // pixel manipulation
|
---|
189 | ImageData getImageData(qreal sx, qreal sy, qreal sw, qreal sh);
|
---|
190 | void putImageData(ImageData image, qreal dx, qreal dy);
|
---|
191 |
|
---|
192 | //! [2]
|
---|
193 | signals:
|
---|
194 | void changed(const QImage &image);
|
---|
195 | //! [2]
|
---|
196 |
|
---|
197 | protected:
|
---|
198 | void timerEvent(QTimerEvent *e);
|
---|
199 |
|
---|
200 | private:
|
---|
201 | void beginPainting();
|
---|
202 | const QImage &endPainting();
|
---|
203 | void scheduleChange();
|
---|
204 |
|
---|
205 | int m_changeTimerId;
|
---|
206 | QImage m_image;
|
---|
207 | QPainter m_painter;
|
---|
208 | QPainterPath m_path;
|
---|
209 |
|
---|
210 | enum DirtyFlag {
|
---|
211 | DirtyTransformationMatrix = 0x00001,
|
---|
212 | DirtyClippingRegion = 0x00002,
|
---|
213 | DirtyStrokeStyle = 0x00004,
|
---|
214 | DirtyFillStyle = 0x00008,
|
---|
215 | DirtyGlobalAlpha = 0x00010,
|
---|
216 | DirtyLineWidth = 0x00020,
|
---|
217 | DirtyLineCap = 0x00040,
|
---|
218 | DirtyLineJoin = 0x00080,
|
---|
219 | DirtyMiterLimit = 0x00100,
|
---|
220 | MDirtyPen = DirtyStrokeStyle
|
---|
221 | | DirtyLineWidth
|
---|
222 | | DirtyLineCap
|
---|
223 | | DirtyLineJoin
|
---|
224 | | DirtyMiterLimit,
|
---|
225 | DirtyShadowOffsetX = 0x00200,
|
---|
226 | DirtyShadowOffsetY = 0x00400,
|
---|
227 | DirtyShadowBlur = 0x00800,
|
---|
228 | DirtyShadowColor = 0x01000,
|
---|
229 | DirtyGlobalCompositeOperation = 0x2000,
|
---|
230 | DirtyFont = 0x04000,
|
---|
231 | DirtyTextAlign = 0x08000,
|
---|
232 | DirtyTextBaseline = 0x10000,
|
---|
233 | AllIsFullOfDirt = 0xfffff
|
---|
234 | };
|
---|
235 |
|
---|
236 | struct State {
|
---|
237 | State() : flags(0) {}
|
---|
238 | QMatrix matrix;
|
---|
239 | QPainterPath clipPath;
|
---|
240 | QBrush strokeStyle;
|
---|
241 | QBrush fillStyle;
|
---|
242 | qreal globalAlpha;
|
---|
243 | qreal lineWidth;
|
---|
244 | Qt::PenCapStyle lineCap;
|
---|
245 | Qt::PenJoinStyle lineJoin;
|
---|
246 | qreal miterLimit;
|
---|
247 | qreal shadowOffsetX;
|
---|
248 | qreal shadowOffsetY;
|
---|
249 | qreal shadowBlur;
|
---|
250 | QColor shadowColor;
|
---|
251 | QPainter::CompositionMode globalCompositeOperation;
|
---|
252 | QFont font;
|
---|
253 | int textAlign;
|
---|
254 | int textBaseline;
|
---|
255 | int flags;
|
---|
256 | };
|
---|
257 | State m_state;
|
---|
258 | QStack<State> m_stateStack;
|
---|
259 | };
|
---|
260 |
|
---|
261 | #endif
|
---|