xyanimation.cpp
146 lines
| 4.1 KiB
| text/x-c
|
CppLexer
Jani Honkonen
|
r794 | /**************************************************************************** | ||
** | ||||
** 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$ | ||||
** | ||||
****************************************************************************/ | ||||
Michal Klocek
|
r530 | #include "xyanimation_p.h" | ||
Michal Klocek
|
r1218 | #include "xychart_p.h" | ||
Michal Klocek
|
r996 | #include <QDebug> | ||
Michal Klocek
|
r530 | |||
Q_DECLARE_METATYPE(QVector<QPointF>) | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
Michal Klocek
|
r1218 | XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item), | ||
Michal Klocek
|
r530 | m_item(item), | ||
Michal Klocek
|
r1217 | m_dirty(false), | ||
Michal Klocek
|
r1241 | m_type(NewAnimation) | ||
Michal Klocek
|
r530 | { | ||
Michal Klocek
|
r1241 | setDuration(ChartAnimationDuration); | ||
setEasingCurve(QEasingCurve::OutQuart); | ||||
Michal Klocek
|
r530 | } | ||
XYAnimation::~XYAnimation() | ||||
{ | ||||
} | ||||
Michal Klocek
|
r1217 | void XYAnimation::setAnimationType(Animation type) | ||
{ | ||||
Michal Klocek
|
r1241 | if (state() != QAbstractAnimation::Stopped) stop(); | ||
Michal Klocek
|
r1217 | m_type=type; | ||
} | ||||
Michal Klocek
|
r1241 | void XYAnimation::setValues(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints, int index) | ||
Michal Klocek
|
r530 | { | ||
Michal Klocek
|
r1241 | if (state() != QAbstractAnimation::Stopped) stop(); | ||
Michal Klocek
|
r1220 | |||
Michal Klocek
|
r1241 | if (m_item->isDirty()) { | ||
m_oldPoints = oldPoints; | ||||
m_newPoints = newPoints; | ||||
m_dirty=false; | ||||
Michal Klocek
|
r530 | } | ||
Michal Klocek
|
r622 | else { | ||
Michal Klocek
|
r1241 | if(m_dirty) { | ||
m_newPoints = newPoints; | ||||
Michal Klocek
|
r622 | m_oldPoints = oldPoints; | ||
Michal Klocek
|
r1241 | m_dirty=false; | ||
} | ||||
} | ||||
int x = m_oldPoints.count(); | ||||
int y = m_newPoints.count(); | ||||
if (abs(x - y) == 1) { | ||||
if (y < x){ | ||||
if(!newPoints.isEmpty()) m_newPoints.insert(index,newPoints[index]); | ||||
m_index=index;if(newPoints.isEmpty()) | ||||
m_dirty=true; | ||||
} | ||||
if (y > x){ | ||||
m_oldPoints.insert(index, x > 0 ? m_oldPoints[index-1] : newPoints[index]);//add | ||||
Michal Klocek
|
r622 | } | ||
Michal Klocek
|
r1241 | }else{ | ||
m_newPoints=newPoints; | ||||
m_dirty=false; | ||||
m_oldPoints.resize(m_newPoints.size()); | ||||
Michal Klocek
|
r622 | } | ||
Michal Klocek
|
r1241 | |||
setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); | ||||
setKeyValueAt(1.0, qVariantFromValue(m_newPoints)); | ||||
Michal Klocek
|
r530 | } | ||
Marek Rosa
|
r738 | QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const | ||
Michal Klocek
|
r530 | { | ||
QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start); | ||||
QVector<QPointF> endVector = qVariantValue<QVector<QPointF> >(end); | ||||
QVector<QPointF> result; | ||||
Marek Rosa
|
r738 | switch (m_type) { | ||
Michal Klocek
|
r602 | |||
Michal Klocek
|
r1241 | case ReplacePointAnimation: | ||
case AddPointAnimation: | ||||
case RemovePointAnimation: | ||||
{ | ||||
Marek Rosa
|
r738 | if (startVector.count() != endVector.count()) | ||
Michal Klocek
|
r530 | break; | ||
Marek Rosa
|
r738 | |||
for(int i = 0; i < startVector.count(); i++) { | ||||
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); | ||||
Michal Klocek
|
r530 | } | ||
Marek Rosa
|
r738 | |||
} | ||||
break; | ||||
Michal Klocek
|
r1241 | case NewAnimation: { | ||
Michal Klocek
|
r974 | for(int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++) | ||
Marek Rosa
|
r738 | result << endVector[i]; | ||
} | ||||
break; | ||||
default: | ||||
qWarning() << "Unknown type of animation"; | ||||
break; | ||||
Michal Klocek
|
r530 | } | ||
return qVariantFromValue(result); | ||||
} | ||||
Marek Rosa
|
r738 | void XYAnimation::updateCurrentValue (const QVariant &value) | ||
Michal Klocek
|
r530 | { | ||
if(state()!=QAbstractAnimation::Stopped){ //workaround | ||||
QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value); | ||||
Michal Klocek
|
r1217 | m_item->setGeometryPoints(vector); | ||
m_item->updateGeometry(); | ||||
Michal Klocek
|
r1241 | m_item->setDirty(true); | ||
Michal Klocek
|
r530 | } | ||
} | ||||
Michal Klocek
|
r1241 | void XYAnimation::updateState( QAbstractAnimation::State newState, QAbstractAnimation::State oldState ) | ||
{ | ||||
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); | ||||
} | ||||
} | ||||
} | ||||
#include "moc_chartanimation_p.cpp" | ||||
Michal Klocek
|
r530 | QTCOMMERCIALCHART_END_NAMESPACE | ||