1 /****************************************************************************
3 ** Copyright (C) 2011 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 #include "qsgimagebase_p.h"
43 #include "qsgimagebase_p_p.h"
45 #include <QtDeclarative/qdeclarativeinfo.h>
49 QSGImageBase::QSGImageBase(QSGItem *parent)
50 : QSGImplicitSizeItem(*(new QSGImageBasePrivate), parent)
52 setFlag(ItemHasContents);
55 QSGImageBase::QSGImageBase(QSGImageBasePrivate &dd, QSGItem *parent)
56 : QSGImplicitSizeItem(dd, parent)
58 setFlag(ItemHasContents);
61 QSGImageBase::~QSGImageBase()
65 QSGImageBase::Status QSGImageBase::status() const
67 Q_D(const QSGImageBase);
72 qreal QSGImageBase::progress() const
74 Q_D(const QSGImageBase);
79 bool QSGImageBase::asynchronous() const
81 Q_D(const QSGImageBase);
85 void QSGImageBase::setAsynchronous(bool async)
88 if (d->async != async) {
90 emit asynchronousChanged();
94 QUrl QSGImageBase::source() const
96 Q_D(const QSGImageBase);
100 void QSGImageBase::setSource(const QUrl &url)
103 //equality is fairly expensive, so we bypass for simple, common case
104 if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
108 emit sourceChanged(d->url);
110 if (isComponentComplete())
114 void QSGImageBase::setSourceSize(const QSize& size)
117 if (d->sourcesize == size)
120 d->sourcesize = size;
121 d->explicitSourceSize = true;
122 emit sourceSizeChanged();
123 if (isComponentComplete())
127 QSize QSGImageBase::sourceSize() const
129 Q_D(const QSGImageBase);
131 int width = d->sourcesize.width();
132 int height = d->sourcesize.height();
133 return QSize(width != -1 ? width : d->pix.width(), height != -1 ? height : d->pix.height());
136 void QSGImageBase::resetSourceSize()
139 if (!d->explicitSourceSize)
141 d->explicitSourceSize = false;
142 d->sourcesize = QSize();
143 emit sourceSizeChanged();
144 if (isComponentComplete())
148 bool QSGImageBase::cache() const
150 Q_D(const QSGImageBase);
154 void QSGImageBase::setCache(bool cache)
157 if (d->cache == cache)
162 if (isComponentComplete())
166 QPixmap QSGImageBase::pixmap() const
168 Q_D(const QSGImageBase);
169 return d->pix.pixmap();
172 void QSGImageBase::setMirror(bool mirror)
175 if (mirror == d->mirror)
180 if (isComponentComplete())
183 emit mirrorChanged();
186 bool QSGImageBase::mirror() const
188 Q_D(const QSGImageBase);
192 void QSGImageBase::load()
196 if (d->url.isEmpty()) {
201 emit progressChanged(d->progress);
202 emit statusChanged(d->status);
205 QDeclarativePixmap::Options options;
207 options |= QDeclarativePixmap::Asynchronous;
209 options |= QDeclarativePixmap::Cache;
212 d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), options);
214 if (d->pix.isLoading()) {
217 emit progressChanged(d->progress);
218 emit statusChanged(d->status);
220 static int thisRequestProgress = -1;
221 static int thisRequestFinished = -1;
222 if (thisRequestProgress == -1) {
223 thisRequestProgress =
224 QSGImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
225 thisRequestFinished =
226 QSGImageBase::staticMetaObject.indexOfSlot("requestFinished()");
229 d->pix.connectFinished(this, thisRequestFinished);
230 d->pix.connectDownloadProgress(this, thisRequestProgress);
238 void QSGImageBase::requestFinished()
242 QSGImageBase::Status oldStatus = d->status;
243 qreal oldProgress = d->progress;
245 if (d->pix.isError()) {
247 qmlInfo(this) << d->pix.error();
256 if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
257 emit sourceSizeChanged();
259 if (d->status != oldStatus)
260 emit statusChanged(d->status);
261 if (d->progress != oldProgress)
262 emit progressChanged(d->progress);
267 void QSGImageBase::requestProgress(qint64 received, qint64 total)
270 if (d->status == Loading && total > 0) {
271 d->progress = qreal(received)/total;
272 emit progressChanged(d->progress);
276 void QSGImageBase::componentComplete()
279 QSGItem::componentComplete();
280 if (d->url.isValid())
284 void QSGImageBase::pixmapChange()
287 setImplicitWidth(d->pix.width());
288 setImplicitHeight(d->pix.height());