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>
60 #include <QWaitCondition>
61 #include "qsgimage_p.h"
67 QT_MODULE(Declarative)
69 QColor colorFromString(const QString &name);
71 class QSGCanvasGradient : public QObject
75 QSGCanvasGradient(const QGradient &gradient) : m_gradient(gradient) {}
78 QGradient value() { return m_gradient; }
79 void addColorStop(float pos, const QString &color) { m_gradient.setColorAt(pos, colorFromString(color));}
85 Q_DECLARE_METATYPE(QSGCanvasGradient*)
88 class QSGContext2DWorkerAgent;
89 class QSGContext2DPrivate;
91 class QSGContext2D : public QObject
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)
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)
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)
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)
117 enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging};
118 enum TextAlignType { Start=0, End, Left, Right, Center};
154 GlobalCompositeOperation,
186 QSGContext2D(QObject *parent = 0);
187 QSGContext2D(QSGContext2D *ctx2d, QSGContext2DWorkerAgent* agentData);
190 QSGCanvasItem* canvas() const;
192 void setSize(int width, int height);
193 void setSize(const QSize &size);
198 QPaintDevice* paintDevice();
199 const QImage& toImage() const;
200 bool requireCachedImage() const;
201 void setCachedImage(const QImage& image);
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)
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);
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)
223 void setLineWidth(qreal w);
224 void setLineCap(const QString &s);
225 void setLineJoin(const QString &s);
226 void setMiterLimit(qreal m);
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;
237 qreal shadowOffsetX() const; // (default 0)
238 qreal shadowOffsetY() const; // (default 0)
239 qreal shadowBlur() const; // (default 0)
240 QString shadowColor() const; // (default black)
242 void setShadowOffsetX(qreal x);
243 void setShadowOffsetY(qreal y);
244 void setShadowBlur(qreal b);
245 void setShadowColor(const QString &str);
248 void save(); // push state on state stack
249 void restore(); // pop state stack and restore state
251 // QTextMetrics measureText(const QString& text);
253 void fillText(const QString &text, qreal x, qreal y);
254 void strokeText(const QString &text, qreal x, qreal y);
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,
261 void setTransform(qreal m11, qreal m12, qreal m21, qreal m22,
264 QSGCanvasGradient *createLinearGradient(qreal x0, qreal y0,
266 QSGCanvasGradient *createRadialGradient(qreal x0, qreal y0,
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);
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,
291 bool isPointInPath(qreal x, qreal y) const;
294 QSGImage *createImage(const QString &url);
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);
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);
304 void paint(QPainter* painter);
306 void processCommands(const QScriptValue& commands);
311 bool isDirty() const;
312 QScriptValue scriptValue() const;
313 void setScriptEngine(QScriptEngine *eng);
314 QScriptEngine *scriptEngine() const;
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(); }
326 VariantRef &operator=(const VariantRef &o) {
327 if (o.a) o.a->addref();
328 if (a) a->release(); a = o.a;
334 struct Sync : public QEvent {
335 Sync() : QEvent(QEvent::User) {}
336 QSGContext2DWorkerAgent *data;
338 inline bool inWorkerThread() const;
339 QSGContext2D *agent();
340 const QString& agentScript() const;
345 QPainterPath clipPath;
350 Qt::PenCapStyle lineCap;
351 Qt::PenJoinStyle lineJoin;
357 QPainter::CompositionMode globalCompositeOperation;
359 QSGContext2D::TextAlignType textAlign;
360 QSGContext2D::TextBaseLineType textBaseline;
364 QMatrix worldMatrix() const;
367 virtual bool event(QEvent *);
370 void processCommand(const QScriptValue& command);
372 Q_DECLARE_PRIVATE(QSGContext2D)
378 Q_DECLARE_METATYPE(QSGContext2D::VariantRef)
379 QML_DECLARE_TYPE(QSGContext2D)
383 #endif // QSGCONTEXT2D_P_H