summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKonstantin Ritt <ritt.ks@gmail.com>2019-04-09 16:52:16 +0300
committerKonstantin Ritt <ritt.ks@gmail.com>2019-04-09 16:51:51 +0000
commitfbaf1d8e3292929e79b7c7dcdbe8f28533809ceb (patch)
tree8d3df41f0bb9662dd475e6fb0cf3573f21b4569d
parent863cbcf01813192a07bc8656b5b2a7bb7beb5b41 (diff)
LottieAnimation: make `source` property of QUrl type
and replace QFile with QQmlFile to make LottieAnimation component support file, qrc and whatever scheme QNetworkAccessManager supports. On Android, assets scheme is also supported now. Change-Id: I014130c8154c7aa386ca271083bcf206755bd937 Reviewed-by: Rebecca Worledge <rebecca.worledge@theqtcompany.com>
-rw-r--r--src/imports/lottieanimation.cpp52
-rw-r--r--src/imports/lottieanimation.h15
-rw-r--r--tests/auto/imports/tst_main.qml2
-rw-r--r--tests/manual/testApp/main.qml2
4 files changed, 44 insertions, 27 deletions
diff --git a/src/imports/lottieanimation.cpp b/src/imports/lottieanimation.cpp
index 3cf0c1c..deeecc1 100644
--- a/src/imports/lottieanimation.cpp
+++ b/src/imports/lottieanimation.cpp
@@ -42,6 +42,7 @@
#include <QMetaObject>
#include <QLoggingCategory>
#include <QThread>
+#include <QQmlFile>
#include <math.h>
#include <QtBodymovin/private/bmbase_p.h>
@@ -87,7 +88,7 @@ Q_LOGGING_CATEGORY(lcLottieQtBodymovinParser, "qt.lottieqt.bodymovin.parser");
LottieAnimation {
loops: 2
quality: LottieAnimation.MediumQuality
- source: ":/animation.json"
+ source: "animation.json"
autoPlay: false
onStatusChanged: {
if (status === LottieAnimation.Ready) {
@@ -177,8 +178,8 @@ void LottieAnimation::componentComplete()
{
QQuickPaintedItem::componentComplete();
- if (m_source.length())
- loadSource(m_source);
+ if (m_source.isValid())
+ load();
}
void LottieAnimation::paint(QPainter *painter)
@@ -240,7 +241,7 @@ void LottieAnimation::paint(QPainter *painter)
\qml
LottieAnimation {
- source: ":/animation.json"
+ source: "animation.json"
autoPlay: false
onStatusChanged: {
if (status === LottieAnimation.Ready)
@@ -263,26 +264,29 @@ void LottieAnimation::setStatus(LottieAnimation::Status status)
}
/*!
- \qmlproperty string LottieAnimation::source
+ \qmlproperty url LottieAnimation::source
- The path of the Bodymovin asset that LottieAnimation plays.
+ The source of the Bodymovin asset that LottieAnimation plays.
+
+ LottieAnimation can handle any URL scheme supported by Qt.
+ The URL may be absolute, or relative to the URL of the component.
Setting the source property starts loading the animation asynchronously.
To monitor progress of loading, connect to the \l status change signal.
*/
-QString LottieAnimation::source() const
+QUrl LottieAnimation::source() const
{
return m_source;
}
-void LottieAnimation::setSource(const QString &source)
+void LottieAnimation::setSource(const QUrl &source)
{
if (m_source != source) {
m_source = source;
emit sourceChanged();
if (isComponentComplete())
- loadSource(source);
+ load();
}
}
@@ -346,7 +350,7 @@ int LottieAnimation::currentFrame() const
\qml
LottieAnimation {
- source: ":/animation.json"
+ source: "animation.json"
onStatusChanged: {
if (status === LottieAnimation.Ready)
frameRate = 60;
@@ -588,24 +592,34 @@ void LottieAnimation::setDirection(LottieAnimation::Direction direction)
m_frameRenderThread->gotoFrame(this, m_currentFrame);
}
-bool LottieAnimation::loadSource(QString filename)
+void LottieAnimation::load()
{
setStatus(Loading);
- QFile sourceFile(filename);
- if (!sourceFile.open(QIODevice::ReadOnly)) {
+ m_file.reset(new QQmlFile(qmlEngine(this), m_source));
+ if (m_file->isLoading())
+ m_file->connectFinished(this, SLOT(loadFinished()));
+ else
+ loadFinished();
+}
+
+void LottieAnimation::loadFinished()
+{
+ if (Q_UNLIKELY(m_file->isError())) {
+ m_file.reset();
setStatus(Error);
- return false;
+ return;
}
- QByteArray json = sourceFile.readAll();
+ Q_ASSERT(m_file->isReady());
+ const QByteArray json = m_file->dataByteArray();
+ m_file.reset();
+
if (Q_UNLIKELY(parse(json) == -1)) {
setStatus(Error);
- return false;
+ return;
}
- sourceFile.close();
-
QMetaObject::invokeMethod(m_frameRenderThread, "registerAnimator", Q_ARG(LottieAnimation*, this));
if (m_autoPlay)
@@ -614,8 +628,6 @@ bool LottieAnimation::loadSource(QString filename)
m_frameRenderThread->start();
setStatus(Ready);
-
- return true;
}
QByteArray LottieAnimation::jsonSource() const
diff --git a/src/imports/lottieanimation.h b/src/imports/lottieanimation.h
index 13bf885..b08ed40 100644
--- a/src/imports/lottieanimation.h
+++ b/src/imports/lottieanimation.h
@@ -41,6 +41,8 @@
QT_BEGIN_NAMESPACE
+class QQmlFile;
+
class BMBase;
class BMLayer;
class BatchRenderer;
@@ -48,7 +50,7 @@ class BatchRenderer;
class LottieAnimation : public QQuickPaintedItem
{
Q_OBJECT
- Q_PROPERTY(QString source READ source WRITE setSource NOTIFY sourceChanged)
+ Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
Q_PROPERTY(int frameRate READ frameRate WRITE setFrameRate RESET resetFrameRate NOTIFY frameRateChanged)
Q_PROPERTY(int startFrame READ startFrame NOTIFY startFrameChanged)
Q_PROPERTY(int endFrame READ endFrame NOTIFY endFrameChanged)
@@ -78,8 +80,8 @@ public:
Status status() const;
- QString source() const;
- void setSource(const QString &source);
+ QUrl source() const;
+ void setSource(const QUrl &source);
int frameRate() const;
void setFrameRate(int frameRate);
@@ -122,6 +124,8 @@ signals:
void endFrameChanged();
protected slots:
+ void loadFinished();
+
void renderNextFrame();
protected:
@@ -132,7 +136,7 @@ protected:
void setStartFrame(int startFrame);
void setEndFrame(int endFrame);
- bool loadSource(QString filename);
+ void load();
virtual int parse(QByteArray jsonSource);
@@ -149,7 +153,8 @@ protected:
qreal m_animWidth = 0;
qreal m_animHeight = 0;
QHash<QString, int> m_markers;
- QString m_source;
+ QUrl m_source;
+ QScopedPointer<QQmlFile> m_file;
QTimer *m_frameAdvance = nullptr;
void gotoFrame(int frame);
diff --git a/tests/auto/imports/tst_main.qml b/tests/auto/imports/tst_main.qml
index 788d385..6e59da9 100644
--- a/tests/auto/imports/tst_main.qml
+++ b/tests/auto/imports/tst_main.qml
@@ -43,7 +43,7 @@ Item {
x: 0
y: 0
quality: LottieAnimation.HighQuality
- source: ":/rec_pos_col_opa.json"
+ source: "rec_pos_col_opa.json"
onFinished: {
bmAnim.start();
diff --git a/tests/manual/testApp/main.qml b/tests/manual/testApp/main.qml
index fdc1878..56d6f89 100644
--- a/tests/manual/testApp/main.qml
+++ b/tests/manual/testApp/main.qml
@@ -56,7 +56,7 @@ Window {
y: 10 * index
loops: LottieAnimation.Infinite
quality: LottieAnimation.MediumQuality
- source: ":/rect_rotate.json"
+ source: "rect_rotate.json"
}
}
}