Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / graphicsitems / qdeclarativeimagebase.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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
14 ** this package.
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 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
30 **
31 **
32 **
33 **
34 **
35 **
36 **
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
51 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeItem *parent)
52   : QDeclarativeImplicitSizeItem(*(new QDeclarativeImageBasePrivate), parent)
53 {
54 }
55
56 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent)
57   : QDeclarativeImplicitSizeItem(dd, parent)
58 {
59 }
60
61 QDeclarativeImageBase::~QDeclarativeImageBase()
62 {
63 }
64
65 QDeclarativeImageBase::Status QDeclarativeImageBase::status() const
66 {
67     Q_D(const QDeclarativeImageBase);
68     return d->status;
69 }
70
71
72 qreal QDeclarativeImageBase::progress() const
73 {
74     Q_D(const QDeclarativeImageBase);
75     return d->progress;
76 }
77
78
79 bool QDeclarativeImageBase::asynchronous() const
80 {
81     Q_D(const QDeclarativeImageBase);
82     return d->async;
83 }
84
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
94 QUrl QDeclarativeImageBase::source() const
95 {
96     Q_D(const QDeclarativeImageBase);
97     return d->url;
98 }
99
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
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
127 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
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
148 bool QDeclarativeImageBase::cache() const
149 {
150     Q_D(const QDeclarativeImageBase);
151     return d->cache;
152 }
153
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
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
180 bool QDeclarativeImageBase::mirror() const
181 {
182     Q_D(const QDeclarativeImageBase);
183     return d->mirror;
184 }
185
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
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
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
269 void QDeclarativeImageBase::componentComplete()
270 {
271     Q_D(QDeclarativeImageBase);
272     QDeclarativeItem::componentComplete();
273     if (d->url.isValid())
274         load();
275 }
276
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