1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QVARIANTANIMATION_H
5#define QVARIANTANIMATION_H
6
7#include <QtCore/qabstractanimation.h>
8#include <QtCore/qeasingcurve.h>
9#include <QtCore/qlist.h>
10#include <QtCore/qpair.h>
11#include <QtCore/qvariant.h>
12
13QT_REQUIRE_CONFIG(animation);
14
15QT_BEGIN_NAMESPACE
16
17class QVariantAnimationPrivate;
18class Q_CORE_EXPORT QVariantAnimation : public QAbstractAnimation
19{
20 Q_OBJECT
21 Q_PROPERTY(QVariant startValue READ startValue WRITE setStartValue)
22 Q_PROPERTY(QVariant endValue READ endValue WRITE setEndValue)
23 Q_PROPERTY(QVariant currentValue READ currentValue NOTIFY valueChanged)
24 Q_PROPERTY(int duration READ duration WRITE setDuration BINDABLE bindableDuration)
25 Q_PROPERTY(QEasingCurve easingCurve READ easingCurve WRITE setEasingCurve
26 BINDABLE bindableEasingCurve)
27
28public:
29 typedef QPair<qreal, QVariant> KeyValue;
30 typedef QList<KeyValue> KeyValues;
31
32 QVariantAnimation(QObject *parent = nullptr);
33 ~QVariantAnimation();
34
35 QVariant startValue() const;
36 void setStartValue(const QVariant &value);
37
38 QVariant endValue() const;
39 void setEndValue(const QVariant &value);
40
41 QVariant keyValueAt(qreal step) const;
42 void setKeyValueAt(qreal step, const QVariant &value);
43
44 KeyValues keyValues() const;
45 void setKeyValues(const KeyValues &values);
46
47 QVariant currentValue() const;
48
49 int duration() const override;
50 void setDuration(int msecs);
51 QBindable<int> bindableDuration();
52
53 QEasingCurve easingCurve() const;
54 void setEasingCurve(const QEasingCurve &easing);
55 QBindable<QEasingCurve> bindableEasingCurve();
56
57 typedef QVariant (*Interpolator)(const void *from, const void *to, qreal progress);
58
59Q_SIGNALS:
60 void valueChanged(const QVariant &value);
61
62protected:
63 QVariantAnimation(QVariantAnimationPrivate &dd, QObject *parent = nullptr);
64 bool event(QEvent *event) override;
65
66 void updateCurrentTime(int) override;
67 void updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) override;
68
69 virtual void updateCurrentValue(const QVariant &value);
70 virtual QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const;
71
72private:
73 template <typename T> friend void qRegisterAnimationInterpolator(QVariant (*func)(const T &, const T &, qreal));
74 static void registerInterpolator(Interpolator func, int interpolationType);
75
76 Q_DISABLE_COPY(QVariantAnimation)
77 Q_DECLARE_PRIVATE(QVariantAnimation)
78};
79
80template <typename T>
81void qRegisterAnimationInterpolator(QVariant (*func)(const T &from, const T &to, qreal progress)) {
82 QVariantAnimation::registerInterpolator(func: reinterpret_cast<QVariantAnimation::Interpolator>(reinterpret_cast<void(*)()>(func)), interpolationType: qMetaTypeId<T>());
83}
84
85QT_END_NAMESPACE
86
87#endif //QVARIANTANIMATION_H
88

source code of qtbase/src/corelib/animation/qvariantanimation.h