1 /****************************************************************************
2 **
3 ** Copyright (C) 2016 The Qt Company Ltd.
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the Qt Charts module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:GPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU
19 ** General Public License version 3 or (at your option) any later version
20 ** approved by the KDE Free Qt Foundation. The licenses are as published by
21 ** the Free Software Foundation and appearing in the file LICENSE.GPL3
22 ** included in the packaging of this file. Please review the following
23 ** information to ensure the GNU General Public License requirements will
24 ** be met: https://www.gnu.org/licenses/gpl-3.0.html.
25 **
26 ** $QT_END_LICENSE$
27 **
28 ****************************************************************************/
29 
30 #include <private/piesliceanimation_p.h>
31 #include <private/piechartitem_p.h>
32 
Q_DECLARE_METATYPE(QT_CHARTS_NAMESPACE::PieSliceData)33 Q_DECLARE_METATYPE(QT_CHARTS_NAMESPACE::PieSliceData)
34 
35 QT_CHARTS_BEGIN_NAMESPACE
36 
37 qreal linearPos(qreal start, qreal end, qreal pos)
38 {
39     return start + ((end - start) * pos);
40 }
41 
linearPos(QPointF start,QPointF end,qreal pos)42 QPointF linearPos(QPointF start, QPointF end, qreal pos)
43 {
44     qreal x = linearPos(start.x(), end.x(), pos);
45     qreal y = linearPos(start.y(), end.y(), pos);
46     return QPointF(x, y);
47 }
48 
linearPos(QPen start,QPen end,qreal pos)49 QPen linearPos(QPen start, QPen end, qreal pos)
50 {
51     QColor c;
52     c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
53     c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
54     c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
55     end.setColor(c);
56     return end;
57 }
58 
linearPos(QBrush start,QBrush end,qreal pos)59 QBrush linearPos(QBrush start, QBrush end, qreal pos)
60 {
61     QColor c;
62     c.setRedF(linearPos(start.color().redF(), end.color().redF(), pos));
63     c.setGreenF(linearPos(start.color().greenF(), end.color().greenF(), pos));
64     c.setBlueF(linearPos(start.color().blueF(), end.color().blueF(), pos));
65     end.setColor(c);
66     return end;
67 }
68 
PieSliceAnimation(PieSliceItem * sliceItem)69 PieSliceAnimation::PieSliceAnimation(PieSliceItem *sliceItem)
70     : ChartAnimation(sliceItem),
71       m_sliceItem(sliceItem),
72       m_currentValue(m_sliceItem->m_data)
73 {
74 
75 }
76 
~PieSliceAnimation()77 PieSliceAnimation::~PieSliceAnimation()
78 {
79 }
80 
setValue(const PieSliceData & startValue,const PieSliceData & endValue)81 void PieSliceAnimation::setValue(const PieSliceData &startValue, const PieSliceData &endValue)
82 {
83     if (state() != QAbstractAnimation::Stopped)
84         stop();
85 
86     m_currentValue = startValue;
87 
88     setKeyValueAt(0.0, QVariant::fromValue(startValue));
89     setKeyValueAt(1.0, QVariant::fromValue(endValue));
90 }
91 
updateValue(const PieSliceData & endValue)92 void PieSliceAnimation::updateValue(const PieSliceData &endValue)
93 {
94     if (state() != QAbstractAnimation::Stopped)
95         stop();
96 
97     setKeyValueAt(0.0, QVariant::fromValue(m_currentValue));
98     setKeyValueAt(1.0, QVariant::fromValue(endValue));
99 }
100 
currentSliceValue()101 PieSliceData PieSliceAnimation::currentSliceValue()
102 {
103     // NOTE:
104     // We must use an internal current value because QVariantAnimation::currentValue() is updated
105     // before the animation is actually started. So if we get 2 updateValue() calls in a row the currentValue()
106     // will have the end value set from the first call and the second call will interpolate that instead of
107     // the original current value as it was before the first call.
108     return m_currentValue;
109 }
110 
interpolated(const QVariant & start,const QVariant & end,qreal progress) const111 QVariant PieSliceAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const
112 {
113     PieSliceData startValue = qvariant_cast<PieSliceData>(start);
114     PieSliceData endValue = qvariant_cast<PieSliceData>(end);
115 
116     PieSliceData result;
117     result = endValue;
118     result.m_center = linearPos(startValue.m_center, endValue.m_center, progress);
119     result.m_radius = linearPos(startValue.m_radius, endValue.m_radius, progress);
120     result.m_startAngle = linearPos(startValue.m_startAngle, endValue.m_startAngle, progress);
121     result.m_angleSpan = linearPos(startValue.m_angleSpan, endValue.m_angleSpan, progress);
122     result.m_slicePen = linearPos(startValue.m_slicePen, endValue.m_slicePen, progress);
123     result.m_sliceBrush = linearPos(startValue.m_sliceBrush, endValue.m_sliceBrush, progress);
124     result.m_holeRadius = linearPos(startValue.m_holeRadius, endValue.m_holeRadius, progress);
125 
126     return QVariant::fromValue(result);
127 }
128 
updateCurrentValue(const QVariant & value)129 void PieSliceAnimation::updateCurrentValue(const QVariant &value)
130 {
131     if (state() != QAbstractAnimation::Stopped) { //workaround
132         m_currentValue = qvariant_cast<PieSliceData>(value);
133         m_sliceItem->setLayout(m_currentValue);
134     }
135 }
136 
137 QT_CHARTS_END_NAMESPACE
138