264752edddf2d90d663b5dd10e7be1c9cef0e78b
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgcanvasitem.cpp
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 ** 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
14 ** this package.
15 **
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.
23 **
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.
27 **
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include <QtGui/qpainter.h>
43
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>
52
53 QT_BEGIN_NAMESPACE
54
55 class QSGCanvasItemPrivate : public QSGPaintedItemPrivate
56 {
57 public:
58     QSGCanvasItemPrivate();
59     ~QSGCanvasItemPrivate();
60     QSGContext2D* context;
61 };
62
63
64 /*!
65     \internal
66 */
67 QSGCanvasItemPrivate::QSGCanvasItemPrivate()
68     : QSGPaintedItemPrivate()
69     , context(0)
70 {
71 }
72
73 QSGCanvasItemPrivate::~QSGCanvasItemPrivate()
74 {
75 }
76
77 /*!
78     Constructs a QSGCanvasItem with the given \a parent item.
79  */
80 QSGCanvasItem::QSGCanvasItem(QSGItem *parent)
81     : QSGPaintedItem(*(new QSGCanvasItemPrivate), parent)
82 {
83 }
84
85 /*!
86     Destroys the QSGCanvasItem.
87 */
88 QSGCanvasItem::~QSGCanvasItem()
89 {
90 }
91
92 void QSGCanvasItem::paint(QPainter *painter)
93 {
94     Q_D(QSGCanvasItem);
95
96     if (d->context) {
97         d->context->paint(painter);
98         emit canvasUpdated();
99     }
100 }
101
102 QScriptValue QSGCanvasItem::getContext(const QString &contextId)
103 {
104     Q_D(QSGCanvasItem);
105     QScriptEngine* e = QDeclarativeEnginePrivate::getScriptEngine(qmlEngine(this));
106     if (contextId == QLatin1String("2d")) {
107         if (!d->context) {
108             d->context = new QSGContext2D(this);
109             d->context->setScriptEngine(e);
110             connect(d->context, SIGNAL(changed()), this, SLOT(requestPaint()));
111         }
112         return d->context->scriptValue();
113     }
114     qDebug("Canvas:requesting unsupported context");
115     return e->undefinedValue();
116 }
117
118 void QSGCanvasItem::requestPaint()
119 {
120     //TODO:update(d->context->dirtyRect());
121     update();
122 }
123
124 bool QSGCanvasItem::save(const QString &filename) const
125 {
126     Q_D(const QSGCanvasItem);
127     QSGPainterNode* node = static_cast<QSGPainterNode*>(d->paintNode);
128     if (node) {
129         QImage image = node->toImage();
130         image.save(filename);
131     }
132     return false;
133 }
134
135 QString QSGCanvasItem::toDataURL(const QString& mimeType) const
136 {
137     Q_D(const QSGCanvasItem);
138
139     QSGPainterNode* node = static_cast<QSGPainterNode*>(d->paintNode);
140     if (node) {
141         QImage image = node->toImage();
142         QByteArray ba;
143         QBuffer buffer(&ba);
144         buffer.open(QIODevice::WriteOnly);
145         QString mime = mimeType;
146         QString type;
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");
159         else {
160             type = QLatin1String("PNG");
161             mime = QLatin1String("image/png");
162         }
163         image.save(&buffer, type.toAscii());
164         buffer.close();
165         QString dataUrl = QLatin1String("data:%1;base64,%2");
166         return dataUrl.arg(mime).arg(ba.toBase64().constData());
167     }
168     return QLatin1String("data:,");
169 }
170
171 QT_END_NAMESPACE