chartanimator.cpp
301 lines
| 8.6 KiB
| text/x-c
|
CppLexer
|
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$ | ||||
** | ||||
****************************************************************************/ | ||||
|
r530 | #include "chartanimator_p.h" | ||
#include "axisanimation_p.h" | ||||
#include "xyanimation_p.h" | ||||
|
r622 | #include "splineanimation_p.h" | ||
|
r530 | #include "xychartitem_p.h" | ||
|
r618 | #include "pieanimation_p.h" | ||
|
r671 | #include "baranimation_p.h" | ||
#include "barchartitem_p.h" | ||||
|
r560 | #include "areachartitem_p.h" | ||
|
r622 | #include "splinechartitem_p.h" | ||
#include "scatterchartitem_p.h" | ||||
|
r530 | #include <QTimer> | ||
Q_DECLARE_METATYPE(QVector<QPointF>) | ||||
Q_DECLARE_METATYPE(QVector<qreal>) | ||||
|
r681 | Q_DECLARE_METATYPE(QVector<QRectF>) | ||
|
r530 | |||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
const static int duration = 1000; | ||||
|
r738 | ChartAnimator::ChartAnimator(QObject *parent):QObject(parent), | ||
m_state(ShowState) | ||||
|
r530 | { | ||
} | ||||
ChartAnimator::~ChartAnimator() | ||||
{ | ||||
} | ||||
|
r738 | void ChartAnimator::addAnimation(Axis *item) | ||
|
r530 | { | ||
|
r738 | ChartAnimation *animation = m_animations.value(item); | ||
|
r530 | |||
|
r738 | if (!animation) { | ||
|
r530 | animation = new AxisAnimation(item); | ||
|
r738 | m_animations.insert(item, animation); | ||
|
r530 | } | ||
item->setAnimator(this); | ||||
} | ||||
|
r738 | void ChartAnimator::addAnimation(SplineChartItem *item) | ||
|
r622 | { | ||
|
r738 | ChartAnimation *animation = m_animations.value(item); | ||
|
r622 | |||
|
r738 | if (!animation) { | ||
|
r622 | animation = new SplineAnimation(item); | ||
|
r738 | m_animations.insert(item, animation); | ||
|
r622 | } | ||
item->setAnimator(this); | ||||
} | ||||
|
r738 | void ChartAnimator::addAnimation(ScatterChartItem *item) | ||
|
r622 | { | ||
|
r738 | ChartAnimation *animation = m_animations.value(item); | ||
|
r622 | |||
|
r738 | if (!animation) { | ||
|
r622 | animation = new XYAnimation(item); | ||
|
r738 | m_animations.insert(item, animation); | ||
|
r622 | } | ||
item->setAnimator(this); | ||||
} | ||||
|
r738 | void ChartAnimator::addAnimation(LineChartItem *item) | ||
|
r530 | { | ||
|
r738 | ChartAnimation *animation = m_animations.value(item); | ||
|
r530 | |||
|
r738 | if (!animation) { | ||
|
r530 | animation = new XYAnimation(item); | ||
|
r738 | m_animations.insert(item, animation); | ||
|
r530 | } | ||
item->setAnimator(this); | ||||
} | ||||
|
r738 | void ChartAnimator::addAnimation(PieChartItem *item) | ||
|
r618 | { | ||
|
r738 | ChartAnimation *animation = m_animations.value(item); | ||
|
r618 | |||
|
r738 | if (!animation) { | ||
|
r618 | animation = new PieAnimation(item); | ||
|
r738 | m_animations.insert(item, animation); | ||
|
r618 | } | ||
item->setAnimator(this); | ||||
} | ||||
|
r738 | void ChartAnimator::addAnimation(BarChartItem *item) | ||
|
r671 | { | ||
|
r738 | ChartAnimation *animation = m_animations.value(item); | ||
|
r671 | |||
|
r738 | if (!animation) { | ||
|
r671 | animation = new BarAnimation(item); | ||
|
r738 | m_animations.insert(item, animation); | ||
|
r671 | } | ||
item->setAnimator(this); | ||||
} | ||||
|
r738 | void ChartAnimator::removeAnimation(Chart *item) | ||
|
r530 | { | ||
item->setAnimator(0); | ||||
m_animations.remove(item); | ||||
} | ||||
|
r738 | void ChartAnimator::updateLayout(Axis *item , QVector<qreal> &newLayout) | ||
|
r530 | { | ||
|
r738 | AxisAnimation *animation = static_cast<AxisAnimation*>(m_animations.value(item)); | ||
|
r530 | |||
|
r560 | Q_ASSERT(animation); | ||
|
r530 | |||
QVector<qreal> oldLayout = item->layout(); | ||||
|
r738 | if (newLayout.count() == 0) | ||
return; | ||||
switch (m_state) { | ||||
case ZoomOutState: { | ||||
QRectF rect = item->geometry(); | ||||
oldLayout.resize(newLayout.count()); | ||||
|
r974 | for(int i = 0, j = oldLayout.count() - 1; i < (oldLayout.count() + 1) / 2; ++i, --j) { | ||
|
r738 | oldLayout[i] = item->axisType() == Axis::X_AXIS ? rect.left() : rect.bottom(); | ||
oldLayout[j] = item->axisType() == Axis::X_AXIS ? rect.right() : rect.top(); | ||||
} | ||||
} | ||||
break; | ||||
case ZoomInState: { | ||||
int index = qMin(oldLayout.count() * (item->axisType() == Axis::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]; | ||||
|
r530 | } | ||
|
r738 | break; | ||
case ScrollDownState: | ||||
case ScrollRightState: { | ||||
oldLayout.resize(newLayout.count()); | ||||
|
r974 | for(int i = 0, j = i + 1; i < oldLayout.count() - 1; ++i, ++j) | ||
|
r738 | oldLayout[i]= oldLayout[j]; | ||
} | ||||
break; | ||||
case ScrollUpState: | ||||
case ScrollLeftState: { | ||||
oldLayout.resize(newLayout.count()); | ||||
|
r974 | for(int i = oldLayout.count() - 1, j = i - 1; i > 0; --i, --j) | ||
|
r738 | oldLayout[i]= oldLayout[j]; | ||
} | ||||
break; | ||||
default: { | ||||
oldLayout.resize(newLayout.count()); | ||||
QRectF rect = item->geometry(); | ||||
|
r974 | for(int i = 0, j = oldLayout.count() - 1; i < oldLayout.count(); ++i, --j) | ||
|
r738 | oldLayout[i] = item->axisType() == Axis::X_AXIS ? rect.left() : rect.top(); | ||
} | ||||
break; | ||||
} | ||||
if (animation->state() != QAbstractAnimation::Stopped) | ||||
animation->stop(); | ||||
|
r530 | |||
animation->setDuration(duration); | ||||
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)); | ||||
|
r738 | QTimer::singleShot(0, animation, SLOT(start())); | ||
|
r530 | } | ||
|
r738 | void ChartAnimator::updateLayout(SplineChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, QVector<QPointF> &oldControlPoints, QVector<QPointF> &newControlPoints, int index) | ||
|
r530 | { | ||
|
r738 | SplineAnimation *animation = static_cast<SplineAnimation *>(m_animations.value(item)); | ||
|
r530 | |||
|
r560 | Q_ASSERT(animation); | ||
|
r530 | |||
|
r738 | if (newPoints.count() < 2 || newControlPoints.count() < 2) | ||
return; | ||||
|
r530 | |||
|
r738 | bool empty = oldPoints.count() == 0; | ||
|
r622 | |||
|
r530 | |||
|
r738 | if (animation->state() != QAbstractAnimation::Stopped) | ||
animation->stop(); | ||||
|
r530 | |||
animation->setDuration(duration); | ||||
|
r738 | if (!empty) | ||
animation->setAnimationType(ChartAnimation::MoveDownAnimation); | ||||
|
r530 | else | ||
|
r738 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); | ||
|
r622 | |||
|
r530 | animation->setEasingCurve(QEasingCurve::OutQuart); | ||
|
r738 | animation->setValues(oldPoints, newPoints, oldControlPoints, newControlPoints, index); | ||
|
r622 | |||
|
r738 | QTimer::singleShot(0, animation, SLOT(start())); | ||
|
r530 | } | ||
|
r622 | |||
|
r738 | void ChartAnimator::updateLayout(XYChartItem *item, QVector<QPointF> &oldPoints, QVector<QPointF> &newPoints, int index) | ||
|
r530 | { | ||
|
r738 | XYAnimation *animation = static_cast<XYAnimation *>(m_animations.value(item)); | ||
|
r530 | |||
|
r560 | Q_ASSERT(animation); | ||
|
r530 | |||
|
r738 | if (newPoints.count() == 0) | ||
return; | ||||
|
r622 | |||
|
r738 | bool empty = oldPoints.count() == 0; | ||
|
r622 | |||
|
r738 | if (animation->state() != QAbstractAnimation::Stopped) | ||
|
r622 | animation->stop(); | ||
|
r530 | animation->setDuration(duration); | ||
|
r738 | if (!empty) | ||
animation->setAnimationType(ChartAnimation::MoveDownAnimation); | ||||
|
r622 | else | ||
|
r738 | animation->setAnimationType(ChartAnimation::LineDrawAnimation); | ||
|
r622 | |||
|
r530 | animation->setEasingCurve(QEasingCurve::OutQuart); | ||
|
r738 | animation->setValues(oldPoints, newPoints, index); | ||
|
r530 | |||
|
r738 | QTimer::singleShot(0, animation, SLOT(start())); | ||
|
r530 | } | ||
|
r738 | void ChartAnimator::addAnimation(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty) | ||
|
r618 | { | ||
|
r738 | PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item)); | ||
|
r618 | Q_ASSERT(animation); | ||
|
r668 | animation->addSlice(slice, sliceData, isEmpty); | ||
|
r621 | } | ||
|
r738 | void ChartAnimator::removeAnimation(PieChartItem *item, QPieSlice *slice) | ||
|
r621 | { | ||
|
r738 | PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item)); | ||
|
r621 | Q_ASSERT(animation); | ||
animation->removeSlice(slice); | ||||
} | ||||
|
r738 | void ChartAnimator::updateLayout(PieChartItem *item, const PieLayout &layout) | ||
|
r621 | { | ||
|
r738 | PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item)); | ||
|
r621 | Q_ASSERT(animation); | ||
animation->updateValues(layout); | ||||
|
r618 | } | ||
|
r738 | void ChartAnimator::updateLayout(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData) | ||
|
r618 | { | ||
|
r738 | PieAnimation *animation = static_cast<PieAnimation *>(m_animations.value(item)); | ||
|
r618 | Q_ASSERT(animation); | ||
|
r668 | animation->updateValue(slice, sliceData); | ||
|
r618 | } | ||
|
r738 | void ChartAnimator::updateLayout(BarChartItem *item, const QVector<QRectF> &oldLayout, const QVector<QRectF> &newLayout) | ||
|
r671 | { | ||
|
r738 | BarAnimation *animation = static_cast<BarAnimation *>(m_animations.value(item)); | ||
|
r671 | Q_ASSERT(animation); | ||
|
r681 | animation->setDuration(duration); | ||
animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); | ||||
animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); | ||||
|
r738 | QTimer::singleShot(0, animation, SLOT(start())); | ||
|
r671 | } | ||
|
r738 | void ChartAnimator::setState(State state, const QPointF &point) | ||
|
r531 | { | ||
|
r738 | m_state = state; | ||
m_point = point; | ||||
|
r531 | } | ||
|
r974 | #include "moc_chartanimator_p.cpp" | ||
|
r530 | QTCOMMERCIALCHART_END_NAMESPACE | ||