diff --git a/src/animations/animations.pri b/src/animations/animations.pri index c167dec..7558500 100644 --- a/src/animations/animations.pri +++ b/src/animations/animations.pri @@ -8,7 +8,9 @@ SOURCES += \ $$PWD/pieanimation.cpp \ $$PWD/piesliceanimation.cpp \ $$PWD/splineanimation.cpp \ - $$PWD/baranimation.cpp + $$PWD/baranimation.cpp \ + $$PWD/stackedbaranimation.cpp \ + $$PWD/percentbaranimation.cpp @@ -20,5 +22,6 @@ PRIVATE_HEADERS += \ $$PWD/pieanimation_p.h \ $$PWD/piesliceanimation_p.h \ $$PWD/splineanimation_p.h \ - $$PWD/baranimation_p.h - + $$PWD/baranimation_p.h \ + $$PWD/stackedbaranimation_p.h \ + $$PWD/percentbaranimation_p.h diff --git a/src/animations/baranimation.cpp b/src/animations/baranimation.cpp index 7882fe8..6e5bbbe 100644 --- a/src/animations/baranimation.cpp +++ b/src/animations/baranimation.cpp @@ -20,12 +20,9 @@ #include "baranimation_p.h" #include "barchartitem_p.h" -#include #include Q_DECLARE_METATYPE(QVector) -//Q_DECLARE_METATYPE(BarLayout) // TODO? - QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -33,6 +30,7 @@ BarAnimation::BarAnimation(BarChartItem *item) :ChartAnimation(item), m_item(item) { + setEasingCurve(QEasingCurve::OutQuart); } BarAnimation::~BarAnimation() @@ -53,9 +51,7 @@ QVariant BarAnimation::interpolated(const QVariant &from, const QVariant &to, qr qreal x = endVector[i].topLeft().x(); qreal y = endVector[i].topLeft().y() + endVector[i].height() - h; - QPointF topLeft(x,y); - QSizeF size(w,h); - QRectF value(topLeft,size); + QRectF value(x,y,w,h); result << value; } return qVariantFromValue(result); @@ -67,6 +63,14 @@ void BarAnimation::updateCurrentValue(const QVariant &value) m_item->setLayout(layout); } +void BarAnimation::updateLayout(const QVector &oldLayout, const QVector &newLayout) +{ + setDuration(ChartAnimationDuration); + setKeyValueAt(0.0, qVariantFromValue(oldLayout)); + setKeyValueAt(1.0, qVariantFromValue(newLayout)); + QTimer::singleShot(0, this, SLOT(start())); +} + #include "moc_baranimation_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/baranimation_p.h b/src/animations/baranimation_p.h index cf82425..a1df554 100644 --- a/src/animations/baranimation_p.h +++ b/src/animations/baranimation_p.h @@ -51,11 +51,10 @@ public: // from QVariantAnimation virtual QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const; virtual void updateCurrentValue(const QVariant &value); -public Q_SLOTS: + void updateLayout(const QVector &oldLayout, const QVector &newLayout); private: BarChartItem *m_item; - QHash m_animations; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/chartanimator.cpp b/src/animations/chartanimator.cpp index 7421124..276ca61 100644 --- a/src/animations/chartanimator.cpp +++ b/src/animations/chartanimator.cpp @@ -26,6 +26,10 @@ #include "pieanimation_p.h" #include "baranimation_p.h" #include "barchartitem_p.h" +#include "stackedbaranimation_p.h" +#include "stackedbarchartitem_p.h" +#include "percentbaranimation_p.h" +#include "percentbarchartitem_p.h" #include "areachartitem_p.h" #include "splinechartitem_p.h" #include "scatterchartitem_p.h" @@ -59,6 +63,7 @@ void ChartAnimator::addAnimation(PieChartItem *item) void ChartAnimator::addAnimation(BarChartItem *item) { + // This can handle also GroupedBarChartItem because bars are side by side ChartAnimation *animation = m_animations.value(item); if (!animation) { @@ -69,6 +74,29 @@ void ChartAnimator::addAnimation(BarChartItem *item) item->setAnimator(this); } +void ChartAnimator::addAnimation(StackedBarChartItem *item) +{ + ChartAnimation *animation = m_animations.value(item); + + if (!animation) { + animation = new StackedBarAnimation(item); + m_animations.insert(item, animation); + } + + item->setAnimator(this); +} + +void ChartAnimator::addAnimation(PercentBarChartItem *item) +{ + ChartAnimation *animation = m_animations.value(item); + + if (!animation) { + animation = new PercentBarAnimation(item); + m_animations.insert(item, animation); + } + + item->setAnimator(this); +} void ChartAnimator::removeAnimation(Chart *item) { @@ -100,19 +128,41 @@ void ChartAnimator::updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, void ChartAnimator::updateLayout(BarChartItem *item, const QVector &oldLayout, const QVector &newLayout) { BarAnimation *animation = static_cast(m_animations.value(item)); - m_animations.remove(item); if (animation) { + m_animations.remove(item); animation->deleteLater(); animation = 0; + addAnimation(item); } - addAnimation(item); animation = static_cast(m_animations.value(item)); - animation->setDuration(ChartAnimationDuration); - animation->setKeyValueAt(0.0, qVariantFromValue(oldLayout)); - animation->setKeyValueAt(1.0, qVariantFromValue(newLayout)); - QTimer::singleShot(0, animation, SLOT(start())); + animation->updateLayout(oldLayout,newLayout); +} + +void ChartAnimator::updateLayout(StackedBarChartItem *item, const QVector &oldLayout, const QVector &newLayout) +{ + StackedBarAnimation *animation = static_cast(m_animations.value(item)); + if (animation) { + m_animations.remove(item); + animation->deleteLater(); + animation = 0; + addAnimation(item); + } + animation = static_cast(m_animations.value(item)); + animation->updateLayout(oldLayout,newLayout); } +void ChartAnimator::updateLayout(PercentBarChartItem *item, const QVector &oldLayout, const QVector &newLayout) +{ + PercentBarAnimation *animation = static_cast(m_animations.value(item)); + if (animation) { + m_animations.remove(item); + animation->deleteLater(); + animation = 0; + addAnimation(item); + } + animation = static_cast(m_animations.value(item)); + animation->updateLayout(oldLayout,newLayout); +} #include "moc_chartanimator_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/chartanimator_p.h b/src/animations/chartanimator_p.h index a6093dc..9caf13a 100644 --- a/src/animations/chartanimator_p.h +++ b/src/animations/chartanimator_p.h @@ -55,7 +55,9 @@ public: virtual ~ChartAnimator(); void addAnimation(PieChartItem *item); - void addAnimation(BarChartItem *item); + void addAnimation(BarChartItem *item); // This can handle also grouped bar chart. + void addAnimation(StackedBarChartItem *item); + void addAnimation(PercentBarChartItem *item); void removeAnimation(Chart *item); void addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty); @@ -63,6 +65,8 @@ public: void updateAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData); void updateLayout(BarChartItem *item, const QVector &oldLayout, const QVector &newLayout); + void updateLayout(StackedBarChartItem *item, const QVector &oldLayout, const QVector &newLayout); + void updateLayout(PercentBarChartItem *item, const QVector &oldLayout, const QVector &newLayout); private: QMap m_animations; diff --git a/src/animations/percentbaranimation.cpp b/src/animations/percentbaranimation.cpp new file mode 100644 index 0000000..a4cc481 --- /dev/null +++ b/src/animations/percentbaranimation.cpp @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "percentbaranimation_p.h" +#include "percentbarchartitem_p.h" +#include + +Q_DECLARE_METATYPE(QVector) + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +PercentBarAnimation::PercentBarAnimation(BarChartItem *item) + :ChartAnimation(item), + m_item(item) +{ + setEasingCurve(QEasingCurve::OutQuart); +} + +PercentBarAnimation::~PercentBarAnimation() +{ +} + +QVariant PercentBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const +{ + QVector startVector = qVariantValue >(from); + QVector endVector = qVariantValue >(to); + QVector result; + + Q_ASSERT(startVector.count() == endVector.count()); + + qreal zeroPos = ((m_item->geometry().height() + m_item->geometry().y()) * (1 - progress)); + + for(int i = 0; i < startVector.count(); i++) { + qreal w = endVector[i].width(); + qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress); + qreal x = endVector[i].topLeft().x(); + qreal y = startVector[i].topLeft().y() + ((endVector[i].topLeft().y() - startVector[i].topLeft().y()) * progress) + + zeroPos; + + QRectF value(x,y,w,h); + result << value; + } + return qVariantFromValue(result); +} + +void PercentBarAnimation::updateCurrentValue(const QVariant &value) +{ + QVector layout = qVariantValue >(value); + m_item->setLayout(layout); +} + +void PercentBarAnimation::updateLayout(const QVector &oldLayout, const QVector &newLayout) +{ + setDuration(ChartAnimationDuration); + setKeyValueAt(0.0, qVariantFromValue(oldLayout)); + setKeyValueAt(1.0, qVariantFromValue(newLayout)); + QTimer::singleShot(0, this, SLOT(start())); +} + +#include "moc_percentbaranimation_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/percentbaranimation_p.h b/src/animations/percentbaranimation_p.h new file mode 100644 index 0000000..1f06675 --- /dev/null +++ b/src/animations/percentbaranimation_p.h @@ -0,0 +1,43 @@ +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef PERCENTBARANIMATION_P_H +#define PERCENTBARANIMATION_P_H + +#include "chartanimation_p.h" +#include "barchartitem_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class PercentBarChartItem; +class QBarSet; +class BarSetAnimation; + +class PercentBarAnimation : public ChartAnimation +{ + Q_OBJECT +public: + PercentBarAnimation(BarChartItem *item); + ~PercentBarAnimation(); + +public: + virtual QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const; + virtual void updateCurrentValue(const QVariant &value); + + void updateLayout(const QVector &oldLayout, const QVector &newLayout); + +private: + BarChartItem *m_item; + QHash m_animations; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // PERCENTBARANIMATION_P_H diff --git a/src/animations/stackedbaranimation.cpp b/src/animations/stackedbaranimation.cpp new file mode 100644 index 0000000..a08fb9c --- /dev/null +++ b/src/animations/stackedbaranimation.cpp @@ -0,0 +1,79 @@ +/**************************************************************************** +** +** 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$ +** +****************************************************************************/ + +#include "stackedbaranimation_p.h" +#include "stackedbarchartitem_p.h" +#include + +Q_DECLARE_METATYPE(QVector) + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +StackedBarAnimation::StackedBarAnimation(BarChartItem *item) + :ChartAnimation(item), + m_item(item) +{ + setEasingCurve(QEasingCurve::OutQuart); +} + +StackedBarAnimation::~StackedBarAnimation() +{ +} + +QVariant StackedBarAnimation::interpolated(const QVariant &from, const QVariant &to, qreal progress) const +{ + QVector startVector = qVariantValue >(from); + QVector endVector = qVariantValue >(to); + QVector result; + + Q_ASSERT(startVector.count() == endVector.count()); + + qreal zeroPos = ((m_item->geometry().height() + m_item->geometry().y()) * (1 - progress)); + + for(int i = 0; i < startVector.count(); i++) { + qreal w = endVector[i].width(); + qreal h = startVector[i].height() + ((endVector[i].height() - startVector[i].height()) * progress); + qreal x = endVector[i].topLeft().x(); + qreal y = startVector[i].topLeft().y() + ((endVector[i].topLeft().y() - startVector[i].topLeft().y()) * progress) + + zeroPos; + + QRectF value(x,y,w,h); + result << value; + } + return qVariantFromValue(result); +} + +void StackedBarAnimation::updateCurrentValue(const QVariant &value) +{ + QVector layout = qVariantValue >(value); + m_item->setLayout(layout); +} + + +void StackedBarAnimation::updateLayout(const QVector &oldLayout, const QVector &newLayout) +{ + setDuration(ChartAnimationDuration); + setKeyValueAt(0.0, qVariantFromValue(oldLayout)); + setKeyValueAt(1.0, qVariantFromValue(newLayout)); + QTimer::singleShot(0, this, SLOT(start())); +} +#include "moc_stackedbaranimation_p.cpp" + +QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/stackedbaranimation_p.h b/src/animations/stackedbaranimation_p.h new file mode 100644 index 0000000..dd6a45b --- /dev/null +++ b/src/animations/stackedbaranimation_p.h @@ -0,0 +1,43 @@ +// W A R N I N G +// ------------- +// +// This file is not part of the QtCommercial Chart API. It exists purely as an +// implementation detail. This header file may change from version to +// version without notice, or even be removed. +// +// We mean it. + +#ifndef STACKEDBARANIMATION_P_H +#define STACKEDBARANIMATION_P_H + +#include "chartanimation_p.h" +#include "barchartitem_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +class StackedBarChartItem; +class QBarSet; +class BarSetAnimation; + +class StackedBarAnimation : public ChartAnimation +{ + Q_OBJECT +public: + StackedBarAnimation(BarChartItem *item); + ~StackedBarAnimation(); + +public: + virtual QVariant interpolated(const QVariant &from, const QVariant &to, qreal progress) const; + virtual void updateCurrentValue(const QVariant &value); + + void updateLayout(const QVector &oldLayout, const QVector &newLayout); + +private: + BarChartItem *m_item; + QHash m_animations; + +}; + +QTCOMMERCIALCHART_END_NAMESPACE + +#endif // STACKEDBARANIMATION_P_H diff --git a/src/barchart/bar.cpp b/src/barchart/bar.cpp index b7235ab..f893017 100644 --- a/src/barchart/bar.cpp +++ b/src/barchart/bar.cpp @@ -19,7 +19,6 @@ ****************************************************************************/ #include "bar_p.h" -#include #include #include diff --git a/src/barchart/barchartitem_p.h b/src/barchart/barchartitem_p.h index de25a00..55f8759 100644 --- a/src/barchart/barchartitem_p.h +++ b/src/barchart/barchartitem_p.h @@ -54,7 +54,7 @@ public: QRectF boundingRect() const; virtual QVector calculateLayout(); - void applyLayout(const QVector &layout); + virtual void applyLayout(const QVector &layout); void setLayout(const QVector &layout); void updateLayout(const QVector &layout); diff --git a/src/barchart/percentbarchartitem.cpp b/src/barchart/percentbarchartitem.cpp index 4aa52bf..a4d8ff6 100644 --- a/src/barchart/percentbarchartitem.cpp +++ b/src/barchart/percentbarchartitem.cpp @@ -22,7 +22,7 @@ #include "bar_p.h" #include "qbarseries_p.h" #include "qbarset.h" -#include +#include "chartanimator_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -92,6 +92,15 @@ QVector PercentBarChartItem::calculateLayout() return layout; } +void PercentBarChartItem::applyLayout(const QVector &layout) +{ + if (animator()) { + animator()->updateLayout(this, m_layout, layout); + } else { + setLayout(layout); + update(); + } +} #include "moc_percentbarchartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/percentbarchartitem_p.h b/src/barchart/percentbarchartitem_p.h index 5c5fe51..494a168 100644 --- a/src/barchart/percentbarchartitem_p.h +++ b/src/barchart/percentbarchartitem_p.h @@ -46,6 +46,7 @@ public: private: virtual QVector calculateLayout(); + virtual void applyLayout(const QVector &layout); }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/stackedbarchartitem.cpp b/src/barchart/stackedbarchartitem.cpp index 15a7c9f..fb0c5e7 100644 --- a/src/barchart/stackedbarchartitem.cpp +++ b/src/barchart/stackedbarchartitem.cpp @@ -23,6 +23,7 @@ #include "qbarset_p.h" #include "qbarseries_p.h" #include "qbarset.h" +#include "chartanimator_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE @@ -85,6 +86,16 @@ QVector StackedBarChartItem::calculateLayout() return layout; } +void StackedBarChartItem::applyLayout(const QVector &layout) +{ + if (animator()) { + animator()->updateLayout(this, m_layout, layout); + } else { + setLayout(layout); + update(); + } +} + #include "moc_stackedbarchartitem_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/barchart/stackedbarchartitem_p.h b/src/barchart/stackedbarchartitem_p.h index 7c255de..15b54b9 100644 --- a/src/barchart/stackedbarchartitem_p.h +++ b/src/barchart/stackedbarchartitem_p.h @@ -45,6 +45,7 @@ public: private: virtual QVector calculateLayout(); + virtual void applyLayout(const QVector &layout); }; QTCOMMERCIALCHART_END_NAMESPACE