1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
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 The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
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 or version 3 as published by the Free
20 ** Software Foundation and appearing in the file LICENSE.LGPLv21 and
21 ** LICENSE.LGPLv3 included in the packaging of this file. Please review the
22 ** following information to ensure the GNU Lesser General Public License
23 ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
24 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 **
26 ** As a special exception, The Qt Company gives you certain additional
27 ** rights. These rights are described in The Qt Company LGPL Exception
28 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 **
30 ** GNU General Public License Usage
31 ** Alternatively, this file may be used under the terms of the GNU
32 ** General Public License version 3.0 as published by the Free Software
33 ** Foundation and appearing in the file LICENSE.GPL included in the
34 ** packaging of this file.  Please review the following information to
35 ** ensure the GNU General Public License version 3.0 requirements will be
36 ** met: http://www.gnu.org/copyleft/gpl.html.
37 **
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 
QDeclarativeImageBase(QDeclarativeItem * parent)51 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeItem *parent)
52   : QDeclarativeImplicitSizeItem(*(new QDeclarativeImageBasePrivate), parent)
53 {
54 }
55 
QDeclarativeImageBase(QDeclarativeImageBasePrivate & dd,QDeclarativeItem * parent)56 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent)
57   : QDeclarativeImplicitSizeItem(dd, parent)
58 {
59 }
60 
~QDeclarativeImageBase()61 QDeclarativeImageBase::~QDeclarativeImageBase()
62 {
63 }
64 
status() const65 QDeclarativeImageBase::Status QDeclarativeImageBase::status() const
66 {
67     Q_D(const QDeclarativeImageBase);
68     return d->status;
69 }
70 
71 
progress() const72 qreal QDeclarativeImageBase::progress() const
73 {
74     Q_D(const QDeclarativeImageBase);
75     return d->progress;
76 }
77 
78 
asynchronous() const79 bool QDeclarativeImageBase::asynchronous() const
80 {
81     Q_D(const QDeclarativeImageBase);
82     return d->async;
83 }
84 
setAsynchronous(bool async)85 void QDeclarativeImageBase::setAsynchronous(bool async)
86 {
87     Q_D(QDeclarativeImageBase);
88     if (d->async != async) {
89         d->async = async;
90         emit asynchronousChanged();
91     }
92 }
93 
source() const94 QUrl QDeclarativeImageBase::source() const
95 {
96     Q_D(const QDeclarativeImageBase);
97     return d->url;
98 }
99 
setSource(const QUrl & url)100 void QDeclarativeImageBase::setSource(const QUrl &url)
101 {
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)
105         return;
106 
107     d->url = url;
108     emit sourceChanged(d->url);
109 
110     if (isComponentComplete())
111         load();
112 }
113 
setSourceSize(const QSize & size)114 void QDeclarativeImageBase::setSourceSize(const QSize& size)
115 {
116     Q_D(QDeclarativeImageBase);
117     if (d->sourcesize == size)
118         return;
119 
120     d->sourcesize = size;
121     d->explicitSourceSize = true;
122     emit sourceSizeChanged();
123     if (isComponentComplete())
124         load();
125 }
126 
sourceSize() const127 QSize QDeclarativeImageBase::sourceSize() const
128 {
129     Q_D(const QDeclarativeImageBase);
130 
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());
134 }
135 
resetSourceSize()136 void QDeclarativeImageBase::resetSourceSize()
137 {
138     Q_D(QDeclarativeImageBase);
139     if (!d->explicitSourceSize)
140         return;
141     d->explicitSourceSize = false;
142     d->sourcesize = QSize();
143     emit sourceSizeChanged();
144     if (isComponentComplete())
145         load();
146 }
147 
cache() const148 bool QDeclarativeImageBase::cache() const
149 {
150     Q_D(const QDeclarativeImageBase);
151     return d->cache;
152 }
153 
setCache(bool cache)154 void QDeclarativeImageBase::setCache(bool cache)
155 {
156     Q_D(QDeclarativeImageBase);
157     if (d->cache == cache)
158         return;
159 
160     d->cache = cache;
161     emit cacheChanged();
162     if (isComponentComplete())
163         load();
164 }
165 
setMirror(bool mirror)166 void QDeclarativeImageBase::setMirror(bool mirror)
167 {
168     Q_D(QDeclarativeImageBase);
169     if (mirror == d->mirror)
170         return;
171 
172     d->mirror = mirror;
173 
174     if (isComponentComplete())
175         update();
176 
177     emit mirrorChanged();
178 }
179 
mirror() const180 bool QDeclarativeImageBase::mirror() const
181 {
182     Q_D(const QDeclarativeImageBase);
183     return d->mirror;
184 }
185 
load()186 void QDeclarativeImageBase::load()
187 {
188     Q_D(QDeclarativeImageBase);
189 
190     if (d->url.isEmpty()) {
191         d->pix.clear(this);
192         d->status = Null;
193         d->progress = 0.0;
194         pixmapChange();
195         emit progressChanged(d->progress);
196         emit statusChanged(d->status);
197         update();
198     } else {
199         QDeclarativePixmap::Options options;
200         if (d->async)
201             options |= QDeclarativePixmap::Asynchronous;
202         if (d->cache)
203             options |= QDeclarativePixmap::Cache;
204         d->pix.clear(this);
205         d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), options);
206 
207         if (d->pix.isLoading()) {
208             d->progress = 0.0;
209             d->status = Loading;
210             emit progressChanged(d->progress);
211             emit statusChanged(d->status);
212 
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()");
220             }
221 
222             d->pix.connectFinished(this, thisRequestFinished);
223             d->pix.connectDownloadProgress(this, thisRequestProgress);
224 
225         } else {
226             requestFinished();
227         }
228     }
229 }
230 
requestFinished()231 void QDeclarativeImageBase::requestFinished()
232 {
233     Q_D(QDeclarativeImageBase);
234 
235     QDeclarativeImageBase::Status oldStatus = d->status;
236     qreal oldProgress = d->progress;
237 
238     if (d->pix.isError()) {
239         d->status = Error;
240         qmlInfo(this) << d->pix.error();
241     } else {
242         d->status = Ready;
243     }
244 
245     d->progress = 1.0;
246 
247     pixmapChange();
248 
249     if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
250         emit sourceSizeChanged();
251 
252     if (d->status != oldStatus)
253         emit statusChanged(d->status);
254     if (d->progress != oldProgress)
255         emit progressChanged(d->progress);
256 
257     update();
258 }
259 
requestProgress(qint64 received,qint64 total)260 void QDeclarativeImageBase::requestProgress(qint64 received, qint64 total)
261 {
262     Q_D(QDeclarativeImageBase);
263     if (d->status == Loading && total > 0) {
264         d->progress = qreal(received)/total;
265         emit progressChanged(d->progress);
266     }
267 }
268 
componentComplete()269 void QDeclarativeImageBase::componentComplete()
270 {
271     Q_D(QDeclarativeImageBase);
272     QDeclarativeItem::componentComplete();
273     if (d->url.isValid())
274         load();
275 }
276 
pixmapChange()277 void QDeclarativeImageBase::pixmapChange()
278 {
279     Q_D(QDeclarativeImageBase);
280     setImplicitWidth(d->pix.width());
281     setImplicitHeight(d->pix.height());
282 }
283 
284 QT_END_NAMESPACE
285