diff --git a/src/axisanimationitem.cpp b/src/axisanimationitem.cpp index bfb066c..d2138ad 100644 --- a/src/axisanimationitem.cpp +++ b/src/axisanimationitem.cpp @@ -1,5 +1,5 @@ #include "axisanimationitem_p.h" -#include +#include Q_DECLARE_METATYPE(QVector) @@ -8,7 +8,8 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE const static int duration = 500; AxisAnimationItem::AxisAnimationItem(AxisType type,QGraphicsItem* parent) : -AxisItem(type,parent) +AxisItem(type,parent), +m_animation(new AxisAnimator(this,this)) { } @@ -16,19 +17,21 @@ AxisAnimationItem::~AxisAnimationItem() { } -void AxisAnimationItem::updateItems(QVector& vector1) +void AxisAnimationItem::updateItems(QVector& oldLayout,QVector& newLayout) { - QVector vector0 = vector1; - calculateLayout(vector1); - if(vector1.count()==0) return; - vector0.resize(vector1.size()); - - AxisAnimator *animation = new AxisAnimator(this,this); - animation->setDuration(duration); - animation->setEasingCurve(QEasingCurve::InOutBack); - animation->setKeyValueAt(0.0, qVariantFromValue(vector0)); - animation->setKeyValueAt(1.0, qVariantFromValue(vector1)); - animation->start(QAbstractAnimation::DeleteWhenStopped); + if(newLayout.count()==0) return; + oldLayout.resize(newLayout.size()); + + if(m_animation->state()!=QAbstractAnimation::Stopped){ + m_animation->stop(); + } + + m_animation->setDuration(duration); + m_animation->setEasingCurve(QEasingCurve::InOutBack); + m_animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); + m_animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); + QTimer::singleShot(0,m_animation,SLOT(start())); + oldLayout = newLayout; } void AxisAnimationItem::setLabelsAngle(int angle) diff --git a/src/axisanimationitem_p.h b/src/axisanimationitem_p.h index f4b70b1..6b89cb5 100644 --- a/src/axisanimationitem_p.h +++ b/src/axisanimationitem_p.h @@ -8,7 +8,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -class QChartAxis; +class AxisAnimator; class AxisAnimationItem : public AxisItem { @@ -21,8 +21,9 @@ public: void setLabelsAngle(int angle); protected: - void updateItems(QVector& vector); - + virtual void updateItems(QVector& oldLayout,QVector& newLayout); +private: + AxisAnimator *m_animation; }; class AxisAnimator: public QVariantAnimation diff --git a/src/axisitem.cpp b/src/axisitem.cpp index e086ff8..6843850 100644 --- a/src/axisitem.cpp +++ b/src/axisitem.cpp @@ -67,11 +67,11 @@ void AxisItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Q_UNUSED(widget); } -void AxisItem::updateItems(QVector& vector) +void AxisItem::updateItems(QVector& oldLayout,QVector& newLayout) { - calculateLayout(vector); - if(vector.count()==0) return; - applyLayout(vector); + if(newLayout.count()==0) return; + applyLayout(newLayout); + oldLayout=newLayout; } void AxisItem::handleAxisUpdate(QChartAxis* axis) @@ -127,15 +127,19 @@ void AxisItem::handleLabelsChanged(QChartAxis* axis,const QStringList& labels) createItems(-diff); } m_thicksList=labels; - m_layoutVector.resize(m_thicksList.size()); - updateItems(m_layoutVector); + QVector vector = calculateLayout(); + updateItems(m_layoutVector,vector); if(diff!=0) handleAxisUpdate(axis); } void AxisItem::handleGeometryChanged(const QRectF& rect) { m_rect = rect; - updateItems(m_layoutVector); + + if(m_thicksList.size()==0) return; + + QVector vector = calculateLayout(); + updateItems(m_layoutVector,vector); } void AxisItem::setAxisOpacity(qreal opacity) @@ -237,8 +241,11 @@ void AxisItem::setGridPen(const QPen& pen) } } -void AxisItem::calculateLayout(QVector& points) +QVector AxisItem::calculateLayout() const { + QVector points; + points.resize(m_thicksList.size()); + switch (m_type) { case X_AXIS: @@ -246,7 +253,7 @@ void AxisItem::calculateLayout(QVector& points) const qreal deltaX = m_rect.width()/(m_thicksList.size()-1); for (int i = 0; i < m_thicksList.size(); ++i) { int x = i * deltaX + m_rect.left(); - points[i]=x; + points[i] = x; } } break; @@ -255,11 +262,12 @@ void AxisItem::calculateLayout(QVector& points) const qreal deltaY = m_rect.height()/(m_thicksList.size()-1); for (int i = 0; i < m_thicksList.size(); ++i) { int y = i * -deltaY + m_rect.bottom(); - points[i]=y; + points[i] = y; } } break; } + return points; } void AxisItem::applyLayout(const QVector& points) diff --git a/src/axisitem_p.h b/src/axisitem_p.h index ab9a2db..1d12e8a 100644 --- a/src/axisitem_p.h +++ b/src/axisitem_p.h @@ -54,9 +54,10 @@ public slots: void handleLabelsChanged(QChartAxis* axis,const QStringList& labels); //labels from dataset void handleGeometryChanged(const QRectF& size); // geometry from presenter public: - virtual void updateItems(QVector& points); - virtual void calculateLayout(QVector& points); - virtual void applyLayout(const QVector& points); + virtual void updateItems(QVector& oldLayout,QVector& newLayout); + QVector calculateLayout() const; + void applyLayout(const QVector& points); + private: void clear(int count); void createItems(int count);