1
/****************************************************************************
3
** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4
** Contact: http://www.qt-project.org/legal
6
** This file is part of the QtQml module of the Qt Toolkit.
8
** $QT_BEGIN_LICENSE:LGPL$
9
** Commercial License Usage
10
** Licensees holding valid commercial Qt licenses may use this file in
11
** accordance with the commercial license agreement provided with the
12
** Software or, alternatively, in accordance with the terms contained in
13
** a written agreement between you and Digia. For licensing terms and
14
** conditions see http://qt.digia.com/licensing. For further information
15
** use the contact form at http://qt.digia.com/contact-us.
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, Digia gives you certain additional
26
** rights. These rights are described in the Digia Qt LGPL Exception
27
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29
** GNU General Public License Usage
30
** Alternatively, this file may be used under the terms of the GNU
31
** General Public License version 3.0 as published by the Free Software
32
** Foundation and appearing in the file LICENSE.GPL included in the
33
** packaging of this file. Please review the following information to
34
** ensure the GNU General Public License version 3.0 requirements will be
35
** met: http://www.gnu.org/copyleft/gpl.html.
40
****************************************************************************/
42
#include "qquickimagebase_p.h"
43
#include "qquickimagebase_p_p.h"
45
#include <QtQml/qqmlinfo.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);
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
emit sourceSizeChanged();
122
if (isComponentComplete())
126
QSize QQuickImageBase::sourceSize() const
128
Q_D(const QQuickImageBase);
130
int width = d->sourcesize.width();
131
int height = d->sourcesize.height();
132
return QSize(width != -1 ? width : d->pix.width(), height != -1 ? height : d->pix.height());
135
void QQuickImageBase::resetSourceSize()
137
setSourceSize(QSize());
140
bool QQuickImageBase::cache() const
142
Q_D(const QQuickImageBase);
146
void QQuickImageBase::setCache(bool cache)
148
Q_D(QQuickImageBase);
149
if (d->cache == cache)
154
if (isComponentComplete())
158
QImage QQuickImageBase::image() const
160
Q_D(const QQuickImageBase);
161
return d->pix.image();
164
void QQuickImageBase::setMirror(bool mirror)
166
Q_D(QQuickImageBase);
167
if (mirror == d->mirror)
172
if (isComponentComplete())
175
emit mirrorChanged();
178
bool QQuickImageBase::mirror() const
180
Q_D(const QQuickImageBase);
184
void QQuickImageBase::load()
186
Q_D(QQuickImageBase);
188
if (d->url.isEmpty()) {
190
if (d->progress != 0.0) {
192
emit progressChanged(d->progress);
196
emit statusChanged(d->status);
198
if (sourceSize() != d->oldSourceSize) {
199
d->oldSourceSize = sourceSize();
200
emit sourceSizeChanged();
205
QQuickPixmap::Options options;
207
options |= QQuickPixmap::Asynchronous;
209
options |= QQuickPixmap::Cache;
210
d->pix.load(qmlEngine(this), d->url, d->sourcesize, options);
212
if (d->pix.isLoading()) {
213
if (d->progress != 0.0) {
215
emit progressChanged(d->progress);
217
if (d->status != Loading) {
219
emit statusChanged(d->status);
222
static int thisRequestProgress = -1;
223
static int thisRequestFinished = -1;
224
if (thisRequestProgress == -1) {
225
thisRequestProgress =
226
QQuickImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
227
thisRequestFinished =
228
QQuickImageBase::staticMetaObject.indexOfSlot("requestFinished()");
231
d->pix.connectFinished(this, thisRequestFinished);
232
d->pix.connectDownloadProgress(this, thisRequestProgress);
233
update(); //pixmap may have invalidated texture, updatePaintNode needs to be called before the next repaint
240
void QQuickImageBase::requestFinished()
242
Q_D(QQuickImageBase);
244
if (d->pix.isError()) {
245
qmlInfo(this) << d->pix.error();
248
if (d->progress != 0.0) {
250
emit progressChanged(d->progress);
254
if (d->progress != 1.0) {
256
emit progressChanged(d->progress);
260
emit statusChanged(d->status);
261
if (sourceSize() != d->oldSourceSize) {
262
d->oldSourceSize = sourceSize();
263
emit sourceSizeChanged();
268
void QQuickImageBase::requestProgress(qint64 received, qint64 total)
270
Q_D(QQuickImageBase);
271
if (d->status == Loading && total > 0) {
272
d->progress = qreal(received)/total;
273
emit progressChanged(d->progress);
277
void QQuickImageBase::componentComplete()
279
Q_D(QQuickImageBase);
280
QQuickItem::componentComplete();
281
if (d->url.isValid())
285
void QQuickImageBase::pixmapChange()
287
Q_D(QQuickImageBase);
288
setImplicitSize(d->pix.width(), d->pix.height());