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