Qt 4.8
qdeclarativeimagebase.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/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 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
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>
48 
50 
53 {
54 }
55 
57  : QDeclarativeImplicitSizeItem(dd, parent)
58 {
59 }
60 
62 {
63 }
64 
66 {
68  return d->status;
69 }
70 
71 
73 {
75  return d->progress;
76 }
77 
78 
80 {
82  return d->async;
83 }
84 
86 {
88  if (d->async != async) {
89  d->async = async;
91  }
92 }
93 
95 {
97  return d->url;
98 }
99 
101 {
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 
115 {
117  if (d->sourcesize == size)
118  return;
119 
120  d->sourcesize = size;
121  d->explicitSourceSize = true;
123  if (isComponentComplete())
124  load();
125 }
126 
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 
137 {
139  if (!d->explicitSourceSize)
140  return;
141  d->explicitSourceSize = false;
142  d->sourcesize = QSize();
144  if (isComponentComplete())
145  load();
146 }
147 
148 bool QDeclarativeImageBase::cache() const
149 {
150  Q_D(const QDeclarativeImageBase);
151  return d->cache;
152 }
153 
155 {
157  if (d->cache == cache)
158  return;
159 
160  d->cache = cache;
161  emit cacheChanged();
162  if (isComponentComplete())
163  load();
164 }
165 
167 {
169  if (mirror == d->mirror)
170  return;
171 
172  d->mirror = mirror;
173 
174  if (isComponentComplete())
175  update();
176 
178 }
179 
181 {
182  Q_D(const QDeclarativeImageBase);
183  return d->mirror;
184 }
185 
187 {
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)
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 =
220  }
221 
222  d->pix.connectFinished(this, thisRequestFinished);
223  d->pix.connectDownloadProgress(this, thisRequestProgress);
224 
225  } else {
226  requestFinished();
227  }
228  }
229 }
230 
232 {
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())
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 
261 {
263  if (d->status == Loading && total > 0) {
264  d->progress = qreal(received)/total;
265  emit progressChanged(d->progress);
266  }
267 }
268 
270 {
273  if (d->url.isValid())
274  load();
275 }
276 
278 {
280  setImplicitWidth(d->pix.width());
281  setImplicitHeight(d->pix.height());
282 }
283 
bool asynchronous() const
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDeclarativeParserStatus ** d
bool isComponentComplete() const
Returns true if construction of the QML component is complete; otherwise returns false.
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition: qurl.cpp:4317
virtual void setMirror(bool mirror)
static const QMetaObject staticMetaObject
This variable stores the meta-object for the class.
Definition: qobject.h:128
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
void update(const QRectF &rect=QRectF())
Schedules a redraw of the area covered by rect in this item.
#define Q_D(Class)
Definition: qglobal.h:2482
virtual void setSourceSize(const QSize &)
void statusChanged(QDeclarativeImageBase::Status)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QDeclarativeItem class provides the most basic of all visual items in QML.
#define emit
Definition: qobjectdefs.h:76
void sourceChanged(const QUrl &)
int indexOfSlot(const char *slot) const
Finds slot and returns its index; otherwise returns -1.
__int64 qint64
Definition: qglobal.h:942
void setImplicitWidth(qreal)
Sets the implied width of the item to w.
Status status() const
QDeclarativeImageBase(QDeclarativeItem *parent=0)
void progressChanged(qreal progress)
Q_DECLARATIVE_EXPORT QDeclarativeEngine * qmlEngine(const QObject *)
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
virtual void componentComplete()
The QSize class defines the size of a two-dimensional object using integer point precision.
Definition: qsize.h:53
QDeclarativeInfo qmlInfo(const QObject *me)
void setImplicitHeight(qreal)
Sets the implied height of the item to h.
qreal progress() const
virtual void setSource(const QUrl &url)
QSize sourceSize() const
void requestProgress(qint64, qint64)