summaryrefslogtreecommitdiffstats
path: root/src/corelib/animation/qpropertyanimation.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/animation/qpropertyanimation.cpp')
-rw-r--r--src/corelib/animation/qpropertyanimation.cpp146
1 files changed, 77 insertions, 69 deletions
diff --git a/src/corelib/animation/qpropertyanimation.cpp b/src/corelib/animation/qpropertyanimation.cpp
index be2bed0a07..d461668dbb 100644
--- a/src/corelib/animation/qpropertyanimation.cpp
+++ b/src/corelib/animation/qpropertyanimation.cpp
@@ -1,41 +1,5 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
/*!
\class QPropertyAnimation
@@ -58,6 +22,10 @@
\snippet code/src_corelib_animation_qpropertyanimation.cpp 0
+ \note You can also control an animation's lifespan by choosing a
+ \l{QAbstractAnimation::DeletionPolicy}{delete policy} while starting the
+ animation.
+
The property name and the QObject instance of which property
should be animated are passed to the constructor. You can then
specify the start and end value of the property. The procedure is
@@ -85,13 +53,15 @@
#include "qpropertyanimation_p.h"
#include <QtCore/QMutex>
+#include <QtCore/QHash>
#include <QtCore/private/qlocking_p.h>
QT_BEGIN_NAMESPACE
void QPropertyAnimationPrivate::updateMetaProperty()
{
- if (!target || propertyName.isEmpty()) {
+ const QObject *target = targetObject.valueBypassingBindings();
+ if (!target || propertyName.value().isEmpty()) {
propertyType = QMetaType::UnknownType;
propertyIndex = -1;
return;
@@ -99,18 +69,22 @@ void QPropertyAnimationPrivate::updateMetaProperty()
//propertyType will be set to a valid type only if there is a Q_PROPERTY
//otherwise it will be set to QVariant::Invalid at the end of this function
- propertyType = targetValue->property(propertyName).userType();
- propertyIndex = targetValue->metaObject()->indexOfProperty(propertyName);
+ propertyType = target->property(propertyName.value()).userType();
+ propertyIndex = target->metaObject()->indexOfProperty(propertyName.value());
if (propertyType != QMetaType::UnknownType)
convertValues(propertyType);
if (propertyIndex == -1) {
//there is no Q_PROPERTY on the object
propertyType = QMetaType::UnknownType;
- if (!targetValue->dynamicPropertyNames().contains(propertyName))
- qWarning("QPropertyAnimation: you're trying to animate a non-existing property %s of your QObject", propertyName.constData());
- } else if (!targetValue->metaObject()->property(propertyIndex).isWritable()) {
- qWarning("QPropertyAnimation: you're trying to animate the non-writable property %s of your QObject", propertyName.constData());
+ if (!target->dynamicPropertyNames().contains(propertyName))
+ qWarning("QPropertyAnimation: you're trying to animate a non-existing property %s of "
+ "your QObject",
+ propertyName.value().constData());
+ } else if (!target->metaObject()->property(propertyIndex).isWritable()) {
+ qWarning("QPropertyAnimation: you're trying to animate the non-writable property %s of "
+ "your QObject",
+ propertyName.value().constData());
}
}
@@ -119,10 +93,8 @@ void QPropertyAnimationPrivate::updateProperty(const QVariant &newValue)
if (state == QAbstractAnimation::Stopped)
return;
- if (!target) {
- q_func()->stop(); //the target was destroyed we need to stop the animation
+ if (!targetObject)
return;
- }
if (newValue.userType() == propertyType) {
//no conversion is needed, we directly call the QMetaObject::metacall
@@ -130,9 +102,9 @@ void QPropertyAnimationPrivate::updateProperty(const QVariant &newValue)
int status = -1;
int flags = 0;
void *argv[] = { const_cast<void *>(newValue.constData()), const_cast<QVariant *>(&newValue), &status, &flags };
- QMetaObject::metacall(targetValue, QMetaObject::WriteProperty, propertyIndex, argv);
+ QMetaObject::metacall(targetObject, QMetaObject::WriteProperty, propertyIndex, argv);
} else {
- targetValue->setProperty(propertyName.constData(), newValue);
+ targetObject->setProperty(propertyName.value().constData(), newValue);
}
}
@@ -175,22 +147,37 @@ QPropertyAnimation::~QPropertyAnimation()
*/
QObject *QPropertyAnimation::targetObject() const
{
- return d_func()->target.data();
+ return d_func()->targetObject;
+}
+
+QBindable<QObject *> QPropertyAnimation::bindableTargetObject()
+{
+ return &d_func()->targetObject;
}
void QPropertyAnimation::setTargetObject(QObject *target)
{
Q_D(QPropertyAnimation);
- if (d->target.data() == target)
- return;
-
if (d->state != QAbstractAnimation::Stopped) {
qWarning("QPropertyAnimation::setTargetObject: you can't change the target of a running animation");
return;
}
- d->target = d->targetValue = target;
+ d->targetObject.removeBindingUnlessInWrapper();
+ const QObject *oldTarget = d->targetObject.valueBypassingBindings();
+ if (oldTarget == target)
+ return;
+
+ if (oldTarget != nullptr)
+ QObject::disconnect(oldTarget, &QObject::destroyed, this, nullptr);
+ d->targetObject.setValueBypassingBindings(target);
+
+ if (target != nullptr) {
+ QObject::connect(target, &QObject::destroyed, this,
+ [d] { d->targetObjectDestroyed(); });
+ }
d->updateMetaProperty();
+ d->targetObject.notify();
}
/*!
@@ -214,10 +201,20 @@ void QPropertyAnimation::setPropertyName(const QByteArray &propertyName)
return;
}
- d->propertyName = propertyName;
+ d->propertyName.removeBindingUnlessInWrapper();
+
+ if (d->propertyName.valueBypassingBindings() == propertyName)
+ return;
+
+ d->propertyName.setValueBypassingBindings(propertyName);
d->updateMetaProperty();
+ d->propertyName.notify();
}
+QBindable<QByteArray> QPropertyAnimation::bindablePropertyName()
+{
+ return &d_func()->propertyName;
+}
/*!
\reimp
@@ -251,9 +248,10 @@ void QPropertyAnimation::updateState(QAbstractAnimation::State newState,
{
Q_D(QPropertyAnimation);
- if (!d->target && oldState == Stopped) {
- qWarning("QPropertyAnimation::updateState (%s): Changing state of an animation without target",
- d->propertyName.constData());
+ if (!d->targetObject && oldState == Stopped) {
+ qWarning("QPropertyAnimation::updateState (%s): Changing state of an animation without "
+ "target",
+ d->propertyName.value().constData());
return;
}
@@ -261,14 +259,21 @@ void QPropertyAnimation::updateState(QAbstractAnimation::State newState,
QPropertyAnimation *animToStop = nullptr;
{
- static QBasicMutex mutex;
+ Q_CONSTINIT static QBasicMutex mutex;
auto locker = qt_unique_lock(mutex);
- typedef QPair<QObject *, QByteArray> QPropertyAnimationPair;
+ using QPropertyAnimationPair = std::pair<QObject *, QByteArray>;
typedef QHash<QPropertyAnimationPair, QPropertyAnimation*> QPropertyAnimationHash;
- static QPropertyAnimationHash hash;
- //here we need to use value because we need to know to which pointer
- //the animation was referring in case stopped because the target was destroyed
- QPropertyAnimationPair key(d->targetValue, d->propertyName);
+ Q_CONSTINIT static QPropertyAnimationHash hash;
+
+ // in case the targetObject gets deleted, the following happens:
+ // 1. targetObject's destroyed signal calls our targetObjectDestroyed.
+ // 2. targetObjectDestroyed calls stop()
+ // 3. QAbstractAnimation::stop() calls setState(Stopped)
+ // 4. setState(Stopped) calls updateState(newState, oldState)
+ // 5. we arrive here. d->targetObject is not yet set to nullptr, we can safely use it.
+ Q_ASSERT(d->targetObject);
+
+ QPropertyAnimationPair key(d->targetObject, d->propertyName);
if (newState == Running) {
d->updateMetaProperty();
animToStop = hash.value(key, nullptr);
@@ -276,7 +281,8 @@ void QPropertyAnimation::updateState(QAbstractAnimation::State newState,
locker.unlock();
// update the default start value
if (oldState == Stopped) {
- d->setDefaultStartEndValue(d->targetValue->property(d->propertyName.constData()));
+ d->setDefaultStartEndValue(
+ d->targetObject->property(d->propertyName.value().constData()));
//let's check if we have a start value and an end value
const char *what = nullptr;
if (!startValue().isValid() && (d->direction == Backward || !d->defaultStartEndValue.isValid())) {
@@ -289,9 +295,11 @@ void QPropertyAnimation::updateState(QAbstractAnimation::State newState,
what = "end";
}
if (Q_UNLIKELY(what)) {
- qWarning("QPropertyAnimation::updateState (%s, %s, %ls): starting an animation without %s value",
- d->propertyName.constData(), d->target.data()->metaObject()->className(),
- qUtf16Printable(d->target.data()->objectName()), what);
+ qWarning("QPropertyAnimation::updateState (%s, %s, %ls): starting an animation "
+ "without %s value",
+ d->propertyName.value().constData(),
+ d->targetObject->metaObject()->className(),
+ qUtf16Printable(d->targetObject->objectName()), what);
}
}
} else if (hash.value(key) == this) {