|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 #include <QFile> |
|
50 |
|
51 QT_BEGIN_NAMESPACE |
|
52 |
|
53 QDeclarativeImageBase::QDeclarativeImageBase(QDeclarativeImageBasePrivate &dd, QDeclarativeItem *parent) |
|
54 : QDeclarativeItem(dd, parent) |
|
55 { |
|
56 } |
|
57 |
|
58 QDeclarativeImageBase::~QDeclarativeImageBase() |
|
59 { |
|
60 Q_D(QDeclarativeImageBase); |
|
61 if (d->pendingPixmapCache) |
|
62 QDeclarativePixmapCache::cancel(d->url, this); |
|
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 |
|
95 QUrl QDeclarativeImageBase::source() const |
|
96 { |
|
97 Q_D(const QDeclarativeImageBase); |
|
98 return d->url; |
|
99 } |
|
100 |
|
101 void QDeclarativeImageBase::setSource(const QUrl &url) |
|
102 { |
|
103 Q_D(QDeclarativeImageBase); |
|
104 //equality is fairly expensive, so we bypass for simple, common case |
|
105 if ((d->url.isEmpty() == url.isEmpty()) && url == d->url) |
|
106 return; |
|
107 |
|
108 if (d->pendingPixmapCache) { |
|
109 QDeclarativePixmapCache::cancel(d->url, this); |
|
110 d->pendingPixmapCache = false; |
|
111 } |
|
112 |
|
113 d->url = url; |
|
114 emit sourceChanged(d->url); |
|
115 |
|
116 if (isComponentComplete()) |
|
117 load(); |
|
118 } |
|
119 |
|
120 void QDeclarativeImageBase::setSourceSize(const QSize& size) |
|
121 { |
|
122 Q_D(QDeclarativeImageBase); |
|
123 if (d->sourcesize == size) |
|
124 return; |
|
125 d->sourcesize = size; |
|
126 emit sourceSizeChanged(); |
|
127 if (isComponentComplete()) |
|
128 load(); |
|
129 } |
|
130 |
|
131 QSize QDeclarativeImageBase::sourceSize() const |
|
132 { |
|
133 Q_D(const QDeclarativeImageBase); |
|
134 return d->sourcesize.isValid() ? d->sourcesize : QSize(implicitWidth(),implicitHeight()); |
|
135 } |
|
136 |
|
137 void QDeclarativeImageBase::load() |
|
138 { |
|
139 Q_D(QDeclarativeImageBase); |
|
140 if (d->progress != 0.0) { |
|
141 d->progress = 0.0; |
|
142 emit progressChanged(d->progress); |
|
143 } |
|
144 |
|
145 if (d->url.isEmpty()) { |
|
146 d->pix = QPixmap(); |
|
147 d->status = Null; |
|
148 setImplicitWidth(0); |
|
149 setImplicitHeight(0); |
|
150 emit statusChanged(d->status); |
|
151 pixmapChange(); |
|
152 update(); |
|
153 } else { |
|
154 d->status = Loading; |
|
155 int reqwidth = d->sourcesize.width(); |
|
156 int reqheight = d->sourcesize.height(); |
|
157 QSize impsize; |
|
158 QString errorString; |
|
159 QDeclarativePixmapReply::Status status = QDeclarativePixmapCache::get(d->url, &d->pix, &errorString, &impsize, d->async, reqwidth, reqheight); |
|
160 if (status != QDeclarativePixmapReply::Ready && status != QDeclarativePixmapReply::Error) { |
|
161 QDeclarativePixmapReply *reply = QDeclarativePixmapCache::request(qmlEngine(this), d->url, reqwidth, reqheight); |
|
162 d->pendingPixmapCache = true; |
|
163 |
|
164 static int replyDownloadProgress = -1; |
|
165 static int replyFinished = -1; |
|
166 static int thisRequestProgress = -1; |
|
167 static int thisRequestFinished = -1; |
|
168 if (replyDownloadProgress == -1) { |
|
169 replyDownloadProgress = |
|
170 QDeclarativePixmapReply::staticMetaObject.indexOfSignal("downloadProgress(qint64,qint64)"); |
|
171 replyFinished = |
|
172 QDeclarativePixmapReply::staticMetaObject.indexOfSignal("finished()"); |
|
173 thisRequestProgress = |
|
174 QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestProgress(qint64,qint64)"); |
|
175 thisRequestFinished = |
|
176 QDeclarativeImageBase::staticMetaObject.indexOfSlot("requestFinished()"); |
|
177 } |
|
178 |
|
179 QMetaObject::connect(reply, replyFinished, this, |
|
180 thisRequestFinished, Qt::DirectConnection); |
|
181 QMetaObject::connect(reply, replyDownloadProgress, this, |
|
182 thisRequestProgress, Qt::DirectConnection); |
|
183 } else { |
|
184 //### should be unified with requestFinished |
|
185 if (status == QDeclarativePixmapReply::Ready) { |
|
186 setImplicitWidth(impsize.width()); |
|
187 setImplicitHeight(impsize.height()); |
|
188 |
|
189 if (d->status == Loading) |
|
190 d->status = Ready; |
|
191 |
|
192 if (!d->sourcesize.isValid()) |
|
193 emit sourceSizeChanged(); |
|
194 } else { |
|
195 d->status = Error; |
|
196 qmlInfo(this) << errorString; |
|
197 } |
|
198 d->progress = 1.0; |
|
199 emit statusChanged(d->status); |
|
200 emit progressChanged(d->progress); |
|
201 pixmapChange(); |
|
202 update(); |
|
203 } |
|
204 } |
|
205 |
|
206 emit statusChanged(d->status); |
|
207 } |
|
208 |
|
209 void QDeclarativeImageBase::requestFinished() |
|
210 { |
|
211 Q_D(QDeclarativeImageBase); |
|
212 |
|
213 d->pendingPixmapCache = false; |
|
214 |
|
215 QSize impsize; |
|
216 QString errorString; |
|
217 if (QDeclarativePixmapCache::get(d->url, &d->pix, &errorString, &impsize, d->async, d->sourcesize.width(), d->sourcesize.height()) != QDeclarativePixmapReply::Ready) { |
|
218 d->status = Error; |
|
219 qmlInfo(this) << errorString; |
|
220 } |
|
221 setImplicitWidth(impsize.width()); |
|
222 setImplicitHeight(impsize.height()); |
|
223 |
|
224 if (d->status == Loading) |
|
225 d->status = Ready; |
|
226 d->progress = 1.0; |
|
227 emit statusChanged(d->status); |
|
228 emit progressChanged(1.0); |
|
229 if (!d->sourcesize.isValid()) |
|
230 emit sourceSizeChanged(); |
|
231 pixmapChange(); |
|
232 update(); |
|
233 } |
|
234 |
|
235 void QDeclarativeImageBase::requestProgress(qint64 received, qint64 total) |
|
236 { |
|
237 Q_D(QDeclarativeImageBase); |
|
238 if (d->status == Loading && total > 0) { |
|
239 d->progress = qreal(received)/total; |
|
240 emit progressChanged(d->progress); |
|
241 } |
|
242 } |
|
243 |
|
244 void QDeclarativeImageBase::componentComplete() |
|
245 { |
|
246 Q_D(QDeclarativeImageBase); |
|
247 QDeclarativeItem::componentComplete(); |
|
248 if (d->url.isValid()) |
|
249 load(); |
|
250 } |
|
251 |
|
252 void QDeclarativeImageBase::pixmapChange() |
|
253 { |
|
254 } |
|
255 |
|
256 QT_END_NAMESPACE |