aboutsummaryrefslogtreecommitdiffstats
path: root/src/imports/timeline/qquickkeyframe.cpp
blob: 3e98192f28b30eeb247efc93d56de567fb556e20 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd
** All rights reserved.
** For any questions to The Qt Company, please use contact form at http://www.qt.io/contact-us
**
** This file is part of the Qt Enterprise Qt Quick Timeline Add-on.
**
** Licensees holding valid Qt Enterprise licenses may use this file in
** accordance with the Qt Enterprise License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company.
**
** If you have questions regarding the use of this file, please use
** contact form at http://www.qt.io/contact-us
**
****************************************************************************/

#include "qquickkeyframe_p.h"

#include "qquicktimeline_p.h"

#include <QtCore/QVariantAnimation>
#include <QtCore/qmath.h>
#include <QtGui/qpainter.h>
#include <QtQuick/private/qquickitem_p.h>
#include <QtQml/QQmlProperty>

#include <private/qvariantanimation_p.h>

#include <algorithm>

QT_BEGIN_NAMESPACE

class QQuickKeyframesPrivate : public QObjectPrivate
{
    Q_DECLARE_PUBLIC(QQuickKeyframes)
public:
    QQuickKeyframesPrivate()
        : target(nullptr),
          componentComplete(false)
    {
    }

    QObject *target;
    QString propertyName;
    bool componentComplete;

protected:
    void setupKeyframes();

    static void append_keyframe(QQmlListProperty<QQuickKeyframe> *list, QQuickKeyframe *a);
    static int keyframe_count(QQmlListProperty<QQuickKeyframe> *list);
    static QQuickKeyframe* keyframe_at(QQmlListProperty<QQuickKeyframe> *list, int pos);
    static void clear_keyframes(QQmlListProperty<QQuickKeyframe> *list);

    QList<QQuickKeyframe *> keyframes;
    QList<QQuickKeyframe *> sortedKeyframes;

    QVariant originalValue;
};

void QQuickKeyframesPrivate::setupKeyframes()
{
    sortedKeyframes = keyframes;
    std::sort(sortedKeyframes.begin(), sortedKeyframes.end(), [](const QQuickKeyframe *first, const QQuickKeyframe *second) {
        return first->frame() < second->frame();
    });
}

void QQuickKeyframesPrivate::append_keyframe(QQmlListProperty<QQuickKeyframe> *list, QQuickKeyframe *a)
{
    QQuickKeyframes *q = static_cast<QQuickKeyframes *>(list->object);
    q->d_func()->keyframes.append(a);
    q->d_func()->setupKeyframes();
    q->reset();
}

int QQuickKeyframesPrivate::keyframe_count(QQmlListProperty<QQuickKeyframe> *list)
{
    QQuickKeyframes *q = static_cast<QQuickKeyframes *>(list->object);
    return q->d_func()->keyframes.count();
}

QQuickKeyframe* QQuickKeyframesPrivate::keyframe_at(QQmlListProperty<QQuickKeyframe> *list, int pos)
{
    QQuickKeyframes *q = static_cast<QQuickKeyframes *>(list->object);
    return q->d_func()->keyframes.at(pos);
}

void QQuickKeyframesPrivate::clear_keyframes(QQmlListProperty<QQuickKeyframe> *list)
{
    QQuickKeyframes *q = static_cast<QQuickKeyframes *>(list->object);
    while (q->d_func()->keyframes.count()) {
        QQuickKeyframe *firstKeyframe = q->d_func()->keyframes.at(0);
        q->d_func()->keyframes.removeAll(firstKeyframe);
    }
}

class QQuickKeyframePrivate : public QObjectPrivate
{
    Q_DECLARE_PUBLIC(QQuickKeyframe)
public:
    QQuickKeyframePrivate()
        : frame(0)
    {
    }

    qreal frame;
    QEasingCurve easingCurve;
    QVariant value;
};

QQuickKeyframe::QQuickKeyframe(QObject *parent)
    : QObject(*(new QQuickKeyframePrivate), parent)
{

}

qreal QQuickKeyframe::frame() const
{
    Q_D(const QQuickKeyframe);
    return d->frame;
}

void QQuickKeyframe::setFrame(qreal f)
{
    Q_D(QQuickKeyframe);
    if (d->frame == f)
        return;
    d->frame = f;
    emit frameChanged();
}

void QQuickKeyframe::reset()
{
    QQuickKeyframes *keyframes = qobject_cast<QQuickKeyframes*>(parent());
    if (keyframes)
        keyframes->reset();
}

QQuickKeyframe::QQuickKeyframe(QQuickKeyframePrivate &dd, QObject *parent)
    : QObject(dd, parent)
{

}

QQuickKeyframes::QQuickKeyframes(QObject *parent)
    : QObject(*(new QQuickKeyframesPrivate), parent)
{

}

QQmlListProperty<QQuickKeyframe> QQuickKeyframes::keyframes()
{
    Q_D(QQuickKeyframes);

    return QQmlListProperty<QQuickKeyframe>(this, &d->keyframes, QQuickKeyframesPrivate::append_keyframe,
                                            QQuickKeyframesPrivate::keyframe_count,
                                            QQuickKeyframesPrivate::keyframe_at,
                                            QQuickKeyframesPrivate::clear_keyframes);
}

QObject *QQuickKeyframes::target() const
{
    Q_D(const QQuickKeyframes);
    return d->target;
}

void QQuickKeyframes::setTargetObject(QObject *o)
{
    Q_D(QQuickKeyframes);
    if (d->target == o)
        return;
    d->target = o;

    if (!property().isEmpty())
        init();

    emit targetChanged();
}

QString QQuickKeyframes::property() const
{
    Q_D(const QQuickKeyframes);
    return d->propertyName;
}

void QQuickKeyframes::setProperty(const QString &n)
{
    Q_D(QQuickKeyframes);
    if (d->propertyName == n)
        return;
    d->propertyName = n;

    if (target())
        init();

    emit propertyChanged();
}

QVariant QQuickKeyframes::evaluate(qreal frame) const
{
    Q_D(const QQuickKeyframes);

    if (d->sortedKeyframes.isEmpty())
        return QVariant();

    static QQuickKeyframe dummy;
    QQuickTimeline *timeline = qobject_cast<QQuickTimeline*>(parent());
    if (timeline)
        dummy.setFrame(timeline->startFrame() - 0.0001);
    dummy.setValue(d->originalValue);

     QQuickKeyframe *lastFrame = &dummy;

    for (auto keyFrame :  qAsConst(d->sortedKeyframes)) {
        if (qFuzzyCompare(frame, keyFrame->frame()) || frame < keyFrame->frame())
            return keyFrame->evaluate(lastFrame, frame, QQmlProperty(target(), property()).property().userType());
        lastFrame = keyFrame;
    }

    return lastFrame->value();
}

void QQuickKeyframes::setProperty(qreal frame)
{
    if (target()) {
        QQmlProperty qmlProperty(target(), property());

        qmlProperty.write(evaluate(frame));
    }
}

void QQuickKeyframes::init()
{
    Q_D(QQuickKeyframes);
    if (target())
        d->originalValue = QQmlProperty::read(target(), property());
}

void QQuickKeyframes::resetDefaultValue()
{
    Q_D(QQuickKeyframes);
    QQmlProperty::write(target(), property(), d->originalValue);
}

void QQuickKeyframes::reset()
{
    Q_D(QQuickKeyframes);
    if (!d->componentComplete)
        return;

    auto *timeline = qobject_cast<QQuickTimeline*>(parent());
    if (timeline)
        setProperty(timeline->currentFrame());
}

void QQuickKeyframes::setupKeyframes()
{
    Q_D(QQuickKeyframes);

    if (d->componentComplete)
        d->setupKeyframes();
}

void QQuickKeyframes::classBegin()
{
    Q_D(QQuickKeyframes);
    d->componentComplete = false;
}

void QQuickKeyframes::componentComplete()
{
    Q_D(QQuickKeyframes);
    d->componentComplete = true;
    setupKeyframes();
}

QEasingCurve QQuickKeyframe::easing() const
{
    Q_D(const QQuickKeyframe);
    return d->easingCurve;
}

void QQuickKeyframe::setEasing(const QEasingCurve &e)
{
    Q_D(QQuickKeyframe);
    if (d->easingCurve == e)
        return;

    d->easingCurve = e;
    emit easingCurveChanged();
}

QVariant QQuickKeyframe::value() const
{
    Q_D(const QQuickKeyframe);
    return d->value;
}

void QQuickKeyframe::setValue(QVariant v)
{
    Q_D(QQuickKeyframe);
    if (d->value == v)
        return;
    d->value = v;

    reset();

    emit valueChanged();
}

QVariant QQuickKeyframe::evaluate(QQuickKeyframe *pre, qreal frametime, int userType) const
{
    QVariantAnimation::Interpolator interpolator = QVariantAnimationPrivate::getInterpolator(userType);
    if (!pre)
        return value();

    QVariant preValue = pre->value();
    qreal preFrame = pre->frame();

    qreal duration = frame() - preFrame;
    qreal offset = frametime - preFrame;

    qreal progress = easing().valueForProgress(offset / duration);

    preValue.convert(userType);
    QVariant convertedValue = value();
    convertedValue.convert(userType);

    return interpolator(preValue.constData(), convertedValue.constData(), progress);
}

QT_END_NAMESPACE