splineanimation.cpp
203 lines
| 7.2 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
Michal Klocek
|
r1288 | ** | ||
** Copyright (C) 2012 Digia Plc | ||||
** All rights reserved. | ||||
** For any questions to Digia, please use contact form at http://qt.digia.com | ||||
** | ||||
** This file is part of the Qt Commercial Charts Add-on. | ||||
** | ||||
** $QT_BEGIN_LICENSE$ | ||||
** Licensees holding valid Qt Commercial licenses may use this file in | ||||
** accordance with the Qt Commercial License Agreement provided with the | ||||
** Software or, alternatively, in accordance with the terms contained in | ||||
** a written agreement between you and Digia. | ||||
** | ||||
** If you have questions regarding the use of this file, please use | ||||
** contact form at http://qt.digia.com | ||||
** $QT_END_LICENSE$ | ||||
** | ||||
****************************************************************************/ | ||||
Jani Honkonen
|
r794 | |||
Michal Klocek
|
r622 | #include "splineanimation_p.h" | ||
#include "splinechartitem_p.h" | ||||
Michal Klocek
|
r996 | #include <QDebug> | ||
Michal Klocek
|
r622 | |||
Q_DECLARE_METATYPE(QVector<QPointF>) | ||||
Q_DECLARE_METATYPE(SplineVector) | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Jani Honkonen
|
r2097 | SplineAnimation::SplineAnimation(SplineChartItem* item): XYAnimation(item), | ||
m_item(item), | ||||
m_valid(false) | ||||
Michal Klocek
|
r622 | { | ||
} | ||||
SplineAnimation::~SplineAnimation() | ||||
{ | ||||
} | ||||
Michal Klocek
|
r1271 | void SplineAnimation::setup(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index) | ||
Michal Klocek
|
r622 | { | ||
Tero Ahola
|
r1782 | if (newPoints.count() * 2 - 2 != newControlPoints.count() || newControlPoints.count() < 2) { | ||
Jani Honkonen
|
r2097 | m_valid = false; | ||
m_dirty = false; | ||||
Tero Ahola
|
r1782 | m_item->setGeometryPoints(newPoints); | ||
m_item->setControlGeometryPoints(newControlPoints); | ||||
m_item->setDirty(false); | ||||
m_item->updateGeometry(); | ||||
return; | ||||
Michal Klocek
|
r1770 | } | ||
Michal Klocek
|
r622 | |||
Michal Klocek
|
r1288 | m_type = NewAnimation; | ||
if (state() != QAbstractAnimation::Stopped) { | ||||
stop(); | ||||
Jani Honkonen
|
r2097 | m_dirty = false; | ||
Michal Klocek
|
r622 | } | ||
Michal Klocek
|
r1288 | |||
Jani Honkonen
|
r2097 | if (!m_dirty) { | ||
Michal Klocek
|
r1288 | m_dirty = true; | ||
m_oldSpline.first = oldPoints; | ||||
m_oldSpline.second = oldControlPoints; | ||||
} | ||||
Jani Honkonen
|
r2097 | m_newSpline.first = newPoints; | ||
m_newSpline.second = newControlPoints; | ||||
Michal Klocek
|
r1288 | |||
Michal Klocek
|
r1770 | |||
Michal Klocek
|
r1288 | int x = m_oldSpline.first.count(); | ||
int y = m_newSpline.first.count(); | ||||
Jani Honkonen
|
r2097 | if (x - y == 1 && index >= 0 && y > 0) { | ||
Michal Klocek
|
r1288 | //remove point | ||
Jani Honkonen
|
r2097 | if (index > 0) { | ||
m_newSpline.first.insert(index, newPoints[index - 1]); | ||||
m_newSpline.second.insert((index - 1) * 2, newPoints[index - 1]); | ||||
m_newSpline.second.insert((index - 1) * 2 + 1, newPoints[index - 1]); | ||||
} else { | ||||
Michal Klocek
|
r1288 | m_newSpline.first.insert(index, newPoints[index]); | ||
Jani Honkonen
|
r2097 | m_newSpline.second.insert(index * 2, newPoints[index]); | ||
Michal Klocek
|
r1288 | m_newSpline.second.insert(index * 2 + 1, newPoints[index]); | ||
Michal Klocek
|
r622 | } | ||
Jani Honkonen
|
r2097 | m_index = index; | ||
Michal Klocek
|
r1288 | m_type = RemovePointAnimation; | ||
} | ||||
Jani Honkonen
|
r2097 | if (x - y == -1 && index >= 0) { | ||
Michal Klocek
|
r1288 | //add point | ||
Jani Honkonen
|
r2097 | if (index > 0) { | ||
m_oldSpline.first.insert(index, newPoints[index - 1]); | ||||
m_oldSpline.second.insert((index - 1) * 2, newPoints[index - 1]); | ||||
m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index - 1]); | ||||
} else { | ||||
Michal Klocek
|
r1288 | m_oldSpline.first.insert(index, newPoints[index]); | ||
m_oldSpline.second.insert((index - 1) * 2, newPoints[index]); | ||||
m_oldSpline.second.insert((index - 1) * 2 + 1, newPoints[index]); | ||||
Michal Klocek
|
r622 | } | ||
Jani Honkonen
|
r2097 | m_index = index; | ||
Michal Klocek
|
r1288 | m_type = AddPointAnimation; | ||
} | ||||
x = m_oldSpline.first.count(); | ||||
y = m_newSpline.first.count(); | ||||
Jani Honkonen
|
r2097 | if (x != y) { | ||
Michal Klocek
|
r1288 | m_type = NewAnimation; | ||
Jani Honkonen
|
r2097 | } else if (m_type == NewAnimation) { | ||
Michal Klocek
|
r1288 | m_type = ReplacePointAnimation; | ||
Michal Klocek
|
r622 | } | ||
Michal Klocek
|
r1288 | |||
setKeyValueAt(0.0, qVariantFromValue(m_oldSpline)); | ||||
setKeyValueAt(1.0, qVariantFromValue(m_newSpline)); | ||||
Michal Klocek
|
r1770 | |||
Jani Honkonen
|
r2097 | m_valid = true; | ||
Michal Klocek
|
r1288 | |||
Michal Klocek
|
r622 | } | ||
Jani Honkonen
|
r2097 | QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const | ||
Michal Klocek
|
r622 | { | ||
Michal Klocek
|
r1288 | SplineVector startPair = qVariantValue< SplineVector >(start); | ||
SplineVector endPair = qVariantValue< SplineVector >(end); | ||||
Michal Klocek
|
r622 | SplineVector result; | ||
Michal Klocek
|
r1217 | switch (animationType()) { | ||
Jani Honkonen
|
r2097 | case RemovePointAnimation: | ||
case AddPointAnimation: | ||||
case ReplacePointAnimation: { | ||||
Michal Klocek
|
r1288 | if (startPair.first.count() != endPair.first.count()) | ||
Jani Honkonen
|
r2097 | break; | ||
Michal Klocek
|
r1288 | Q_ASSERT(startPair.first.count() * 2 - 2 == startPair.second.count()); | ||
Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count()); | ||||
Jani Honkonen
|
r2097 | for (int i = 0; i < endPair.first.count(); i++) { | ||
Michal Klocek
|
r1288 | qreal x = startPair.first[i].x() + ((endPair.first[i].x() - startPair.first[i].x()) * progress); | ||
qreal y = startPair.first[i].y() + ((endPair.first[i].y() - startPair.first[i].y()) * progress); | ||||
Jani Honkonen
|
r2097 | result.first << QPointF(x, y); | ||
Michal Klocek
|
r1288 | if (i + 1 >= endPair.first.count()) | ||
Jani Honkonen
|
r2097 | continue; | ||
Michal Klocek
|
r1288 | x = startPair.second[i * 2].x() + ((endPair.second[i * 2].x() - startPair.second[i * 2].x()) * progress); | ||
y = startPair.second[i * 2].y() + ((endPair.second[i * 2].y() - startPair.second[i * 2].y()) * progress); | ||||
Jani Honkonen
|
r2097 | result.second << QPointF(x, y); | ||
Michal Klocek
|
r1288 | x = startPair.second[i * 2 + 1].x() + ((endPair.second[i * 2 + 1].x() - startPair.second[i * 2 + 1].x()) * progress); | ||
y = startPair.second[i * 2 + 1].y() + ((endPair.second[i * 2 + 1].y() - startPair.second[i * 2 + 1].y()) * progress); | ||||
Jani Honkonen
|
r2097 | result.second << QPointF(x, y); | ||
Michal Klocek
|
r1288 | } | ||
} | ||||
Marek Rosa
|
r738 | break; | ||
Jani Honkonen
|
r2097 | case NewAnimation: { | ||
Michal Klocek
|
r1288 | Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count()); | ||
Jani Honkonen
|
r2097 | int count = endPair.first.count() * qBound(qreal(0), progress, qreal(1)); | ||
for (int i = 0; i < count; i++) { | ||||
Michal Klocek
|
r1288 | result.first << endPair.first[i]; | ||
Jani Honkonen
|
r2097 | if (i + 1 == count) | ||
break; | ||||
Michal Klocek
|
r1288 | result.second << endPair.second[2 * i]; | ||
result.second << endPair.second[2 * i + 1]; | ||||
} | ||||
Marek Rosa
|
r738 | } | ||
break; | ||||
Jani Honkonen
|
r2097 | default: | ||
Michal Klocek
|
r1217 | qWarning() << "Unknown type of animation"; | ||
Marek Rosa
|
r738 | break; | ||
Michal Klocek
|
r622 | } | ||
return qVariantFromValue(result); | ||||
} | ||||
Jani Honkonen
|
r2097 | void SplineAnimation::updateCurrentValue(const QVariant &value) | ||
Michal Klocek
|
r622 | { | ||
Michal Klocek
|
r1770 | if (state() != QAbstractAnimation::Stopped && m_valid) { //workaround | ||
Michal Klocek
|
r622 | QPair<QVector<QPointF >, QVector<QPointF > > pair = qVariantValue< QPair< QVector<QPointF>, QVector<QPointF> > >(value); | ||
Michal Klocek
|
r1217 | m_item->setGeometryPoints(pair.first); | ||
m_item->setControlGeometryPoints(pair.second); | ||||
m_item->updateGeometry(); | ||||
Michal Klocek
|
r1288 | m_item->setDirty(true); | ||
m_dirty = false; | ||||
Michal Klocek
|
r622 | } | ||
} | ||||
Michal Klocek
|
r1288 | void SplineAnimation::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) | ||
{ | ||||
Michal Klocek
|
r1770 | XYAnimation::updateState(newState, oldState); | ||
Jani Honkonen
|
r2097 | if (oldState == QAbstractAnimation::Running && newState == QAbstractAnimation::Stopped) { | ||
if (m_item->isDirty() && m_type == RemovePointAnimation) { | ||||
if (!m_newSpline.first.isEmpty()) { | ||||
Michal Klocek
|
r1288 | m_newSpline.first.remove(m_index); | ||
Jani Honkonen
|
r2097 | m_newSpline.second.remove((m_index - 1) * 2); | ||
m_newSpline.second.remove((m_index - 1) * 2); | ||||
Michal Klocek
|
r1288 | } | ||
m_item->setGeometryPoints(m_newSpline.first); | ||||
m_item->setControlGeometryPoints(m_newSpline.second); | ||||
} | ||||
} | ||||
Michal Klocek
|
r1770 | |||
Jani Honkonen
|
r2097 | if (oldState == QAbstractAnimation::Stopped && newState == QAbstractAnimation::Running) { | ||
if (!m_valid) | ||||
stop(); | ||||
Michal Klocek
|
r1770 | } | ||
Michal Klocek
|
r1288 | } | ||
Michal Klocek
|
r622 | |||
QTCOMMERCIALCHART_END_NAMESPACE | ||||