1 // Commit: 462429f5692f810bdd4e04b916db5f9af428d9e4
2 /****************************************************************************
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** No Commercial Usage
12 ** This file contains pre-release code and may not be distributed.
13 ** You may use this file in accordance with the terms and conditions
14 ** contained in the Technology Preview License Agreement accompanying
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
41 ****************************************************************************/
43 #include "qsgimagebase_p.h"
44 #include "qsgimagebase_p_p.h"
46 #include <QtDeclarative/qdeclarativeinfo.h>
50 QSGImageBase::QSGImageBase(QSGItem *parent)
51 : QSGImplicitSizeItem(*(new QSGImageBasePrivate), parent)
53 setFlag(ItemHasContents);
56 QSGImageBase::QSGImageBase(QSGImageBasePrivate &dd, QSGItem *parent)
57 : QSGImplicitSizeItem(dd, parent)
59 setFlag(ItemHasContents);
62 QSGImageBase::~QSGImageBase()
66 QSGImageBase::Status QSGImageBase::status() const
68 Q_D(const QSGImageBase);
73 qreal QSGImageBase::progress() const
75 Q_D(const QSGImageBase);
80 bool QSGImageBase::asynchronous() const
82 Q_D(const QSGImageBase);
86 void QSGImageBase::setAsynchronous(bool async)
89 if (d->async != async) {
91 emit asynchronousChanged();
95 QUrl QSGImageBase::source() const
97 Q_D(const QSGImageBase);
101 void QSGImageBase::setSource(const QUrl &url)
104 //equality is fairly expensive, so we bypass for simple, common case
105 if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
109 emit sourceChanged(d->url);
111 if (isComponentComplete())
115 void QSGImageBase::setSourceSize(const QSize& size)
118 if (d->sourcesize == size)
121 d->sourcesize = size;
122 d->explicitSourceSize = true;
123 emit sourceSizeChanged();
124 if (isComponentComplete())
128 QSize QSGImageBase::sourceSize() const
130 Q_D(const QSGImageBase);
132 int width = d->sourcesize.width();
133 int height = d->sourcesize.height();
134 return QSize(width != -1 ? width : d->pix.width(), height != -1 ? height : d->pix.height());
137 bool QSGImageBase::cache() const
139 Q_D(const QSGImageBase);
143 void QSGImageBase::setCache(bool cache)
146 if (d->cache == cache)
151 if (isComponentComplete())
155 void QSGImageBase::setMirror(bool mirror)
158 if (mirror == d->mirror)
163 if (isComponentComplete())
166 emit mirrorChanged();
169 bool QSGImageBase::mirror() const
171 Q_D(const QSGImageBase);
175 void QSGImageBase::load()
179 if (d->url.isEmpty()) {
184 setImplicitHeight(0);
185 emit progressChanged(d->progress);
186 emit statusChanged(d->status);
190 QDeclarativePixmap::Options options;
192 options |= QDeclarativePixmap::Asynchronous;
194 options |= QDeclarativePixmap::Cache;
196 d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), options);
198 if (d->pix.isLoading()) {
201 emit progressChanged(d->progress);
202 emit statusChanged(d->status);
204 static int thisRequestProgress = -1;
205 static int thisRequestFinished = -1;
206 if (thisRequestProgress == -1) {
207 thisRequestProgress =
208 QSGImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
209 thisRequestFinished =
210 QSGImageBase::staticMetaObject.indexOfSlot("requestFinished()");
213 d->pix.connectFinished(this, thisRequestFinished);
214 d->pix.connectDownloadProgress(this, thisRequestProgress);
222 void QSGImageBase::requestFinished()
226 QSGImageBase::Status oldStatus = d->status;
227 qreal oldProgress = d->progress;
229 if (d->pix.isError()) {
231 qmlInfo(this) << d->pix.error();
238 setImplicitWidth(d->pix.width());
239 setImplicitHeight(d->pix.height());
241 if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
242 emit sourceSizeChanged();
244 if (d->status != oldStatus)
245 emit statusChanged(d->status);
246 if (d->progress != oldProgress)
247 emit progressChanged(d->progress);
252 void QSGImageBase::requestProgress(qint64 received, qint64 total)
255 if (d->status == Loading && total > 0) {
256 d->progress = qreal(received)/total;
257 emit progressChanged(d->progress);
261 void QSGImageBase::componentComplete()
264 QSGItem::componentComplete();
265 if (d->url.isValid())
269 void QSGImageBase::pixmapChange()