1// Copyright (C) 2018 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "qlottierect_p.h"
5
6#include <QJsonObject>
7#include <QJsonArray>
8#include <QLoggingCategory>
9
10#include <QDebug>
11
12#include "qlottietrimpath_p.h"
13
14QT_BEGIN_NAMESPACE
15
16QLottieRect::QLottieRect(const QLottieRect &other)
17 : QLottieShape(other)
18{
19 m_position = other.m_position;
20 m_size = other.m_size;
21 m_roundness = other.m_roundness;
22}
23
24QLottieRect::QLottieRect(const QJsonObject &definition, QLottieBase *parent)
25{
26 setParent(parent);
27 QLottieBase::parse(definition);
28 if (m_hidden)
29 return;
30
31 qCDebug(lcLottieQtLottieParser) << "QLottieRect::QLottieRect():" << m_name;
32
33 QJsonObject position = definition.value(key: QLatin1String("p")).toObject();
34 position = resolveExpression(definition: position);
35 m_position.construct(definition: position);
36
37 QJsonObject size = definition.value(key: QLatin1String("s")).toObject();
38 size = resolveExpression(definition: size);
39 m_size.construct(definition: size);
40
41 QJsonObject roundness = definition.value(key: QLatin1String("r")).toObject();
42 roundness = resolveExpression(definition: roundness);
43 m_roundness.construct(definition: roundness);
44
45 m_direction = definition.value(key: QLatin1String("d")).toInt();
46}
47
48
49QLottieBase *QLottieRect::clone() const
50{
51 return new QLottieRect(*this);
52}
53
54bool QLottieRect::setProperty(QLottieLiteral::PropertyType propertyType, QVariant value)
55{
56 switch (propertyType) {
57 case QLottieLiteral::RectPosition:
58 qCDebug(lcLottieQtLottieParser) << "Set position" << value.toPointF();
59 m_position.setValue(value.toPointF());
60 break;
61 default:
62 return false;
63 }
64 return true;
65}
66
67void QLottieRect::updateProperties(int frame)
68{
69 m_size.update(frame);
70 m_position.update(frame);
71 m_roundness.update(frame);
72
73 // AE uses center of a shape as it's position,
74 // in Qt a translation is needed
75 QPointF pos = QPointF(m_position.value().x() - m_size.value().width() / 2,
76 m_position.value().y() - m_size.value().height() / 2);
77
78 m_path = QPainterPath();
79 m_path.addRoundedRect(rect: QRectF(pos, m_size.value()),
80 xRadius: m_roundness.value(), yRadius: m_roundness.value());
81
82 if (hasReversedDirection())
83 m_path = m_path.toReversed();
84}
85
86void QLottieRect::render(QLottieRenderer &renderer) const
87{
88 renderer.render(rect: *this);
89}
90
91bool QLottieRect::acceptsTrim() const
92{
93 return true;
94}
95
96QPointF QLottieRect::position() const
97{
98 return m_position.value();
99}
100
101QSizeF QLottieRect::size() const
102{
103 return m_size.value();
104}
105
106qreal QLottieRect::roundness() const
107{
108 return m_roundness.value();
109}
110
111QT_END_NAMESPACE
112

source code of qtlottie/src/lottie/qlottierect.cpp