Port QVariantAnimation to the new property system

Skipped startValue/endValue properties, since they are computed and
writable, which is not supported at the moment.

Skipped currentValue, since its setter might be called inside the
getter, which is not recommended.

Task-number: QTBUG-85520
Change-Id: I1f872b4fcc7227ed91b6915891bbc66019151826
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Andreas Buhr <andreas.buhr@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
This commit is contained in:
Sona Kurazyan 2020-12-11 16:06:56 +01:00
parent 0d76a5cd2c
commit 7a1fdcedfc
5 changed files with 66 additions and 6 deletions

View File

@ -230,7 +230,8 @@ void QVariantAnimationPrivate::recalculateCurrentInterval(bool force/*=false*/)
return;
const qreal endProgress = (direction == QAbstractAnimation::Forward) ? qreal(1) : qreal(0);
const qreal progress = easing.valueForProgress(((duration == 0) ? endProgress : qreal(currentTime) / qreal(duration)));
const qreal progress = easing.value().valueForProgress(
duration == 0 ? endProgress : qreal(currentTime) / qreal(duration));
//0 and 1 are still the boundaries
if (force || (currentInterval.start.first > 0 && progress < currentInterval.start.first)
@ -386,8 +387,17 @@ QEasingCurve QVariantAnimation::easingCurve() const
void QVariantAnimation::setEasingCurve(const QEasingCurve &easing)
{
Q_D(QVariantAnimation);
const bool valueChanged = easing != d->easing;
d->easing = easing;
d->recalculateCurrentInterval();
if (valueChanged)
d->easing.notify();
}
QBindable<QEasingCurve> QVariantAnimation::bindableEasingCurve()
{
Q_D(QVariantAnimation);
return &d->easing;
}
typedef QList<QVariantAnimation::Interpolator> QInterpolatorVector;
@ -506,10 +516,19 @@ void QVariantAnimation::setDuration(int msecs)
qWarning("QVariantAnimation::setDuration: cannot set a negative duration");
return;
}
if (d->duration == msecs)
if (d->duration == msecs) {
d->duration.removeBindingUnlessInWrapper();
return;
}
d->duration = msecs;
d->recalculateCurrentInterval();
d->duration.notify();
}
QBindable<int> QVariantAnimation::bindableDuration()
{
Q_D(QVariantAnimation);
return &d->duration;
}
/*!

View File

@ -57,8 +57,9 @@ class Q_CORE_EXPORT QVariantAnimation : public QAbstractAnimation
Q_PROPERTY(QVariant startValue READ startValue WRITE setStartValue)
Q_PROPERTY(QVariant endValue READ endValue WRITE setEndValue)
Q_PROPERTY(QVariant currentValue READ currentValue NOTIFY valueChanged)
Q_PROPERTY(int duration READ duration WRITE setDuration)
Q_PROPERTY(QEasingCurve easingCurve READ easingCurve WRITE setEasingCurve)
Q_PROPERTY(int duration READ duration WRITE setDuration BINDABLE bindableDuration)
Q_PROPERTY(QEasingCurve easingCurve READ easingCurve WRITE setEasingCurve
BINDABLE bindableEasingCurve)
public:
typedef QPair<qreal, QVariant> KeyValue;
@ -83,9 +84,11 @@ public:
int duration() const override;
void setDuration(int msecs);
QBindable<int> bindableDuration();
QEasingCurve easingCurve() const;
void setEasingCurve(const QEasingCurve &easing);
QBindable<QEasingCurve> bindableEasingCurve();
typedef QVariant (*Interpolator)(const void *from, const void *to, qreal progress);

View File

@ -56,6 +56,7 @@
#include <QtCore/qmetaobject.h>
#include "private/qabstractanimation_p.h"
#include "private/qproperty_p.h"
#include <type_traits>
@ -87,8 +88,14 @@ public:
QVariantAnimation::KeyValue start, end;
} currentInterval;
QEasingCurve easing;
int duration;
void setEasingCurve(const QEasingCurve &easing) { q_func()->setEasingCurve(easing); }
Q_OBJECT_COMPAT_PROPERTY(QVariantAnimationPrivate, QEasingCurve, easing,
&QVariantAnimationPrivate::setEasingCurve)
void setDuration(int msecs) { q_func()->setDuration(msecs); }
Q_OBJECT_COMPAT_PROPERTY(QVariantAnimationPrivate, int, duration,
&QVariantAnimationPrivate::setDuration)
QVariantAnimation::KeyValues keyValues;
QVariantAnimation::Interpolator interpolator;

View File

@ -7,4 +7,6 @@
qt_internal_add_test(tst_qvariantanimation
SOURCES
tst_qvariantanimation.cpp
LIBRARIES
Qt::TestPrivate
)

View File

@ -29,6 +29,7 @@
#include <QtCore/qvariantanimation.h>
#include <QTest>
#include <QtTest/private/qpropertytesthelper_p.h>
class tst_QVariantAnimation : public QObject
{
@ -44,6 +45,8 @@ private slots:
void keyValues();
void duration();
void interpolation();
void durationBindings();
void easingCurveBindings();
};
class TestableQVariantAnimation : public QVariantAnimation
@ -154,6 +157,32 @@ void tst_QVariantAnimation::interpolation()
QCOMPARE(pointAnim.currentValue().toPoint(), QPoint(50, 50));
}
void tst_QVariantAnimation::durationBindings()
{
QVariantAnimation animation;
// duration property
QProperty<int> duration;
animation.bindableDuration().setBinding(Qt::makePropertyBinding(duration));
// negative values must be ignored
QTest::ignoreMessage(QtWarningMsg,
"QVariantAnimation::setDuration: cannot set a negative duration");
duration = -1;
QVERIFY(animation.duration() != duration);
QTestPrivate::testReadWritePropertyBasics(animation, 42, 43, "duration");
}
void tst_QVariantAnimation::easingCurveBindings()
{
QVariantAnimation animation;
QTestPrivate::testReadWritePropertyBasics(animation, QEasingCurve(QEasingCurve::InQuad),
QEasingCurve(QEasingCurve::BezierSpline),
"easingCurve");
}
QTEST_MAIN(tst_QVariantAnimation)
#include "tst_qvariantanimation.moc"