| 1 | // Copyright (C) 2016 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
| 3 | |
| 4 | #include <private/axisanimation_p.h> |
| 5 | #include <private/chartaxiselement_p.h> |
| 6 | #include <private/qabstractaxis_p.h> |
| 7 | |
| 8 | |
| 9 | QT_BEGIN_NAMESPACE |
| 10 | |
| 11 | |
| 12 | AxisAnimation::AxisAnimation(ChartAxisElement *axis, int duration, QEasingCurve &curve) |
| 13 | : ChartAnimation(axis), |
| 14 | m_axis(axis), |
| 15 | m_type(DefaultAnimation) |
| 16 | { |
| 17 | setDuration(duration); |
| 18 | setEasingCurve(curve); |
| 19 | } |
| 20 | |
| 21 | AxisAnimation::~AxisAnimation() |
| 22 | { |
| 23 | } |
| 24 | |
| 25 | void AxisAnimation::setAnimationType(Animation type) |
| 26 | { |
| 27 | if (state() != QAbstractAnimation::Stopped) |
| 28 | stop(); |
| 29 | m_type = type; |
| 30 | } |
| 31 | |
| 32 | void AxisAnimation::setAnimationPoint(const QPointF &point) |
| 33 | { |
| 34 | if (state() != QAbstractAnimation::Stopped) |
| 35 | stop(); |
| 36 | m_point = point; |
| 37 | } |
| 38 | |
| 39 | void AxisAnimation::setValues(QList<qreal> &oldLayout, const QList<qreal> &newLayout) |
| 40 | { |
| 41 | if (state() != QAbstractAnimation::Stopped) stop(); |
| 42 | |
| 43 | switch (m_type) { |
| 44 | case ZoomOutAnimation: { |
| 45 | QRectF rect = m_axis->gridGeometry(); |
| 46 | oldLayout.resize(size: newLayout.size()); |
| 47 | |
| 48 | for (int i = 0, j = oldLayout.size() - 1; i < (oldLayout.size() + 1) / 2; ++i, --j) { |
| 49 | oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.bottom(); |
| 50 | oldLayout[j] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.right() : rect.top(); |
| 51 | } |
| 52 | } |
| 53 | break; |
| 54 | case ZoomInAnimation: { |
| 55 | int index = qMin(a: oldLayout.size() * (m_axis->axis()->orientation() == Qt::Horizontal ? m_point.x() : (1 - m_point.y())), b: newLayout.size() - (qreal)1.0); |
| 56 | oldLayout.resize(size: newLayout.size()); |
| 57 | |
| 58 | if (index < 0) |
| 59 | break; |
| 60 | for (int i = 0; i < oldLayout.size(); i++) |
| 61 | oldLayout[i] = oldLayout[index]; |
| 62 | } |
| 63 | break; |
| 64 | case MoveForwardAnimation: { |
| 65 | oldLayout.resize(size: newLayout.size()); |
| 66 | |
| 67 | for (int i = 0, j = i + 1; i < oldLayout.size() - 1; ++i, ++j) |
| 68 | oldLayout[i] = oldLayout[j]; |
| 69 | } |
| 70 | break; |
| 71 | case MoveBackwordAnimation: { |
| 72 | oldLayout.resize(size: newLayout.size()); |
| 73 | |
| 74 | for (int i = oldLayout.size() - 1, j = i - 1; i > 0; --i, --j) |
| 75 | oldLayout[i] = oldLayout[j]; |
| 76 | } |
| 77 | break; |
| 78 | default: { |
| 79 | oldLayout.resize(size: newLayout.size()); |
| 80 | QRectF rect = m_axis->gridGeometry(); |
| 81 | for (int i = 0, j = oldLayout.size() - 1; i < oldLayout.size(); ++i, --j) |
| 82 | oldLayout[i] = m_axis->axis()->orientation() == Qt::Horizontal ? rect.left() : rect.top(); |
| 83 | } |
| 84 | break; |
| 85 | } |
| 86 | |
| 87 | QVariantAnimation::KeyValues value; |
| 88 | setKeyValues(value); //workaround for wrong interpolation call |
| 89 | setKeyValueAt(step: 0.0, value: QVariant::fromValue(value: oldLayout)); |
| 90 | setKeyValueAt(step: 1.0, value: QVariant::fromValue(value: newLayout)); |
| 91 | } |
| 92 | |
| 93 | QVariant AxisAnimation::interpolated(const QVariant &start, const QVariant &end, qreal progress) const |
| 94 | { |
| 95 | const auto startList = qvariant_cast<QList<qreal>>(v: start); |
| 96 | const auto endList = qvariant_cast<QList<qreal>>(v: end); |
| 97 | QList<qreal> result; |
| 98 | |
| 99 | Q_ASSERT(startList.size() == endList.size()); |
| 100 | |
| 101 | for (int i = 0; i < startList.size(); i++) { |
| 102 | const qreal value = startList[i] + ((endList[i] - startList[i]) * progress); |
| 103 | result << value; |
| 104 | } |
| 105 | return QVariant::fromValue(value: result); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | void AxisAnimation::updateCurrentValue(const QVariant &value) |
| 110 | { |
| 111 | if (state() != QAbstractAnimation::Stopped) { // workaround |
| 112 | const QList<qreal> list = qvariant_cast<QList<qreal>>(v: value); |
| 113 | m_axis->setLayout(list); |
| 114 | m_axis->updateGeometry(); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | QT_END_NAMESPACE |
| 119 | |