diff options
Diffstat (limited to 'src/corelib/tools/qeasingcurve.cpp')
-rw-r--r-- | src/corelib/tools/qeasingcurve.cpp | 186 |
1 files changed, 78 insertions, 108 deletions
diff --git a/src/corelib/tools/qeasingcurve.cpp b/src/corelib/tools/qeasingcurve.cpp index d7f69d3e0a..52602a0256 100644 --- a/src/corelib/tools/qeasingcurve.cpp +++ b/src/corelib/tools/qeasingcurve.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 /* @@ -308,7 +272,7 @@ #endif #include <QtCore/qpoint.h> -#include <QtCore/qvector.h> +#include <QtCore/qlist.h> QT_BEGIN_NAMESPACE @@ -320,7 +284,8 @@ static bool isConfigFunction(QEasingCurve::Type type) type == QEasingCurve::TCBSpline; } -struct TCBPoint { +struct TCBPoint +{ QPointF _point; qreal _t; qreal _c; @@ -357,7 +322,7 @@ QDataStream &operator>>(QDataStream &stream, TCBPoint &point) return stream; } -typedef QVector<TCBPoint> TCBPoints; +typedef QList<TCBPoint> TCBPoints; class QEasingCurveFunction { @@ -375,7 +340,7 @@ public: qreal _p; qreal _a; qreal _o; - QVector<QPointF> _bezierCurves; + QList<QPointF> _bezierCurves; TCBPoints _tcbPoints; }; @@ -443,12 +408,12 @@ class QEasingCurvePrivate public: QEasingCurvePrivate() : type(QEasingCurve::Linear), - config(0), + config(nullptr), func(&easeNone) { } QEasingCurvePrivate(const QEasingCurvePrivate &other) : type(other.type), - config(other.config ? other.config->copy() : 0), + config(other.config ? other.config->copy() : nullptr), func(other.func) { } ~QEasingCurvePrivate() { delete config; } @@ -468,8 +433,8 @@ struct BezierEase : public QEasingCurveFunction qreal p3x, p3y; }; - QVector<SingleCubicBezier> _curves; - QVector<qreal> _intervals; + QList<SingleCubicBezier> _curves; + QList<qreal> _intervals; int _curveCount; bool _init; bool _valid; @@ -482,7 +447,7 @@ struct BezierEase : public QEasingCurveFunction { if (_bezierCurves.constLast() == QPointF(1.0, 1.0)) { _init = true; - _curveCount = _bezierCurves.count() / 3; + _curveCount = _bezierCurves.size() / 3; for (int i=0; i < _curveCount; i++) { _intervals[i] = _bezierCurves.at(i * 3 + 2).x(); @@ -501,17 +466,17 @@ struct BezierEase : public QEasingCurveFunction _curves[0].p3y = _bezierCurves.at(2).y(); } else if (i == (_curveCount - 1)) { - _curves[i].p0x = _bezierCurves.at(_bezierCurves.count() - 4).x(); - _curves[i].p0y = _bezierCurves.at(_bezierCurves.count() - 4).y(); + _curves[i].p0x = _bezierCurves.at(_bezierCurves.size() - 4).x(); + _curves[i].p0y = _bezierCurves.at(_bezierCurves.size() - 4).y(); - _curves[i].p1x = _bezierCurves.at(_bezierCurves.count() - 3).x(); - _curves[i].p1y = _bezierCurves.at(_bezierCurves.count() - 3).y(); + _curves[i].p1x = _bezierCurves.at(_bezierCurves.size() - 3).x(); + _curves[i].p1y = _bezierCurves.at(_bezierCurves.size() - 3).y(); - _curves[i].p2x = _bezierCurves.at(_bezierCurves.count() - 2).x(); - _curves[i].p2y = _bezierCurves.at(_bezierCurves.count() - 2).y(); + _curves[i].p2x = _bezierCurves.at(_bezierCurves.size() - 2).x(); + _curves[i].p2y = _bezierCurves.at(_bezierCurves.size() - 2).y(); - _curves[i].p3x = _bezierCurves.at(_bezierCurves.count() - 1).x(); - _curves[i].p3y = _bezierCurves.at(_bezierCurves.count() - 1).y(); + _curves[i].p3x = _bezierCurves.at(_bezierCurves.size() - 1).x(); + _curves[i].p3y = _bezierCurves.at(_bezierCurves.size() - 1).y(); } else { _curves[i].p0x = _bezierCurves.at(i * 3 - 1).x(); _curves[i].p0y = _bezierCurves.at(i * 3 - 1).y(); @@ -570,7 +535,7 @@ struct BezierEase : public QEasingCurveFunction qreal value(qreal x) override { - Q_ASSERT(_bezierCurves.count() % 3 == 0); + Q_ASSERT(_bezierCurves.size() % 3 == 0); if (_bezierCurves.isEmpty()) { return x; @@ -590,7 +555,7 @@ struct BezierEase : public QEasingCurveFunction if (!(x < 1)) return 1; - SingleCubicBezier *singleCubicBezier = 0; + SingleCubicBezier *singleCubicBezier = nullptr; getBezierSegment(singleCubicBezier, x); return evaluateSegmentForY(*singleCubicBezier, findTForX(*singleCubicBezier, x)); @@ -605,8 +570,8 @@ struct BezierEase : public QEasingCurveFunction const qreal s = 1 - t; - const qreal s_squared = s*s; - const qreal t_squared = t*t; + const qreal s_squared = s * s; + const qreal t_squared = t * t; const qreal s_cubic = s_squared * s; const qreal t_cubic = t_squared * t; @@ -623,8 +588,8 @@ struct BezierEase : public QEasingCurveFunction const qreal s = 1 - t; - const qreal s_squared = s*s; - const qreal t_squared = t*t; + const qreal s_squared = s * s; + const qreal t_squared = t * t; const qreal s_cubic = s_squared * s; const qreal t_cubic = t_squared * t; @@ -639,7 +604,7 @@ struct BezierEase : public QEasingCurveFunction const qreal p2 = singleCubicBezier.p2x; const qreal p3 = singleCubicBezier.p3x; - const qreal t_squared = t*t; + const qreal t_squared = t * t; return -3*p0 + 3*p1 + 6*p0*t - 12*p1*t + 6*p2*t + 3*p3*t_squared - 3*p0*t_squared + 9*p1*t_squared - 9*p2*t_squared; } @@ -739,7 +704,7 @@ struct BezierEase : public QEasingCurveFunction qreal cos = b * x - a * x_squared; if (cos < 0) - return 0.225 * (cos * 1 *-cos - cos) + cos; + return 0.225 * (cos * 1 * -cos - cos) + cos; return 0.225 * (cos * cos - cos) + cos; } @@ -794,15 +759,15 @@ struct BezierEase : public QEasingCurveFunction if (D >= 0) { const qreal D_sqrt = qSqrt(D); - qreal u = _cbrt( -q * 0.5 + D_sqrt); - qreal v = _cbrt( -q * 0.5 - D_sqrt); + qreal u = _cbrt(-q * 0.5 + D_sqrt); + qreal v = _cbrt(-q * 0.5 - D_sqrt); qreal z1 = u + v; qreal t1 = z1 - a_by3; if (inRange(t1)) return t1; - qreal z2 = -1 *u; + qreal z2 = -1 * u; qreal t2 = z2 - a_by3; return t2; } @@ -825,7 +790,7 @@ struct BezierEase : public QEasingCurveFunction cosacos(g, s1, s2, s3); - qreal z1 = -1* f * s2; + qreal z1 = -1 * f * s2; qreal t1 = z1 - a_by3; if (inRange(t1)) return t1; @@ -904,7 +869,7 @@ struct TCBEase : public BezierEase qreal value(qreal x) override { - Q_ASSERT(_bezierCurves.count() % 3 == 0); + Q_ASSERT(_bezierCurves.size() % 3 == 0); if (_bezierCurves.isEmpty()) { qWarning("QEasingCurve: Invalid tcb curve"); @@ -940,7 +905,7 @@ struct ElasticEase : public QEasingCurveFunction { qreal p = (_p < 0) ? qreal(0.3) : _p; qreal a = (_a < 0) ? qreal(1.0) : _a; - switch(_t) { + switch (_t) { case QEasingCurve::InElastic: return easeInElastic(t, a, p); case QEasingCurve::OutElastic: @@ -973,7 +938,7 @@ struct BounceEase : public QEasingCurveFunction qreal value(qreal t) override { qreal a = (_a < 0) ? qreal(1.0) : _a; - switch(_t) { + switch (_t) { case QEasingCurve::InBounce: return easeInBounce(t, a); case QEasingCurve::OutBounce: @@ -1011,7 +976,7 @@ struct BackEase : public QEasingCurveFunction if (!(t < 1)) return 1; qreal o = (_o < 0) ? qreal(1.70158) : _o; - switch(_t) { + switch (_t) { case QEasingCurve::InBack: return easeInBack(t, o); case QEasingCurve::OutBack: @@ -1028,7 +993,7 @@ struct BackEase : public QEasingCurveFunction static QEasingCurve::EasingFunction curveToFunc(QEasingCurve::Type curve) { - switch(curve) { + switch (curve) { case QEasingCurve::Linear: return &easeNone; case QEasingCurve::InQuad: @@ -1087,7 +1052,7 @@ static QEasingCurve::EasingFunction curveToFunc(QEasingCurve::Type curve) return &easeInOutCirc; case QEasingCurve::OutInCirc: return &easeOutInCirc; - // Internal for, compatibility with QTimeLine only ?? + // Internal - needed for QTimeLine backward-compatibility: case QEasingCurve::InCurve: return &easeInCurve; case QEasingCurve::OutCurve: @@ -1097,13 +1062,13 @@ static QEasingCurve::EasingFunction curveToFunc(QEasingCurve::Type curve) case QEasingCurve::CosineCurve: return &easeCosineCurve; default: - return 0; + return nullptr; }; } static QEasingCurveFunction *curveToFunctionObject(QEasingCurve::Type type) { - switch(type) { + switch (type) { case QEasingCurve::InElastic: case QEasingCurve::OutElastic: case QEasingCurve::InOutElastic: @@ -1127,7 +1092,7 @@ static QEasingCurveFunction *curveToFunctionObject(QEasingCurve::Type type) return new QEasingCurveFunction(type, qreal(0.3), qreal(1.0), qreal(1.70158)); } - return 0; + return nullptr; } /*! @@ -1188,32 +1153,37 @@ QEasingCurve::~QEasingCurve() */ /*! - Compare this easing curve with \a other and returns \c true if they are - equal. It will also compare the properties of a curve. + \fn bool QEasingCurve::operator==(const QEasingCurve &lhs, const QEasingCurve &rhs) + + Compares easing curve \a lhs with \a rhs and returns \c true if they are + equal; otherwise returns \c false. + It will also compare the properties of the curves. */ -bool QEasingCurve::operator==(const QEasingCurve &other) const +bool comparesEqual(const QEasingCurve &lhs, const QEasingCurve &rhs) noexcept { - bool res = d_ptr->func == other.d_ptr->func - && d_ptr->type == other.d_ptr->type; + bool res = lhs.d_ptr->func == rhs.d_ptr->func + && lhs.d_ptr->type == rhs.d_ptr->type; if (res) { - if (d_ptr->config && other.d_ptr->config) { - // catch the config content - res = d_ptr->config->operator==(*(other.d_ptr->config)); - - } else if (d_ptr->config || other.d_ptr->config) { - // one one has a config object, which could contain default values - res = qFuzzyCompare(amplitude(), other.amplitude()) && - qFuzzyCompare(period(), other.period()) && - qFuzzyCompare(overshoot(), other.overshoot()); + if (lhs.d_ptr->config && rhs.d_ptr->config) { + // catch the config content + res = lhs.d_ptr->config->operator==(*(rhs.d_ptr->config)); + + } else if (lhs.d_ptr->config || rhs.d_ptr->config) { + // one one has a config object, which could contain default values + res = qFuzzyCompare(lhs.amplitude(), rhs.amplitude()) + && qFuzzyCompare(lhs.period(), rhs.period()) + && qFuzzyCompare(lhs.overshoot(), rhs.overshoot()); } } return res; } /*! - \fn bool QEasingCurve::operator!=(const QEasingCurve &other) const - Compare this easing curve with \a other and returns \c true if they are not equal. - It will also compare the properties of a curve. + \fn bool QEasingCurve::operator!=(const QEasingCurve &lhs, const QEasingCurve &rhs) + + Compares easing curve \a lhs with \a rhs and returns \c true if they are + not equal; otherwise returns \c false. + It will also compare the properties of the curves. \sa operator==() */ @@ -1275,7 +1245,7 @@ void QEasingCurve::setPeriod(qreal period) */ qreal QEasingCurve::overshoot() const { - return d_ptr->config ? d_ptr->config->_o : qreal(1.70158) ; + return d_ptr->config ? d_ptr->config->_o : qreal(1.70158); } /*! @@ -1307,10 +1277,10 @@ void QEasingCurve::addCubicBezierSegment(const QPointF & c1, const QPointF & c2, d_ptr->config->_bezierCurves << c1 << c2 << endPoint; } -QVector<QPointF> static inline tcbToBezier(const TCBPoints &tcbPoints) +QList<QPointF> static inline tcbToBezier(const TCBPoints &tcbPoints) { - const int count = tcbPoints.count(); - QVector<QPointF> bezierPoints; + const int count = tcbPoints.size(); + QList<QPointF> bezierPoints; bezierPoints.reserve(3 * (count - 1)); for (int i = 1; i < count; i++) { @@ -1356,7 +1326,7 @@ QVector<QPointF> static inline tcbToBezier(const TCBPoints &tcbPoints) /*! Adds a segment of a TCB bezier spline to define a custom easing curve. It is only applicable if type() is QEasingCurve::TCBSpline. - The spline has to start explitly at (0.0, 0.0) and has to end at (1.0, 1.0) to + The spline has to start explicitly at (0.0, 0.0) and has to end at (1.0, 1.0) to be a valid easing curve. The tension \a t changes the length of the tangent vector. The continuity \a c changes the sharpness in change between the tangents. @@ -1372,7 +1342,7 @@ void QEasingCurve::addTCBSegment(const QPointF &nextPoint, qreal t, qreal c, qre if (!d_ptr->config) d_ptr->config = curveToFunctionObject(d_ptr->type); - d_ptr->config->_tcbPoints.append(TCBPoint(nextPoint, t, c ,b)); + d_ptr->config->_tcbPoints.append(TCBPoint(nextPoint, t, c, b)); if (nextPoint == QPointF(1.0, 1.0)) { d_ptr->config->_bezierCurves = tcbToBezier(d_ptr->config->_tcbPoints); @@ -1388,9 +1358,9 @@ void QEasingCurve::addTCBSegment(const QPointF &nextPoint, qreal t, qreal c, qre If the easing curve does not have a custom bezier easing curve the list is empty. */ -QVector<QPointF> QEasingCurve::toCubicSpline() const +QList<QPointF> QEasingCurve::toCubicSpline() const { - return d_ptr->config ? d_ptr->config->_bezierCurves : QVector<QPointF>(); + return d_ptr->config ? d_ptr->config->_bezierCurves : QList<QPointF>(); } /*! @@ -1406,8 +1376,8 @@ void QEasingCurvePrivate::setType_helper(QEasingCurve::Type newType) qreal amp = -1.0; qreal period = -1.0; qreal overshoot = -1.0; - QVector<QPointF> bezierCurves; - QVector<TCBPoint> tcbPoints; + QList<QPointF> bezierCurves; + QList<TCBPoint> tcbPoints; if (config) { amp = config->_a; @@ -1417,7 +1387,7 @@ void QEasingCurvePrivate::setType_helper(QEasingCurve::Type newType) tcbPoints = std::move(config->_tcbPoints); delete config; - config = 0; + config = nullptr; } if (isConfigFunction(newType) || (amp != -1.0) || (period != -1.0) || (overshoot != -1.0) || @@ -1431,11 +1401,11 @@ void QEasingCurvePrivate::setType_helper(QEasingCurve::Type newType) config->_o = overshoot; config->_bezierCurves = std::move(bezierCurves); config->_tcbPoints = std::move(tcbPoints); - func = 0; + func = nullptr; } else if (newType != QEasingCurve::Custom) { func = curveToFunc(newType); } - Q_ASSERT((func == 0) == (config != 0)); + Q_ASSERT((func == nullptr) == (config != nullptr)); type = newType; } @@ -1482,7 +1452,7 @@ void QEasingCurve::setCustomType(EasingFunction func) */ QEasingCurve::EasingFunction QEasingCurve::customType() const { - return d_ptr->type == Custom ? d_ptr->func : 0; + return d_ptr->type == Custom ? d_ptr->func : nullptr; } /*! @@ -1507,7 +1477,7 @@ QDebug operator<<(QDebug debug, const QEasingCurve &item) { QDebugStateSaver saver(debug); debug << "type:" << item.d_ptr->type - << "func:" << item.d_ptr->func; + << "func:" << reinterpret_cast<const void *>(item.d_ptr->func); if (item.d_ptr->config) { debug << QString::fromLatin1("period:%1").arg(item.d_ptr->config->_p, 0, 'f', 20) << QString::fromLatin1("amp:%1").arg(item.d_ptr->config->_a, 0, 'f', 20) |