1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include <private/piesliceanimation_p.h>
5#include <private/piechartitem_p.h>
6
7
8QT_BEGIN_NAMESPACE
9
10qreal linearPos(qreal start, qreal end, qreal pos)
11{
12 return start + ((end - start) * pos);
13}
14
15QPointF linearPos(QPointF start, QPointF end, qreal pos)
16{
17 qreal x = linearPos(start: start.x(), end: end.x(), pos);
18 qreal y = linearPos(start: start.y(), end: end.y(), pos);
19 return QPointF(x, y);
20}
21
22QPen linearPos(QPen start, QPen end, qreal pos)
23{
24 QColor c;
25 c.setRedF(linearPos(start: start.color().redF(), end: end.color().redF(), pos));
26 c.setGreenF(linearPos(start: start.color().greenF(), end: end.color().greenF(), pos));
27 c.setBlueF(linearPos(start: start.color().blueF(), end: end.color().blueF(), pos));
28 end.setColor(c);
29 return end;
30}
31
32QBrush linearPos(QBrush start, QBrush end, qreal pos)
33{
34 QColor c;
35 c.setRedF(linearPos(start: start.color().redF(), end: end.color().redF(), pos));
36 c.setGreenF(linearPos(start: start.color().greenF(), end: end.color().greenF(), pos));
37 c.setBlueF(linearPos(start: start.color().blueF(), end: end.color().blueF(), pos));
38 end.setColor(c);
39 return end;
40}
41
42PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem)
43 : ChartAnimation(sliceItem),
44 m_sliceItem(sliceItem),
45 m_currentValue(m_sliceItem->m_data)
46{
47
48}
49
50PieSliceAnimation::~PieSliceAnimation()
51{
52}
53
54void PieSliceAnimation::setValue(const PieSliceData &startValue, const PieSliceData &endValue)
55{
56 if (state() != QAbstractAnimation::Stopped)
57 stop();
58
59 m_currentValue = startValue;
60
61 setKeyValueAt(step: 0.0, value: QVariant::fromValue(value: startValue));
62 setKeyValueAt(step: 1.0, value: QVariant::fromValue(value: endValue));
63}
64
65void PieSliceAnimation::updateValue(const PieSliceData &endValue)
66{
67 if (state() != QAbstractAnimation::Stopped)
68 stop();
69
70 setKeyValueAt(step: 0.0, value: QVariant::fromValue(value: m_currentValue));
71 setKeyValueAt(step: 1.0, value: QVariant::fromValue(value: endValue));
72}
73
74PieSliceData PieSliceAnimation::currentSliceValue()
75{
76 // NOTE:
77 // We must use an internal current value because QVariantAnimation::currentValue() is updated
78 // before the animation is actually started. So if we get 2 updateValue() calls in a row the currentValue()
79 // will have the end value set from the first call and the second call will interpolate that instead of
80 // the original current value as it was before the first call.
81 return m_currentValue;
82}
83
84QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
85{
86 PieSliceData startValue = qvariant_cast<PieSliceData>(v: start);
87 PieSliceData endValue = qvariant_cast<PieSliceData>(v: end);
88
89 PieSliceData result;
90 result = endValue;
91 result.m_center = linearPos(start: startValue.m_center, end: endValue.m_center, pos: progress);
92 result.m_radius = linearPos(start: startValue.m_radius, end: endValue.m_radius, pos: progress);
93 result.m_startAngle = linearPos(start: startValue.m_startAngle, end: endValue.m_startAngle, pos: progress);
94 result.m_angleSpan = linearPos(start: startValue.m_angleSpan, end: endValue.m_angleSpan, pos: progress);
95 result.m_slicePen = linearPos(start: startValue.m_slicePen, end: endValue.m_slicePen, pos: progress);
96 result.m_sliceBrush = linearPos(start: startValue.m_sliceBrush, end: endValue.m_sliceBrush, pos: progress);
97 result.m_holeRadius = linearPos(start: startValue.m_holeRadius, end: endValue.m_holeRadius, pos: progress);
98
99 return QVariant::fromValue(value: result);
100}
101
102void PieSliceAnimation::updateCurrentValue(const QVariant &value)
103{
104 if (state() != QAbstractAnimation::Stopped) { //workaround
105 m_currentValue = qvariant_cast<PieSliceData>(v: value);
106 m_sliceItem->setLayout(m_currentValue);
107 }
108}
109
110QT_END_NAMESPACE
111

source code of qtcharts/src/charts/animations/piesliceanimation.cpp