From b6fb05e4e56f04198e3fbfea7e47f1925293f12f 2012-03-07 07:14:37 From: Michal Klocek Date: 2012-03-07 07:14:37 Subject: [PATCH] Refactor animation to fit line,spline,scatter * adds xyanimationitem * adds xyanimator * fixes spline linePen call * updates presenter example * adds missing tilte getter in chartview --- diff --git a/example/presenterchart/chartview.cpp b/example/presenterchart/chartview.cpp index d063740..3b0d708 100644 --- a/example/presenterchart/chartview.cpp +++ b/example/presenterchart/chartview.cpp @@ -1,10 +1,14 @@ #include "chartview.h" #include +#include +#include #include ChartView::ChartView(QWidget* parent):QChartView(parent), m_index(0) { + setChartTitle("Three random line charts"); + QObject::connect(&m_timer,SIGNAL(timeout()),this,SLOT(handleTimeout())); m_timer.setInterval(3000); @@ -15,11 +19,12 @@ m_index(0) QPen blue(Qt::blue); blue.setWidth(3); series0->setPen(blue); - QLineSeries* series1 = new QLineSeries(this); + QScatterSeries* series1 = new QScatterSeries(this); QPen red(Qt::red); red.setWidth(3); series1->setPen(red); - QLineSeries* series2 = new QLineSeries(this); + series1->setBrush(Qt::white); + QSplineSeries* series2 = new QSplineSeries(this); QPen green(Qt::green); green.setWidth(3); series2->setPen(green); @@ -27,16 +32,21 @@ m_index(0) int numPoints = 10; for (int x = 0; x <= numPoints; ++x) { - series0->add(x, qrand() % 100); - series1->add(x, qrand() % 100); - series2->add(x, qrand() % 100); + qreal y = qrand() % 100; + series0->add(x,y); + series1->add(x,y); + series2->add(x,y); } - addSeries(series0); - m_series< m_series; + QStringList m_titles; int m_index; }; diff --git a/example/presenterchart/main.cpp b/example/presenterchart/main.cpp index 17000f0..ba65e4d 100644 --- a/example/presenterchart/main.cpp +++ b/example/presenterchart/main.cpp @@ -8,7 +8,6 @@ int main(int argc, char *argv[]) QMainWindow window; ChartView chartView(&window); chartView.setRenderHint(QPainter::Antialiasing); - chartView.setChartTitle("Three random line charts"); chartView.setAnimationOptions(QChart::AllAnimations); window.setCentralWidget(&chartView); window.resize(400, 300); diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index a026abd..e8a08ee 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -20,7 +20,6 @@ #include "stackedbarpresenter_p.h" #include "percentbarpresenter_p.h" #include "linechartitem_p.h" -#include "linechartanimationitem_p.h" #include "piepresenter_p.h" #include "scatterchartitem_p.h" #include "splinechartitem_p.h" @@ -143,7 +142,7 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) QLineSeries* lineSeries = static_cast(series); LineChartItem* item; if(m_options.testFlag(QChart::SeriesAnimations)){ - item = new LineChartAnimationItem(lineSeries,m_chart); + item = new LineChartAnimationItem(lineSeries,m_chart); }else{ item = new LineChartItem(lineSeries,m_chart); } @@ -215,19 +214,27 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) break; } case QSeries::SeriesTypeScatter: { - QScatterSeries *scatterSeries = qobject_cast(series); - ScatterChartItem *scatterPresenter = new ScatterChartItem(scatterSeries, m_chart); - QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), - scatterPresenter, SLOT(handleGeometryChanged(const QRectF&))); - QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)), - scatterPresenter, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); - m_chartTheme->decorate(scatterPresenter, scatterSeries, m_chartItems.count()); - m_chartItems.insert(scatterSeries, scatterPresenter); - if (m_rect.isValid()) - scatterPresenter->handleGeometryChanged(m_rect); - scatterPresenter->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY()); - break; - } + QScatterSeries *scatterSeries = qobject_cast(series); + ScatterChartItem *item; + if(m_options.testFlag(QChart::SeriesAnimations)) { + item = new ScatterChartAnimationItem(scatterSeries,m_chart); + } else { + item = new ScatterChartItem(scatterSeries, m_chart); + } + QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), + item, SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(domain, SIGNAL(domainChanged(qreal,qreal,qreal,qreal)), + item, SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); + //initialize + if (m_rect.isValid()) + item->handleGeometryChanged(m_rect); + item->handleDomainChanged(domain->minX(), domain->maxX(), domain->minY(), domain->maxY()); + //decorate + m_chartTheme->decorate(item, scatterSeries, m_chartItems.count()); + m_chartItems.insert(scatterSeries, item); + + break; + } case QSeries::SeriesTypePie: { QPieSeries *s = qobject_cast(series); PiePresenter* pie = new PiePresenter(m_chart, s); @@ -254,16 +261,24 @@ void ChartPresenter::handleSeriesAdded(QSeries* series,Domain* domain) } case QSeries::SeriesTypeSpline: { - QSplineSeries* splineSeries = qobject_cast(series); - SplineChartItem* splinePresenter = new SplineChartItem(splineSeries, m_chart); - QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), splinePresenter, SLOT(handleGeometryChanged(const QRectF&))); - QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),splinePresenter,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); - //initialize - splinePresenter->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); - m_chartTheme->decorate(splinePresenter, splineSeries, m_chartItems.count()); - m_chartItems.insert(splineSeries, splinePresenter); - break; - } + + QSplineSeries* splineSeries = qobject_cast(series); + SplineChartItem* item; + if(m_options.testFlag(QChart::SeriesAnimations)) { + item = new SplineChartAnimationItem(splineSeries, m_chart); + } else { + item = new SplineChartItem(splineSeries, m_chart); + } + QObject::connect(this, SIGNAL(geometryChanged(const QRectF&)), item, SLOT(handleGeometryChanged(const QRectF&))); + QObject::connect(domain,SIGNAL(domainChanged(qreal,qreal,qreal,qreal)),item,SLOT(handleDomainChanged(qreal,qreal,qreal,qreal))); + //initialize + item->handleDomainChanged(domain->minX(),domain->maxX(),domain->minY(),domain->maxY()); + if(m_rect.isValid()) item->handleGeometryChanged(m_rect); + //decorate + m_chartTheme->decorate(item, splineSeries, m_chartItems.count()); + m_chartItems.insert(splineSeries, item); + break; + } default: { qDebug()<< "Series type" << series->type() << "not implemented."; break; diff --git a/src/charttheme.cpp b/src/charttheme.cpp index 7f1ac23..074d4d1 100644 --- a/src/charttheme.cpp +++ b/src/charttheme.cpp @@ -165,12 +165,12 @@ void ChartTheme::decorate(LineChartItem* item, QLineSeries* series,int count) { QPen pen; if(pen != series->pen()){ - item->setPen(series->pen()); + item->setLinePen(series->pen()); return; } pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); pen.setWidthF(2); - item->setPen(pen); + item->setLinePen(pen); } void ChartTheme::decorate(BarPresenter* item, QBarSeries* series,int count) @@ -305,13 +305,14 @@ void ChartTheme::decorate(SplineChartItem* item, QSplineSeries* series, int coun Q_ASSERT(series); QPen pen; + if(pen != series->pen()){ - item->setPen(series->pen()); - return; + item->setLinePen(series->pen()); + }else{ + pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); + pen.setWidthF(series->pen().widthF()); + item->setLinePen(series->pen()); } - pen.setColor(m_seriesColor.at(count%m_seriesColor.size())); - pen.setWidthF(series->pen().widthF()); - item->setPen(pen); // QColor color = m_seriesColor.at(count % m_seriesColor.size()); // TODO: define alpha in the theme? or in the series? diff --git a/src/linechart/linechart.pri b/src/linechart/linechart.pri index a3332b7..82f0226 100644 --- a/src/linechart/linechart.pri +++ b/src/linechart/linechart.pri @@ -2,13 +2,11 @@ INCLUDEPATH += $$PWD DEPENDPATH += $$PWD SOURCES += \ - $$PWD/linechartanimationitem.cpp \ $$PWD/linechartitem.cpp \ $$PWD/qlineseries.cpp PRIVATE_HEADERS += \ - $$PWD/linechartitem_p.h \ - $$PWD/linechartanimationitem_p.h + $$PWD/linechartitem_p.h PUBLIC_HEADERS += \ $$PWD/qlineseries.h \ No newline at end of file diff --git a/src/linechart/linechartanimationitem.cpp b/src/linechart/linechartanimationitem.cpp index e55e39a..9c7820b 100644 --- a/src/linechart/linechartanimationitem.cpp +++ b/src/linechart/linechartanimationitem.cpp @@ -38,7 +38,6 @@ void LineChartAnimationItem::updatePoints(QVector& newPoints) m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); QTimer::singleShot(0,m_animation,SLOT(start())); - m_points = newPoints; m_dirty=false; diff --git a/src/linechart/linechartitem.cpp b/src/linechart/linechartitem.cpp index 07c6752..c1e72b6 100644 --- a/src/linechart/linechartitem.cpp +++ b/src/linechart/linechartitem.cpp @@ -93,16 +93,15 @@ void LineChartItem::setGeometry(QVector& points) XYChartItem::setGeometry(points); } -void LineChartItem::setPen(const QPen& pen) +void LineChartItem::setLinePen(const QPen& pen) { m_pen = pen; } - void LineChartItem::handleUpdated() { m_items.setVisible(m_series->pointsVisible()); - setPen(m_series->pen()); + setLinePen(m_series->pen()); update(); } diff --git a/src/linechart/linechartitem_p.h b/src/linechart/linechartitem_p.h index 48629a5..7fd6489 100644 --- a/src/linechart/linechartitem_p.h +++ b/src/linechart/linechartitem_p.h @@ -3,6 +3,7 @@ #include "qchartglobal.h" #include "xychartitem_p.h" +#include "xychartanimationitem_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -22,7 +23,7 @@ public: void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QPainterPath shape() const; - void setPen(const QPen& pen); + void setLinePen(const QPen& pen); void setPointsVisible(bool visible); public slots: @@ -42,10 +43,12 @@ private: QRectF m_rect; QPen m_pen; - friend class LineChartAnimatator; + template friend class XYChartAnimator; }; +typedef XYChartAnimationItem LineChartAnimationItem; + QTCOMMERCIALCHART_END_NAMESPACE #endif diff --git a/src/qchart.cpp b/src/qchart.cpp index 612fa3d..6d6cb07 100644 --- a/src/qchart.cpp +++ b/src/qchart.cpp @@ -117,7 +117,18 @@ void QChart::setChartBackgroundPen(const QPen& pen) void QChart::setChartTitle(const QString& title) { createChartTitleItem(); - m_titleItem->setPlainText(title); + m_titleItem->setText(title); +} + +/*! + Gets the chart \a title. The description text that is rendered above the chart. +*/ +QString QChart::chartTitle() const +{ + if(m_titleItem) + return m_titleItem->text(); + else + return QString(); } /*! @@ -141,7 +152,7 @@ void QChart::createChartBackgroundItem() void QChart::createChartTitleItem() { if(!m_titleItem) { - m_titleItem = new QGraphicsTextItem(this); + m_titleItem = new QGraphicsSimpleTextItem(this); m_titleItem->setZValue(ChartPresenter::BackgroundZValue); } } diff --git a/src/qchart.h b/src/qchart.h index 5f29265..36734cb 100644 --- a/src/qchart.h +++ b/src/qchart.h @@ -57,6 +57,7 @@ public: QChart::ChartTheme chartTheme() const; void setChartTitle(const QString& title); + QString chartTitle() const; void setChartTitleFont(const QFont& font); void setChartBackgroundBrush(const QBrush& brush); void setChartBackgroundPen(const QPen& pen); @@ -82,7 +83,7 @@ private: private: Q_DISABLE_COPY(QChart) QGraphicsRectItem* m_backgroundItem; - QGraphicsTextItem* m_titleItem; + QGraphicsSimpleTextItem* m_titleItem; QRectF m_rect; ChartDataSet *m_dataset; ChartPresenter *m_presenter; diff --git a/src/qchartview.cpp b/src/qchartview.cpp index ad900d9..800f31a 100644 --- a/src/qchartview.cpp +++ b/src/qchartview.cpp @@ -142,6 +142,14 @@ void QChartView::setChartTitle(const QString& title) } /*! + Gets the chart \a title. A description text that is rendered above the chart. +*/ +QString QChartView::chartTitle() const +{ + return m_chart->chartTitle(); +} + +/*! Sets the \a font that is used for rendering the description text that is rendered above the chart. */ void QChartView::setChartTitleFont(const QFont& font) diff --git a/src/qchartview.h b/src/qchartview.h index 0272edb..77f97c9 100644 --- a/src/qchartview.h +++ b/src/qchartview.h @@ -30,6 +30,7 @@ public: int margin() const; void setChartTitle(const QString& title); + QString chartTitle() const; void setChartTitleFont(const QFont& font); void setChartBackgroundBrush(const QBrush& brush); void setChartBackgroundPen(const QPen& pen); diff --git a/src/scatterseries/scatterchartitem.cpp b/src/scatterseries/scatterchartitem.cpp index a0f7504..ade29cf 100644 --- a/src/scatterseries/scatterchartitem.cpp +++ b/src/scatterseries/scatterchartitem.cpp @@ -6,7 +6,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE -ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsObject *parent) : +ScatterChartItem::ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent) : XYChartItem(series,parent), m_series(series), m_items(this), diff --git a/src/scatterseries/scatterchartitem_p.h b/src/scatterseries/scatterchartitem_p.h index 29af424..feaf6c9 100644 --- a/src/scatterseries/scatterchartitem_p.h +++ b/src/scatterseries/scatterchartitem_p.h @@ -3,7 +3,8 @@ #include "qchartglobal.h" #include "xychartitem_p.h" -#include +#include "xychartanimationitem_p.h" +#include #include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -14,7 +15,7 @@ class ScatterChartItem : public XYChartItem { Q_OBJECT public: - explicit ScatterChartItem(QScatterSeries *series, QGraphicsObject *parent = 0); + explicit ScatterChartItem(QScatterSeries *series, QGraphicsItem *parent = 0); public: //from QGraphicsItem @@ -43,8 +44,12 @@ private: int m_shape; int m_size; QRectF m_rect; + + template friend class XYChartAnimator; }; +typedef XYChartAnimationItem ScatterChartAnimationItem; + QTCOMMERCIALCHART_END_NAMESPACE #endif // SCATTERPRESENTER_H diff --git a/src/splinechart/splinechartitem.cpp b/src/splinechart/splinechartitem.cpp index 0a6a5dd..1b97f74 100644 --- a/src/splinechart/splinechartitem.cpp +++ b/src/splinechart/splinechartitem.cpp @@ -1,12 +1,16 @@ #include "splinechartitem_p.h" +#include "chartpresenter_p.h" #include QTCOMMERCIALCHART_BEGIN_NAMESPACE -SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsObject *parent) : +SplineChartItem::SplineChartItem(QSplineSeries* series, QGraphicsItem *parent) : XYChartItem(series, parent), m_series(series) { + setZValue(ChartPresenter::LineChartZValue); + QObject::connect(series,SIGNAL(updated()),this,SLOT(handleUpdated())); + handleUpdated(); } QRectF SplineChartItem::boundingRect() const @@ -26,6 +30,7 @@ QPointF SplineChartItem::calculateGeometryControlPoint(int index) const void SplineChartItem::setGeometry(QVector& points) { + if(points.size()==0) return; QPainterPath splinePath; @@ -44,11 +49,22 @@ void SplineChartItem::setGeometry(QVector& points) XYChartItem::setGeometry(points); } -void SplineChartItem::setPen(const QPen& pen) +void SplineChartItem::setLinePen(const QPen& pen) { m_pen = pen; + qDebug()<pointsVisible()); + setLinePen(m_series->pen()); + update(); +} + +//painter void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget) { @@ -56,6 +72,7 @@ void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o Q_UNUSED(option); painter->save(); painter->setClipRect(clipRect()); + painter->setPen(m_pen); painter->drawPath(m_path); const QVector points = XYChartItem::points(); @@ -79,6 +96,8 @@ void SplineChartItem::paint(QPainter *painter, const QStyleOptionGraphicsItem *o painter->restore(); } + + #include "moc_splinechartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/splinechart/splinechartitem_p.h b/src/splinechart/splinechartitem_p.h index 0890e82..01eaa5a 100644 --- a/src/splinechart/splinechartitem_p.h +++ b/src/splinechart/splinechartitem_p.h @@ -1,10 +1,10 @@ #ifndef SPLINECHARTITEM_P_H #define SPLINECHARTITEM_P_H -#include "chartitem_p.h" -#include #include "qsplineseries.h" #include "xychartitem_p.h" +#include "xychartanimationitem_p.h" +#include QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -12,16 +12,19 @@ class SplineChartItem : public XYChartItem { Q_OBJECT public: - SplineChartItem(QSplineSeries* series, QGraphicsObject *parent = 0); + SplineChartItem(QSplineSeries* series, QGraphicsItem *parent = 0); //from QGraphicsItem QRectF boundingRect() const; void paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget); QPainterPath shape() const; - void setPen(const QPen& pen); + void setLinePen(const QPen& pen); void setPointsVisible(bool visible); +public slots: + void handleUpdated(); + protected: void setGeometry(QVector& points); @@ -33,8 +36,12 @@ private: QPainterPath m_path; QRectF m_rect; QPen m_pen; + + template friend class XYChartAnimator; }; +typedef XYChartAnimationItem SplineChartAnimationItem; + QTCOMMERCIALCHART_END_NAMESPACE #endif // SPLINECHARTITEM_P_H diff --git a/src/xychart/xychart.pri b/src/xychart/xychart.pri index 9a339c4..97dca93 100644 --- a/src/xychart/xychart.pri +++ b/src/xychart/xychart.pri @@ -3,10 +3,12 @@ DEPENDPATH += $$PWD SOURCES += \ $$PWD/xychartitem.cpp \ - $$PWD/qxyseries.cpp + $$PWD/qxyseries.cpp PRIVATE_HEADERS += \ $$PWD/xychartitem_p.h \ + $$PWD/xychartanimationitem_p.h \ + $$PWD/xychartanimator_p.h PUBLIC_HEADERS += \ $$PWD/qxyseries.h \ No newline at end of file diff --git a/src/xychart/xychartanimationitem_p.h b/src/xychart/xychartanimationitem_p.h new file mode 100644 index 0000000..b7a75a3 --- /dev/null +++ b/src/xychart/xychartanimationitem_p.h @@ -0,0 +1,100 @@ +#ifndef XYCHARTANIMATIONITEM_P_H_ +#define XYCHARTANIMATIONITEM_P_H_ +#include "qchartglobal.h" +#include "xychartanimator_p.h" +#include +#include +#include + +Q_DECLARE_METATYPE(QVector) + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + + +const static int duration = 1000; + +template +class XYChartAnimationItem : public T { + +public: + XYChartAnimationItem(U *series, QGraphicsItem *parent = 0); + virtual ~XYChartAnimationItem(); + + void animationStarted(); + +protected: + virtual void updatePoints(QVector& newPoints); + virtual void updatePoint(QVector& newPoints); + +private: + XYChartAnimator *m_animation; + QVector m_points; + bool m_dirty; +}; + +template +XYChartAnimationItem::XYChartAnimationItem(U *series,QGraphicsItem *parent): +T(series,parent), +m_animation(new XYChartAnimator(this,this)), +m_dirty(false) +{ +} + +template +XYChartAnimationItem::~XYChartAnimationItem() +{ +} + +template +void XYChartAnimationItem::updatePoints(QVector& newPoints) +{ + QVector oldPoints = T::points(); + + if(newPoints.count()==0) return; + oldPoints.resize(newPoints.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(oldPoints)); + m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); + QTimer::singleShot(0,m_animation,SLOT(start())); + + m_points = newPoints; + m_dirty=false; +} + +template +void XYChartAnimationItem::updatePoint(QVector& newPoints) +{ + + if(m_animation->state()!=QAbstractAnimation::Stopped) { + m_animation->stop(); + m_dirty=true; + } + + if(m_dirty) { + m_points=newPoints; + m_dirty=false; + } + + m_animation->setDuration(duration); + m_animation->setEasingCurve(QEasingCurve::InOutBack); + m_animation->setKeyValueAt(0.0, qVariantFromValue(m_points)); + m_animation->setKeyValueAt(1.0, qVariantFromValue(newPoints)); + + QTimer::singleShot(0,m_animation,SLOT(start())); +} + +template +void XYChartAnimationItem::animationStarted() +{ + m_dirty=true; +} + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif diff --git a/src/xychart/xychartanimator_p.h b/src/xychart/xychartanimator_p.h new file mode 100644 index 0000000..0698671 --- /dev/null +++ b/src/xychart/xychartanimator_p.h @@ -0,0 +1,96 @@ +#ifndef XYCHARTANIMATOR_P_H_ +#define XYCHARTANIMATOR_P_H_ +#include "qchartglobal.h" +#include +#include + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +template +class XYChartAnimationItem; + + +template +class XYChartAnimator : public QVariantAnimation +{ +public: + enum Animation { LineDrawAnimation, MoveDownAnimation, MoveUpAnimation }; + XYChartAnimator(XYChartAnimationItem *item, QObject *parent = 0 ); + ~XYChartAnimator(); + +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: + XYChartAnimationItem *m_item; + Animation m_type; +}; + +template +XYChartAnimator::XYChartAnimator(XYChartAnimationItem *item , QObject *parent):QVariantAnimation(parent), + m_item(item), + m_type(MoveDownAnimation) +{ +} + +template +XYChartAnimator::~XYChartAnimator() +{ +} + +template +QVariant XYChartAnimator::interpolated(const QVariant &start, const QVariant & end, qreal progress ) const +{ + QVector startVector = qVariantValue >(start); + QVector endVector = qVariantValue >(end); + QVector result; + + switch(m_type) { + + case MoveDownAnimation: { + + Q_ASSERT(startVector.count() == endVector.count()); + for(int i =0;i< startVector.count();i++) { + qreal x = startVector[i].x() + ((endVector[i].x()- startVector[i].x()) * progress); + qreal y = startVector[i].y() + ((endVector[i].y()- startVector[i].y()) * progress); + result << QPointF(x,y); + } + + } + break; + case LineDrawAnimation:{ + for(int i =0;i< endVector.count()* qBound(0.0, progress, 1.0);i++) { + result << endVector[i]; + } + } + break; + default: + qWarning()<<"Unknow type of animation"; + break; + } + + return qVariantFromValue(result); +} + +template +void XYChartAnimator::updateCurrentValue (const QVariant & value ) +{ + QVector vector = qVariantValue >(value); + if(state()!=QAbstractAnimation::Stopped){ //workaround + m_item->setGeometry(vector); + } +} + +template +void XYChartAnimator::updateState(QAbstractAnimation::State newState, QAbstractAnimation::State oldState) +{ + Q_UNUSED(oldState) + if (newState==QAbstractAnimation::Running) m_item->animationStarted(); + QVariantAnimation::updateState(newState,oldState); +} + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif /* XYCHARTANIMATOR_P_H_ */ diff --git a/src/xychart/xychartitem.cpp b/src/xychart/xychartitem.cpp index fd2ed6f..70f312e 100644 --- a/src/xychart/xychartitem.cpp +++ b/src/xychart/xychartitem.cpp @@ -55,16 +55,14 @@ QVector XYChartItem::calculateGeometryPoints() const return points; } - void XYChartItem::updatePoints(QVector& points) { setGeometry(points); } -void XYChartItem::updatePoint(int index,QPointF& newPoint) +void XYChartItem::updatePoint(QVector& points) { - m_points.replace(index,newPoint); - setGeometry(m_points); + setGeometry(points); } void XYChartItem::setGeometry(QVector& points) @@ -89,7 +87,6 @@ void XYChartItem::handlePointRemoved(int index) { Q_ASSERT(indexcount()); Q_ASSERT(index>=0); - QPointF point = calculateGeometryPoint(index); QVector points = m_points; points.remove(index); @@ -102,7 +99,9 @@ void XYChartItem::handlePointReplaced(int index) Q_ASSERT(indexcount()); Q_ASSERT(index>=0); QPointF point = calculateGeometryPoint(index); - updatePoint(index,point); + QVector points = m_points; + m_points.replace(index,point); + updatePoint(points); update(); } diff --git a/src/xychart/xychartitem_p.h b/src/xychart/xychartitem_p.h index 9b1193d..7047492 100644 --- a/src/xychart/xychartitem_p.h +++ b/src/xychart/xychartitem_p.h @@ -29,8 +29,9 @@ public slots: protected: virtual void updatePoints(QVector& points); - virtual void updatePoint(int index,QPointF& newPoint); + virtual void updatePoint(QVector& points); virtual void setGeometry(QVector& points); + QPointF calculateGeometryPoint(const QPointF& point) const; QPointF calculateGeometryPoint(int index) const; QVector calculateGeometryPoints() const; @@ -48,6 +49,8 @@ private: QRectF m_clipRect; QVector m_points; + template friend class XYChartAnimator; + }; QTCOMMERCIALCHART_END_NAMESPACE