Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / qtquick1 / graphicsitems / qdeclarativeimagebase.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "QtQuick1/private/qdeclarativeimagebase_p.h"
43 #include "QtQuick1/private/qdeclarativeimagebase_p_p.h"
44
45 #include <QtDeclarative/qdeclarativeengine.h>
46 #include <QtDeclarative/qdeclarativeinfo.h>
47 #include <QtQuick1/private/qdeclarativepixmapcache_p.h>
48
49 QT_BEGIN_NAMESPACE
50
51
52
53 QDeclarative1ImageBase::QDeclarative1ImageBase(QDeclarativeItem *parent)
54   : QDeclarative1ImplicitSizeItem(*(new QDeclarative1ImageBasePrivate), parent)
55 {
56 }
57
58 QDeclarative1ImageBase::QDeclarative1ImageBase(QDeclarative1ImageBasePrivate &dd, QDeclarativeItem *parent)
59   : QDeclarative1ImplicitSizeItem(dd, parent)
60 {
61 }
62
63 QDeclarative1ImageBase::~QDeclarative1ImageBase()
64 {
65 }
66
67 QDeclarative1ImageBase::Status QDeclarative1ImageBase::status() const
68 {
69     Q_D(const QDeclarative1ImageBase);
70     return d->status;
71 }
72
73
74 qreal QDeclarative1ImageBase::progress() const
75 {
76     Q_D(const QDeclarative1ImageBase);
77     return d->progress;
78 }
79
80
81 bool QDeclarative1ImageBase::asynchronous() const
82 {
83     Q_D(const QDeclarative1ImageBase);
84     return d->async;
85 }
86
87 void QDeclarative1ImageBase::setAsynchronous(bool async)
88 {
89     Q_D(QDeclarative1ImageBase);
90     if (d->async != async) {
91         d->async = async;
92         emit asynchronousChanged();
93     }
94 }
95
96 QUrl QDeclarative1ImageBase::source() const
97 {
98     Q_D(const QDeclarative1ImageBase);
99     return d->url;
100 }
101
102 void QDeclarative1ImageBase::setSource(const QUrl &url)
103 {
104     Q_D(QDeclarative1ImageBase);
105     //equality is fairly expensive, so we bypass for simple, common case
106     if ((d->url.isEmpty() == url.isEmpty()) && url == d->url)
107         return;
108
109     d->url = url;
110     emit sourceChanged(d->url);
111
112     if (isComponentComplete())
113         load();
114 }
115
116 void QDeclarative1ImageBase::setSourceSize(const QSize& size)
117 {
118     Q_D(QDeclarative1ImageBase);
119     if (d->sourcesize == size)
120         return;
121
122     d->sourcesize = size;
123     d->explicitSourceSize = true;
124     emit sourceSizeChanged();
125     if (isComponentComplete())
126         load();
127 }
128
129 QSize QDeclarative1ImageBase::sourceSize() const
130 {
131     Q_D(const QDeclarative1ImageBase);
132
133     int width = d->sourcesize.width();
134     int height = d->sourcesize.height();
135     return QSize(width != -1 ? width : d->pix.width(), height != -1 ? height : d->pix.height());
136 }
137
138 void QDeclarative1ImageBase::resetSourceSize()
139 {
140     Q_D(QDeclarative1ImageBase);
141     if (!d->explicitSourceSize)
142         return;
143     d->explicitSourceSize = false;
144     d->sourcesize = QSize();
145     emit sourceSizeChanged();
146     if (isComponentComplete())
147         load();
148 }
149
150 bool QDeclarative1ImageBase::cache() const
151 {
152     Q_D(const QDeclarative1ImageBase);
153     return d->cache;
154 }
155
156 void QDeclarative1ImageBase::setCache(bool cache)
157 {
158     Q_D(QDeclarative1ImageBase);
159     if (d->cache == cache)
160         return;
161
162     d->cache = cache;
163     emit cacheChanged();
164     if (isComponentComplete())
165         load();
166 }
167
168 void QDeclarative1ImageBase::setMirror(bool mirror)
169 {
170     Q_D(QDeclarative1ImageBase);
171     if (mirror == d->mirror)
172         return;
173
174     d->mirror = mirror;
175
176     if (isComponentComplete())
177         update();
178
179     emit mirrorChanged();
180 }
181
182 bool QDeclarative1ImageBase::mirror() const
183 {
184     Q_D(const QDeclarative1ImageBase);
185     return d->mirror;
186 }
187
188 void QDeclarative1ImageBase::load()
189 {
190     Q_D(QDeclarative1ImageBase);
191
192     if (d->url.isEmpty()) {
193         d->pix.clear(this);
194         d->status = Null;
195         d->progress = 0.0;
196         pixmapChange();
197         emit progressChanged(d->progress);
198         emit statusChanged(d->status);
199         update();
200     } else {
201         QDeclarative1Pixmap::Options options;
202         if (d->async)
203             options |= QDeclarative1Pixmap::Asynchronous;
204         if (d->cache)
205             options |= QDeclarative1Pixmap::Cache;
206         d->pix.clear(this);
207         d->pix.load(qmlEngine(this), d->url, d->explicitSourceSize ? sourceSize() : QSize(), options);
208
209         if (d->pix.isLoading()) {
210             d->progress = 0.0;
211             d->status = Loading;
212             emit progressChanged(d->progress);
213             emit statusChanged(d->status);
214
215             static int thisRequestProgress = -1;
216             static int thisRequestFinished = -1;
217             if (thisRequestProgress == -1) {
218                 thisRequestProgress =
219                     QDeclarative1ImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)");
220                 thisRequestFinished =
221                     QDeclarative1ImageBase::staticMetaObject.indexOfSlot("requestFinished()");
222             }
223
224             d->pix.connectFinished(this, thisRequestFinished);
225             d->pix.connectDownloadProgress(this, thisRequestProgress);
226
227         } else {
228             requestFinished();
229         }
230     }
231 }
232
233 void QDeclarative1ImageBase::requestFinished()
234 {
235     Q_D(QDeclarative1ImageBase);
236
237     QDeclarative1ImageBase::Status oldStatus = d->status;
238     qreal oldProgress = d->progress;
239
240     if (d->pix.isError()) {
241         d->status = Error;
242         qmlInfo(this) << d->pix.error();
243     } else {
244         d->status = Ready;
245     }
246
247     d->progress = 1.0;
248
249     pixmapChange();
250
251     if (d->sourcesize.width() != d->pix.width() || d->sourcesize.height() != d->pix.height())
252         emit sourceSizeChanged();
253
254     if (d->status != oldStatus)
255         emit statusChanged(d->status);
256     if (d->progress != oldProgress)
257         emit progressChanged(d->progress);
258
259     update();
260 }
261
262 void QDeclarative1ImageBase::requestProgress(qint64 received, qint64 total)
263 {
264     Q_D(QDeclarative1ImageBase);
265     if (d->status == Loading && total > 0) {
266         d->progress = qreal(received)/total;
267         emit progressChanged(d->progress);
268     }
269 }
270
271 void QDeclarative1ImageBase::componentComplete()
272 {
273     Q_D(QDeclarative1ImageBase);
274     QDeclarativeItem::componentComplete();
275     if (d->url.isValid())
276         load();
277 }
278
279 void QDeclarative1ImageBase::pixmapChange()
280 {
281     Q_D(QDeclarative1ImageBase);
282     setImplicitWidth(d->pix.width());
283     setImplicitHeight(d->pix.height());
284 }
285
286
287
288 QT_END_NAMESPACE