924c4bf780ec8c1ae2ad5981f435312c23d51c52
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgcontext2d_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 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 QtDeclarative 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 QSGCONTEXT2D_P_H
43 #define QSGCONTEXT2D_P_H
44
45 #include <QtDeclarative/qdeclarative.h>
46 #include <QtDeclarative/qdeclarativecomponent.h>
47
48 #include "qsgtexturematerial.h"
49
50 #include <QtGui/qpainter.h>
51 #include <QtGui/qpainterpath.h>
52 #include <QtGui/qpixmap.h>
53 #include <QtCore/qstring.h>
54 #include <QtCore/qstack.h>
55 #include <QtCore/qmetatype.h>
56 #include <QtCore/qcoreevent.h>
57 #include <QtCore/qvariant.h>
58 #include <QtScript/qscriptvalue.h>
59 #include <private/qv8engine_p.h>
60 #include <QMutex>
61 #include <QWaitCondition>
62 #include "qsgimage_p.h"
63
64 QT_BEGIN_HEADER
65
66 QT_BEGIN_NAMESPACE
67
68 QT_MODULE(Declarative)
69
70 QColor colorFromString(const QString &name);
71
72 class QSGCanvasGradient : public QObject
73 {
74     Q_OBJECT
75 public:
76     QSGCanvasGradient(const QGradient &gradient) : m_gradient(gradient) {}
77
78 public slots:
79     QGradient value() { return m_gradient; }
80     void addColorStop(float pos, const QString &color) { m_gradient.setColorAt(pos, colorFromString(color));}
81
82 public:
83     QGradient m_gradient;
84 };
85
86 Q_DECLARE_METATYPE(QSGCanvasGradient*)
87
88
89 class QSGContext2DWorkerAgent;
90 class QSGContext2DPrivate;
91 class QSGCanvasItem;
92 class QSGContext2D : public QObject
93 {
94     Q_OBJECT
95     // compositing
96     Q_PROPERTY(qreal globalAlpha READ globalAlpha WRITE setGlobalAlpha)
97     Q_PROPERTY(QString globalCompositeOperation READ globalCompositeOperation WRITE setGlobalCompositeOperation)
98     Q_PROPERTY(QVariant strokeStyle READ strokeStyle WRITE setStrokeStyle)
99     Q_PROPERTY(QVariant fillStyle READ fillStyle WRITE setFillStyle)
100     Q_PROPERTY(QColor strokeColor READ strokeColor WRITE setStrokeColor)
101     Q_PROPERTY(QColor fillColor READ fillColor WRITE setFillColor)
102     // line caps/joins
103     Q_PROPERTY(qreal lineWidth READ lineWidth WRITE setLineWidth)
104     Q_PROPERTY(QString lineCap READ lineCap WRITE setLineCap)
105     Q_PROPERTY(QString lineJoin READ lineJoin WRITE setLineJoin)
106     Q_PROPERTY(qreal miterLimit READ miterLimit WRITE setMiterLimit)
107     // shadows
108     Q_PROPERTY(qreal shadowOffsetX READ shadowOffsetX WRITE setShadowOffsetX)
109     Q_PROPERTY(qreal shadowOffsetY READ shadowOffsetY WRITE setShadowOffsetY)
110     Q_PROPERTY(qreal shadowBlur READ shadowBlur WRITE setShadowBlur)
111     Q_PROPERTY(QString shadowColor READ shadowColor WRITE setShadowColor)
112     // fonts
113     Q_PROPERTY(QString font READ font WRITE setFont)
114     Q_PROPERTY(QString textBaseline READ textBaseline WRITE setTextBaseline)
115     Q_PROPERTY(QString textAlign READ textAlign WRITE setTextAlign)
116     Q_ENUMS(PaintCommand)
117 public:
118     enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging};
119     enum TextAlignType { Start=0, End, Left, Right, Center};
120     enum PaintCommand {
121         Invalid = 0,
122         Save,
123         Restore,
124         //matrix operations
125         UpdateMatrix,
126         Scale,
127         Rotate,
128         Translate,
129         Transform,
130         SetTransform,
131
132         ClearRect,
133         FillRect,
134
135         //path operations
136         UpdatePath,
137         BeginPath,
138         ClosePath,
139         MoveTo,
140         LineTo,
141         QuadraticCurveTo,
142         BezierCurveTo,
143         ArcTo,
144         Rect,
145         Arc,
146         Fill,
147         Stroke,
148         Clip,
149         StrokeRect,
150
151         //brushes and pens
152         UpdateBrush,
153         UpdatePen,
154         GlobalAlpha,
155         GlobalCompositeOperation,
156         StrokeStyle,
157         FillStyle,
158         StrokeColor,
159         FillColor,
160         LineWidth,
161         LineCap,
162         LineJoin,
163         MiterLimit,
164
165         //shadows
166         UpdateShadow,
167         ShadowOffsetX,
168         ShadowOffsetY,
169         ShadowBlur,
170         ShadowColor,
171
172         //font&text
173         Font,
174         TextBaseline,
175         TextAlign,
176         FillText,
177         StrokeText,
178
179         //image
180         DrawImage1,
181         DrawImage2,
182         DrawImage3,
183         GetImageData,
184         PutImageData
185     };
186
187     QSGContext2D(QObject *parent = 0);
188     QSGContext2D(QSGContext2D *ctx2d, QSGContext2DWorkerAgent* agentData);
189     ~QSGContext2D();
190
191     QSGCanvasItem*  canvas() const;
192
193     void setSize(int width, int height);
194     void setSize(const QSize &size);
195     QSize size() const;
196
197     void clear();
198     void reset();
199     QPaintDevice* paintDevice();
200     const QImage& toImage() const;
201     bool requireCachedImage() const;
202     void setCachedImage(const QImage& image);
203     // compositing
204     qreal globalAlpha() const; // (default 1.0)
205     QString globalCompositeOperation() const; // (default over)
206     QVariant strokeStyle() const; // (default black)
207     QVariant fillStyle() const; // (default black)
208     QColor strokeColor() const; // (default black)
209     QColor fillColor() const; // (default black)
210
211     void setGlobalAlpha(qreal alpha);
212     void setGlobalCompositeOperation(const QString &op);
213     void setStrokeStyle(const QVariant &style);
214     void setFillStyle(const QVariant &style);
215     void setStrokeColor(const QColor& color);
216     void setFillColor(const QColor& color);
217
218     // line caps/joins
219     qreal lineWidth() const; // (default 1)
220     QString lineCap() const; // "butt", "round", "square" (default "butt")
221     QString lineJoin() const; // "round", "bevel", "miter" (default "miter")
222     qreal miterLimit() const; // (default 10)
223
224     void setLineWidth(qreal w);
225     void setLineCap(const QString &s);
226     void setLineJoin(const QString &s);
227     void setMiterLimit(qreal m);
228
229     void setFont(const QString &font);
230     QString font() const;
231     void setTextBaseline(const QString &font);
232     QString textBaseline() const;
233     void setTextAlign(const QString &font);
234     QString textAlign() const;
235
236
237     // shadows
238     qreal shadowOffsetX() const; // (default 0)
239     qreal shadowOffsetY() const; // (default 0)
240     qreal shadowBlur() const; // (default 0)
241     QString shadowColor() const; // (default black)
242
243     void setShadowOffsetX(qreal x);
244     void setShadowOffsetY(qreal y);
245     void setShadowBlur(qreal b);
246     void setShadowColor(const QString &str);
247
248 public slots:
249     void save(); // push state on state stack
250     void restore(); // pop state stack and restore state
251
252     //    QTextMetrics measureText(const QString& text);
253
254     void fillText(const QString &text, qreal x, qreal y);
255     void strokeText(const QString &text, qreal x, qreal y);
256
257     void scale(qreal x, qreal y);
258     void rotate(qreal angle);
259     void translate(qreal x, qreal y);
260     void transform(qreal m11, qreal m12, qreal m21, qreal m22,
261                    qreal dx, qreal dy);
262     void setTransform(qreal m11, qreal m12, qreal m21, qreal m22,
263                       qreal dx, qreal dy);
264
265     QSGCanvasGradient *createLinearGradient(qreal x0, qreal y0,
266                                          qreal x1, qreal y1);
267     QSGCanvasGradient *createRadialGradient(qreal x0, qreal y0,
268                                          qreal r0, qreal x1,
269                                          qreal y1, qreal r1);
270
271     // rects
272     void clearRect(qreal x, qreal y, qreal w, qreal h);
273     void fillRect(qreal x, qreal y, qreal w, qreal h);
274     void strokeRect(qreal x, qreal y, qreal w, qreal h);
275
276     // path API
277     void beginPath();
278     void closePath();
279     void moveTo(qreal x, qreal y);
280     void lineTo(qreal x, qreal y);
281     void quadraticCurveTo(qreal cpx, qreal cpy, qreal x, qreal y);
282     void bezierCurveTo(qreal cp1x, qreal cp1y,
283                        qreal cp2x, qreal cp2y, qreal x, qreal y);
284     void arcTo(qreal x1, qreal y1, qreal x2, qreal y2, qreal radius);
285     void rect(qreal x, qreal y, qreal w, qreal h);
286     void arc(qreal x, qreal y, qreal radius,
287              qreal startAngle, qreal endAngle,
288              bool anticlockwise);
289     void fill();
290     void stroke();
291     void clip();
292     bool isPointInPath(qreal x, qreal y) const;
293
294     //path string parser
295     //implement the W3C SVG path spec:
296     //http://www.w3.org/TR/SVG/paths.html
297     void setPathString(const QString& path);
298
299     QSGImage *createImage(const QString &url);
300
301     void drawImage(const QString& imgUrl, qreal dx, qreal dy);
302     void drawImage(const QString& imgUrl, qreal dx, qreal dy, qreal dw, qreal dh);
303     void drawImage(const QString& imgUrl, qreal sx, qreal sy, qreal sw, qreal sh, qreal dx, qreal dy, qreal dw, qreal dh);
304
305     // pixel manipulation
306     QList<int> getImageData(qreal sx, qreal sy, qreal sw, qreal sh);
307     void putImageData(const QVariant& imageData, qreal x, qreal y, qreal w, qreal h);
308
309     void paint(QPainter* painter);
310     void sync();
311     void processCommands(const QScriptValue& commands);
312 signals:
313     void changed();
314     void painted();
315 public:
316     bool isDirty() const;
317     v8::Handle<v8::Object> v8value() const;
318     void setV8Engine(QV8Engine *eng);
319
320     void addref();
321     void release();
322
323     struct VariantRef
324     {
325         VariantRef() : a(0) {}
326         VariantRef(const VariantRef &r) : a(r.a) { if (a) a->addref(); }
327         VariantRef(QSGContext2D *_a) : a(_a) { if (a) a->addref(); }
328         ~VariantRef() { if (a) a->release(); }
329
330         VariantRef &operator=(const VariantRef &o) {
331             if (o.a) o.a->addref();
332             if (a) a->release(); a = o.a;
333             return *this;
334         }
335
336         QSGContext2D *a;
337     };
338     struct Sync : public QEvent {
339         Sync() : QEvent(QEvent::User) {}
340         QSGContext2DWorkerAgent *data;
341     };
342     struct State {
343         QMatrix matrix;
344         QPainterPath clipPath;
345         QBrush strokeStyle;
346         QBrush fillStyle;
347         qreal globalAlpha;
348         qreal lineWidth;
349         Qt::PenCapStyle lineCap;
350         Qt::PenJoinStyle lineJoin;
351         qreal miterLimit;
352         qreal shadowOffsetX;
353         qreal shadowOffsetY;
354         qreal shadowBlur;
355         QColor shadowColor;
356         QPainter::CompositionMode globalCompositeOperation;
357         QFont font;
358         QSGContext2D::TextAlignType textAlign;
359         QSGContext2D::TextBaseLineType textBaseline;
360         QPen pen;
361     };
362
363     QMatrix worldMatrix() const;
364
365 protected:
366     virtual bool event(QEvent *);
367
368 private:
369     void processCommand(const QScriptValue& command);
370
371     Q_DECLARE_PRIVATE(QSGContext2D)
372 };
373
374
375 QT_END_NAMESPACE
376
377 Q_DECLARE_METATYPE(QSGContext2D::VariantRef)
378 QML_DECLARE_TYPE(QSGContext2D)
379
380 QT_END_HEADER
381
382 #endif // QSGCONTEXT2D_P_H