1 /****************************************************************************
3 ** Copyright (C) 2012 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 "qquickimagebase_p.h"
43 #include "qquickimagebase_p_p.h"
45 #include <QtDeclarative/qdeclarativeinfo.h>
49 QQuickImageBase::QQuickImageBase(QQuickItem *parent)
50 : QQuickImplicitSizeItem(*(new QQuickImageBasePrivate), parent)
52 setFlag(ItemHasContents);
55 QQuickImageBase::QQuickImageBase(QQuickImageBasePrivate &dd, QQuickItem *parent)
56 : QQuickImplicitSizeItem(dd, parent)
58 setFlag(ItemHasContents);
61 QQuickImageBase::~QQuickImageBase()
65 QQuickImageBase::Status QQuickImageBase::status() const
67 Q_D(const QQuickImageBase);
72 qreal QQuickImageBase::progress() const
74 Q_D(const QQuickImageBase);
79 bool QQuickImageBase::asynchronous() const
81 Q_D(const QQuickImageBase);
85 void QQuickImageBase::setAsynchronous(bool async)
88 if (d->async != async) {
90 emit asynchronousChanged();
94 QUrl QQuickImageBase::source() const
96 Q_D(const QQuickImageBase);
100 void QQuickImageBase::setSource(const QUrl &url)
102 Q_D(QQuickImageBase);
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 QQuickImageBase::setSourceSize(const QSize& size)
116 Q_D(QQuickImageBase);
117 if (d->sourcesize == size)
120 d->sourcesize = size;
121 d->explicitSourceSize = true;
122 emit sourceSizeChanged();
123 if (isComponentComplete())
127 QSize QQuickImageBase::sourceSize() const
129 Q_D(const QQuickImageBase);
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 QQuickImageBase::resetSourceSize()
138 Q_D(QQuickImageBase);
139 if (!d->explicitSourceSize)
141 d->explicitSourceSize = false;
142 d->sourcesize = QSize();
143 emit sourceSizeChanged();
144 if (isComponentComplete())
148 bool QQuickImageBase::cache() const
150 Q_D(const QQuickImageBase);
154 void QQuickImageBase::setCache(bool cache)
156 Q_D(QQuickImageBase);
157 if (d->cache == cache)
162 if (isComponentComplete())
166 QImage QQuickImageBase::image() const
168 Q_D(const QQuickImageBase);
169 return d->pix.image();
172 void QQuickImageBase::setMirror(bool mirror)
174 Q_D(QQuickImageBase);
175 if (mirror == d->mirror)
180 if (isComponentComplete())
183 emit mirrorChanged();
186 bool QQuickImageBase::mirror() const
188 Q_D(const QQuickImageBase);
192 void QQuickImageBase::load()
194 Q_D(QQuickImageBase);
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 QQuickImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
225 thisRequestFinished =
226 QQuickImageBase::staticMetaObject.indexOfSlot("requestFinished()");
229 d->pix.connectFinished(this, thisRequestFinished);
230 d->pix.connectDownloadProgress(this, thisRequestProgress);
238 void QQuickImageBase::requestFinished()
240 Q_D(QQuickImageBase);
242 QQuickImageBase::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 QQuickImageBase::requestProgress(qint64 received, qint64 total)
269 Q_D(QQuickImageBase);
270 if (d->status == Loading && total > 0) {
271 d->progress = qreal(received)/total;
272 emit progressChanged(d->progress);
276 void QQuickImageBase::componentComplete()
278 Q_D(QQuickImageBase);
279 QQuickItem::componentComplete();
280 if (d->url.isValid())
284 void QQuickImageBase::pixmapChange()
286 Q_D(QQuickImageBase);
287 setImplicitSize(d->pix.width(), d->pix.height());