1 /****************************************************************************
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QSGCONTEXT2D_P_H
43 #define QSGCONTEXT2D_P_H
45 #include <QtDeclarative/qdeclarative.h>
46 #include <QtDeclarative/qdeclarativecomponent.h>
48 #include "qsgtexturematerial.h"
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>
61 #include <QWaitCondition>
62 #include "qsgimage_p.h"
68 QT_MODULE(Declarative)
70 QColor colorFromString(const QString &name);
72 class QSGCanvasGradient : public QObject
76 QSGCanvasGradient(const QGradient &gradient) : m_gradient(gradient) {}
79 QGradient value() { return m_gradient; }
80 void addColorStop(float pos, const QString &color) { m_gradient.setColorAt(pos, colorFromString(color));}
86 Q_DECLARE_METATYPE(QSGCanvasGradient*)
89 class QSGContext2DWorkerAgent;
90 class QSGContext2DPrivate;
92 class QSGContext2D : public QObject
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)
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)
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)
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)
118 enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging};
119 enum TextAlignType { Start=0, End, Left, Right, Center};
155 GlobalCompositeOperation,
187 QSGContext2D(QObject *parent = 0);
188 QSGContext2D(QSGContext2D *ctx2d, QSGContext2DWorkerAgent* agentData);
191 QSGCanvasItem* canvas() const;
193 void setSize(int width, int height);
194 void setSize(const QSize &size);
199 QPaintDevice* paintDevice();
200 const QImage& toImage() const;
201 bool requireCachedImage() const;
202 void setCachedImage(const QImage& image);
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)
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);
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)
224 void setLineWidth(qreal w);
225 void setLineCap(const QString &s);
226 void setLineJoin(const QString &s);
227 void setMiterLimit(qreal m);
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;
238 qreal shadowOffsetX() const; // (default 0)
239 qreal shadowOffsetY() const; // (default 0)
240 qreal shadowBlur() const; // (default 0)
241 QString shadowColor() const; // (default black)
243 void setShadowOffsetX(qreal x);
244 void setShadowOffsetY(qreal y);
245 void setShadowBlur(qreal b);
246 void setShadowColor(const QString &str);
249 void save(); // push state on state stack
250 void restore(); // pop state stack and restore state
252 // QTextMetrics measureText(const QString& text);
254 void fillText(const QString &text, qreal x, qreal y);
255 void strokeText(const QString &text, qreal x, qreal y);
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,
262 void setTransform(qreal m11, qreal m12, qreal m21, qreal m22,
265 QSGCanvasGradient *createLinearGradient(qreal x0, qreal y0,
267 QSGCanvasGradient *createRadialGradient(qreal x0, qreal y0,
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);
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,
292 bool isPointInPath(qreal x, qreal y) const;
295 //implement the W3C SVG path spec:
296 //http://www.w3.org/TR/SVG/paths.html
297 void setPathString(const QString& path);
299 QSGImage *createImage(const QString &url);
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);
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);
309 void paint(QPainter* painter);
311 void processCommands(const QScriptValue& commands);
316 bool isDirty() const;
317 v8::Handle<v8::Object> v8value() const;
318 void setV8Engine(QV8Engine *eng);
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(); }
330 VariantRef &operator=(const VariantRef &o) {
331 if (o.a) o.a->addref();
332 if (a) a->release(); a = o.a;
338 struct Sync : public QEvent {
339 Sync() : QEvent(QEvent::User) {}
340 QSGContext2DWorkerAgent *data;
344 QPainterPath clipPath;
349 Qt::PenCapStyle lineCap;
350 Qt::PenJoinStyle lineJoin;
356 QPainter::CompositionMode globalCompositeOperation;
358 QSGContext2D::TextAlignType textAlign;
359 QSGContext2D::TextBaseLineType textBaseline;
363 QMatrix worldMatrix() const;
366 virtual bool event(QEvent *);
369 void processCommand(const QScriptValue& command);
371 Q_DECLARE_PRIVATE(QSGContext2D)
377 Q_DECLARE_METATYPE(QSGContext2D::VariantRef)
378 QML_DECLARE_TYPE(QSGContext2D)
382 #endif // QSGCONTEXT2D_P_H