1 /*
2  * This file is part of KQuickCharts
3  * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
4  *
5  * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
6  */
7 
8 #include "ValueHistorySource.h"
9 
10 #include "QmlDeprecated.h"
11 
12 #if QUICKCHARTS_BUILD_DEPRECATED_SINCE(5, 78)
13 
ValueHistorySource(QObject * parent)14 ValueHistorySource::ValueHistorySource(QObject *parent)
15     : ChartDataSource(parent)
16 {
17     QML_DEPRECATED("ValueHistorySource", "5.78", "Use HistoryProxySource instead")
18 }
19 
itemCount() const20 int ValueHistorySource::itemCount() const
21 {
22     return m_maximumHistory;
23 }
24 
item(int index) const25 QVariant ValueHistorySource::item(int index) const
26 {
27     if (index < 0 || index >= m_history.count()) {
28         return QVariant{};
29     }
30 
31     return m_history.at(index);
32 }
33 
minimum() const34 QVariant ValueHistorySource::minimum() const
35 {
36     auto item = std::min_element(m_history.cbegin(), m_history.cend());
37     if (item != m_history.cend()) {
38         return *item;
39     }
40 
41     return QVariant{};
42 }
43 
maximum() const44 QVariant ValueHistorySource::maximum() const
45 {
46     auto item = std::max_element(m_history.cbegin(), m_history.cend());
47     if (item != m_history.cend()) {
48         return *item;
49     }
50 
51     return QVariant{};
52 }
53 
value() const54 QVariant ValueHistorySource::value() const
55 {
56     return m_value;
57 }
58 
setValue(const QVariant & newValue)59 void ValueHistorySource::setValue(const QVariant &newValue)
60 {
61     m_value = newValue;
62 
63     if (!m_updateTimer) {
64         m_history.prepend(newValue);
65         while (m_history.size() > m_maximumHistory) {
66             m_history.removeLast();
67         }
68 
69         Q_EMIT dataChanged();
70     }
71 }
72 
maximumHistory() const73 int ValueHistorySource::maximumHistory() const
74 {
75     return m_maximumHistory;
76 }
77 
setMaximumHistory(int newMaximumHistory)78 void ValueHistorySource::setMaximumHistory(int newMaximumHistory)
79 {
80     if (newMaximumHistory == m_maximumHistory) {
81         return;
82     }
83 
84     m_maximumHistory = newMaximumHistory;
85     while (m_history.size() > m_maximumHistory) {
86         m_history.removeLast();
87     }
88     Q_EMIT maximumHistoryChanged();
89 }
90 
interval() const91 int ValueHistorySource::interval() const
92 {
93     return m_updateTimer ? m_updateTimer->interval() : -1;
94 }
95 
setInterval(int newInterval)96 void ValueHistorySource::setInterval(int newInterval)
97 {
98     if (m_updateTimer && newInterval == m_updateTimer->interval()) {
99         return;
100     }
101 
102     if (newInterval > 0) {
103         if (!m_updateTimer) {
104             m_updateTimer = std::make_unique<QTimer>();
105             // See ModelHistorySource.cpp line 110
106             m_updateTimer->setTimerType(Qt::PreciseTimer);
107             connect(m_updateTimer.get(), &QTimer::timeout, this, [this]() {
108                 m_history.prepend(m_value);
109                 while (m_history.size() > m_maximumHistory) {
110                     m_history.removeLast();
111                 }
112                 Q_EMIT dataChanged();
113             });
114         }
115         m_updateTimer->setInterval(newInterval);
116         m_updateTimer->start();
117     } else {
118         m_updateTimer.reset();
119     }
120 
121     Q_EMIT intervalChanged();
122 }
123 
clear()124 void ValueHistorySource::clear()
125 {
126     m_history.clear();
127     Q_EMIT dataChanged();
128 }
129 
130 #endif // QUICKCHARTS_BUILD_DEPRECATED_SINCE
131