From b11722f8058842260f9b7e1e72418768d24ff91b 2012-05-29 07:10:45 From: Michal Klocek Date: 2012-05-29 07:10:45 Subject: [PATCH] adds QLineAnimation fixes --- diff --git a/src/animations/splineanimation.cpp b/src/animations/splineanimation.cpp index 51fce1a..8229744 100644 --- a/src/animations/splineanimation.cpp +++ b/src/animations/splineanimation.cpp @@ -37,7 +37,7 @@ SplineAnimation::~SplineAnimation() { } -void SplineAnimation::setValues(QVector &oldPoints, QVector &newPoints, QVector &oldControlPoints, QVector &newControlPoints, int index) +void SplineAnimation::setup(QVector &oldPoints, QVector &newPoints, QVector &oldControlPoints, QVector &newControlPoints, int index) { int x = oldPoints.count(); int y = newPoints.count(); diff --git a/src/animations/splineanimation_p.h b/src/animations/splineanimation_p.h index 6f1d8da..6ca2f42 100644 --- a/src/animations/splineanimation_p.h +++ b/src/animations/splineanimation_p.h @@ -34,7 +34,7 @@ class SplineAnimation : public XYAnimation public: SplineAnimation(SplineChartItem *item); ~SplineAnimation(); - void setValues(QVector &oldPoints, QVector &newPoints, QVector &oldContorlPoints, QVector &newControlPoints, int index); + void setup(QVector &oldPoints, QVector &newPoints, QVector &oldContorlPoints, QVector &newControlPoints, int index = -1); protected: QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress) const; diff --git a/src/animations/xyanimation.cpp b/src/animations/xyanimation.cpp index 2b9ada5..e93e6fb 100644 --- a/src/animations/xyanimation.cpp +++ b/src/animations/xyanimation.cpp @@ -39,50 +39,53 @@ XYAnimation::~XYAnimation() { } -void XYAnimation::setAnimationType(Animation type) +void XYAnimation::setup(const QVector &oldPoints, const QVector &newPoints, int index) { - if (state() != QAbstractAnimation::Stopped) stop(); - m_type=type; -} + m_type = NewAnimation; -void XYAnimation::setValues(const QVector &oldPoints, const QVector &newPoints, int index) -{ - if (state() != QAbstractAnimation::Stopped) stop(); + if (state() != QAbstractAnimation::Stopped){ + stop(); + m_dirty=false; + } - if (m_item->isDirty()) { - m_oldPoints = oldPoints; - m_newPoints = newPoints; - m_dirty=false; - } - else { - if(m_dirty) { - m_newPoints = newPoints; - m_oldPoints = oldPoints; - m_dirty=false; - } - } + if(!m_dirty){ + m_dirty = true; + m_oldPoints = oldPoints; + } - int x = m_oldPoints.count(); - int y = m_newPoints.count(); + m_newPoints = newPoints; - 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 - } - }else{ - m_newPoints=newPoints; - m_dirty=false; - m_oldPoints.resize(m_newPoints.size()); - } + int x = m_oldPoints.count(); + int y = m_newPoints.count(); + + if(x - y == 1 && index >= 0 && !newPoints.isEmpty()){ + //remove point + m_newPoints.insert(index, index >= 1 ? m_newPoints[index-1] : newPoints[index]); + m_index=index; + m_type = RemovePointAnimation; + } + + if(x - y == -1 && index >= 0){ + //add point + m_oldPoints.insert(index, x > 0 && index > 1 ? m_oldPoints[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; + } setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); setKeyValueAt(1.0, qVariantFromValue(m_newPoints)); - } QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const @@ -97,8 +100,9 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q case AddPointAnimation: case RemovePointAnimation: { - if (startVector.count() != endVector.count()) + if (startVector.count() != endVector.count()){ break; + } for(int i = 0; i < startVector.count(); i++) { qreal x = startVector[i].x() + ((endVector[i].x() - startVector[i].x()) * progress); @@ -124,10 +128,13 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q void XYAnimation::updateCurrentValue (const QVariant &value) { if(state()!=QAbstractAnimation::Stopped){ //workaround + QVector vector = qVariantValue >(value); m_item->setGeometryPoints(vector); m_item->updateGeometry(); m_item->setDirty(true); + m_dirty=false; + } } diff --git a/src/animations/xyanimation_p.h b/src/animations/xyanimation_p.h index 50b3478..5c88740 100644 --- a/src/animations/xyanimation_p.h +++ b/src/animations/xyanimation_p.h @@ -34,8 +34,7 @@ public: enum Animation { AddPointAnimation, RemovePointAnimation, ReplacePointAnimation, NewAnimation }; XYAnimation(XYChart *item); ~XYAnimation(); - void setValues(const QVector &oldPoints, const QVector &newPoints,int index); - void setAnimationType(Animation type); + void setup(const QVector &oldPoints, const QVector &newPoints,int index = -1); Animation animationType() const { return m_type; }; protected: diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index 019fe46..ca6183c 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -86,7 +86,9 @@ void SplineChartItem::updateChart(QVector &oldPoints, QVector } if (m_animation) { - m_animation->setValues(oldPoints,newPoints,m_controlPoints,controlPoints,index); + m_animation->setup(oldPoints,newPoints,m_controlPoints,controlPoints,index); + setGeometryPoints(newPoints); + setDirty(false); presenter()->startAnimation(m_animation); } else { @@ -107,7 +109,9 @@ void SplineChartItem::updateGeometry() const QVector &controlPoints = controlGeometryPoints(); if ((points.size()<2) || (controlPoints.size()<2)) { + prepareGeometryChange(); m_path = QPainterPath(); + m_rect = QRect(); return; } diff --git a/src/xychart/xychart.cpp b/src/xychart/xychart.cpp index 1cd7a52..5a5d61f 100644 --- a/src/xychart/xychart.cpp +++ b/src/xychart/xychart.cpp @@ -117,8 +117,9 @@ QPointF XYChart::calculateDomainPoint(const QPointF &point) const void XYChart::updateChart(QVector &oldPoints, QVector &newPoints,int index) { + if (m_animation) { - m_animation->setValues(oldPoints, newPoints, index); + m_animation->setup(oldPoints, newPoints, index); setGeometryPoints(newPoints); setDirty(false); presenter()->startAnimation(m_animation); @@ -138,10 +139,6 @@ void XYChart::handlePointAdded(int index) QVector points; - if(m_animation) { - m_animation->setAnimationType(XYAnimation::AddPointAnimation); - } - if(m_dirty) { points = calculateGeometryPoints(); } else { @@ -160,10 +157,6 @@ void XYChart::handlePointRemoved(int index) QVector points; - if(m_animation) { - m_animation->setAnimationType(XYAnimation::RemovePointAnimation); - } - if(m_dirty) { points = calculateGeometryPoints(); } else { @@ -181,10 +174,6 @@ void XYChart::handlePointReplaced(int index) QVector points; - if(m_animation) { - m_animation->setAnimationType(XYAnimation::ReplacePointAnimation); - } - if(m_dirty) { points = calculateGeometryPoints(); } else { @@ -206,10 +195,6 @@ void XYChart::handleDomainChanged(qreal minX, qreal maxX, qreal minY, qreal maxY QVector points = calculateGeometryPoints(); - if(m_animation) { - m_animation->setAnimationType(XYAnimation::ReplacePointAnimation); - } - updateChart(m_points,points); } @@ -224,10 +209,6 @@ void XYChart::handleGeometryChanged(const QRectF &rect) QVector points = calculateGeometryPoints(); - if(m_animation) { - m_animation->setAnimationType(XYAnimation::NewAnimation); - } - updateChart(m_points,points); } diff --git a/src/xychart/xychart_p.h b/src/xychart/xychart_p.h index 14717db..d61bcbf 100644 --- a/src/xychart/xychart_p.h +++ b/src/xychart/xychart_p.h @@ -65,7 +65,7 @@ Q_SIGNALS: void clicked(const QPointF& point); protected: - virtual void updateChart(QVector &oldPoints,QVector &newPoints,int index = 0); + virtual void updateChart(QVector &oldPoints,QVector &newPoints,int index = -1); QPointF calculateGeometryPoint(const QPointF &point) const; QPointF calculateGeometryPoint(int index) const; QPointF calculateDomainPoint(const QPointF &point) const;