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 ** 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 "private/qdeclarativeimagebase_p.h"
43 #include "private/qdeclarativeimagebase_p_p.h"
45 #include <qdeclarativeengine.h>
46 #include <qdeclarativeinfo.h>
47 #include <qdeclarativepixmapcache_p.h>
51 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeItem *parent)
52 : QDeclarativeImplicitSizeItem(*(new QDeclarativeImageBasePrivate), parent)
56 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent)
57 : QDeclarativeImplicitSizeItem(dd, parent)
61 QDeclarativeImageBase::~QDeclarativeImageBase()
65 QDeclarativeImageBase::Status QDeclarativeImageBase::status() const
67 Q_D(const QDeclarativeImageBase);
72 qreal QDeclarativeImageBase::progress() const
74 Q_D(const QDeclarativeImageBase);
79 bool QDeclarativeImageBase::asynchronous() const
81 Q_D(const QDeclarativeImageBase);
85 void QDeclarativeImageBase::setAsynchronous(bool async)
87 Q_D(QDeclarativeImageBase);
88 if (d->async != async) {
90 emit asynchronousChanged();
94 QUrl QDeclarativeImageBase::source() const
96 Q_D(const QDeclarativeImageBase);
100 void QDeclarativeImageBase::setSource(const QUrl &url)
102 Q_D(QDeclarativeImageBase);
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 QDeclarativeImageBase::setSourceSize(const QSize& size)
116 Q_D(QDeclarativeImageBase);
117 if (d->sourcesize == size)
120 d->sourcesize = size;
121 d->explicitSourceSize = true;
122 emit sourceSizeChanged();
123 if (isComponentComplete())
127 QSize QDeclarativeImageBase::sourceSize() const
129 Q_D(const QDeclarativeImageBase);
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 QDeclarativeImageBase::resetSourceSize()
138 Q_D(QDeclarativeImageBase);
139 if (!d->explicitSourceSize)
141 d->explicitSourceSize = false;
142 d->sourcesize = QSize();
143 emit sourceSizeChanged();
144 if (isComponentComplete())
148 bool QDeclarativeImageBase::cache() const
150 Q_D(const QDeclarativeImageBase);
154 void QDeclarativeImageBase::setCache(bool cache)
156 Q_D(QDeclarativeImageBase);
157 if (d->cache == cache)
162 if (isComponentComplete())
166 void QDeclarativeImageBase::setMirror(bool mirror)
168 Q_D(QDeclarativeImageBase);
169 if (mirror == d->mirror)
174 if (isComponentComplete())
177 emit mirrorChanged();
180 bool QDeclarativeImageBase::mirror() const
182 Q_D(const QDeclarativeImageBase);
186 void QDeclarativeImageBase::load()
188 Q_D(QDeclarativeImageBase);
190 if (d->url.isEmpty()) {
195 emit progressChanged(d->progress);
196 emit statusChanged(d->status);
199 QDeclarativePixmap::Options options;
201 options |= QDeclarativePixmap::Asynchronous;
203 options |= QDeclarativePixmap::Cache;
205 d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), options);
207 if (d->pix.isLoading()) {
210 emit progressChanged(d->progress);
211 emit statusChanged(d->status);
213 static int thisRequestProgress = -1;
214 static int thisRequestFinished = -1;
215 if (thisRequestProgress == -1) {
216 thisRequestProgress =
217 QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
218 thisRequestFinished =
219 QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestFinished()");
222 d->pix.connectFinished(this, thisRequestFinished);
223 d->pix.connectDownloadProgress(this, thisRequestProgress);
231 void QDeclarativeImageBase::requestFinished()
233 Q_D(QDeclarativeImageBase);
235 QDeclarativeImageBase::Status oldStatus = d->status;
236 qreal oldProgress = d->progress;
238 if (d->pix.isError()) {
240 qmlInfo(this) << d->pix.error();
249 if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
250 emit sourceSizeChanged();
252 if (d->status != oldStatus)
253 emit statusChanged(d->status);
254 if (d->progress != oldProgress)
255 emit progressChanged(d->progress);
260 void QDeclarativeImageBase::requestProgress(qint64 received, qint64 total)
262 Q_D(QDeclarativeImageBase);
263 if (d->status == Loading && total > 0) {
264 d->progress = qreal(received)/total;
265 emit progressChanged(d->progress);
269 void QDeclarativeImageBase::componentComplete()
271 Q_D(QDeclarativeImageBase);
272 QDeclarativeItem::componentComplete();
273 if (d->url.isValid())
277 void QDeclarativeImageBase::pixmapChange()
279 Q_D(QDeclarativeImageBase);
280 setImplicitWidth(d->pix.width());
281 setImplicitHeight(d->pix.height());