1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "QtQuick1/private/qdeclarativeimagebase_p.h"
43 #include "QtQuick1/private/qdeclarativeimagebase_p_p.h"
45 #include <QtDeclarative/qdeclarativeengine.h>
46 #include <QtDeclarative/qdeclarativeinfo.h>
47 #include <QtQuick1/private/qdeclarativepixmapcache_p.h>
53 QDeclarative1ImageBase::QDeclarative1ImageBase(QDeclarativeItem *parent)
54 : QDeclarative1ImplicitSizeItem(*(new QDeclarative1ImageBasePrivate), parent)
58 QDeclarative1ImageBase::QDeclarative1ImageBase(QDeclarative1ImageBasePrivate &dd, QDeclarativeItem *parent)
59 : QDeclarative1ImplicitSizeItem(dd, parent)
63 QDeclarative1ImageBase::~QDeclarative1ImageBase()
67 QDeclarative1ImageBase::Status QDeclarative1ImageBase::status() const
69 Q_D(const QDeclarative1ImageBase);
74 qreal QDeclarative1ImageBase::progress() const
76 Q_D(const QDeclarative1ImageBase);
81 bool QDeclarative1ImageBase::asynchronous() const
83 Q_D(const QDeclarative1ImageBase);
87 void QDeclarative1ImageBase::setAsynchronous(bool async)
89 Q_D(QDeclarative1ImageBase);
90 if (d->async != async) {
92 emit asynchronousChanged();
96 QUrl QDeclarative1ImageBase::source() const
98 Q_D(const QDeclarative1ImageBase);
102 void QDeclarative1ImageBase::setSource(const QUrl &url)
104 Q_D(QDeclarative1ImageBase);
105 //equality is fairly expensive, so we bypass for simple, common case
106 if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
110 emit sourceChanged(d->url);
112 if (isComponentComplete())
116 void QDeclarative1ImageBase::setSourceSize(const QSize& size)
118 Q_D(QDeclarative1ImageBase);
119 if (d->sourcesize == size)
122 d->sourcesize = size;
123 d->explicitSourceSize = true;
124 emit sourceSizeChanged();
125 if (isComponentComplete())
129 QSize QDeclarative1ImageBase::sourceSize() const
131 Q_D(const QDeclarative1ImageBase);
133 int width = d->sourcesize.width();
134 int height = d->sourcesize.height();
135 return QSize(width != -1 ? width : d->pix.width(), height != -1 ? height : d->pix.height());
138 void QDeclarative1ImageBase::resetSourceSize()
140 Q_D(QDeclarative1ImageBase);
141 if (!d->explicitSourceSize)
143 d->explicitSourceSize = false;
144 d->sourcesize = QSize();
145 emit sourceSizeChanged();
146 if (isComponentComplete())
150 bool QDeclarative1ImageBase::cache() const
152 Q_D(const QDeclarative1ImageBase);
156 void QDeclarative1ImageBase::setCache(bool cache)
158 Q_D(QDeclarative1ImageBase);
159 if (d->cache == cache)
164 if (isComponentComplete())
168 void QDeclarative1ImageBase::setMirror(bool mirror)
170 Q_D(QDeclarative1ImageBase);
171 if (mirror == d->mirror)
176 if (isComponentComplete())
179 emit mirrorChanged();
182 bool QDeclarative1ImageBase::mirror() const
184 Q_D(const QDeclarative1ImageBase);
188 void QDeclarative1ImageBase::load()
190 Q_D(QDeclarative1ImageBase);
192 if (d->url.isEmpty()) {
197 emit progressChanged(d->progress);
198 emit statusChanged(d->status);
201 QDeclarative1Pixmap::Options options;
203 options |= QDeclarative1Pixmap::Asynchronous;
205 options |= QDeclarative1Pixmap::Cache;
207 d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), options);
209 if (d->pix.isLoading()) {
212 emit progressChanged(d->progress);
213 emit statusChanged(d->status);
215 static int thisRequestProgress = -1;
216 static int thisRequestFinished = -1;
217 if (thisRequestProgress == -1) {
218 thisRequestProgress =
219 QDeclarative1ImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
220 thisRequestFinished =
221 QDeclarative1ImageBase::staticMetaObject.indexOfSlot("requestFinished()");
224 d->pix.connectFinished(this, thisRequestFinished);
225 d->pix.connectDownloadProgress(this, thisRequestProgress);
233 void QDeclarative1ImageBase::requestFinished()
235 Q_D(QDeclarative1ImageBase);
237 QDeclarative1ImageBase::Status oldStatus = d->status;
238 qreal oldProgress = d->progress;
240 if (d->pix.isError()) {
242 qmlInfo(this) << d->pix.error();
251 if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
252 emit sourceSizeChanged();
254 if (d->status != oldStatus)
255 emit statusChanged(d->status);
256 if (d->progress != oldProgress)
257 emit progressChanged(d->progress);
262 void QDeclarative1ImageBase::requestProgress(qint64 received, qint64 total)
264 Q_D(QDeclarative1ImageBase);
265 if (d->status == Loading && total > 0) {
266 d->progress = qreal(received)/total;
267 emit progressChanged(d->progress);
271 void QDeclarative1ImageBase::componentComplete()
273 Q_D(QDeclarative1ImageBase);
274 QDeclarativeItem::componentComplete();
275 if (d->url.isValid())
279 void QDeclarative1ImageBase::pixmapChange()
281 Q_D(QDeclarative1ImageBase);
282 setImplicitWidth(d->pix.width());
283 setImplicitHeight(d->pix.height());