From 8f0e18a7e02cc4f9018d95379efbfb9cf167dd56 2013-06-06 10:40:45 From: Miikka Heikkinen Date: 2013-06-06 10:40:45 Subject: [PATCH] Fix resetting animation options mid-animation. Setting animation options while animations were running could cause crash if layout in old animation didn't match the current layout, as animation options setting didn't destroy or stop old animations when it created new ones. Change-Id: If45ee8daf3facd49ce7dd127bb37678a31e6a2d3 Reviewed-by: Tomi Korpipää --- diff --git a/src/animations/animations.pri b/src/animations/animations.pri index 1a0870f..28b6db1 100644 --- a/src/animations/animations.pri +++ b/src/animations/animations.pri @@ -10,7 +10,8 @@ SOURCES += \ $$PWD/baranimation.cpp \ $$PWD/scatteranimation.cpp \ $$PWD/boxplotanimation.cpp \ - $$PWD/boxwhiskersanimation.cpp + $$PWD/boxwhiskersanimation.cpp \ + $$PWD/chartanimation.cpp PRIVATE_HEADERS += \ $$PWD/axisanimation_p.h \ diff --git a/src/animations/chartanimation.cpp b/src/animations/chartanimation.cpp new file mode 100644 index 0000000..042ffc9 --- /dev/null +++ b/src/animations/chartanimation.cpp @@ -0,0 +1,46 @@ +/**************************************************************************** +** +** Copyright (C) 2013 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 "chartanimation_p.h" + +QTCOMMERCIALCHART_BEGIN_NAMESPACE + +ChartAnimation::ChartAnimation(QObject *parent) : + QVariantAnimation(parent), + m_destructing(false) +{ +} + +void ChartAnimation::stopAndDestroyLater() +{ + m_destructing = true; + stop(); + deleteLater(); +} + +void ChartAnimation::startChartAnimation() +{ + if (!m_destructing) + start(); +} + +QTCOMMERCIALCHART_END_NAMESPACE + + diff --git a/src/animations/chartanimation_p.h b/src/animations/chartanimation_p.h index 1334bd5..880ba03 100644 --- a/src/animations/chartanimation_p.h +++ b/src/animations/chartanimation_p.h @@ -41,7 +41,15 @@ class ChartAnimation: public QVariantAnimation { Q_OBJECT public: - ChartAnimation(QObject *parent = 0): QVariantAnimation(parent) {}; + ChartAnimation(QObject *parent = 0); + + void stopAndDestroyLater(); + +public Q_SLOTS: + void startChartAnimation(); + +protected: + bool m_destructing; }; QTCOMMERCIALCHART_END_NAMESPACE diff --git a/src/areachart/qareaseries.cpp b/src/areachart/qareaseries.cpp index 3ef2d1b..ef1689a 100644 --- a/src/areachart/qareaseries.cpp +++ b/src/areachart/qareaseries.cpp @@ -395,11 +395,17 @@ void QAreaSeriesPrivate::initializeAnimations(QChart::AnimationOptions options) { Q_Q(QAreaSeries); AreaChartItem *area = static_cast(m_item.data()); + + if (area->upperLineItem()->animation()) + area->upperLineItem()->animation()->stopAndDestroyLater(); + if (q->lowerSeries() && area->lowerLineItem()->animation()) + area->lowerLineItem()->animation()->stopAndDestroyLater(); + if (options.testFlag(QChart::SeriesAnimations)) { area->upperLineItem()->setAnimation(new XYAnimation(area->upperLineItem())); if (q->lowerSeries()) - area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem())); - }else{ + area->lowerLineItem()->setAnimation(new XYAnimation(area->lowerLineItem())); + } else { area->upperLineItem()->setAnimation(0); if (q->lowerSeries()) area->lowerLineItem()->setAnimation(0); diff --git a/src/axis/qabstractaxis.cpp b/src/axis/qabstractaxis.cpp index f214f55..d2ea1a8 100644 --- a/src/axis/qabstractaxis.cpp +++ b/src/axis/qabstractaxis.cpp @@ -997,11 +997,13 @@ void QAbstractAxisPrivate::initializeAnimations(QChart::AnimationOptions options { ChartAxisElement *axis = m_item.data(); Q_ASSERT(axis); - if (options.testFlag(QChart::GridAxisAnimations)) { + if (axis->animation()) + axis->animation()->stopAndDestroyLater(); + + if (options.testFlag(QChart::GridAxisAnimations)) axis->setAnimation(new AxisAnimation(axis)); - } else { + else axis->setAnimation(0); - } } diff --git a/src/barchart/qabstractbarseries.cpp b/src/barchart/qabstractbarseries.cpp index 7e4eeb9..1fdd89b 100644 --- a/src/barchart/qabstractbarseries.cpp +++ b/src/barchart/qabstractbarseries.cpp @@ -887,11 +887,13 @@ void QAbstractBarSeriesPrivate::initializeAnimations(QChart::AnimationOptions op { AbstractBarChartItem *bar = static_cast(m_item.data()); Q_ASSERT(bar); - if (options.testFlag(QChart::SeriesAnimations)) { + if (bar->animation()) + bar->animation()->stopAndDestroyLater(); + + if (options.testFlag(QChart::SeriesAnimations)) bar->setAnimation(new BarAnimation(bar)); - }else{ + else bar->setAnimation(0); - } QAbstractSeriesPrivate::initializeAnimations(options); } diff --git a/src/boxplotchart/qboxplotseries.cpp b/src/boxplotchart/qboxplotseries.cpp index fca2c45..3bc62cc 100644 --- a/src/boxplotchart/qboxplotseries.cpp +++ b/src/boxplotchart/qboxplotseries.cpp @@ -484,10 +484,13 @@ void QBoxPlotSeriesPrivate::initializeAnimations(QChart::AnimationOptions option { BoxPlotChartItem *item = static_cast(m_item.data()); Q_ASSERT(item); + if (item->animation()) + item->animation()->stopAndDestroyLater(); + if (options.testFlag(QChart::SeriesAnimations)) item->setAnimation(new BoxPlotAnimation(item)); else - item->setAnimation((BoxPlotAnimation *)0); + item->setAnimation(0); QAbstractSeriesPrivate::initializeAnimations(options); } diff --git a/src/chartpresenter.cpp b/src/chartpresenter.cpp index 947557b..82f7a42 100644 --- a/src/chartpresenter.cpp +++ b/src/chartpresenter.cpp @@ -126,14 +126,17 @@ void ChartPresenter::handleSeriesRemoved(QAbstractSeries *series) void ChartPresenter::setAnimationOptions(QChart::AnimationOptions options) { if (m_options != options) { + QChart::AnimationOptions oldOptions = m_options; m_options = options; - - foreach(QAbstractSeries* series, m_series){ - series->d_ptr->initializeAnimations(m_options); + if (options.testFlag(QChart::SeriesAnimations) != oldOptions.testFlag(QChart::SeriesAnimations)) { + foreach (QAbstractSeries *series, m_series) + series->d_ptr->initializeAnimations(m_options); } - foreach(QAbstractAxis* axis, m_axes){ - axis->d_ptr->initializeAnimations(m_options); + if (options.testFlag(QChart::GridAxisAnimations) != oldOptions.testFlag(QChart::GridAxisAnimations)) { + foreach (QAbstractAxis *axis, m_axes) + axis->d_ptr->initializeAnimations(m_options); } + m_layout->invalidate(); // So that existing animations don't just stop halfway } } @@ -182,21 +185,10 @@ void ChartPresenter::createTitleItem() } } - -void ChartPresenter::handleAnimationFinished() -{ - m_animations.removeAll(qobject_cast(sender())); - if (m_animations.empty()) - emit animationsFinished(); -} - void ChartPresenter::startAnimation(ChartAnimation *animation) { - if (animation->state() != QAbstractAnimation::Stopped) animation->stop(); - QObject::connect(animation, SIGNAL(finished()), this, SLOT(handleAnimationFinished()), Qt::UniqueConnection); - if (!m_animations.isEmpty()) - m_animations.append(animation); - QTimer::singleShot(0, animation, SLOT(start())); + animation->stop(); + QTimer::singleShot(0, animation, SLOT(startChartAnimation())); } void ChartPresenter::setBackgroundBrush(const QBrush &brush) diff --git a/src/chartpresenter_p.h b/src/chartpresenter_p.h index 49ba55d..9230cbf 100644 --- a/src/chartpresenter_p.h +++ b/src/chartpresenter_p.h @@ -159,12 +159,6 @@ public Q_SLOTS: void handleAxisAdded(QAbstractAxis *axis); void handleAxisRemoved(QAbstractAxis *axis); -private Q_SLOTS: - void handleAnimationFinished(); - -Q_SIGNALS: - void animationsFinished(); - private: QChart *m_chart; QList m_chartItems; @@ -174,7 +168,6 @@ private: QChart::AnimationOptions m_options; State m_state; QPointF m_statePoint; - QList m_animations; AbstractChartLayout *m_layout; ChartBackground *m_background; QAbstractGraphicsShapeItem *m_plotAreaBackground; diff --git a/src/piechart/qpieseries.cpp b/src/piechart/qpieseries.cpp index a566757..e7df1c4 100644 --- a/src/piechart/qpieseries.cpp +++ b/src/piechart/qpieseries.cpp @@ -863,11 +863,13 @@ void QPieSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::Animatio { PieChartItem *item = static_cast(m_item.data()); Q_ASSERT(item); - if (options.testFlag(QChart::SeriesAnimations)) { + if (item->animation()) + item->animation()->stopAndDestroyLater(); + + if (options.testFlag(QChart::SeriesAnimations)) item->setAnimation(new PieAnimation(item)); - }else{ + else item->setAnimation(0); - } QAbstractSeriesPrivate::initializeAnimations(options); } diff --git a/src/scatterchart/qscatterseries.cpp b/src/scatterchart/qscatterseries.cpp index 9b3b2ce..29113c0 100644 --- a/src/scatterchart/qscatterseries.cpp +++ b/src/scatterchart/qscatterseries.cpp @@ -288,6 +288,9 @@ void QScatterSeriesPrivate::initializeAnimations(QChart::AnimationOptions option ScatterChartItem *item = static_cast(m_item.data()); Q_ASSERT(item); + if (item->animation()) + item->animation()->stopAndDestroyLater(); + if (options.testFlag(QChart::SeriesAnimations)) item->setAnimation(new ScatterAnimation(item)); else diff --git a/src/splinechart/qsplineseries.cpp b/src/splinechart/qsplineseries.cpp index 7c55b10..a76d71a 100644 --- a/src/splinechart/qsplineseries.cpp +++ b/src/splinechart/qsplineseries.cpp @@ -139,11 +139,13 @@ void QSplineSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::Anima { SplineChartItem *item = static_cast(m_item.data()); Q_ASSERT(item); - if (options.testFlag(QChart::SeriesAnimations)) { + if (item->animation()) + item->animation()->stopAndDestroyLater(); + + if (options.testFlag(QChart::SeriesAnimations)) item->setAnimation(new SplineAnimation(item)); - }else{ + else item->setAnimation(0); - } QAbstractSeriesPrivate::initializeAnimations(options); } diff --git a/src/xychart/qxyseries.cpp b/src/xychart/qxyseries.cpp index be43148..a317187 100644 --- a/src/xychart/qxyseries.cpp +++ b/src/xychart/qxyseries.cpp @@ -582,11 +582,13 @@ void QXYSeriesPrivate::initializeAnimations(QtCommercialChart::QChart::Animation { XYChart *item = static_cast(m_item.data()); Q_ASSERT(item); - if (options.testFlag(QChart::SeriesAnimations)) { + if (item->animation()) + item->animation()->stopAndDestroyLater(); + + if (options.testFlag(QChart::SeriesAnimations)) item->setAnimation(new XYAnimation(item)); - }else{ + else item->setAnimation(0); - } QAbstractSeriesPrivate::initializeAnimations(options); }