summaryrefslogtreecommitdiffstats
path: root/src/curveeditor/animationcurve.cpp
diff options
context:
space:
mode:
authorKnud Dollereder <knud.dollereder@qt.io>2019-02-07 11:46:17 +0100
committerThomas Hartmann <thomas.hartmann@qt.io>2019-02-07 15:09:35 +0000
commiteb031b8ef94da5601dc3e1e133d9abe2c8dfbd5b (patch)
tree400e4445e8570cd1418af2dbedc4f2bf266ad8dc /src/curveeditor/animationcurve.cpp
parent566a9ce6a4fc840d5d4b1d5793166c023643e2c6 (diff)
Introduce curves
and extend the style-options. Change-Id: Ie5652517dca5766cb6d7eab5d29dde9c030e762b Reviewed-by: Miikka Heikkinen <miikka.heikkinen@qt.io> Reviewed-by: Thomas Hartmann <thomas.hartmann@qt.io>
Diffstat (limited to 'src/curveeditor/animationcurve.cpp')
-rw-r--r--src/curveeditor/animationcurve.cpp143
1 files changed, 143 insertions, 0 deletions
diff --git a/src/curveeditor/animationcurve.cpp b/src/curveeditor/animationcurve.cpp
new file mode 100644
index 0000000..4818a81
--- /dev/null
+++ b/src/curveeditor/animationcurve.cpp
@@ -0,0 +1,143 @@
+/****************************************************************************
+**
+** Copyright (C) 2019 The Qt Company Ltd.
+** Contact: https://www.qt.io/licensing/
+**
+** This file is part of the Qt Design Tooling
+**
+** 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 General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU
+** General Public License version 3 as published by the Free Software
+** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
+** 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-3.0.html.
+**
+****************************************************************************/
+
+#include "animationcurve.h"
+
+#include <assert.h>
+#include <cmath>
+
+namespace DesignTools {
+
+Keyframe::Keyframe()
+ : m_position()
+ , m_leftHandle()
+ , m_rightHandle()
+{}
+
+Keyframe::Keyframe(const QPointF &position)
+ : m_position(position)
+ , m_leftHandle()
+ , m_rightHandle()
+{}
+
+Keyframe::Keyframe(const QPointF &position, const QPointF &leftHandle, const QPointF &rightHandle)
+ : m_position(position)
+ , m_leftHandle(leftHandle)
+ , m_rightHandle(rightHandle)
+{}
+
+bool Keyframe::hasLeftHandle() const
+{
+ return !m_leftHandle.isNull();
+}
+
+bool Keyframe::hasRightHandle() const
+{
+ return !m_rightHandle.isNull();
+}
+
+QPointF Keyframe::position() const
+{
+ return m_position;
+}
+
+QPointF Keyframe::leftHandle() const
+{
+ return m_leftHandle;
+}
+
+QPointF Keyframe::rightHandle() const
+{
+ return m_rightHandle;
+}
+
+void Keyframe::setPosition(const QPointF &pos)
+{
+ m_position = pos;
+}
+
+void Keyframe::setLeftHandle(const QPointF &pos)
+{
+ m_leftHandle = pos;
+}
+
+void Keyframe::setRightHandle(const QPointF &pos)
+{
+ m_rightHandle = pos;
+}
+
+
+CurveSegment::CurveSegment()
+ : m_left()
+ , m_right()
+{}
+
+CurveSegment::CurveSegment(const Keyframe &left, const Keyframe &right)
+ : m_left(left)
+ , m_right(right)
+{}
+
+QPointF CurveSegment::evaluate(double t) const
+{
+ assert(t >= 0. && t <= 1.);
+
+ const double it = 1.0 - t;
+
+ auto binomialPolynomial = [t, it](double p0, double p1, double p2, double p3) {
+ return p0 * std::pow(it, 3.0) + p1 * 3.0 * std::pow(it, 2.0) * t
+ + p2 * 3.0 * it * std::pow(t, 2.0) + p3 * std::pow(t, 3.0);
+ };
+
+ const double x = binomialPolynomial(
+ m_left.position().x(), m_left.rightHandle().x(),
+ m_right.leftHandle().x(), m_right.position().x());
+
+ const double y = binomialPolynomial(
+ m_left.position().y(), m_left.rightHandle().y(),
+ m_right.leftHandle().y(), m_right.position().y());
+
+ return QPointF(x, y);
+}
+
+
+AnimationCurve::AnimationCurve()
+ : m_frames()
+{}
+
+AnimationCurve::AnimationCurve(const std::vector<Keyframe> &frames)
+ : m_frames(frames)
+{}
+
+bool AnimationCurve::isValid() const
+{
+ return !m_frames.empty();
+}
+
+std::vector<Keyframe> AnimationCurve::keyframes() const
+{
+ return m_frames;
+}
+
+} // End namespace DesignTools.