[844] | 1 | /****************************************************************************
|
---|
| 2 | **
|
---|
| 3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
| 4 | ** All rights reserved.
|
---|
| 5 | ** Contact: Nokia Corporation ([email protected])
|
---|
| 6 | **
|
---|
| 7 | ** This file is part of the QtDeclarative module of the Qt Toolkit.
|
---|
| 8 | **
|
---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
| 10 | ** Commercial Usage
|
---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
| 14 | ** a written agreement between you and Nokia.
|
---|
| 15 | **
|
---|
| 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.
|
---|
| 23 | **
|
---|
| 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.
|
---|
| 27 | **
|
---|
| 28 | ** GNU General Public License Usage
|
---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
| 32 | ** packaging of this file. Please review the following information to
|
---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
| 35 | **
|
---|
| 36 | ** If you have questions regarding the use of this file, please contact
|
---|
| 37 | ** Nokia at [email protected].
|
---|
| 38 | ** $QT_END_LICENSE$
|
---|
| 39 | **
|
---|
| 40 | ****************************************************************************/
|
---|
| 41 |
|
---|
| 42 | #include "private/qdeclarativeimagebase_p.h"
|
---|
| 43 | #include "private/qdeclarativeimagebase_p_p.h"
|
---|
| 44 |
|
---|
| 45 | #include <qdeclarativeengine.h>
|
---|
| 46 | #include <qdeclarativeinfo.h>
|
---|
| 47 | #include <qdeclarativepixmapcache_p.h>
|
---|
| 48 |
|
---|
| 49 | QT_BEGIN_NAMESPACE
|
---|
| 50 |
|
---|
| 51 | QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent)
|
---|
| 52 | : QDeclarativeItem(dd, parent)
|
---|
| 53 | {
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | QDeclarativeImageBase::~QDeclarativeImageBase()
|
---|
| 57 | {
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | QDeclarativeImageBase::Status QDeclarativeImageBase::status() const
|
---|
| 61 | {
|
---|
| 62 | Q_D(const QDeclarativeImageBase);
|
---|
| 63 | return d->status;
|
---|
| 64 | }
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 | qreal QDeclarativeImageBase::progress() const
|
---|
| 68 | {
|
---|
| 69 | Q_D(const QDeclarativeImageBase);
|
---|
| 70 | return d->progress;
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | bool QDeclarativeImageBase::asynchronous() const
|
---|
| 75 | {
|
---|
| 76 | Q_D(const QDeclarativeImageBase);
|
---|
| 77 | return d->async;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | void QDeclarativeImageBase::setAsynchronous(bool async)
|
---|
| 81 | {
|
---|
| 82 | Q_D(QDeclarativeImageBase);
|
---|
| 83 | if (d->async != async) {
|
---|
| 84 | d->async = async;
|
---|
| 85 | emit asynchronousChanged();
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | QUrl QDeclarativeImageBase::source() const
|
---|
| 90 | {
|
---|
| 91 | Q_D(const QDeclarativeImageBase);
|
---|
| 92 | return d->url;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | void QDeclarativeImageBase::setSource(const QUrl &url)
|
---|
| 96 | {
|
---|
| 97 | Q_D(QDeclarativeImageBase);
|
---|
| 98 | //equality is fairly expensive, so we bypass for simple, common case
|
---|
| 99 | if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
|
---|
| 100 | return;
|
---|
| 101 |
|
---|
| 102 | d->url = url;
|
---|
| 103 | emit sourceChanged(d->url);
|
---|
| 104 |
|
---|
| 105 | if (isComponentComplete())
|
---|
| 106 | load();
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | void QDeclarativeImageBase::setSourceSize(const QSize& size)
|
---|
| 110 | {
|
---|
| 111 | Q_D(QDeclarativeImageBase);
|
---|
| 112 | if (d->sourcesize == size)
|
---|
| 113 | return;
|
---|
| 114 |
|
---|
| 115 | d->sourcesize = size;
|
---|
| 116 | d->explicitSourceSize = true;
|
---|
| 117 | emit sourceSizeChanged();
|
---|
| 118 | if (isComponentComplete())
|
---|
| 119 | load();
|
---|
| 120 | }
|
---|
| 121 |
|
---|
| 122 | QSize QDeclarativeImageBase::sourceSize() const
|
---|
| 123 | {
|
---|
| 124 | Q_D(const QDeclarativeImageBase);
|
---|
| 125 |
|
---|
| 126 | int width = d->sourcesize.width();
|
---|
| 127 | int height = d->sourcesize.height();
|
---|
| 128 | return QSize(width != -1 ? width : implicitWidth(), height != -1 ? height : implicitHeight());
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | void QDeclarativeImageBase::load()
|
---|
| 132 | {
|
---|
| 133 | Q_D(QDeclarativeImageBase);
|
---|
| 134 |
|
---|
| 135 | if (d->url.isEmpty()) {
|
---|
| 136 | d->pix.clear();
|
---|
| 137 | d->status = Null;
|
---|
| 138 | d->progress = 0.0;
|
---|
| 139 | setImplicitWidth(0);
|
---|
| 140 | setImplicitHeight(0);
|
---|
| 141 | emit progressChanged(d->progress);
|
---|
| 142 | emit statusChanged(d->status);
|
---|
| 143 | pixmapChange();
|
---|
| 144 | update();
|
---|
| 145 | } else {
|
---|
| 146 | d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), d->async);
|
---|
| 147 |
|
---|
| 148 | if (d->pix.isLoading()) {
|
---|
| 149 | d->progress = 0.0;
|
---|
| 150 | d->status = Loading;
|
---|
| 151 | emit progressChanged(d->progress);
|
---|
| 152 | emit statusChanged(d->status);
|
---|
| 153 |
|
---|
| 154 | static int thisRequestProgress = -1;
|
---|
| 155 | static int thisRequestFinished = -1;
|
---|
| 156 | if (thisRequestProgress == -1) {
|
---|
| 157 | thisRequestProgress =
|
---|
| 158 | QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
|
---|
| 159 | thisRequestFinished =
|
---|
| 160 | QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestFinished()");
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | d->pix.connectFinished(this, thisRequestFinished);
|
---|
| 164 | d->pix.connectDownloadProgress(this, thisRequestProgress);
|
---|
| 165 |
|
---|
| 166 | } else {
|
---|
| 167 | requestFinished();
|
---|
| 168 | }
|
---|
| 169 | }
|
---|
| 170 | }
|
---|
| 171 |
|
---|
| 172 | void QDeclarativeImageBase::requestFinished()
|
---|
| 173 | {
|
---|
| 174 | Q_D(QDeclarativeImageBase);
|
---|
| 175 |
|
---|
| 176 | QDeclarativeImageBase::Status oldStatus = d->status;
|
---|
| 177 | qreal oldProgress = d->progress;
|
---|
| 178 |
|
---|
| 179 | if (d->pix.isError()) {
|
---|
| 180 | d->status = Error;
|
---|
| 181 | qmlInfo(this) << d->pix.error();
|
---|
| 182 | } else {
|
---|
| 183 | d->status = Ready;
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | d->progress = 1.0;
|
---|
| 187 |
|
---|
| 188 | setImplicitWidth(d->pix.width());
|
---|
| 189 | setImplicitHeight(d->pix.height());
|
---|
| 190 |
|
---|
| 191 | if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
|
---|
| 192 | emit sourceSizeChanged();
|
---|
| 193 |
|
---|
| 194 | if (d->status != oldStatus)
|
---|
| 195 | emit statusChanged(d->status);
|
---|
| 196 | if (d->progress != oldProgress)
|
---|
| 197 | emit progressChanged(d->progress);
|
---|
| 198 | pixmapChange();
|
---|
| 199 | update();
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | void QDeclarativeImageBase::requestProgress(qint64 received, qint64 total)
|
---|
| 203 | {
|
---|
| 204 | Q_D(QDeclarativeImageBase);
|
---|
| 205 | if (d->status == Loading && total > 0) {
|
---|
| 206 | d->progress = qreal(received)/total;
|
---|
| 207 | emit progressChanged(d->progress);
|
---|
| 208 | }
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | void QDeclarativeImageBase::componentComplete()
|
---|
| 212 | {
|
---|
| 213 | Q_D(QDeclarativeImageBase);
|
---|
| 214 | QDeclarativeItem::componentComplete();
|
---|
| 215 | if (d->url.isValid())
|
---|
| 216 | load();
|
---|
| 217 | }
|
---|
| 218 |
|
---|
| 219 | void QDeclarativeImageBase::pixmapChange()
|
---|
| 220 | {
|
---|
| 221 | }
|
---|
| 222 |
|
---|
| 223 | QT_END_NAMESPACE
|
---|