qt5base-lts/examples/widgets/animation/easing/animation.h
Jan Arve Sæther 30947c7fd4 Pass a parent pointer to the Animation instance to avoid memory leak
The leak is just on termination, but we should show good practices in
our examples

Pick-to: 6.4 6.3 6.2
Change-Id: I39abb7545d3c68a1a537b865ba3fcb5e60c22fbf
Reviewed-by: Venugopal Shivashankar <Venugopal.Shivashankar@qt.io>
2022-09-15 17:57:54 +02:00

64 lines
1.7 KiB
C++

// Copyright (C) 2016 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#ifndef ANIMATION_H
#define ANIMATION_H
#include <QtWidgets>
#include <QtCore/qpropertyanimation.h>
class Animation : public QPropertyAnimation {
public:
enum PathType {
LinearPath,
CirclePath,
NPathTypes
};
Animation(QObject *target, const QByteArray &prop, QObject *parent = nullptr)
: QPropertyAnimation(target, prop, parent)
{
setPathType(LinearPath);
}
void setPathType(PathType pathType)
{
if (pathType >= NPathTypes)
qWarning("Unknown pathType %d", pathType);
m_pathType = pathType;
m_path = QPainterPath();
}
void updateCurrentTime(int currentTime) override
{
if (m_pathType == CirclePath) {
if (m_path.isEmpty()) {
QPointF to = endValue().toPointF();
QPointF from = startValue().toPointF();
m_path.moveTo(from);
m_path.addEllipse(QRectF(from, to));
}
int dura = duration();
const qreal progress = ((dura == 0) ? 1 : ((((currentTime - 1) % dura) + 1) / qreal(dura)));
qreal easedProgress = easingCurve().valueForProgress(progress);
if (easedProgress > 1.0) {
easedProgress -= 1.0;
} else if (easedProgress < 0) {
easedProgress += 1.0;
}
QPointF pt = m_path.pointAtPercent(easedProgress);
updateCurrentValue(pt);
emit valueChanged(pt);
} else {
QPropertyAnimation::updateCurrentTime(currentTime);
}
}
QPainterPath m_path;
PathType m_pathType;
};
#endif // ANIMATION_H