| 1 | // Copyright (C) 2018 The Qt Company Ltd. | 
|---|---|
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only | 
| 3 | |
| 4 | #include "bmimage_p.h" | 
| 5 | |
| 6 | #include <QDir> | 
| 7 | #include <QFileInfo> | 
| 8 | #include <QJsonObject> | 
| 9 | |
| 10 | #include "bmtrimpath_p.h" | 
| 11 | |
| 12 | QT_BEGIN_NAMESPACE | 
| 13 | |
| 14 | BMImage::BMImage(const BMImage &other) | 
| 15 | : BMBase(other) | 
| 16 | { | 
| 17 | m_position = other.m_position; | 
| 18 | m_radius = other.m_radius; | 
| 19 | m_image = other.m_image; | 
| 20 | } | 
| 21 | |
| 22 | BMImage::BMImage(const QJsonObject &definition, const QVersionNumber &version, BMBase *parent) | 
| 23 | { | 
| 24 | setParent(parent); | 
| 25 | construct(definition, version); | 
| 26 | } | 
| 27 | |
| 28 | BMBase *BMImage::clone() const | 
| 29 | { | 
| 30 | return new BMImage(*this); | 
| 31 | } | 
| 32 | |
| 33 | void BMImage::construct(const QJsonObject &definition, const QVersionNumber &version) | 
| 34 | { | 
| 35 | BMBase::parse(definition); | 
| 36 | if (m_hidden) | 
| 37 | return; | 
| 38 | |
| 39 |     qCDebug(lcLottieQtBodymovinParser) << "BMImage::construct():"<< m_name;  | 
| 40 | |
| 41 |     QJsonObject asset = definition.value(key: QLatin1String("asset")).toObject();  | 
| 42 |     QString assetString = asset.value(key: QLatin1String("p")).toString();  | 
| 43 | |
| 44 |     if (assetString.startsWith(s: QLatin1String("data:image"))) {  | 
| 45 |         QStringList assetsDataStringList = assetString.split(sep: QLatin1String(","));  | 
| 46 | if (assetsDataStringList.size() > 1) { | 
| 47 | QByteArray assetData = QByteArray::fromBase64(base64: assetsDataStringList[1].toLatin1()); | 
| 48 | m_image.loadFromData(data: assetData); | 
| 49 | } | 
| 50 | } | 
| 51 | else { | 
| 52 |         QFileInfo info(asset.value(key: QLatin1String("fileSource")).toString());  | 
| 53 |         QString url = info.path() + QDir::separator() + asset.value(key: QLatin1String("u")).toString() + assetString;  | 
| 54 | QString path = QUrl(url).toLocalFile(); | 
| 55 | m_image.load(fileName: path); | 
| 56 | if (m_image.isNull()) { | 
| 57 |             qWarning() << "Unable to load file "<< path;  | 
| 58 | } | 
| 59 | } | 
| 60 | |
| 61 |     QJsonObject position = definition.value(key: QLatin1String("p")).toObject();  | 
| 62 | position = resolveExpression(definition: position); | 
| 63 | m_position.construct(definition: position, version); | 
| 64 | |
| 65 |     QJsonObject radius = definition.value(key: QLatin1String("r")).toObject();  | 
| 66 | radius = resolveExpression(definition: radius); | 
| 67 | m_radius.construct(definition: radius, version); | 
| 68 | } | 
| 69 | |
| 70 | void BMImage::updateProperties(int frame) | 
| 71 | { | 
| 72 | m_position.update(frame); | 
| 73 | m_radius.update(frame); | 
| 74 | |
| 75 | m_center = QPointF(m_position.value().x() - m_radius.value() / 2, | 
| 76 | m_position.value().y() - m_radius.value() / 2); | 
| 77 | } | 
| 78 | |
| 79 | void BMImage::render(LottieRenderer &renderer) const | 
| 80 | { | 
| 81 | renderer.render(image: *this); | 
| 82 | } | 
| 83 | |
| 84 | QPointF BMImage::position() const | 
| 85 | { | 
| 86 | return m_position.value(); | 
| 87 | } | 
| 88 | |
| 89 | qreal BMImage::radius() const | 
| 90 | { | 
| 91 | return m_radius.value(); | 
| 92 | } | 
| 93 | |
| 94 | QT_END_NAMESPACE | 
| 95 | 
