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