Qt 4.8
qdeclarativeanimatedimage.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/qdeclarativeanimatedimage_p.h"
43 #include "private/qdeclarativeanimatedimage_p_p.h"
44 
45 #ifndef QT_NO_MOVIE
46 
47 #include <qdeclarativeinfo.h>
48 #include <private/qdeclarativeengine_p.h>
49 
50 #include <QMovie>
51 #include <QNetworkRequest>
52 #include <QNetworkReply>
53 
55 
151 {
152 }
153 
155 {
157  delete d->_movie;
158 }
159 
171 {
173  if(!d->_movie)
174  return false;
175  return d->_movie->state()==QMovie::Paused;
176 }
177 
179 {
181  if(pause == d->paused)
182  return;
183  d->paused = pause;
184  if(!d->_movie)
185  return;
186  d->_movie->setPaused(pause);
187 }
199 {
201  if (!d->_movie)
202  return false;
203  return d->_movie->state()!=QMovie::NotRunning;
204 }
205 
207 {
209  if(play == d->playing)
210  return;
211  d->playing = play;
212  if (!d->_movie)
213  return;
214  if (play)
215  d->_movie->start();
216  else
217  d->_movie->stop();
218 }
219 
231 {
233  if (!d->_movie)
234  return d->preset_currentframe;
235  return d->_movie->currentFrameNumber();
236 }
237 
239 {
241  if (!d->_movie) {
242  d->preset_currentframe = frame;
243  return;
244  }
245  d->_movie->jumpToFrame(frame);
246 }
247 
249 {
251  if (!d->_movie)
252  return 0;
253  return d->_movie->frameCount();
254 }
255 
257 {
259  if (url == d->url)
260  return;
261 
262  delete d->_movie;
263  d->_movie = 0;
264 
265  if (d->reply) {
266  d->reply->deleteLater();
267  d->reply = 0;
268  }
269 
270  d->url = url;
271  emit sourceChanged(d->url);
272 
273  if (isComponentComplete())
274  load();
275 }
276 
278 {
280 
281  QDeclarativeImageBase::Status oldStatus = d->status;
282  qreal oldProgress = d->progress;
283 
284  if (d->url.isEmpty()) {
285  delete d->_movie;
286  d->setPixmap(QPixmap());
287  d->progress = 0;
288  d->status = Null;
289  if (d->status != oldStatus)
290  emit statusChanged(d->status);
291  if (d->progress != oldProgress)
292  emit progressChanged(d->progress);
293  } else {
294 #ifndef QT_NO_LOCALFILE_OPTIMIZED_QML
296  if (!lf.isEmpty()) {
297  //### should be unified with movieRequestFinished
298  d->_movie = new QMovie(lf);
299  if (!d->_movie->isValid()){
300  qmlInfo(this) << "Error Reading Animated Image File " << d->url.toString();
301  delete d->_movie;
302  d->_movie = 0;
303  d->status = Error;
304  if (d->status != oldStatus)
305  emit statusChanged(d->status);
306  return;
307  }
309  this, SLOT(playingStatusChanged()));
310  connect(d->_movie, SIGNAL(frameChanged(int)),
311  this, SLOT(movieUpdate()));
312  d->_movie->setCacheMode(QMovie::CacheAll);
313  if(d->playing)
314  d->_movie->start();
315  else
316  d->_movie->jumpToFrame(0);
317  if(d->paused)
318  d->_movie->setPaused(true);
319  d->setPixmap(d->_movie->currentPixmap());
320  d->status = Ready;
321  d->progress = 1.0;
322  if (d->status != oldStatus)
323  emit statusChanged(d->status);
324  if (d->progress != oldProgress)
325  emit progressChanged(d->progress);
326  return;
327  }
328 #endif
329  d->status = Loading;
330  d->progress = 0;
331  emit statusChanged(d->status);
332  emit progressChanged(d->progress);
333  QNetworkRequest req(d->url);
335  d->reply = qmlEngine(this)->networkAccessManager()->get(req);
336  QObject::connect(d->reply, SIGNAL(finished()),
337  this, SLOT(movieRequestFinished()));
338  QObject::connect(d->reply, SIGNAL(downloadProgress(qint64,qint64)),
340  }
341 }
342 
343 #define ANIMATEDIMAGE_MAXIMUM_REDIRECT_RECURSION 16
344 
346 {
348 
349  d->redirectCount++;
350  if (d->redirectCount < ANIMATEDIMAGE_MAXIMUM_REDIRECT_RECURSION) {
351  QVariant redirect = d->reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
352  if (redirect.isValid()) {
353  QUrl url = d->reply->url().resolved(redirect.toUrl());
354  d->reply->deleteLater();
355  d->reply = 0;
356  setSource(url);
357  return;
358  }
359  }
360  d->redirectCount=0;
361 
362  d->_movie = new QMovie(d->reply);
363  if (!d->_movie->isValid()){
364 #ifndef QT_NO_DEBUG_STREAM
365  qmlInfo(this) << "Error Reading Animated Image File " << d->url;
366 #endif
367  delete d->_movie;
368  d->_movie = 0;
369  d->status = Error;
370  emit statusChanged(d->status);
371  return;
372  }
374  this, SLOT(playingStatusChanged()));
375  connect(d->_movie, SIGNAL(frameChanged(int)),
376  this, SLOT(movieUpdate()));
377  d->_movie->setCacheMode(QMovie::CacheAll);
378  if(d->playing)
379  d->_movie->start();
380  if (d->paused || !d->playing) {
381  d->_movie->jumpToFrame(d->preset_currentframe);
382  d->preset_currentframe = 0;
383  }
384  if(d->paused)
385  d->_movie->setPaused(true);
386  d->setPixmap(d->_movie->currentPixmap());
387  d->status = Ready;
388  emit statusChanged(d->status);
389 }
390 
392 {
394  d->setPixmap(d->_movie->currentPixmap());
395  emit frameChanged();
396 }
397 
399 {
401  if((d->_movie->state() != QMovie::NotRunning) != d->playing){
402  d->playing = (d->_movie->state() != QMovie::NotRunning);
404  }
405  if((d->_movie->state() == QMovie::Paused) != d->paused){
406  d->playing = (d->_movie->state() == QMovie::Paused);
408  }
409 }
410 
412 {
414  QDeclarativeItem::componentComplete(); // NOT QDeclarativeImage
415  if (d->url.isValid())
416  load();
417  if (!d->reply) {
418  setCurrentFrame(d->preset_currentframe);
419  d->preset_currentframe = 0;
420  }
421 }
422 
424 
425 #endif // QT_NO_MOVIE
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
static QString urlToLocalFileOrQrc(const QUrl &url)
virtual void setSource(const QUrl &)
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QDeclarativeAnimatedImage(QDeclarativeItem *parent=0)
The QMovie class is a convenience class for playing movies with QImageReader.
Definition: qmovie.h:74
QDeclarativeParserStatus ** d
bool isComponentComplete() const
Returns true if construction of the QML component is complete; otherwise returns false.
#define SLOT(a)
Definition: qobjectdefs.h:226
#define ANIMATEDIMAGE_MAXIMUM_REDIRECT_RECURSION
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_D(Class)
Definition: qglobal.h:2482
void setAttribute(Attribute code, const QVariant &value)
Sets the attribute associated with code code to be value value.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
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.
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
#define emit
Definition: qobjectdefs.h:76
void sourceChanged(const QUrl &)
__int64 qint64
Definition: qglobal.h:942
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
MovieState
This enum describes the different states of QMovie.
Definition: qmovie.h:82
void progressChanged(qreal progress)
Q_DECLARATIVE_EXPORT QDeclarativeEngine * qmlEngine(const QObject *)
virtual void componentComplete()
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
The QPixmap class is an off-screen image representation that can be used as a paint device...
Definition: qpixmap.h:71
QDeclarativeInfo qmlInfo(const QObject *me)
QNetworkAccessManager * networkAccessManager() const
Returns a common QNetworkAccessManager which can be used by any QML element instantiated by this engi...
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
void requestProgress(qint64, qint64)
void stateChanged(const QString &)