diff options
author | Michal Klocek <michal.klocek@digia.com> | 2012-05-25 13:47:19 +0300 |
---|---|---|
committer | Michal Klocek <michal.klocek@digia.com> | 2012-05-25 13:49:11 +0300 |
commit | 51695bb27b0ea6cae78968b5b21021ea88320527 (patch) | |
tree | 37651f9b1f6b42f000a3dd1aa8b7dd7c38fd823b /src/animations | |
parent | 897a83c64309456c739ecd282b297a28a123d567 (diff) |
Refactors axis animation, line animations
Diffstat (limited to 'src/animations')
-rw-r--r-- | src/animations/axisanimation.cpp | 75 | ||||
-rw-r--r-- | src/animations/axisanimation_p.h | 11 | ||||
-rw-r--r-- | src/animations/chartanimation_p.h | 1 | ||||
-rw-r--r-- | src/animations/chartanimator.cpp | 102 | ||||
-rw-r--r-- | src/animations/chartanimator_p.h | 12 | ||||
-rw-r--r-- | src/animations/splineanimation.cpp | 7 | ||||
-rw-r--r-- | src/animations/xyanimation.cpp | 80 | ||||
-rw-r--r-- | src/animations/xyanimation_p.h | 8 |
8 files changed, 149 insertions, 147 deletions
diff --git a/src/animations/axisanimation.cpp b/src/animations/axisanimation.cpp index 9f694ed4..e27cf1cc 100644 --- a/src/animations/axisanimation.cpp +++ b/src/animations/axisanimation.cpp @@ -19,7 +19,9 @@ ****************************************************************************/ #include "axisanimation_p.h" +#include "chartaxis_p.h" #include <QTimer> +#include <QDebug> Q_DECLARE_METATYPE(QVector<qreal>) @@ -27,14 +29,84 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE AxisAnimation::AxisAnimation(ChartAxis *axis): ChartAnimation(axis), - m_axis(axis) + m_axis(axis), + m_type(DefaultAnimation) { + setDuration(ChartAnimationDuration); + setEasingCurve(QEasingCurve::OutQuart); } AxisAnimation::~AxisAnimation() { } +void AxisAnimation::setAnimationType(Animation type) +{ + if (state() != QAbstractAnimation::Stopped) stop(); + m_type=type; +} + +void AxisAnimation::setAnimationPoint(const QPointF& point) +{ + if (state() != QAbstractAnimation::Stopped) stop(); + m_point=point; +} + +void AxisAnimation::setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout) +{ + if (state() != QAbstractAnimation::Stopped) stop(); + + if (newLayout.count() == 0) + return; + + switch (m_type) { + case ZoomOutAnimation: { + QRectF rect = m_axis->geometry(); + oldLayout.resize(newLayout.count()); + + for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { + oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom(); + oldLayout[j] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top(); + } + } + break; + case ZoomInAnimation: { + int index = qMin(oldLayout.count() * (m_axis->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0); + oldLayout.resize(newLayout.count()); + + for(int i = 0; i < oldLayout.count(); i++) + oldLayout[i]= oldLayout[index]; + } + break; + case MoveForwardAnimation: { + oldLayout.resize(newLayout.count()); + + for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) + oldLayout[i]= oldLayout[j]; + } + break; + case MoveBackwordAnimation: { + oldLayout.resize(newLayout.count()); + + for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) + oldLayout[i]= oldLayout[j]; + } + break; + default: { + oldLayout.resize(newLayout.count()); + QRectF rect = m_axis->geometry(); + for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j) + oldLayout[i] = m_axis->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top(); + } + break; + } + + QVariantAnimation::KeyValues value; + setKeyValues(value); //workaround for wrong interpolation call + setKeyValueAt(0.0, qVariantFromValue(oldLayout)); + setKeyValueAt(1.0, qVariantFromValue(newLayout)); +} + QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const { QVector<qreal> startVector = qVariantValue<QVector<qreal> >(start); @@ -58,6 +130,7 @@ void AxisAnimation::updateCurrentValue (const QVariant &value ) QVector<qreal> vector = qVariantValue<QVector<qreal> >(value); Q_ASSERT(vector.count() != 0); m_axis->setLayout(vector); + m_axis->updateGeometry(); } } diff --git a/src/animations/axisanimation_p.h b/src/animations/axisanimation_p.h index ff2317cf..97ec839e 100644 --- a/src/animations/axisanimation_p.h +++ b/src/animations/axisanimation_p.h @@ -21,22 +21,29 @@ #ifndef AXISANIMATION_H #define AXISANIMATION_H -#include "chartaxis_p.h" #include "chartanimation_p.h" - +#include <QPointF> QTCOMMERCIALCHART_BEGIN_NAMESPACE +class ChartAxis; + class AxisAnimation: public ChartAnimation { public: + enum Animation { DefaultAnimation, ZoomOutAnimation, ZoomInAnimation, MoveForwardAnimation, MoveBackwordAnimation}; AxisAnimation(ChartAxis *axis); ~AxisAnimation(); + void setAnimationType(Animation type); + void setAnimationPoint(const QPointF& point); + void setValues(QVector<qreal> &oldLayout, QVector<qreal> &newLayout); protected: QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress ) const; void updateCurrentValue(const QVariant &value ); private: ChartAxis *m_axis; + Animation m_type; + QPointF m_point; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/chartanimation_p.h b/src/animations/chartanimation_p.h index 0e855e8f..cba058cc 100644 --- a/src/animations/chartanimation_p.h +++ b/src/animations/chartanimation_p.h @@ -30,6 +30,7 @@ const static int ChartAnimationDuration = 1000; class ChartAnimation: public QVariantAnimation { + Q_OBJECT public: ChartAnimation(QObject *parent = 0):QVariantAnimation(parent){}; }; diff --git a/src/animations/chartanimator.cpp b/src/animations/chartanimator.cpp index abc03667..be5a1b90 100644 --- a/src/animations/chartanimator.cpp +++ b/src/animations/chartanimator.cpp @@ -29,6 +29,7 @@ #include "areachartitem_p.h" #include "splinechartitem_p.h" #include "scatterchartitem_p.h" +#include "chartaxis_p.h" #include <QTimer> Q_DECLARE_METATYPE(QVector<QPointF>) @@ -37,27 +38,13 @@ Q_DECLARE_METATYPE(QVector<QRectF>) QTCOMMERCIALCHART_BEGIN_NAMESPACE -ChartAnimator::ChartAnimator(QObject *parent):QObject(parent), - m_state(ShowState) +ChartAnimator::ChartAnimator(QObject *parent):QObject(parent) { } ChartAnimator::~ChartAnimator() { } - -void ChartAnimator::addAnimation(ChartAxis *item) -{ - ChartAnimation *animation = m_animations.value(item); - - if (!animation) { - animation = new AxisAnimation(item); - m_animations.insert(item, animation); - } - - item->setAnimator(this); -} - void ChartAnimator::addAnimation(PieChartItem *item) { ChartAnimation *animation = m_animations.value(item); @@ -89,75 +76,6 @@ void ChartAnimator::removeAnimation(Chart *item) m_animations.remove(item); } -void ChartAnimator::updateLayout(ChartAxis *item , QVector<qreal> &newLayout) -{ - AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item)); - - Q_ASSERT(animation); - - QVector<qreal> oldLayout = item->layout(); - - if (newLayout.count() == 0) - return; - - switch (m_state) { - case ZoomOutState: { - QRectF rect = item->geometry(); - oldLayout.resize(newLayout.count()); - - for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { - oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.bottom(); - oldLayout[j] = item->axisType() == ChartAxis::X_AXIS ? rect.right() : rect.top(); - } - } - break; - case ZoomInState: { - int index = qMin(oldLayout.count() * (item->axisType() == ChartAxis::X_AXIS ? m_point.x() : (1 - m_point.y())), newLayout.count() - 1.0); - oldLayout.resize(newLayout.count()); - - for(int i = 0; i < oldLayout.count(); i++) - oldLayout[i]= oldLayout[index]; - } - break; - case ScrollDownState: - case ScrollRightState: { - oldLayout.resize(newLayout.count()); - - for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) - oldLayout[i]= oldLayout[j]; - } - break; - case ScrollUpState: - case ScrollLeftState: { - oldLayout.resize(newLayout.count()); - - for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) - oldLayout[i]= oldLayout[j]; - } - break; - default: { - oldLayout.resize(newLayout.count()); - QRectF rect = item->geometry(); - for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j) - oldLayout[i] = item->axisType() == ChartAxis::X_AXIS ? rect.left() : rect.top(); - } - break; - } - - - if (animation->state() != QAbstractAnimation::Stopped) - animation->stop(); - - animation->setDuration(ChartAnimationDuration); - animation->setEasingCurve(QEasingCurve::OutQuart); - QVariantAnimation::KeyValues value; - animation->setKeyValues(value); //workaround for wrong interpolation call - animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); - animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); - - QTimer::singleShot(0, animation, SLOT(start())); -} - void ChartAnimator::addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool startupAnimation) { PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item)); @@ -189,22 +107,6 @@ void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldL QTimer::singleShot(0, animation, SLOT(start())); } - -void ChartAnimator::setState(State state, const QPointF &point) -{ - m_state = state; - m_point = point; -} - -void ChartAnimator::startAnimation(XYAnimation* animation) -{ - Q_ASSERT(animation); - if (animation->state() != QAbstractAnimation::Stopped) animation->stop(); - animation->setDuration(ChartAnimationDuration); - animation->setEasingCurve(QEasingCurve::OutQuart); - QTimer::singleShot(0, animation, SLOT(start())); -} - #include "moc_chartanimator_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/chartanimator_p.h b/src/animations/chartanimator_p.h index d22d89fe..a9b747ad 100644 --- a/src/animations/chartanimator_p.h +++ b/src/animations/chartanimator_p.h @@ -42,33 +42,21 @@ class ChartAnimator : public QObject { Q_OBJECT public: - enum State{ShowState, ScrollUpState, ScrollDownState, ScrollLeftState, ScrollRightState, ZoomInState, ZoomOutState}; - ChartAnimator(QObject *parent = 0); virtual ~ChartAnimator(); - void addAnimation(ChartAxis *item); void addAnimation(PieChartItem *item); void addAnimation(BarChartItem *item); void removeAnimation(Chart *item); - void animationStarted(); - void updateLayout(ChartAxis *item, QVector<qreal> &layout); - void addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty); void removeAnimation(PieChartItem *item, PieSliceItem *sliceItem); void updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData); void updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout); - void setState(State state,const QPointF &point = QPointF()); - - void startAnimation(XYAnimation* animation); - private: QMap<Chart *, ChartAnimation *> m_animations; - State m_state; - QPointF m_point; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/splineanimation.cpp b/src/animations/splineanimation.cpp index 700778a6..51fce1ac 100644 --- a/src/animations/splineanimation.cpp +++ b/src/animations/splineanimation.cpp @@ -94,7 +94,10 @@ QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &en switch (animationType()) { - case MoveDownAnimation: { + case RemovePointAnimation: + case AddPointAnimation: + case ReplacePointAnimation: + { if (startPair.first.count() != endPair.first.count()) break; Q_ASSERT(startPair.first.count() * 2 - 2 == startPair.second.count()); @@ -115,7 +118,7 @@ QVariant SplineAnimation::interpolated(const QVariant &start, const QVariant &en } break; - case LineDrawAnimation:{ + case NewAnimation:{ Q_ASSERT(endPair.first.count() * 2 - 2 == endPair.second.count()); int count = endPair.first.count()* qBound(qreal(0), progress, qreal(1)); for(int i = 0; i < count; i++) { diff --git a/src/animations/xyanimation.cpp b/src/animations/xyanimation.cpp index 104f84a6..2b9ada51 100644 --- a/src/animations/xyanimation.cpp +++ b/src/animations/xyanimation.cpp @@ -29,8 +29,10 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE XYAnimation::XYAnimation(XYChart *item):ChartAnimation(item), m_item(item), m_dirty(false), - m_type(MoveDownAnimation) + m_type(NewAnimation) { + setDuration(ChartAnimationDuration); + setEasingCurve(QEasingCurve::OutQuart); } XYAnimation::~XYAnimation() @@ -39,38 +41,48 @@ XYAnimation::~XYAnimation() void XYAnimation::setAnimationType(Animation type) { - if (state() != QAbstractAnimation::Stopped) stop(); + if (state() != QAbstractAnimation::Stopped) stop(); m_type=type; } -void XYAnimation::setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index) +void XYAnimation::setValues(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints, int index) { - if (state() != QAbstractAnimation::Stopped) stop(); + if (state() != QAbstractAnimation::Stopped) stop(); - int x = oldPoints.count(); - int y = newPoints.count(); - - if (x != y && abs(x - y) != 1) { - m_oldPoints = newPoints; - oldPoints.resize(newPoints.size()); - setKeyValueAt(0.0, qVariantFromValue(oldPoints)); - setKeyValueAt(1.0, qVariantFromValue(newPoints)); - m_dirty = false; + if (m_item->isDirty()) { + m_oldPoints = oldPoints; + m_newPoints = newPoints; + m_dirty=false; } else { - if (m_dirty) { + if(m_dirty) { + m_newPoints = newPoints; m_oldPoints = oldPoints; - m_dirty = false; + 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 } - oldPoints = newPoints; - if (y < x) - m_oldPoints.remove(index); //remove - if (y > x) - m_oldPoints.insert(index, x > 0 ? m_oldPoints[index-1] : newPoints[index]); //add - setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); - setKeyValueAt(1.0, qVariantFromValue(newPoints)); - Q_ASSERT(m_oldPoints.count() == newPoints.count()); + }else{ + m_newPoints=newPoints; + m_dirty=false; + m_oldPoints.resize(m_newPoints.size()); } + + setKeyValueAt(0.0, qVariantFromValue(m_oldPoints)); + setKeyValueAt(1.0, qVariantFromValue(m_newPoints)); + } QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress ) const @@ -81,8 +93,10 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q switch (m_type) { - case MoveDownAnimation: { - + case ReplacePointAnimation: + case AddPointAnimation: + case RemovePointAnimation: + { if (startVector.count() != endVector.count()) break; @@ -94,7 +108,7 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q } break; - case LineDrawAnimation: { + case NewAnimation: { for(int i = 0; i < endVector.count() * qBound(qreal(0), progress, qreal(1)); i++) result << endVector[i]; } @@ -110,11 +124,23 @@ QVariant XYAnimation::interpolated(const QVariant &start, const QVariant &end, q void XYAnimation::updateCurrentValue (const QVariant &value) { if(state()!=QAbstractAnimation::Stopped){ //workaround - m_dirty = true; QVector<QPointF> vector = qVariantValue<QVector<QPointF> >(value); m_item->setGeometryPoints(vector); m_item->updateGeometry(); + m_item->setDirty(true); } } +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" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/xyanimation_p.h b/src/animations/xyanimation_p.h index 92d644f4..50b3478a 100644 --- a/src/animations/xyanimation_p.h +++ b/src/animations/xyanimation_p.h @@ -31,20 +31,22 @@ class XYChart; class XYAnimation : public ChartAnimation { public: - enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation }; + enum Animation { AddPointAnimation, RemovePointAnimation, ReplacePointAnimation, NewAnimation }; XYAnimation(XYChart *item); ~XYAnimation(); - void setValues(QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints,int index); + void setValues(const QVector<QPointF> &oldPoints, const QVector<QPointF> &newPoints,int index); void setAnimationType(Animation type); Animation animationType() const { return m_type; }; protected: QVariant interpolated(const QVariant &start, const QVariant &end, qreal progress ) const; void updateCurrentValue (const QVariant &value ); - + void updateState( QAbstractAnimation::State newState, QAbstractAnimation::State oldState ); private: XYChart *m_item; QVector<QPointF> m_oldPoints; + QVector<QPointF> m_newPoints; + int m_index; bool m_dirty; Animation m_type; }; |