xyanimation.cpp
164 lines
| 5.1 KiB
| text/x-c
|
CppLexer
|
r2854 | /**************************************************************************** | ||
|
r794 | ** | ||
|
r2854 | ** Copyright (C) 2016 The Qt Company Ltd. | ||
** Contact: https://www.qt.io/licensing/ | ||||
|
r794 | ** | ||
|
r2854 | ** This file is part of the Qt Charts module of the Qt Toolkit. | ||
|
r794 | ** | ||
|
r2854 | ** $QT_BEGIN_LICENSE:GPL$ | ||
|
r2845 | ** 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 | ||||
|
r2854 | ** 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 or (at your option) 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.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-3.0.html. | ||||
|
r794 | ** | ||
|
r2845 | ** $QT_END_LICENSE$ | ||
** | ||||
|
r2854 | ****************************************************************************/ | ||
|
r794 | |||
|
r2714 | #include <private/xyanimation_p.h> | ||
#include <private/xychart_p.h> | ||||
#include <QtCore/QDebug> | ||||
|
r530 | |||
Q_DECLARE_METATYPE(QVector<QPointF>) | ||||
|
r2712 | QT_CHARTS_BEGIN_NAMESPACE | ||
|
r530 | |||
|
r2804 | XYAnimation::XYAnimation(XYChart *item, int duration, QEasingCurve &curve) | ||
|
r2097 | : ChartAnimation(item), | ||
m_type(NewAnimation), | ||||
m_dirty(false), | ||||
m_index(-1), | ||||
m_item(item) | ||||
|
r530 | { | ||
|
r2804 | setDuration(duration); | ||
setEasingCurve(curve); | ||||
|
r530 | } | ||
XYAnimation::~XYAnimation() | ||||
{ | ||||
} | ||||
|
r1271 | void XYAnimation::setup(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints, int index) | ||
|
r1217 | { | ||
|
r2097 | m_type = NewAnimation; | ||
if (state() != QAbstractAnimation::Stopped) { | ||||
stop(); | ||||
m_dirty = false; | ||||
} | ||||
if (!m_dirty) { | ||||
m_dirty = true; | ||||
m_oldPoints = oldPoints; | ||||
} | ||||
m_newPoints = newPoints; | ||||
int x = m_oldPoints.count(); | ||||
int y = m_newPoints.count(); | ||||
|
r2489 | int diff = x - y; | ||
int requestedDiff = oldPoints.count() - y; | ||||
|
r2097 | |||
|
r2489 | // m_oldPoints can be whatever between 0 and actual points count if new animation setup | ||
// interrupts a previous animation, so only do remove and add animations if both | ||||
// stored diff and requested diff indicate add or remove. Also ensure that index is not | ||||
// invalid. | ||||
if (diff == 1 && requestedDiff == 1 && index >= 0 && y > 0 && index <= y) { | ||||
|
r2097 | //remove point | ||
m_newPoints.insert(index, index > 0 ? newPoints[index - 1] : newPoints[index]); | ||||
m_index = index; | ||||
m_type = RemovePointAnimation; | ||||
} | ||||
|
r2489 | if (diff == -1 && requestedDiff == -1 && index >= 0 && index <= x) { | ||
|
r2097 | //add point | ||
m_oldPoints.insert(index, index > 0 ? newPoints[index - 1] : newPoints[index]); | ||||
m_index = index; | ||||
m_type = AddPointAnimation; | ||||
} | ||||
x = m_oldPoints.count(); | ||||
y = m_newPoints.count(); | ||||
if (x != y) | ||||
m_type = NewAnimation; | ||||
else if (m_type == NewAnimation) | ||||
m_type = ReplacePointAnimation; | ||||
|
r1241 | |||
setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); | ||||
setKeyValueAt(1.0, qVariantFromValue(m_newPoints)); | ||||
|
r530 | } | ||
|
r2097 | QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const | ||
|
r530 | { | ||
|
r2241 | QVector<QPointF> startVector = qvariant_cast<QVector<QPointF> >(start); | ||
QVector<QPointF> endVector = qvariant_cast<QVector<QPointF> >(end); | ||||
|
r530 | QVector<QPointF> result; | ||
|
r738 | switch (m_type) { | ||
|
r602 | |||
|
r1241 | case ReplacePointAnimation: | ||
case AddPointAnimation: | ||||
|
r2097 | case RemovePointAnimation: { | ||
if (startVector.count() != endVector.count()) | ||||
|
r530 | break; | ||
|
r738 | |||
|
r2097 | for (int i = 0; i < startVector.count(); i++) { | ||
|
r738 | qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress); | ||
qreal y = startVector[i].y() + ((endVector[i].y() - startVector[i].y()) * progress); | ||||
result << QPointF(x, y); | ||||
|
r530 | } | ||
|
r738 | |||
} | ||||
|
r2097 | break; | ||
|
r1241 | case NewAnimation: { | ||
|
r2097 | for (int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++) | ||
|
r738 | result << endVector[i]; | ||
} | ||||
|
r2097 | break; | ||
|
r738 | default: | ||
qWarning() << "Unknown type of animation"; | ||||
break; | ||||
|
r530 | } | ||
return qVariantFromValue(result); | ||||
} | ||||
|
r2097 | void XYAnimation::updateCurrentValue(const QVariant &value) | ||
|
r530 | { | ||
|
r2097 | if (state() != QAbstractAnimation::Stopped) { //workaround | ||
|
r1271 | |||
|
r2241 | QVector<QPointF> vector = qvariant_cast<QVector<QPointF> >(value); | ||
|
r1217 | m_item->setGeometryPoints(vector); | ||
m_item->updateGeometry(); | ||||
|
r1241 | m_item->setDirty(true); | ||
|
r2097 | m_dirty = false; | ||
|
r1271 | |||
|
r530 | } | ||
} | ||||
|
r2097 | void XYAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) | ||
|
r1241 | { | ||
|
r2097 | if (oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) { | ||
if (m_item->isDirty() && m_type == RemovePointAnimation) { | ||||
if (!m_newPoints.isEmpty()) | ||||
m_newPoints.remove(m_index); | ||||
m_item->setGeometryPoints(m_newPoints); | ||||
} | ||||
} | ||||
|
r1241 | } | ||
#include "moc_chartanimation_p.cpp" | ||||
|
r2712 | QT_CHARTS_END_NAMESPACE | ||