Port QSequentialAnimationGroup to new property system

There is only one property in QSequentialAnimationGroup,
currentAnimation.
This patch ports this property to the new property system

Task-number: QTBUG-85520
Change-Id: Id528d30f551e88a6165bbb6a3c09d44e89257de5
Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Andreas Buhr 2020-12-17 10:04:17 +01:00
parent 97a8727f0e
commit 7b6cef0e65
4 changed files with 62 additions and 8 deletions

View File

@ -301,6 +301,11 @@ QAbstractAnimation *QSequentialAnimationGroup::currentAnimation() const
return d->currentAnimation;
}
QBindable<QAbstractAnimation *> QSequentialAnimationGroup::bindableCurrentAnimation() const
{
return &d_func()->currentAnimation;
}
/*!
\reimp
*/
@ -424,6 +429,8 @@ bool QSequentialAnimationGroup::event(QEvent *event)
void QSequentialAnimationGroupPrivate::setCurrentAnimation(int index, bool intermediate)
{
Q_Q(QSequentialAnimationGroup);
// currentAnimation.removeBindingUnlessInWrapper()
// is not necessary here, since it is read only
index = qMin(index, animations.count() - 1);
@ -443,8 +450,8 @@ void QSequentialAnimationGroupPrivate::setCurrentAnimation(int index, bool inter
if (currentAnimation)
currentAnimation->stop();
currentAnimation = animations.at(index);
currentAnimationIndex = index;
currentAnimation = animations.at(index);
emit q->currentAnimationChanged(currentAnimation);

View File

@ -52,7 +52,8 @@ class QSequentialAnimationGroupPrivate;
class Q_CORE_EXPORT QSequentialAnimationGroup : public QAnimationGroup
{
Q_OBJECT
Q_PROPERTY(QAbstractAnimation* currentAnimation READ currentAnimation NOTIFY currentAnimationChanged)
Q_PROPERTY(QAbstractAnimation *currentAnimation READ currentAnimation NOTIFY
currentAnimationChanged BINDABLE bindableCurrentAnimation)
public:
QSequentialAnimationGroup(QObject *parent = nullptr);
@ -62,10 +63,11 @@ public:
QPauseAnimation *insertPause(int index, int msecs);
QAbstractAnimation *currentAnimation() const;
QBindable<QAbstractAnimation *> bindableCurrentAnimation() const;
int duration() const override;
Q_SIGNALS:
void currentAnimationChanged(QAbstractAnimation *current);
void currentAnimationChanged(QAbstractAnimation *current) const;
protected:
QSequentialAnimationGroup(QSequentialAnimationGroupPrivate &dd, QObject *parent);

View File

@ -53,6 +53,7 @@
#include "qsequentialanimationgroup.h"
#include "private/qanimationgroup_p.h"
#include "private/qproperty_p.h"
QT_REQUIRE_CONFIG(animation);
@ -62,10 +63,7 @@ class QSequentialAnimationGroupPrivate : public QAnimationGroupPrivate
{
Q_DECLARE_PUBLIC(QSequentialAnimationGroup)
public:
QSequentialAnimationGroupPrivate()
: currentAnimation(nullptr), currentAnimationIndex(-1), lastLoop(0)
{ }
QSequentialAnimationGroupPrivate() : currentAnimationIndex(-1), lastLoop(0) { }
struct AnimationIndex
{
@ -87,7 +85,10 @@ public:
bool atEnd() const;
QAbstractAnimation *currentAnimation;
Q_OBJECT_BINDABLE_PROPERTY_WITH_ARGS(QSequentialAnimationGroupPrivate, QAbstractAnimation *,
currentAnimation,
nullptr // initial value
)
int currentAnimationIndex;
// this is the actual duration of uncontrolled animations

View File

@ -71,6 +71,7 @@ private slots:
void insertAnimation();
void clear();
void pauseResume();
void bindings();
};
void tst_QSequentialAnimationGroup::initTestCase()
@ -1667,5 +1668,48 @@ void tst_QSequentialAnimationGroup::pauseResume()
QCOMPARE(spy.count(), 1);
}
void tst_QSequentialAnimationGroup::bindings()
{
// create a group consisting of three animations
QSequentialAnimationGroup group;
QPointer<QAbstractAnimation> anim1 = new DummyPropertyAnimation(&group);
QCOMPARE(group.animationCount(), 1);
QPointer<QAbstractAnimation> anim2 = new DummyPropertyAnimation(&group);
QCOMPARE(group.animationCount(), 2);
QPointer<QAbstractAnimation> anim3 = new DummyPropertyAnimation(&group);
QCOMPARE(group.animationCount(), 3);
// bind a QProperty to group.currentAnimation
QProperty<QAbstractAnimation *> currentAnim;
currentAnim.setBinding([&]() { return group.currentAnimation(); });
// check that everything behaves as expected
QSignalSpy spy(&group, &QSequentialAnimationGroup::currentAnimationChanged);
QVERIFY(spy.isValid());
int totalDuration = group.duration();
group.setCurrentTime(int(totalDuration * 0.5 / 3));
QCOMPARE(currentAnim.value(), anim1.get());
QCOMPARE(spy.count(), 0);
group.setCurrentTime(int(totalDuration * 1.5 / 3));
QCOMPARE(currentAnim.value(), anim2.get());
QCOMPARE(spy.count(), 1);
// change to other style of formulating a binding to test both
currentAnim.setBinding(group.bindableCurrentAnimation().makeBinding());
group.setCurrentTime(int(totalDuration * 2.5 / 3));
QCOMPARE(currentAnim.value(), anim3.get());
QCOMPARE(spy.count(), 2);
// currentAnimation is read-only. Binding it to something should have no effect
QProperty<QAbstractAnimation *> leader;
group.bindableCurrentAnimation().setBinding([&]() { return leader.value(); });
QCOMPARE(group.currentAnimation(), anim3.get());
}
QTEST_MAIN(tst_QSequentialAnimationGroup)
#include "tst_qsequentialanimationgroup.moc"