| 1 | // Copyright (C) 2018 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #ifndef LOTTIEANIMATION_P_H |
| 5 | #define LOTTIEANIMATION_P_H |
| 6 | |
| 7 | // |
| 8 | // W A R N I N G |
| 9 | // ------------- |
| 10 | // |
| 11 | // This file is not part of the Qt API. It exists purely as an |
| 12 | // implementation detail. This header file may change from version to |
| 13 | // version without notice, or even be removed. |
| 14 | // |
| 15 | // We mean it. |
| 16 | // |
| 17 | |
| 18 | #include <QtLottie/qtlottieexports.h> |
| 19 | |
| 20 | #include <QtCore/qbytearray.h> |
| 21 | #include <QtCore/qlist.h> |
| 22 | #include <QtCore/qmetaobject.h> |
| 23 | #include <QtCore/qthread.h> |
| 24 | #include <QtGui/qimage.h> |
| 25 | #include <QtQuick/qquickpainteditem.h> |
| 26 | |
| 27 | QT_BEGIN_NAMESPACE |
| 28 | |
| 29 | class QQmlFile; |
| 30 | |
| 31 | class QBatchRenderer; |
| 32 | |
| 33 | class Q_LOTTIE_EXPORT QLottieAnimation : public QQuickPaintedItem |
| 34 | { |
| 35 | Q_OBJECT |
| 36 | Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged) |
| 37 | Q_PROPERTY(int frameRate READ frameRate WRITE setFrameRate RESET resetFrameRate NOTIFY frameRateChanged) |
| 38 | Q_PROPERTY(int startFrame READ startFrame NOTIFY startFrameChanged) |
| 39 | Q_PROPERTY(int endFrame READ endFrame NOTIFY endFrameChanged) |
| 40 | Q_PROPERTY(Status status READ status WRITE setStatus NOTIFY statusChanged) |
| 41 | Q_PROPERTY(Quality quality READ quality WRITE setQuality NOTIFY qualityChanged) |
| 42 | Q_PROPERTY(bool autoPlay MEMBER m_autoPlay NOTIFY autoPlayChanged) |
| 43 | Q_PROPERTY(int loops MEMBER m_loops NOTIFY loopsChanged) |
| 44 | Q_PROPERTY(Direction direction READ direction WRITE setDirection NOTIFY directionChanged) |
| 45 | |
| 46 | QML_NAMED_ELEMENT(LottieAnimation) |
| 47 | QML_ADDED_IN_VERSION(1, 0) |
| 48 | |
| 49 | public: |
| 50 | enum Status{Null, Loading, Ready, Error}; |
| 51 | Q_ENUM(Status) |
| 52 | |
| 53 | enum Quality{LowQuality, MediumQuality, HighQuality}; |
| 54 | Q_ENUM(Quality) |
| 55 | |
| 56 | enum Direction{Forward = 1, Reverse = -1}; |
| 57 | Q_ENUM(Direction) |
| 58 | |
| 59 | enum LoopCount{Infinite = -1}; |
| 60 | Q_ENUM(LoopCount) |
| 61 | |
| 62 | explicit QLottieAnimation(QQuickItem *parent = nullptr); |
| 63 | ~QLottieAnimation() override; |
| 64 | |
| 65 | void paint(QPainter *painter) override; |
| 66 | |
| 67 | Status status() const; |
| 68 | |
| 69 | QUrl source() const; |
| 70 | void setSource(const QUrl &source); |
| 71 | |
| 72 | int frameRate() const; |
| 73 | void setFrameRate(int frameRate); |
| 74 | void resetFrameRate(); |
| 75 | |
| 76 | Quality quality() const; |
| 77 | void setQuality(Quality quality); |
| 78 | |
| 79 | Direction direction() const; |
| 80 | void setDirection(Direction direction); |
| 81 | |
| 82 | int startFrame() const; |
| 83 | int endFrame() const; |
| 84 | int currentFrame() const; |
| 85 | |
| 86 | QVersionNumber version() const; |
| 87 | |
| 88 | Q_INVOKABLE void start(); |
| 89 | |
| 90 | Q_INVOKABLE void play(); |
| 91 | Q_INVOKABLE void pause(); |
| 92 | Q_INVOKABLE void togglePause(); |
| 93 | Q_INVOKABLE void stop(); |
| 94 | Q_INVOKABLE void gotoAndPlay(int frame); |
| 95 | Q_INVOKABLE bool gotoAndPlay(const QString &frameMarker); |
| 96 | Q_INVOKABLE void gotoAndStop(int frame); |
| 97 | Q_INVOKABLE bool gotoAndStop(const QString &frameMarker); |
| 98 | Q_INVOKABLE double getDuration(bool inFrames = false); |
| 99 | |
| 100 | QByteArray jsonSource() const; |
| 101 | |
| 102 | Q_SIGNALS: |
| 103 | void statusChanged(); |
| 104 | void qualityChanged(); |
| 105 | void sourceChanged(); |
| 106 | void finished(); |
| 107 | void frameRateChanged(); |
| 108 | void autoPlayChanged(); |
| 109 | void loopsChanged(); |
| 110 | void directionChanged(); |
| 111 | void startFrameChanged(); |
| 112 | void endFrameChanged(); |
| 113 | |
| 114 | protected Q_SLOTS: |
| 115 | void loadFinished(); |
| 116 | |
| 117 | void renderNextFrame(); |
| 118 | |
| 119 | protected: |
| 120 | void componentComplete() override; |
| 121 | |
| 122 | void setStatus(Status status); |
| 123 | |
| 124 | void setStartFrame(int startFrame); |
| 125 | void setEndFrame(int endFrame); |
| 126 | |
| 127 | void load(); |
| 128 | |
| 129 | virtual int parse(const QByteArray &jsonSource); |
| 130 | |
| 131 | protected: |
| 132 | QBatchRenderer *m_frameRenderThread = nullptr; |
| 133 | QMetaObject::Connection m_waitForFrameConn; |
| 134 | |
| 135 | Status m_status = Null; |
| 136 | QVersionNumber m_version = QVersionNumber(); |
| 137 | int m_startFrame = 0; |
| 138 | int m_endFrame = 0; |
| 139 | int m_currentFrame = 0; |
| 140 | int m_frameRate = 30; |
| 141 | int m_animFrameRate = 30; |
| 142 | qreal m_animWidth = 0; |
| 143 | qreal m_animHeight = 0; |
| 144 | QHash<QString, int> m_markers; |
| 145 | QUrl m_source; |
| 146 | QScopedPointer<QQmlFile> m_file; |
| 147 | QTimer *m_frameAdvance = nullptr; |
| 148 | |
| 149 | void gotoFrame(int frame); |
| 150 | void reset(); |
| 151 | |
| 152 | private: |
| 153 | Quality m_quality = HighQuality; |
| 154 | bool m_autoPlay = true; |
| 155 | int m_loops = 1; |
| 156 | int m_currentLoop = 0; |
| 157 | int m_direction = Forward; |
| 158 | QByteArray m_jsonSource; |
| 159 | }; |
| 160 | |
| 161 | QT_END_NAMESPACE |
| 162 | |
| 163 | Q_DECLARE_METATYPE(QLottieAnimation*) |
| 164 | |
| 165 | #endif // LOTTIEANIMATION_P_H |
| 166 | |