linechartanimationitem.cpp
103 lines
| 2.9 KiB
| text/x-c
|
CppLexer
|
r131 | #include "linechartanimationitem_p.h" | ||
|
r145 | #include "linechartitem_p.h" | ||
|
r131 | #include <QPropertyAnimation> | ||
|
r391 | #include <QTimer> | ||
Q_DECLARE_METATYPE(QVector<QPointF>) | ||||
|
r131 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
const static int duration = 500; | ||||
|
r439 | LineChartAnimationItem::LineChartAnimationItem(QLineSeries* series,QGraphicsItem *parent): | ||
LineChartItem(series,parent), | ||||
|
r391 | m_animation(new LineChartAnimatator(this,this)), | ||
m_dirty(false) | ||||
|
r131 | { | ||
} | ||||
LineChartAnimationItem::~LineChartAnimationItem() | ||||
{ | ||||
} | ||||
|
r391 | void LineChartAnimationItem::updateItem(QVector<QPointF>& oldPoints,QVector<QPointF>& newPoints) | ||
|
r131 | { | ||
|
r391 | if(newPoints.count()==0) return; | ||
oldPoints.resize(newPoints.size()); | ||||
|
r131 | |||
|
r391 | if(m_animation->state()!=QAbstractAnimation::Stopped){ | ||
m_animation->stop(); | ||||
} | ||||
m_animation->setDuration(duration); | ||||
m_animation->setEasingCurve(QEasingCurve::InOutBack); | ||||
m_animation->setKeyValueAt(0.0, qVariantFromValue(oldPoints)); | ||||
m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); | ||||
QTimer::singleShot(0,m_animation,SLOT(start())); | ||||
oldPoints = newPoints; | ||||
m_points = newPoints; | ||||
m_dirty=false; | ||||
|
r131 | } | ||
|
r391 | void LineChartAnimationItem::updateItem(QVector<QPointF>& oldPoints,int index,QPointF& newPoint) | ||
|
r131 | { | ||
|
r391 | if(m_animation->state()!=QAbstractAnimation::Stopped){ | ||
m_animation->stop(); | ||||
m_dirty=true; | ||||
|
r131 | } | ||
|
r391 | |||
if(m_dirty){ | ||||
m_points=oldPoints; | ||||
m_dirty=false; | ||||
} | ||||
oldPoints.replace(index,newPoint); | ||||
m_animation->setDuration(duration); | ||||
m_animation->setEasingCurve(QEasingCurve::InOutBack); | ||||
m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points)); | ||||
m_animation->setKeyValueAt(1.0, qVariantFromValue(oldPoints)); | ||||
QTimer::singleShot(0,this,SLOT(startAnimation())); | ||||
|
r131 | } | ||
|
r391 | void LineChartAnimationItem::startAnimation() | ||
|
r131 | { | ||
|
r391 | m_dirty=true; | ||
m_animation->start(); | ||||
|
r131 | } | ||
|
r391 | LineChartAnimatator::LineChartAnimatator(LineChartAnimationItem *item , QObject *parent):QVariantAnimation(parent), | ||
m_item(item) | ||||
|
r389 | { | ||
|
r391 | } | ||
|
r389 | |||
LineChartAnimatator::~LineChartAnimatator() | ||||
{ | ||||
} | ||||
QVariant LineChartAnimatator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const | ||||
{ | ||||
|
r391 | QVector<QPointF> startVector = qVariantValue<QVector<QPointF> >(start); | ||
QVector<QPointF> endVecotr = qVariantValue<QVector<QPointF> >(end); | ||||
QVector<QPointF> result; | ||||
|
r389 | Q_ASSERT(startVector.count() == endVecotr.count()); | ||
for(int i =0 ;i< startVector.count();i++){ | ||||
|
r391 | qreal x = startVector[i].x() + ((endVecotr[i].x()- startVector[i].x()) * progress);//qBound(0.0, progress, 1.0)); | ||
qreal y = startVector[i].y() + ((endVecotr[i].y()- startVector[i].y()) * progress);//qBound(0.0, progress, 1.0)); | ||||
result << QPointF(x,y); | ||||
|
r389 | } | ||
return qVariantFromValue(result); | ||||
} | ||||
void LineChartAnimatator::updateCurrentValue (const QVariant & value ) | ||||
{ | ||||
|
r391 | QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value); | ||
m_item->applyGeometry(vector); | ||||
|
r389 | } | ||
|
r131 | #include "moc_linechartanimationitem_p.cpp" | ||
QTCOMMERCIALCHART_END_NAMESPACE | ||||