diff --git a/src/animations/chartanimator.cpp b/src/animations/chartanimator.cpp index 62270c2..8464def 100644 --- a/src/animations/chartanimator.cpp +++ b/src/animations/chartanimator.cpp @@ -251,18 +251,18 @@ void ChartAnimator::updateLayout(XYChartItem *item, QVector &oldPoints, QTimer::singleShot(0, animation, SLOT(start())); } -void ChartAnimator::addAnimation(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty) +void ChartAnimator::addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty) { PieAnimation *animation = static_cast(m_animations.value(item)); Q_ASSERT(animation); - animation->addSlice(slice, sliceData, isEmpty); + animation->addSlice(sliceItem, sliceData, isEmpty); } -void ChartAnimator::removeAnimation(PieChartItem *item, QPieSlice *slice) +void ChartAnimator::removeAnimation(PieChartItem *item, PieSliceItem *sliceItem) { PieAnimation *animation = static_cast(m_animations.value(item)); Q_ASSERT(animation); - animation->removeSlice(slice); + animation->removeSlice(sliceItem); } void ChartAnimator::updateLayout(PieChartItem *item, const PieLayout &layout) @@ -272,11 +272,11 @@ void ChartAnimator::updateLayout(PieChartItem *item, const PieLayout &layout) animation->updateValues(layout); } -void ChartAnimator::updateLayout(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData) +void ChartAnimator::updateLayout(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData) { PieAnimation *animation = static_cast(m_animations.value(item)); Q_ASSERT(animation); - animation->updateValue(slice, sliceData); + animation->updateValue(sliceItem, sliceData); } void ChartAnimator::updateLayout(BarChartItem *item, const QVector &oldLayout, const QVector &newLayout) diff --git a/src/animations/chartanimator_p.h b/src/animations/chartanimator_p.h index 3b976f0..5891b15 100644 --- a/src/animations/chartanimator_p.h +++ b/src/animations/chartanimator_p.h @@ -59,10 +59,10 @@ public: void updateLayout(SplineChartItem *item, QVector &oldPoints, QVector &newPoints, QVector &oldControlPoints, QVector &newContorlPoints, int index); void updateLayout(ChartAxis *item, QVector &layout); - void addAnimation(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty); - void removeAnimation(PieChartItem *item, QPieSlice *slice); + void addAnimation(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty); + void removeAnimation(PieChartItem *item, PieSliceItem *sliceItem); void updateLayout(PieChartItem *item, const PieLayout &layout); - void updateLayout(PieChartItem *item, QPieSlice *slice, const PieSliceData &sliceData); + void updateLayout(PieChartItem *item, PieSliceItem *sliceItem, const PieSliceData &sliceData); void updateLayout(BarChartItem *item, const QVector &oldLayout, const QVector &newLayout); diff --git a/src/animations/pieanimation.cpp b/src/animations/pieanimation.cpp index 92f5d9c..bcaf140 100644 --- a/src/animations/pieanimation.cpp +++ b/src/animations/pieanimation.cpp @@ -38,13 +38,13 @@ PieAnimation::~PieAnimation() void PieAnimation::updateValues(const PieLayout &newValues) { - foreach (QPieSlice *s, newValues.keys()) + foreach (PieSliceItem *s, newValues.keys()) updateValue(s, newValues.value(s)); } -void PieAnimation::updateValue(QPieSlice *slice, const PieSliceData &sliceData) +void PieAnimation::updateValue(PieSliceItem *sliceItem, const PieSliceData &sliceData) { - PieSliceAnimation *animation = m_animations.value(slice); + PieSliceAnimation *animation = m_animations.value(sliceItem); Q_ASSERT(animation); animation->stop(); @@ -55,10 +55,10 @@ void PieAnimation::updateValue(QPieSlice *slice, const PieSliceData &sliceData) QTimer::singleShot(0, animation, SLOT(start())); } -void PieAnimation::addSlice(QPieSlice *slice, const PieSliceData &sliceData, bool isEmpty) +void PieAnimation::addSlice(PieSliceItem *sliceItem, const PieSliceData &sliceData, bool isEmpty) { - PieSliceAnimation *animation = new PieSliceAnimation(m_item, slice); - m_animations.insert(slice, animation); + PieSliceAnimation *animation = new PieSliceAnimation(sliceItem); + m_animations.insert(sliceItem, animation); PieSliceData startValue = sliceData; startValue.m_radius = 0; @@ -74,15 +74,14 @@ void PieAnimation::addSlice(QPieSlice *slice, const PieSliceData &sliceData, boo QTimer::singleShot(0, animation, SLOT(start())); } -void PieAnimation::removeSlice(QPieSlice *slice) +void PieAnimation::removeSlice(PieSliceItem *sliceItem) { - PieSliceAnimation *animation = m_animations.value(slice); + PieSliceAnimation *animation = m_animations.value(sliceItem); Q_ASSERT(animation); animation->stop(); PieSliceData endValue = animation->currentSliceValue(); endValue.m_radius = 0; - // TODO: find the actual angle where this slice disappears endValue.m_startAngle = endValue.m_startAngle + endValue.m_angleSpan; endValue.m_angleSpan = 0; @@ -90,7 +89,10 @@ void PieAnimation::removeSlice(QPieSlice *slice) animation->setDuration(1000); animation->setEasingCurve(QEasingCurve::OutQuart); - connect(animation, SIGNAL(finished()), this, SLOT(destroySliceAnimationComplete())); + // PieSliceItem is the parent of PieSliceAnimation so the animation will be deleted as well.. + connect(animation, SIGNAL(finished()), sliceItem, SLOT(deleteLater())); + m_animations.remove(sliceItem); + QTimer::singleShot(0, animation, SLOT(start())); } @@ -99,14 +101,6 @@ void PieAnimation::updateCurrentValue(const QVariant &) // nothing to do... } -void PieAnimation::destroySliceAnimationComplete() -{ - PieSliceAnimation *animation = static_cast(sender()); - QPieSlice *slice = m_animations.key(animation); - m_item->destroySlice(slice); - delete m_animations.take(slice); -} - #include "moc_pieanimation_p.cpp" QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/pieanimation_p.h b/src/animations/pieanimation_p.h index c9f1796..fe05136 100644 --- a/src/animations/pieanimation_p.h +++ b/src/animations/pieanimation_p.h @@ -37,19 +37,16 @@ public: PieAnimation(PieChartItem *item); ~PieAnimation(); void updateValues(const PieLayout &newValues); - void updateValue(QPieSlice *slice, const PieSliceData &newValue); - void addSlice(QPieSlice *slice, const PieSliceData &endValue, bool isEmpty); - void removeSlice(QPieSlice *slice); + void updateValue(PieSliceItem *sliceItem, const PieSliceData &newValue); + void addSlice(PieSliceItem *sliceItem, const PieSliceData &endValue, bool isEmpty); + void removeSlice(PieSliceItem *sliceItem); public: // from QVariantAnimation void updateCurrentValue(const QVariant &value); -public Q_SLOTS: - void destroySliceAnimationComplete(); - private: PieChartItem *m_item; - QHash m_animations; + QHash m_animations; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/animations/piesliceanimation.cpp b/src/animations/piesliceanimation.cpp index e05f73b..3cb0ae1 100644 --- a/src/animations/piesliceanimation.cpp +++ b/src/animations/piesliceanimation.cpp @@ -20,7 +20,6 @@ #include "piesliceanimation_p.h" #include "piechartitem_p.h" -#include "qpieslice.h" Q_DECLARE_METATYPE(QTCOMMERCIALCHART_NAMESPACE::PieSliceData) @@ -58,10 +57,9 @@ QBrush linearPos(QBrush start, QBrush end, qreal pos) return end; } -PieSliceAnimation::PieSliceAnimation(PieChartItem *item, QPieSlice *slice) - :QVariantAnimation(item), - m_item(item), - m_slice(slice) +PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem) + :QVariantAnimation(sliceItem), + m_sliceItem(sliceItem) { } @@ -120,7 +118,9 @@ void PieSliceAnimation::updateCurrentValue(const QVariant &value) { if (state() != QAbstractAnimation::Stopped) { //workaround m_currentValue = qVariantValue(value); - m_item->setLayout(m_slice, m_currentValue); + m_sliceItem->setSliceData(m_currentValue); + m_sliceItem->updateGeometry(); + m_sliceItem->update(); } } diff --git a/src/animations/piesliceanimation_p.h b/src/animations/piesliceanimation_p.h index c73bea6..4465213 100644 --- a/src/animations/piesliceanimation_p.h +++ b/src/animations/piesliceanimation_p.h @@ -21,18 +21,17 @@ #ifndef PIESLICEANIMATION_P_H #define PIESLICEANIMATION_P_H -#include "piechartitem_p.h" #include +#include "piesliceitem_p.h" QTCOMMERCIALCHART_BEGIN_NAMESPACE class PieChartItem; -class QPieSlice; class PieSliceAnimation : public QVariantAnimation { public: - PieSliceAnimation(PieChartItem *item, QPieSlice *slice); + PieSliceAnimation(PieSliceItem *sliceItem); ~PieSliceAnimation(); void setValue(const PieSliceData &startValue, const PieSliceData &endValue); void updateValue(const PieSliceData &endValue); @@ -43,8 +42,7 @@ protected: void updateCurrentValue(const QVariant &value); private: - PieChartItem *m_item; - QPieSlice *m_slice; + PieSliceItem *m_sliceItem; PieSliceData m_currentValue; }; diff --git a/src/piechart/piechartitem.cpp b/src/piechart/piechartitem.cpp index e7d5e00..23c2e39 100644 --- a/src/piechart/piechartitem.cpp +++ b/src/piechart/piechartitem.cpp @@ -84,9 +84,9 @@ void PieChartItem::handleSlicesAdded(QList slices) PieSliceData data = sliceData(s); if (animator()) - animator()->addAnimation(this, s, data, isEmpty); + animator()->addAnimation(this, item, data, isEmpty); else - setLayout(s, data); + setLayout(item, data); } } @@ -94,11 +94,16 @@ void PieChartItem::handleSlicesRemoved(QList slices) { presenter()->chartTheme()->decorate(m_series, presenter()->dataSet()->seriesIndex(m_series)); - foreach (QPieSlice *s, slices) { + foreach (QPieSlice *slice, slices) { + + PieSliceItem *item = m_slices.value(slice); + Q_ASSERT(item); + m_slices.remove(slice); + if (animator()) - animator()->removeAnimation(this, s); + animator()->removeAnimation(this, item); // animator deletes the PieSliceItem else - destroySlice(s); + delete item; } } @@ -114,7 +119,7 @@ void PieChartItem::handleSliceChanged() QPieSlice* slice = qobject_cast(sender()); Q_ASSERT(m_slices.contains(slice)); PieSliceData data = sliceData(slice); - updateLayout(slice, data); + updateLayout(m_slices.value(slice), data); update(); } @@ -160,7 +165,7 @@ PieLayout PieChartItem::calculateLayout() PieLayout layout; foreach (QPieSlice* s, m_series->slices()) { if (m_slices.contains(s)) // calculate layout only for those slices that are already visible - layout.insert(s, sliceData(s)); + layout.insert(m_slices.value(s), sliceData(s)); } return layout; } @@ -173,38 +178,29 @@ void PieChartItem::applyLayout(const PieLayout &layout) setLayout(layout); } -void PieChartItem::updateLayout(QPieSlice *slice, const PieSliceData &sliceData) +void PieChartItem::updateLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData) { if (animator()) - animator()->updateLayout(this, slice, sliceData); + animator()->updateLayout(this, sliceItem, sliceData); else - setLayout(slice, sliceData); + setLayout(sliceItem, sliceData); } void PieChartItem::setLayout(const PieLayout &layout) { - foreach (QPieSlice *slice, layout.keys()) { - PieSliceItem *item = m_slices.value(slice); - Q_ASSERT(item); - item->setSliceData(layout.value(slice)); + foreach (PieSliceItem *item, layout.keys()) { + item->setSliceData(layout.value(item)); item->updateGeometry(); item->update(); } } -void PieChartItem::setLayout(QPieSlice *slice, const PieSliceData &sliceData) -{ - // find slice - PieSliceItem *item = m_slices.value(slice); - Q_ASSERT(item); - item->setSliceData(sliceData); - item->updateGeometry(); - item->update(); -} - -void PieChartItem::destroySlice(QPieSlice *slice) +void PieChartItem::setLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData) { - delete m_slices.take(slice); + Q_ASSERT(sliceItem); + sliceItem->setSliceData(sliceData); + sliceItem->updateGeometry(); + sliceItem->update(); } #include "moc_piechartitem_p.cpp" diff --git a/src/piechart/piechartitem_p.h b/src/piechart/piechartitem_p.h index 21203f6..35e9e9f 100644 --- a/src/piechart/piechartitem_p.h +++ b/src/piechart/piechartitem_p.h @@ -30,7 +30,7 @@ QTCOMMERCIALCHART_BEGIN_NAMESPACE class QPieSlice; class ChartPresenter; -typedef QHash PieLayout; +typedef QHash PieLayout; class PieChartItem : public ChartItem { @@ -59,10 +59,9 @@ public: PieSliceData sliceData(QPieSlice *slice); PieLayout calculateLayout(); void applyLayout(const PieLayout &layout); - void updateLayout(QPieSlice *slice, const PieSliceData &sliceData); + void updateLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData); void setLayout(const PieLayout &layout); - void setLayout(QPieSlice *slice, const PieSliceData &sliceData); - void destroySlice(QPieSlice *slice); + void setLayout(PieSliceItem *sliceItem, const PieSliceData &sliceData); private: friend class PieSliceItem;