| 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/candlestick_p.h> |
| 31 | #include <private/candlestickanimation_p.h> |
| 32 | #include <private/candlestickbodywicksanimation_p.h> |
| 33 | |
| 34 | Q_DECLARE_METATYPE(QVector<QRectF>) |
| 35 | Q_DECLARE_METATYPE(QT_CHARTS_NAMESPACE::CandlestickData) |
| 36 | Q_DECLARE_METATYPE(qreal) |
| 37 | |
| 38 | QT_CHARTS_BEGIN_NAMESPACE |
| 39 | |
| 40 | CandlestickBodyWicksAnimation::CandlestickBodyWicksAnimation(Candlestick *candlestick, |
| 41 | CandlestickAnimation *animation, |
| 42 | int duration, QEasingCurve &curve) |
| 43 | : ChartAnimation(candlestick), |
| 44 | m_candlestick(candlestick), |
| 45 | m_candlestickAnimation(animation), |
| 46 | m_changeAnimation(false) |
| 47 | { |
| 48 | setDuration(duration); |
| 49 | setEasingCurve(curve); |
| 50 | } |
| 51 | |
| 52 | CandlestickBodyWicksAnimation::~CandlestickBodyWicksAnimation() |
| 53 | { |
| 54 | if (m_candlestickAnimation) |
| 55 | m_candlestickAnimation->removeCandlestickAnimation(candlestick: m_candlestick); |
| 56 | } |
| 57 | |
| 58 | void CandlestickBodyWicksAnimation::setup(const CandlestickData &startData, |
| 59 | const CandlestickData &endData) |
| 60 | { |
| 61 | setKeyValueAt(step: 0.0, value: QVariant::fromValue(value: startData)); |
| 62 | setKeyValueAt(step: 1.0, value: QVariant::fromValue(value: endData)); |
| 63 | } |
| 64 | |
| 65 | void CandlestickBodyWicksAnimation::setStartData(const CandlestickData &startData) |
| 66 | { |
| 67 | if (state() != QAbstractAnimation::Stopped) |
| 68 | stop(); |
| 69 | |
| 70 | setStartValue(QVariant::fromValue(value: startData)); |
| 71 | } |
| 72 | |
| 73 | void CandlestickBodyWicksAnimation::setEndData(const CandlestickData &endData) |
| 74 | { |
| 75 | if (state() != QAbstractAnimation::Stopped) |
| 76 | stop(); |
| 77 | |
| 78 | setEndValue(QVariant::fromValue(value: endData)); |
| 79 | } |
| 80 | |
| 81 | void CandlestickBodyWicksAnimation::updateCurrentValue(const QVariant &value) |
| 82 | { |
| 83 | CandlestickData data = qvariant_cast<CandlestickData>(v: value); |
| 84 | m_candlestick->setLayout(data); |
| 85 | } |
| 86 | |
| 87 | QVariant CandlestickBodyWicksAnimation::interpolated(const QVariant &from, const QVariant &to, |
| 88 | qreal progress) const |
| 89 | { |
| 90 | CandlestickData startData = qvariant_cast<CandlestickData>(v: from); |
| 91 | CandlestickData endData = qvariant_cast<CandlestickData>(v: to); |
| 92 | CandlestickData result = endData; |
| 93 | |
| 94 | if (m_changeAnimation) { |
| 95 | result.m_open = startData.m_open + progress * (endData.m_open - startData.m_open); |
| 96 | result.m_high = startData.m_high + progress * (endData.m_high - startData.m_high); |
| 97 | result.m_low = startData.m_low + progress * (endData.m_low - startData.m_low); |
| 98 | result.m_close = startData.m_close + progress * (endData.m_close - startData.m_close); |
| 99 | } else { |
| 100 | const qreal median = (endData.m_open + endData.m_close) / 2; |
| 101 | result.m_low = median + progress * (endData.m_low - median); |
| 102 | result.m_close = median + progress * (endData.m_close - median); |
| 103 | result.m_open = median + progress * (endData.m_open - median); |
| 104 | result.m_high = median + progress * (endData.m_high - median); |
| 105 | } |
| 106 | |
| 107 | return QVariant::fromValue(value: result); |
| 108 | } |
| 109 | |
| 110 | QT_CHARTS_END_NAMESPACE |
| 111 | |
| 112 | #include "moc_candlestickbodywicksanimation_p.cpp" |
| 113 | |