chartanimator.cpp
207 lines
| 5.2 KiB
| text/x-c
|
CppLexer
|
r530 | #include "chartanimator_p.h" | ||
#include "axisanimation_p.h" | ||||
#include "xyanimation_p.h" | ||||
#include "xychartitem_p.h" | ||||
|
r618 | #include "pieanimation_p.h" | ||
|
r560 | #include "areachartitem_p.h" | ||
|
r530 | #include <QTimer> | ||
Q_DECLARE_METATYPE(QVector<QPointF>) | ||||
Q_DECLARE_METATYPE(QVector<qreal>) | ||||
QTCOMMERCIALCHART_BEGIN_NAMESPACE | ||||
const static int duration = 1000; | ||||
ChartAnimator::ChartAnimator(QObject *parent):QObject(parent) | ||||
{ | ||||
} | ||||
ChartAnimator::~ChartAnimator() | ||||
{ | ||||
} | ||||
void ChartAnimator::addAnimation(AxisItem* item) | ||||
{ | ||||
|
r560 | ChartAnimation* animation = m_animations.value(item); | ||
|
r530 | |||
if(!animation) { | ||||
animation = new AxisAnimation(item); | ||||
m_animations.insert(item,animation); | ||||
} | ||||
item->setAnimator(this); | ||||
} | ||||
void ChartAnimator::addAnimation(XYChartItem* item) | ||||
{ | ||||
|
r560 | ChartAnimation* animation = m_animations.value(item); | ||
|
r530 | |||
if(!animation) { | ||||
animation = new XYAnimation(item); | ||||
m_animations.insert(item,animation); | ||||
} | ||||
item->setAnimator(this); | ||||
} | ||||
|
r618 | void ChartAnimator::addAnimation(PieChartItem* item) | ||
{ | ||||
ChartAnimation* animation = m_animations.value(item); | ||||
if(!animation) { | ||||
animation = new PieAnimation(item); | ||||
m_animations.insert(item,animation); | ||||
} | ||||
item->setAnimator(this); | ||||
} | ||||
|
r530 | void ChartAnimator::removeAnimation(ChartItem* item) | ||
{ | ||||
item->setAnimator(0); | ||||
m_animations.remove(item); | ||||
} | ||||
void ChartAnimator::applyLayout(AxisItem* item , QVector<qreal>& newLayout) | ||||
{ | ||||
AxisAnimation* animation = static_cast<AxisAnimation*>(m_animations.value(item)); | ||||
|
r560 | Q_ASSERT(animation); | ||
|
r530 | |||
QVector<qreal> oldLayout = item->layout(); | ||||
if(newLayout.count()==0) return; | ||||
|
r531 | 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()==AxisItem::X_AXIS?rect.left():rect.bottom(); | ||||
oldLayout[j]= item->axisType()==AxisItem::X_AXIS?rect.right():rect.top(); | ||||
} | ||||
} | ||||
break; | ||||
case ZoomInState: { | ||||
int index = qMin(oldLayout.count()*(item->axisType()==AxisItem::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()==AxisItem::X_AXIS?rect.left():rect.top(); | ||||
} | ||||
} | ||||
break; | ||||
} | ||||
|
r530 | |||
if(animation->state()!=QAbstractAnimation::Stopped) { | ||||
animation->stop(); | ||||
} | ||||
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)); | ||||
QTimer::singleShot(0,animation,SLOT(start())); | ||||
} | ||||
void ChartAnimator::applyLayout(XYChartItem* item, QVector<QPointF>& newPoints) | ||||
{ | ||||
XYAnimation* animation = static_cast<XYAnimation*>(m_animations.value(item)); | ||||
|
r560 | Q_ASSERT(animation); | ||
|
r530 | |||
QVector<QPointF> oldPoints = item->points(); | ||||
if(newPoints.count()==0) return; | ||||
bool empty = oldPoints.count()==0; | ||||
oldPoints.resize(newPoints.size()); | ||||
if(animation->state()!=QAbstractAnimation::Stopped) { | ||||
animation->stop(); | ||||
} | ||||
animation->setDuration(duration); | ||||
if(!empty) | ||||
animation->setAnimationType(XYAnimation::MoveDownAnimation); | ||||
else | ||||
animation->setAnimationType(XYAnimation::LineDrawAnimation); | ||||
animation->setEasingCurve(QEasingCurve::OutQuart); | ||||
animation->setValues(oldPoints,newPoints); | ||||
QTimer::singleShot(0,animation,SLOT(start())); | ||||
} | ||||
void ChartAnimator::updateLayout(XYChartItem* item, QVector<QPointF>& newPoints) | ||||
{ | ||||
XYAnimation* animation = static_cast<XYAnimation*>(m_animations.value(item)); | ||||
|
r560 | Q_ASSERT(animation); | ||
|
r530 | |||
animation->setDuration(duration); | ||||
animation->setAnimationType(XYAnimation::MoveDownAnimation); | ||||
animation->setEasingCurve(QEasingCurve::OutQuart); | ||||
animation->updateValues(newPoints); | ||||
QTimer::singleShot(0,animation,SLOT(start())); | ||||
} | ||||
|
r618 | void ChartAnimator::applyLayout(PieChartItem* item, QVector<PieSliceLayout> &layout) | ||
{ | ||||
PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); | ||||
Q_ASSERT(animation); | ||||
animation->setValues(layout); | ||||
} | ||||
void ChartAnimator::updateLayout(PieChartItem* item, PieSliceLayout &layout) | ||||
{ | ||||
PieAnimation* animation = static_cast<PieAnimation*>(m_animations.value(item)); | ||||
Q_ASSERT(animation); | ||||
animation->updateValue(layout); | ||||
} | ||||
|
r531 | void ChartAnimator::setState(State state,const QPointF& point) | ||
{ | ||||
m_state=state; | ||||
m_point=point; | ||||
} | ||||
|
r530 | QTCOMMERCIALCHART_END_NAMESPACE | ||