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 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
17 ** Alternatively, this file may be used under the terms of the GNU Lesser
18 ** General Public License version 2.1 as published by the Free Software
19 ** Foundation and appearing in the file LICENSE.LGPL included in the
20 ** packaging of this file. Please review the following information to
21 ** ensure the GNU Lesser General Public License version 2.1 requirements
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 ** In addition, as a special exception, Nokia gives you certain additional
25 ** rights. These rights are described in the Nokia Qt LGPL Exception
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
40 ****************************************************************************/
42 #include <QtGui/qpainter.h>
44 #include "private/qsgadaptationlayer_p.h"
45 #include "qsgcanvasitem_p.h"
46 #include "qsgpainteditem_p.h"
47 #include "qsgcontext2d_p.h"
48 #include "private/qsgpainternode_p.h"
49 #include <qdeclarativeinfo.h>
50 #include "qdeclarativeengine_p.h"
51 #include <QtCore/QBuffer>
55 class QSGCanvasItemPrivate : public QSGPaintedItemPrivate
58 QSGCanvasItemPrivate();
59 ~QSGCanvasItemPrivate();
60 QSGContext2D* context;
67 QSGCanvasItemPrivate::QSGCanvasItemPrivate()
68 : QSGPaintedItemPrivate()
73 QSGCanvasItemPrivate::~QSGCanvasItemPrivate()
78 Constructs a QSGCanvasItem with the given \a parent item.
80 QSGCanvasItem::QSGCanvasItem(QSGItem *parent)
81 : QSGPaintedItem(*(new QSGCanvasItemPrivate), parent)
86 Destroys the QSGCanvasItem.
88 QSGCanvasItem::~QSGCanvasItem()
92 void QSGCanvasItem::paint(QPainter *painter)
97 d->context->paint(painter);
102 QScriptValue QSGCanvasItem::getContext(const QString &contextId)
105 QScriptEngine* e = QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(this));
106 if (contextId == QLatin1String("2d")) {
108 d->context = new QSGContext2D(this);
109 d->context->setScriptEngine(e);
110 connect(d->context, SIGNAL(changed()), this, SLOT(requestPaint()));
112 return d->context->scriptValue();
114 qDebug("Canvas:requesting unsupported context");
115 return e->undefinedValue();
118 void QSGCanvasItem::requestPaint()
120 //TODO:update(d->context->dirtyRect());
124 bool QSGCanvasItem::save(const QString &filename) const
126 Q_D(const QSGCanvasItem);
127 QSGPainterNode* node = static_cast<QSGPainterNode*>(d->paintNode);
129 QImage image = node->toImage();
130 image.save(filename);
135 QString QSGCanvasItem::toDataURL(const QString& mimeType) const
137 Q_D(const QSGCanvasItem);
139 QSGPainterNode* node = static_cast<QSGPainterNode*>(d->paintNode);
141 QImage image = node->toImage();
144 buffer.open(QIODevice::WriteOnly);
145 QString mime = mimeType;
147 if (mimeType == QLatin1String("image/bmp"))
148 type = QLatin1String("BMP");
149 else if (mimeType == QLatin1String("image/jpeg"))
150 type = QLatin1String("JPEG");
151 else if (mimeType == QLatin1String("image/x-portable-pixmap"))
152 type = QLatin1String("PPM");
153 else if (mimeType == QLatin1String("image/tiff"))
154 type = QLatin1String("TIFF");
155 else if (mimeType == QLatin1String("image/xbm"))
156 type = QLatin1String("XBM");
157 else if (mimeType == QLatin1String("image/xpm"))
158 type = QLatin1String("XPM");
160 type = QLatin1String("PNG");
161 mime = QLatin1String("image/png");
163 image.save(&buffer, type.toAscii());
165 QString dataUrl = QLatin1String("data:%1;base64,%2");
166 return dataUrl.arg(mime).arg(ba.toBase64().constData());
168 return QLatin1String("data:,");